//Callback used for handling EVT_INPUT_KEY (that have wxKeyEvents inside), these events are emited only when renderer window is active
//id - windowID, value -1 means ignore it
//type - wxEVT_KEY_DOWN or wxEVT_KEY_UP, should be obtained by getWxEventID("wxEVT_KEY_DOWN")
//keyCode - ascii code
//modifiers - 1=Alt, 2=Ctrl, 4=Shift, value -1 means ignore it
function actionOnKey(id, type, keyCode, modifiers, action) {
    this.id = id;
    this.type = type;
    this.keyCode = keyCode;
    this.modifiers = modifiers;
    this.action = action;

    this.perform = function(evt) {
        var cdata = evt.kevt;
        //LOG("key id=" + cdata.id + " type=" + cdata.type + " keyCode=" + cdata.keyCode + " unicodeKey=" + cdata.unicodeKey + " modifiers=" + cdata.modifiers);
        if ((this.id == -1) || (cdata.id == this.id)) {
            if (this.type == cdata.type) {
                if (cdata.keyCode == this.keyCode) {
                    if ((this.modifiers == -1) || (cdata.modifiers == this.modifiers)) {
                        this.action();
                    }
                }
            }
        }
    };
}

//Callback used for handling buttons on tracking devices (Flystick or SpaceMouse), see EVT_INPUT_TRACKING
//bodyID - the bodyID defined in the event
//buttonID - the ID of the button
//filterRepeating - false=don't filter, true=filter repeating
function actionOnButton(bodyID, buttonID, filterRepeating, action) {
    this.bodyID = bodyID;
    this.buttonID = buttonID;
    this.filterRepeating = filterRepeating;
    this.action = action;
    this.lastButtons = 0;

    this.perform = function(evt) {
        var cdata = evt.deviceData;
        if (cdata.id == this.bodyID) {
            //LOG("Flystick buttons=" + cdata.buttons);
            if (this.filterRepeating) {
                var changeMask = this.lastButtons ^ cdata.buttons;
                var mask = 1 << this.buttonID;
                if ((cdata.buttons & mask) && (changeMask & mask)) {
                    this.action();
                }
                this.lastButtons = cdata.buttons;
            }
            else {
                if (cdata.buttons & (1 << this.buttonID)) {
                    this.action();
                }
            }
        }
    };
}

//Callback used for handling controls on tracking devices (Flystick2 or SpaceMouse), see EVT_INPUT_TRACKING
//bodyID - the bodyID defined in the event
//buttonID - the ID of the control
//filterRepeating - false=don't filter, true=filter repeating
function actionOnControl(bodyID, controlID, filterRepeating, action) {
    this.bodyID = bodyID;
    this.controlID = controlID;
    this.filterRepeating = filterRepeating;
    this.action = action;
    this.lastValue = 0;

    this.perform = function(evt) {
        var cdata = evt.deviceData;
        if ((cdata.id == this.bodyID) && (cdata.controls.length > controlID)) {
            //LOG("Flystick2 controls=" + cdata.controls);
            if (this.filterRepeating) {
                if (this.lastValue != cdata.controls[this.controlID]){
                    this.action(cdata.controls[this.controlID]);
                    this.lastValue = cdata.controls[this.controlID];
                }
            }
            else {
                if (cdata.controls[this.controlID]!=0) {
                    this.action(cdata.controls[this.controlID]);
                }
            }
        }
    };
}
