From bdd34221507f8a3e8375935089da32153c3db8b0 Mon Sep 17 00:00:00 2001
From: esteldunedain <nicolas.d.badano@gmail.com>
Date: Thu, 16 Apr 2015 23:53:54 -0300
Subject: [PATCH] Allow to dynamically modify actions at runtime. An optional
 code entry called  modifierFunction can be added. That code is called before
 evaluating the action, allowing to dynamically change the actions name, icon,
 etc

---
 .../functions/fnc_collectActiveActionTree.sqf        |  7 +++++++
 addons/interact_menu/functions/fnc_compileMenu.sqf   |  6 ++++--
 .../functions/fnc_compileMenuSelfAction.sqf          |  6 ++++--
 addons/interact_menu/functions/fnc_createAction.sqf  | 12 ++++++++++--
 4 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/addons/interact_menu/functions/fnc_collectActiveActionTree.sqf b/addons/interact_menu/functions/fnc_collectActiveActionTree.sqf
index 582fe750a8..6abcebf47e 100644
--- a/addons/interact_menu/functions/fnc_collectActiveActionTree.sqf
+++ b/addons/interact_menu/functions/fnc_collectActiveActionTree.sqf
@@ -22,6 +22,13 @@ private ["_target","_player","_fullPath","_activeChildren","_dynamicChildren","_
 _target = _object;
 _player = ACE_player;
 
+// Check if the function should be modified first
+if !((_origActionData select 10) isEqualTo {}) then {
+    // It should, so make a copy and pass it to the modifierFunction
+    _origActionData = +_origActionData;
+    [_target, ACE_player, _origActionData select 6, _origActionData] call (_origActionData select 10);
+};
+
 // Return nothing if the action itself is not active
 if !([_target, ACE_player, _origActionData select 6] call (_origActionData select 4)) exitWith {
     []
diff --git a/addons/interact_menu/functions/fnc_compileMenu.sqf b/addons/interact_menu/functions/fnc_compileMenu.sqf
index baf8e1c272..7983c493e1 100644
--- a/addons/interact_menu/functions/fnc_compileMenu.sqf
+++ b/addons/interact_menu/functions/fnc_compileMenu.sqf
@@ -27,7 +27,7 @@ if !(isNil {missionNamespace getVariable [_actionsVarName, nil]}) exitWith {};
 private "_recurseFnc";
 _recurseFnc = {
     private ["_actions", "_displayName", "_distance", "_icon", "_statement", "_selection", "_condition", "_showDisabled",
-            "_enableInside", "_canCollapse", "_runOnHover", "_children", "_entry", "_entryCfg", "_insertChildren"];
+            "_enableInside", "_canCollapse", "_runOnHover", "_children", "_entry", "_entryCfg", "_insertChildren", "_modifierFunction"];
     EXPLODE_1_PVT(_this,_actionsCfg);
     _actions = [];
 
@@ -56,6 +56,7 @@ _recurseFnc = {
             _condition = _condition + format [QUOTE( && {[ARR_3(ACE_player, _target, %1)] call EFUNC(common,canInteractWith)} ), getArray (_entryCfg >> "exceptions")];
 
             _insertChildren = compile (getText (_entryCfg >> "insertChildren"));
+            _modifierFunction = compile (getText (_entryCfg >> "modifierFunction"));
 
             _showDisabled = (getNumber (_entryCfg >> "showDisabled")) > 0;
             _enableInside = (getNumber (_entryCfg >> "enableInside")) > 0;
@@ -76,7 +77,8 @@ _recurseFnc = {
                             [],
                             _selection,
                             _distance,
-                            [_showDisabled,_enableInside,_canCollapse,_runOnHover]
+                            [_showDisabled,_enableInside,_canCollapse,_runOnHover],
+                            _modifierFunction
                         ],
                         _children
                     ];
diff --git a/addons/interact_menu/functions/fnc_compileMenuSelfAction.sqf b/addons/interact_menu/functions/fnc_compileMenuSelfAction.sqf
index 968b143701..0258b3dc8a 100644
--- a/addons/interact_menu/functions/fnc_compileMenuSelfAction.sqf
+++ b/addons/interact_menu/functions/fnc_compileMenuSelfAction.sqf
@@ -27,7 +27,7 @@ if !(isNil {missionNamespace getVariable [_actionsVarName, nil]}) exitWith {};
 private "_recurseFnc";
 _recurseFnc = {
     private ["_actions", "_displayName", "_distance", "_icon", "_statement", "_selection", "_condition", "_showDisabled",
-            "_enableInside", "_canCollapse", "_runOnHover", "_children", "_entry", "_entryCfg", "_insertChildren"];
+            "_enableInside", "_canCollapse", "_runOnHover", "_children", "_entry", "_entryCfg", "_insertChildren", "_modifierFunction"];
     EXPLODE_1_PVT(_this,_actionsCfg);
     _actions = [];
 
@@ -46,6 +46,7 @@ _recurseFnc = {
             _condition = _condition + format [QUOTE( && {[ARR_3(ACE_player, _target, %1)] call EFUNC(common,canInteractWith)} ), getArray (_entryCfg >> "exceptions")];
 
             _insertChildren = compile (getText (_entryCfg >> "insertChildren"));
+            _modifierFunction = compile (getText (_entryCfg >> "modifierFunction"));
 
             _showDisabled = (getNumber (_entryCfg >> "showDisabled")) > 0;
             _enableInside = (getNumber (_entryCfg >> "enableInside")) > 0;
@@ -66,7 +67,8 @@ _recurseFnc = {
                             [],
                             [0,0,0],
                             10, //distace
-                            [_showDisabled,_enableInside,_canCollapse,_runOnHover]
+                            [_showDisabled,_enableInside,_canCollapse,_runOnHover],
+                            _modifierFunction
                         ],
                         _children
                     ];
diff --git a/addons/interact_menu/functions/fnc_createAction.sqf b/addons/interact_menu/functions/fnc_createAction.sqf
index 0b0030a1b4..fb3f05b7b5 100644
--- a/addons/interact_menu/functions/fnc_createAction.sqf
+++ b/addons/interact_menu/functions/fnc_createAction.sqf
@@ -14,6 +14,7 @@
  * 7: Position (Position or Selection Name) <POSITION> or <STRING> (Optional)
  * 8: Distance <NUMBER> (Optional)
  * 9: Other parameters <ARRAY> (Optional)
+ * 10: Modifier function <CODE> (Optional)
  *
  * Return value:
  * Action <ARRAY>
@@ -27,7 +28,7 @@
 
 EXPLODE_5_PVT(_this,_actionName,_displayName,_icon,_statement,_condition);
 
-private ["_insertChildren","_customParams","_position","_distance","_params"];
+private ["_insertChildren","_customParams","_position","_distance","_params", "_modifierFunction"];
 
 _insertChildren = if (count _this > 5) then {
     _this select 5
@@ -59,6 +60,12 @@ _params = if (count _this > 9) then {
     [false,false,false,false]
 };
 
+_modifierFunction =  if (count _this > 10) then {
+    _this select 10
+} else {
+    {}
+};
+
 [
     _actionName,
     _displayName,
@@ -70,5 +77,6 @@ _params = if (count _this > 9) then {
     _customParams,
     _position,
     _distance,
-    _params
+    _params,
+    _modifierFunction
 ]