Skip to content

Commit

Permalink
Merge pull request #505 from CBATeam/add-marker-event-handler
Browse files Browse the repository at this point in the history
add event handler for creating and deleting markers
  • Loading branch information
Killswitch00 authored Sep 23, 2016
2 parents 2cab05e + 3b192ca commit 7e376fe
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions addons/events/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class CfgFunctions {
description = "Raises a CBA event on the target client ID's machine.";
file = "\x\cba\addons\events\fnc_ownerEvent.sqf";
};
PATHTO_FNC(addMarkerEventHandler);
PATHTO_FNC(removeMarkerEventHandler);
};
};
};
58 changes: 58 additions & 0 deletions addons/events/fnc_addMarkerEventHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_addMarkerEventHandler
Description:
Adds an event handler that executes code when a marker is created or deleted.
Parameters:
_eventType - Type of event to add. Can be "created" or "deleted". <STRING>
_function - Function to call when marker is created or deleted. <CODE>
Returns:
_eventId - Unique ID. Used with 'CBA_fnc_removeMarkerEventHandler'.
Examples:
(begin example)
_id = ["created", {systemChat str _this}] call CBA_fnc_addMarkerEventHandler;
(end)
Author:
commy2
---------------------------------------------------------------------------- */
#include "script_component.hpp"
SCRIPT(addMarkerEventHandler);

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

if (isNil QGVAR(oldMarkers)) then {
GVAR(oldMarkers) = allMapMarkers;

[{
private _newAllMapMarkers = allMapMarkers;
if !(_newAllMapMarkers isEqualTo GVAR(oldMarkers)) then {
params ["_events"];

{
[QGVAR(markerDeleted), [_x]] call CBA_fnc_localEvent;
} forEach (GVAR(oldMarkers) - _newAllMapMarkers);

{
[QGVAR(markerCreated), [_x]] call CBA_fnc_localEvent;
} forEach (_newAllMapMarkers - GVAR(oldMarkers));

GVAR(oldMarkers) = _newAllMapMarkers;
};
}, 0] call CBA_fnc_addPerFrameHandler;
};

if (_function isEqualTo {}) exitWith {-1};

switch (toLower _eventType) do {
case "created": {
[QGVAR(markerCreated), _function] call CBA_fnc_addEventHandler // return
};
case "deleted": {
[QGVAR(markerDeleted), _function] call CBA_fnc_addEventHandler // return
};
default {-1};
};
36 changes: 36 additions & 0 deletions addons/events/fnc_removeMarkerEventHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_removeMarkerEventHandler
Description:
Removes an event handler previously registered with 'CBA_fnc_addMarkerEventHandler'.
Parameters:
_eventName - Type of event to remove. Can be "created" or "deleted". <STRING>
_eventId - Unique ID of the event handler to remove. <NUMBER>
Returns:
None
Examples:
(begin example)
["created", _id] call CBA_fnc_removeMarkerEventHandler;
(end)
Author:
commy2
---------------------------------------------------------------------------- */
#include "script_component.hpp"
SCRIPT(removeMarkerEventHandler);

params [["_eventType", "", [""]], ["_eventId", -1, [0]]];

switch (toLower _eventType) do {
case "created": {
[QGVAR(markerCreated), _eventId] call CBA_fnc_removeEventHandler;
};
case "deleted": {
[QGVAR(markerDeleted), _eventId] call CBA_fnc_removeEventHandler;
};
};

nil

0 comments on commit 7e376fe

Please sign in to comment.