Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mouse support for CBA_fnc_addKeyHandler #651

Merged
merged 17 commits into from
May 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions addons/events/CfgEventHandlers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class Extended_PostInit_EventHandlers {

class Extended_DisplayLoad_EventHandlers {
class RscDisplayMission {
ADDON = QUOTE(_this call (uiNamespace getVariable 'FUNC(missionDisplayLoad)'));
ADDON = QUOTE(_this call (uiNamespace getVariable 'FUNC(initDisplayMission)'));
};
class RscDisplayCurator {
ADDON = QUOTE(_this call (uiNamespace getVariable 'FUNC(curatorDisplayLoad)'));
ADDON = QUOTE(_this call (uiNamespace getVariable 'FUNC(initDisplayCurator)'));
};
};
13 changes: 9 additions & 4 deletions addons/events/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ if (!isNull (uiNamespace getVariable ["CBA_missionDisplay", displayNull])) then
GVAR(handlerHash) = [[], []] call CBA_fnc_hashCreate;
};

// Key Handlers
PREP(keyHandler);
PREP(keyHandlerDown);
PREP(keyHandlerUp);

["keyDown", FUNC(keyHandlerDown)] call CBA_fnc_addDisplayHandler;
["keyUp", FUNC(keyHandlerUp)] call CBA_fnc_addDisplayHandler;
PREP(mouseHandlerDown);
PREP(mouseHandlerUp);
PREP(mouseWheelHandler);

private _keyHandlers = [];
_keyHandlers resize 250;
_keyHandlers resize 0xFF;

GVAR(keyDownStates) = _keyHandlers apply {[]};
GVAR(keyUpStates) = + GVAR(keyDownStates);
Expand All @@ -78,3 +79,7 @@ GVAR(keyHoldTimers) = call CBA_fnc_createNamespace;

false
}] call CBA_fnc_compileFinal;

GVAR(shift) = false;
GVAR(control) = false;
GVAR(alt) = false;
15 changes: 10 additions & 5 deletions addons/events/XEH_preStart.sqf
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#include "script_component.hpp"

PREP(keyHandler);
PREP(keyHandlerDown);
PREP(keyHandlerUp);
if (hasInterface) then {
PREP(initDisplayMission);
PREP(initDisplayCurator);

PREP(missionDisplayLoad);
PREP(curatorDisplayLoad);
PREP(keyHandler);
PREP(keyHandlerDown);
PREP(keyHandlerUp);
PREP(mouseHandlerDown);
PREP(mouseHandlerUp);
PREP(mouseWheelHandler);
};
8 changes: 7 additions & 1 deletion addons/events/fnc_addKeyHandler.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if !(_type in ["keydown", "keyup"]) exitWith {

// create random hash if none was supplied
if (_hashKey isEqualTo "") then {
_hashKey = format ["%1%2%3%4%5%6%7%8", floor random 100, floor random 100, floor random 100, floor random 100, floor random 100, floor random 100, floor random 100, floor random 100];
_hashKey = format ["%1%2%3%4", floor random 1E4, floor random 1E4, floor random 1E4, floor random 1E4];
};

_hashKey = toLower _hashKey;
Expand All @@ -69,6 +69,12 @@ if (_type isEqualTo "keydown") then {
};

private _hash = [GVAR(keyHandlersDown), GVAR(keyHandlersUp)] select (_type == "keyup");

// fix using addKeyHander twice on different keys makes old handler unremovable
if (!isNil {_hash getVariable _hashKey}) then {
[_hashKey, _type] call CBA_fnc_removeKeyHandler;
};

_hash setVariable [_hashKey, [_key, _settings, _code, _allowHold, _holdDelay]];

private _keyHandlers = [GVAR(keyDownStates), GVAR(keyUpStates)] select (_type == "keyup");
Expand Down
6 changes: 0 additions & 6 deletions addons/events/fnc_curatorDisplayLoad.sqf

This file was deleted.

9 changes: 9 additions & 0 deletions addons/events/fnc_initDisplayCurator.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "script_component.hpp"

params ["_display"];

_display displayAddEventHandler ["KeyDown", {call FUNC(keyHandlerDown)}];
_display displayAddEventHandler ["KeyUp", {call FUNC(keyHandlerUp)}];
_display displayAddEventHandler ["MouseButtonDown", {call FUNC(mouseHandlerDown)}];
_display displayAddEventHandler ["MouseButtonUp", {call FUNC(mouseHandlerUp)}];
_display displayAddEventHandler ["MouseZChanged", {call FUNC(mouseWheelHandler)}];
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

params ["_display"];

// set up CBA_fnc_addDisplayHandler
uiNamespace setVariable ["CBA_missionDisplay", _display];

// re apply missions display event handlers when display is loaded (save game)
Expand All @@ -20,3 +21,10 @@ if (!isNil QGVAR(handlerHash)) then {
// copy hash reference to display namespace again, because it's not serialized in save games
uiNamespace setVariable [QGVAR(handlerHash), GVAR(handlerHash)];
};

// set up CBA_fnc_addKeyHandler
_display displayAddEventHandler ["KeyDown", {call FUNC(keyHandlerDown)}];
_display displayAddEventHandler ["KeyUp", {call FUNC(keyHandlerUp)}];
_display displayAddEventHandler ["MouseButtonDown", {call FUNC(mouseHandlerDown)}];
_display displayAddEventHandler ["MouseButtonUp", {call FUNC(mouseHandlerUp)}];
_display displayAddEventHandler ["MouseZChanged", {call FUNC(mouseWheelHandler)}];
2 changes: 2 additions & 0 deletions addons/events/fnc_keyHandler.sqf
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* ----------------------------------------------------------------------------
Internal Function: CBA_events_fnc_keyHandler

Description:
Executes the key's handler, bwc for ace interaction menu

Author:
commy2
---------------------------------------------------------------------------- */
Expand Down
52 changes: 35 additions & 17 deletions addons/events/fnc_keyHandlerDown.sqf
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* ----------------------------------------------------------------------------
Internal Function: CBA_events_fnc_keyHandlerDown

Description:
Executes the key's handler

Author:
Sickboy, commy2
---------------------------------------------------------------------------- */
Expand All @@ -11,19 +13,44 @@ SCRIPT(keyHandlerDown);

params ["", "_inputKey"];

if (_inputKey == 0) exitWith {};
if (_inputKey isEqualTo 0) exitWith {};

// handle modifiers
switch (true) do {
case (_inputKey in [DIK_LSHIFT, DIK_RSHIFT]): {
GVAR(shift) = true;
};
case (_inputKey in [DIK_LCONTROL, DIK_RCONTROL]): {
GVAR(control) = true;
};
case (_inputKey in [DIK_LMENU, DIK_RMENU]): {
GVAR(alt) = true;
};
};

private _inputSettings = _this select [2,3];
if !(_this select 2) then {
GVAR(shift) = false;
};

if !(_this select 3) then {
GVAR(control) = false;
};

if !(_this select 4) then {
GVAR(alt) = false;
};

private _inputModifiers = [GVAR(shift), GVAR(control), GVAR(alt)];

private _blockInput = false;

{
private _keybindParams = GVAR(keyHandlersDown) getVariable _x;

_keybindParams params ["", "_keybindSettings", "_code", "_allowHold", "_holdDelay"];
_keybindParams params ["", "_keybindModifiers", "_code", "_allowHold", "_holdDelay"];

// Verify if the required modifier keys are present
if (_keybindSettings isEqualTo _inputSettings) then {
if (_keybindModifiers isEqualTo _inputModifiers) then {
private _xUp = _x + "_cbadefaultuphandler";
private _execute = true;
private _holdTime = 0;
Expand All @@ -47,16 +74,8 @@ private _blockInput = false;
_params pushBack + _keybindParams;
_params pushBack _x;

_blockInput = _params call _code;

#ifdef DEBUG_MODE_FULL
if ((isNil "_blockInput") || {!(_blockInput isEqualType false)}) then {
LOG(PFORMAT_2("Keybind Handler returned nil or non-bool", _x, _blockInput));
};
#endif
_blockInput = [_params call _code] param [0, false] || {_blockInput};
};

if (_blockInput isEqualTo true) exitWith {};
};
} forEach (GVAR(keyDownStates) param [_inputKey, []]);

Expand All @@ -65,13 +84,12 @@ private _blockInput = false;
{
private _keybindParams = GVAR(keyHandlersUp) getVariable _x;

_keybindParams params ["", "_keybindSettings"];
_keybindParams params ["", "_keybindModifiers"];

// Verify if the required modifier keys are present
if (_keybindSettings isEqualTo _inputSettings) then {
if (_keybindModifiers isEqualTo _inputModifiers) then {
GVAR(keyDownActiveList) pushBackUnique _x;
};
} forEach (GVAR(keyUpStates) param [_inputKey, []]);

//Only return true if _blockInput is defined and is type bool (handlers could return anything):
(!isNil "_blockInput") && {_blockInput isEqualTo true}
_blockInput
56 changes: 30 additions & 26 deletions addons/events/fnc_keyHandlerUp.sqf
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* ----------------------------------------------------------------------------
Internal Function: CBA_events_fnc_keyHandlerUp

Description:
Executes the key's handler

Author:
Sickboy, commy2
---------------------------------------------------------------------------- */
Expand All @@ -11,40 +13,42 @@ SCRIPT(keyHandlerUp);

params ["", "_inputKey"];

if (_inputKey == 0) exitWith {};
if (_inputKey isEqualTo 0) exitWith {};

// handle modifiers
switch (true) do {
case (_inputKey in [DIK_LSHIFT, DIK_RSHIFT]): {
GVAR(shift) = false;
};
case (_inputKey in [DIK_LCONTROL, DIK_RCONTROL]): {
GVAR(control) = false;
};
case (_inputKey in [DIK_LMENU, DIK_RMENU]): {
GVAR(alt) = false;
};
};

private _blockedUpKeys = [];
private _removeHandlers = [];

{
private _keybindParams = GVAR(keyHandlersUp) getVariable _x;

_keybindParams params ["_keybind", "_keybindSettings", "_code"];
_keybindParams params ["_keybind", "_keybindModifiers", "_code"];

// Verify if the required modifier keys are present
if (_inputKey in [_keybind, DIK_LSHIFT, DIK_RSHIFT, DIK_LCONTROL, DIK_RCONTROL, DIK_LMENU, DIK_RMENU]) then {
if !(_keybind in _blockedUpKeys) then {
if (
_inputKey == _keybind
|| {(_keybindSettings select 0 && {_inputKey in [DIK_LSHIFT, DIK_RSHIFT]})
|| {(_keybindSettings select 1 && {_inputKey in [DIK_LCONTROL, DIK_RCONTROL]})
|| {(_keybindSettings select 2 && {_inputKey in [DIK_LMENU, DIK_RMENU]})}}}
) then {
private _params = + _this;
_params pushBack + _keybindParams;
_params pushBack _x;

private _blockInput = _params call _code;

if (_blockInput isEqualTo true) then {
_blockedUpKeys pushBack _keybind;
};

_removeHandlers pushBack _x;
};
} else {
_removeHandlers pushBack _x;
};
if (
_inputKey isEqualTo _keybind
|| {(_keybindModifiers select 0 && {_inputKey in [DIK_LSHIFT, DIK_RSHIFT]})
|| {(_keybindModifiers select 1 && {_inputKey in [DIK_LCONTROL, DIK_RCONTROL]})
|| {(_keybindModifiers select 2 && {_inputKey in [DIK_LMENU, DIK_RMENU]})}}}
) then {
private _params = + _this;
_params pushBack + _keybindParams;
_params pushBack _x;

_params call _code;

_removeHandlers pushBack _x;
};
} forEach GVAR(keyDownActiveList);

Expand Down
16 changes: 16 additions & 0 deletions addons/events/fnc_mouseHandlerDown.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* ----------------------------------------------------------------------------
Internal Function: CBA_events_fnc_mouseHandlerDown

Description:
Executes the mouse's handler

Author:
commy2
---------------------------------------------------------------------------- */
// #define DEBUG_MODE_FULL
#include "script_component.hpp"
SCRIPT(mouseHandlerDown);

params ["_display", "_inputButton"];

[_display, MOUSE_OFFSET + _inputButton, GVAR(shift), GVAR(control), GVAR(alt)] call FUNC(keyHandlerDown);
16 changes: 16 additions & 0 deletions addons/events/fnc_mouseHandlerUp.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* ----------------------------------------------------------------------------
Internal Function: CBA_events_fnc_mouseHandlerUp

Description:
Executes the mouse's handler

Author:
commy2
---------------------------------------------------------------------------- */
// #define DEBUG_MODE_FULL
#include "script_component.hpp"
SCRIPT(mouseHandlerUp);

params ["_display", "_inputButton"];

[_display, MOUSE_OFFSET + _inputButton, GVAR(shift), GVAR(control), GVAR(alt)] call FUNC(keyHandlerUp);
19 changes: 19 additions & 0 deletions addons/events/fnc_mouseWheelHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* ----------------------------------------------------------------------------
Internal Function: CBA_events_fnc_mouseWheelHandler

Description:
Executes the mouse wheel's handler

Author:
commy2
---------------------------------------------------------------------------- */
// #define DEBUG_MODE_FULL
#include "script_component.hpp"
SCRIPT(mouseWheelHandler);

params ["_display", "_inputDirection"];

private _inputDirection = [0, 1] select (_inputDirection < 0);

[_display, MOUSE_WHEEL_OFFSET + _inputDirection, GVAR(shift), GVAR(control), GVAR(alt)] call FUNC(keyHandlerDown);
[_display, MOUSE_WHEEL_OFFSET + _inputDirection, GVAR(shift), GVAR(control), GVAR(alt)] call FUNC(keyHandlerUp);
4 changes: 4 additions & 0 deletions addons/events/script_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@
#define GETOBJ(obj) (if (obj isEqualType grpNull) then {leader obj} else {obj})

#include "\a3\editor_f\Data\Scripts\dikCodes.h"

// key handler system
#define MOUSE_OFFSET 0xF0
#define MOUSE_WHEEL_OFFSET 0xF8 // MOUSE_OFFSET + 8 possible mouse keys
16 changes: 16 additions & 0 deletions addons/keybinding/XEH_preStart.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@ _supportedKeys = _supportedKeys apply {

GVAR(keyNamesHash) = [_supportedKeys] call CBA_fnc_hashCreate;

// manually add mouse key localizations to our inofficial DIK codes
{
[GVAR(keyNamesHash), str (_x select 0), parseSimpleArray format ["[%1]", keyName (_x select 1)] select 0] call CBA_fnc_hashSet;
} forEach [
[0xF0, 0x10000],
[0xF1, 0x10081],
[0xF2, 0x10002],
[0xF3, 0x10003],
[0xF4, 0x10004],
[0xF5, 0x10005],
[0xF6, 0x10006],
[0xF7, 0x10007],
[0xF8, 0x100004],
[0xF9, 0x100005]
];

GVAR(forbiddenKeys) = [
DIK_XBOX_LEFT_TRIGGER,
DIK_XBOX_RIGHT_TRIGGER,
Expand Down