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

add 'CBA_fnc_addPlayerEventHandler' #328

Merged
merged 4 commits into from
May 16, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions addons/common/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CfgFunctions {
F_FILEPATH(canUseWeapon);
F_FILEPATH(selectWeapon);
F_FILEPATH(switchPlayer);
F_FILEPATH(currentUnit);
};

class Vehicles {
Expand Down
20 changes: 20 additions & 0 deletions addons/common/fnc_currentUnit.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_currentUnit

Description:
Returns the currently controlled unit.
Different from "player" when remote controlling units via zeus.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either shorten to one sentence or separate with empty line for possible future improvements to documentation generation.


Parameters:
None

Returns:
Currently controlled unit <OBJECT>

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

missionNamespace getVariable ["bis_fnc_moduleRemoteControl_unit", player]
8 changes: 8 additions & 0 deletions addons/events/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ class CfgFunctions {
description = "Adds an event handler with arguments.";
file = "\x\cba\addons\events\fnc_addBISEventHandler.sqf";
};
class addPlayerEventHandler {
description = "Adds a player event handler.";
file = "\x\cba\addons\events\fnc_addPlayerEventHandler.sqf";
};
class removePlayerEventHandler {
description = "Removes a player event handler.";
file = "\x\cba\addons\events\fnc_removePlayerEventHandler.sqf";
};
class addDisplayHandler {
description = "Adds an action to the main display.";
file = "\x\cba\addons\events\fnc_addDisplayHandler.sqf";
Expand Down
145 changes: 145 additions & 0 deletions addons/events/fnc_addPlayerEventHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_addPlayerEventHandler

Description:
Adds a player event handler.

Possible events:
"unit" - player controlled unit changed
"weapon" - currently selected weapon change
"loadout" - players loadout changed
"vehicle" - players current vehicle changed
"turret" - position in vehicle changed
"visionMode" - player changed to normal/night/thermal view
"cameraView" - camera mode changed ("Internal", "External", "Gunner" etc.)
"visibleMap" - opened or closed map

Parameters:
_type - Event handler type. <STRING>
_function - Function to add to event. <CODE>

Returns:
_id - The ID of the event handler. <NUMBER>

Examples:
(begin example)
_id = ["unit", {systemChat str _this}] call CBA_fnc_addPlayerEventHandler;
(end)

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

params [["_type", "", [""]], ["_function", {}, [{}]]];

private _id = switch (toLower _type) do {
case ("unit"): {
[QGVAR(unitEvent), _function] call CBA_fnc_addEventHandler // return id
};
case ("weapon"): {
[QGVAR(weaponEvent), _function] call CBA_fnc_addEventHandler // return id
};
case ("loadout"): {
[QGVAR(loadoutEvent), _function] call CBA_fnc_addEventHandler // return id
};
case ("vehicle"): {
[QGVAR(vehicleEvent), _function] call CBA_fnc_addEventHandler // return id
};
case ("turret"): {
[QGVAR(turretEvent), _function] call CBA_fnc_addEventHandler // return id
};
case ("visionmode"): {
[QGVAR(visionModeEvent), _function] call CBA_fnc_addEventHandler // return id
};
case ("cameraview"): {
[QGVAR(cameraViewEvent), _function] call CBA_fnc_addEventHandler // return id
};
case ("visiblemap"): {
[QGVAR(visibleMapEvent), _function] call CBA_fnc_addEventHandler // return id
};
default {-1};
};

// add loop for polling if it doesn't exist yet
if (isNil QGVAR(pollingLoopAdded) && {_id != -1}) then {
GVAR(pollingLoopAdded) = true;

GVAR(oldUnit) = objNull;
GVAR(oldWeapon) = "";
GVAR(oldLoadout) = [];
GVAR(oldLoadoutNoAmmo) = [];
GVAR(oldVehicle) = objNull;
GVAR(oldTurret) = [];
GVAR(oldVisionMode) = -1;
GVAR(oldCameraView) = "";
GVAR(oldVisibleMap) = false;

addMissionEventHandler ["EachFrame", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep track of the EachFrame handler id so that the polling function can be removed if/when all player event handlers are removed again.

private _player = call CBA_fnc_currentUnit;
if !(_player isEqualTo GVAR(oldUnit)) then {
GVAR(oldUnit) = _player;
[QGVAR(unitEvent), [_player]] call CBA_fnc_localEvent;
};

private _data = currentWeapon _player;
if !(_data isEqualTo GVAR(oldWeapon)) then {
GVAR(oldWeapon) = _data;
[QGVAR(weaponEvent), [_player, _data]] call CBA_fnc_localEvent;
};

_data = getUnitLoadout _player;
if !(_data isEqualTo GVAR(oldLoadout)) then {
GVAR(oldLoadout) = _data;

// we don't want to trigger this just because your ammo counter decreased.
_data = + GVAR(oldLoadout);

{
private _weaponInfo = _data param [_forEachIndex, []];
if !(_weaponInfo isEqualTo []) then {
_weaponInfo set [4, _x];
_weaponInfo deleteAt 5;
};
} forEach [primaryWeaponMagazine _player, secondaryWeaponMagazine _player, handgunMagazine _player];

if !(_data isEqualTo GVAR(oldLoadoutNoAmmo)) then {
GVAR(oldLoadoutNoAmmo) = _data;
[QGVAR(loadoutEvent), [_player, GVAR(oldLoadout)]] call CBA_fnc_localEvent;
};
};

_data = vehicle _player;
if !(_data isEqualTo GVAR(oldVehicle)) then {
GVAR(oldVehicle) = _data;
[QGVAR(vehicleEvent), [_player, _data]] call CBA_fnc_localEvent;
};

_data = _player call CBA_fnc_turretPath;
if !(_data isEqualTo GVAR(oldTurret)) then {
GVAR(oldTurret) = _data;
[QGVAR(turretEvent), [_player, _data]] call CBA_fnc_localEvent;
};

_data = currentVisionMode _player;
if !(_data isEqualTo GVAR(oldVisionMode)) then {
GVAR(oldVisionMode) = _data;
[QGVAR(visionModeEvent), [_player, _data]] call CBA_fnc_localEvent;
};

_data = cameraView;
if !(_data isEqualTo GVAR(oldCameraView)) then {
GVAR(oldCameraView) = _data;
[QGVAR(cameraViewEvent), [_player, _data]] call CBA_fnc_localEvent;
};

_data = visibleMap;
if !(_data isEqualTo GVAR(oldVisibleMap)) then {
GVAR(oldVisibleMap) = _data;
[QGVAR(visibleMapEvent), [_player, _data]] call CBA_fnc_localEvent;
};
}];
};

_id
53 changes: 53 additions & 0 deletions addons/events/fnc_removePlayerEventHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_removePlayerEventHandler

Description:
Removes a player event handler.

Parameters:
_type - Event handler type. <STRING>
_id - The ID of the event handler. <NUMBER>

Returns:
None

Examples:
(begin example)
["unit", _id] call CBA_fnc_removePlayerEventHandler;
(end)

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

params [["_type", "", [""]], ["_id", -1, [0]]];

switch (toLower _type) do {
case ("unit"): {
[QGVAR(unitEvent), _id] call CBA_fnc_removeEventHandler;
};
case ("weapon"): {
[QGVAR(weaponEvent), _id] call CBA_fnc_removeEventHandler;
};
case ("loadout"): {
[QGVAR(loadoutEvent), _id] call CBA_fnc_removeEventHandler;
};
case ("vehicle"): {
[QGVAR(vehicleEvent), _id] call CBA_fnc_removeEventHandler;
};
case ("turret"): {
[QGVAR(turretEvent), _id] call CBA_fnc_removeEventHandler;
};
case ("visionmode"): {
[QGVAR(visionModeEvent), _id] call CBA_fnc_removeEventHandler;
};
case ("cameraview"): {
[QGVAR(cameraViewEvent), _id] call CBA_fnc_removeEventHandler;
};
case ("visiblemap"): {
[QGVAR(visibleMapEvent), _id] call CBA_fnc_removeEventHandler;
};
default {nil};
};