Skip to content

Profile Handler

Andrew Troake edited this page Feb 3, 2019 · 3 revisions

The profile handler is responsible for the loading, saving, and deletion of control profiles.

Profile JSON Format

Feilds:

  • id - The unique identifier of the profile represented by an integer. Every profile has a different id. This is used by the python when saving/deleting profiles.

  • name - The name of the profile. This is a string which represents the profile as a friendly name, determined by the creator of the profile.

  • gamepads - An array containing JSON objects for all the gamepads in the profile. Each gamepad object contains a name string, buttons array, and axes array. The buttons and axes arrays are lists of strings to which control each of the axes and buttons are mapped to. The index of the string in each of the arrays is the button or axes that control is mapped to. For example, button 4 is in the 5th position in the array (starts at 0). A button or axes with an empty string is not mapped to any control.

  • onProfilesLoaded - A function to run when the profiles have been successfully loaded. The function call will have one argument, a list of profiles. Example: profileHandler.onProfilesLoaded(function(profiles){});


Example Profile Data:

{
    "name": "Example Profile",
    "id": 63,
    "gamepads": [
        {
            "name": "Left Joystick",
            "buttons": [
                "", "", "", "", "pitchUp", "", "", "", "", ""
            ],
            "axes": [
                "surge", "sway", "yaw", "", "", ""
            ]
        },
        {
            "name": "Right Joystick",
            "buttons": [
                "","pitchDown","","","flip","",""
            ],
            "axes": [
                "","","roll","pitch","","heave"
            ]
        }
    ]
}

How to use

When the document is ready ($(document).ready(function(){})), you can initialize the profile handler on the page by using:

var profileHandler = new profileHandler();

This creates a new instance of the ProfileHandler. When created, the handler will load profile JSON from the server. This profile data can be accessed through:

profileHandler.profiles

With the Profile Handler instance, individual profile data can be received using profileHandler.getProfileById(). Check out above to see the format of the profile JSON.

Profiles can be saved by running profileHandler.saveProfile(profile). This method takes in the modified profile JSON data. Profiles can also be deleted from profileHandler.deleteProfile(is), which takes in the id of the profile to delete.


Class Chaining

Some other JavaScript classes such as the Control Handler require input from the control handler. However, because HTTP loading is done asynchronously, events have to be chained together.

To run code when the profiles are finished loading, you can set a funtion to the onProfilesLoaded feild. This function will run when the profiles have loaded.

profileHandler.onProfilesLoaded = function(profiles){
    controlHandler.profile = profiles[0];
}