From 4226cd383e57c0fd8725df515787f57e8f93a140 Mon Sep 17 00:00:00 2001 From: Grim <69561145+LinkIsGrim@users.noreply.github.com> Date: Mon, 5 Aug 2024 06:38:46 -0300 Subject: [PATCH] Interact Menu - Add inheritance support to removeActionFromClass (#8396) * Add inheritance & exclusion support * Update documentation in wiki * additional check for parent class in findIf * forEach instead of count Co-authored-by: PabstMirror * condition for children of excluded classes * touch everything but the params check * apply configName in same line * fix param data type for _excludedClasses Co-authored-by: PabstMirror * Update addons/interact_menu/functions/fnc_removeActionFromClass.sqf Co-authored-by: johnb432 <58661205+johnb432@users.noreply.github.com> * Fix & formatting * Fix missing _x & headers, remove invalid classes --------- Co-authored-by: Salluci <69561145+Salluci@users.noreply.github.com> Co-authored-by: PabstMirror Co-authored-by: johnb432 <58661205+johnb432@users.noreply.github.com> --- addons/interact_menu/XEH_preInit.sqf | 10 +++-- .../functions/fnc_addActionToClass.sqf | 33 +++++++++------ .../functions/fnc_removeActionFromClass.sqf | 40 +++++++++++++++++-- .../framework/interactionMenu-framework.md | 5 +++ 4 files changed, 69 insertions(+), 19 deletions(-) diff --git a/addons/interact_menu/XEH_preInit.sqf b/addons/interact_menu/XEH_preInit.sqf index 88269bcc04..a62996df68 100644 --- a/addons/interact_menu/XEH_preInit.sqf +++ b/addons/interact_menu/XEH_preInit.sqf @@ -88,8 +88,8 @@ GVAR(inheritedClassesMan) = []; if (GVAR(inheritedClassesAll) pushBackUnique _type == -1) exitWith { END_COUNTER(InitPost); }; { - _x params ["_objectType", "_typeNum", "_parentPath", "_action"]; - if (_object isKindOf _objectType) then { + _x params ["_objectType", "_typeNum", "_parentPath", "_action", "_excludedClasses"]; + if (_type isKindOf _objectType && {_excludedClasses findIf {_type isKindOf _x} == -1}) then { [_type, _typeNum, _parentPath, _action] call FUNC(addActionToClass); }; } forEach GVAR(inheritedActionsAll); @@ -102,8 +102,10 @@ GVAR(inheritedClassesMan) = []; if (GVAR(inheritedClassesMan) pushBackUnique _type == -1) exitWith { END_COUNTER(InitPost); }; { - _x params ["_typeNum", "_parentPath", "_action"]; - [_type, _typeNum, _parentPath, _action] call FUNC(addActionToClass); + _x params ["_typeNum", "_parentPath", "_action", "_excludedClasses"]; + if (_excludedClasses findIf {_type isKindOf _x} == -1) then { // skip excluded classes and children + [_type, _typeNum, _parentPath, _action] call FUNC(addActionToClass); + }; } forEach GVAR(inheritedActionsMan); END_COUNTER(InitPost); }, true, ["VirtualMan_F"]] call CBA_fnc_addClassEventHandler; diff --git a/addons/interact_menu/functions/fnc_addActionToClass.sqf b/addons/interact_menu/functions/fnc_addActionToClass.sqf index ccea8c4654..91197d02d9 100644 --- a/addons/interact_menu/functions/fnc_addActionToClass.sqf +++ b/addons/interact_menu/functions/fnc_addActionToClass.sqf @@ -1,7 +1,7 @@ #include "..\script_component.hpp" /* * Author: esteldunedain - * Insert an ACE action to a class, under a certain path + * Inserts an ACE action to a class, under a certain path. * Note: This function is NOT global. * * Arguments: @@ -10,12 +10,13 @@ * 2: Parent path of the new action * 3: Action * 4: Use Inheritance (default: false) + * 5: Classes excluded from inheritance (children included) (default: []) * * Return Value: * The entry full path, which can be used to remove the entry, or add children entries . * * Example: - * [typeOf cursorTarget, 0, ["ACE_TapShoulderRight"],VulcanPinchAction] call ace_interact_menu_fnc_addActionToClass; + * [typeOf cursorTarget, 0, ["ACE_TapShoulderRight"], VulcanPinchAction] call ace_interact_menu_fnc_addActionToClass; * * Public: Yes */ @@ -25,22 +26,30 @@ if (!params [["_objectType", "", [""]], ["_typeNum", 0, [0]], ["_parentPath", [] ERROR("Bad Params"); [] }; -TRACE_4("addActionToClass",_objectType,_typeNum,_parentPath,_action); +private _useInheritance = _this param [4, false, [false]]; +private _excludedClasses = _this param [5, [], [[]]]; +TRACE_6("addActionToClass",_objectType,_typeNum,_parentPath,_action,_useInheritance,_excludedClasses); -if (param [4, false, [false]]) exitwith { +if (_useInheritance) exitwith { BEGIN_COUNTER(addAction); + private _cfgVehicles = configFile >> "CfgVehicles"; // store this so we don't resolve for every element + _excludedClasses = (_excludedClasses apply {configName (_cfgVehicles >> _x)}) - [""]; // ends up being faster than toLower'ing everything else if (_objectType == "CAManBase") then { - GVAR(inheritedActionsMan) pushBack [_typeNum, _parentPath, _action]; + GVAR(inheritedActionsMan) pushBack [_typeNum, _parentPath, _action, _excludedClasses]; { - [_x, _typeNum, _parentPath, _action] call FUNC(addActionToClass); - } forEach GVAR(inheritedClassesMan); - } else { - GVAR(inheritedActionsAll) pushBack [_objectType, _typeNum, _parentPath, _action]; - { - if (_x isKindOf _objectType) then { + private _type = _x; + if (_excludedClasses findIf {_type isKindOf _x} == -1) then { // skip excluded classes and children [_x, _typeNum, _parentPath, _action] call FUNC(addActionToClass); }; - } forEach GVAR(inheritedClassesAll); + } forEach (GVAR(inheritedClassesMan) - _excludedClasses); + } else { + GVAR(inheritedActionsAll) pushBack [_objectType, _typeNum, _parentPath, _action, _excludedClasses]; + { + private _type = _x; + if (_type isKindOf _objectType && {_excludedClasses findIf {_type isKindOf _x} == -1}) then { + [_type, _typeNum, _parentPath, _action] call FUNC(addActionToClass); + }; + } forEach (GVAR(inheritedClassesAll) - _excludedClasses); }; END_COUNTER(addAction); diff --git a/addons/interact_menu/functions/fnc_removeActionFromClass.sqf b/addons/interact_menu/functions/fnc_removeActionFromClass.sqf index 7585616ef6..87cc8609cc 100644 --- a/addons/interact_menu/functions/fnc_removeActionFromClass.sqf +++ b/addons/interact_menu/functions/fnc_removeActionFromClass.sqf @@ -1,29 +1,63 @@ #include "..\script_component.hpp" /* * Author: esteldunedain - * Removes an action from a class + * Removes an action from a class. * * Arguments: * 0: TypeOf of the class * 1: Type of action, 0 for actions, 1 for self-actions * 2: Full path of the new action + * 3: Remove action from child classes (default: false) * * Return Value: * None * * Example: - * [typeOf cursorTarget, 0,["ACE_TapShoulderRight","VulcanPinch"]] call ace_interact_menu_fnc_removeActionFromClass; + * [typeOf cursorTarget, 0, ["ACE_TapShoulderRight", "VulcanPinch"]] call ace_interact_menu_fnc_removeActionFromClass; * * Public: No */ -params ["_objectType", "_typeNum", "_fullPath"]; +params ["_objectType", "_typeNum", "_fullPath", ["_useInheritance", false, [false]]]; _objectType = _objectType call EFUNC(common,getConfigName); private _res = _fullPath call FUNC(splitPath); _res params ["_parentPath", "_actionName"]; +if (_useInheritance) exitWith { + // Only need to run for classes that have already been initialized + { + [_x, _typeNum, _fullPath] call FUNC(removeActionFromClass); + } forEach (GVAR(inheritedClassesAll) select {_x isKindOf _objectType}); + + // Find same path and actionName, and check if it's a parent class, needs to be checked for all classes + private _index = GVAR(inheritedActionsAll) findIf { + _x params ["_currentType", "", "_currentParentPath", "_currentAction"]; + + [_objectType isKindOf _currentType, _currentParentPath, _currentAction select 0] isEqualTo [true, _parentPath, _actionName] + }; + + // Add to exclude classes + if (_index != -1) then { + (GVAR(inheritedActionsAll) select _index select 4) pushBackUnique _objectType; + }; + + // Children of CAManBase need special treatment because of inheritedActionsMan array + if (_objectType isKindOf "CAManBase") then { + private _index = GVAR(inheritedActionsMan) findIf { + _x params ["", "_currentParentPath", "_currentAction"]; + + [_currentParentPath, _currentAction select 0] isEqualTo [_parentPath, _actionName] + }; + + // Different index because array doesn't include _objectType + if (_index != -1) then { + (GVAR(inheritedActionsMan) select _index select 3) pushBackUnique _objectType; + }; + }; +}; + private _namespace = [GVAR(ActNamespace), GVAR(ActSelfNamespace)] select _typeNum; private _actionTrees = _namespace getOrDefault [_objectType, []]; diff --git a/docs/wiki/framework/interactionMenu-framework.md b/docs/wiki/framework/interactionMenu-framework.md index fb66fb3918..18d5aec70f 100644 --- a/docs/wiki/framework/interactionMenu-framework.md +++ b/docs/wiki/framework/interactionMenu-framework.md @@ -110,6 +110,7 @@ Important: `ace_common_fnc_canInteractWith` is not automatically checked and nee * 2: Parent path of the new action * 3: Action * 4: Use Inheritance (Default: False) + * 5: Classes excluded from inheritance (children included) (Default: []) */ ``` By default this function will not use inheritance, so actions will only be added to the specific class. @@ -169,6 +170,10 @@ Using `addActionToClass` inheritance: _action = ["CheckFuel", "Check Fuel", "", {hint format ["Fuel: %1", fuel _target]}, {true}] call ace_interact_menu_fnc_createAction; ["LandVehicle", 0, ["ACE_MainActions"], _action, true] call ace_interact_menu_fnc_addActionToClass; +// Same as above, but children of "MRAP_01_Base" will not have the action +_action = ["CheckFuel", "Check Fuel", "", {hint format ["Fuel: %1", fuel _target]}, {true}] call ace_interact_menu_fnc_createAction; +["LandVehicle", 0, ["ACE_MainActions"], _action, true, ["MRAP_01_Base"]] call ace_interact_menu_fnc_addActionToClass; + // Adds action to check external fuel levels on tanks. Will be a sub action of the previous action. _action = ["CheckExtTank","Check External Tank","",{hint format ["Ext Tank: %1", 5]},{true}] call ace_interact_menu_fnc_createAction; ["Tank_F", 0, ["ACE_MainActions", "CheckFuel"], _action, true] call ace_interact_menu_fnc_addActionToClass;