diff --git a/addons/events/CfgFunctions.hpp b/addons/events/CfgFunctions.hpp index 3ba9eec4f..c750c20c1 100644 --- a/addons/events/CfgFunctions.hpp +++ b/addons/events/CfgFunctions.hpp @@ -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); }; }; }; diff --git a/addons/events/fnc_addMarkerEventHandler.sqf b/addons/events/fnc_addMarkerEventHandler.sqf new file mode 100644 index 000000000..419a7987b --- /dev/null +++ b/addons/events/fnc_addMarkerEventHandler.sqf @@ -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". + _function - Function to call when marker is created or deleted. + +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}; +}; diff --git a/addons/events/fnc_removeMarkerEventHandler.sqf b/addons/events/fnc_removeMarkerEventHandler.sqf new file mode 100644 index 000000000..12f604acc --- /dev/null +++ b/addons/events/fnc_removeMarkerEventHandler.sqf @@ -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". + _eventId - Unique ID of the event handler to remove. + +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