Skip to content

Commit

Permalink
Merge pull request #328 from CBATeam/playerEvents
Browse files Browse the repository at this point in the history
add 'CBA_fnc_addPlayerEventHandler'
  • Loading branch information
Killswitch00 committed May 16, 2016
2 parents 3fd22f5 + 0c3c2bd commit f1db069
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 0 deletions.
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
19 changes: 19 additions & 0 deletions addons/common/fnc_currentUnit.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_currentUnit
Description:
Returns the controlled unit. ("player" or remote controlled unit via zeus)
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
151 changes: 151 additions & 0 deletions addons/events/fnc_addPlayerEventHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/* ----------------------------------------------------------------------------
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", {}, [{}]]];

_type = toLower _type;

private _id = switch (_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};
};

if (_id != -1) then {
// add loop for polling if it doesn't exist yet
if (isNil QGVAR(playerEHInfo)) then {
GVAR(playerEHInfo) = [];

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

GVAR(playerEHInfo) pushBack addMissionEventHandler ["EachFrame", {
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;
};
}];
};

GVAR(playerEHInfo) pushBack [_type, _id];
};

_id
66 changes: 66 additions & 0 deletions addons/events/fnc_removePlayerEventHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* ----------------------------------------------------------------------------
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]]];

_type = toLower _type;

switch (_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};
};

if (!isNil QGVAR(playerEHInfo)) then {
GVAR(playerEHInfo) deleteAt (GVAR(playerEHInfo) find [_type, _id]);

if (count GVAR(playerEHInfo) == 1) then {
removeMissionEventHandler ["EachFrame", GVAR(playerEHInfo) select 0];
GVAR(playerEHInfo) = nil;
};
};

nil

0 comments on commit f1db069

Please sign in to comment.