From a22ac29dd1c79d7a52023375e4c40ec45589dc44 Mon Sep 17 00:00:00 2001 From: Neil Evers Date: Fri, 3 Sep 2021 22:43:38 +0200 Subject: [PATCH] Convert compositions to hashes (#552) --- addons/compositions/XEH_preInit.sqf | 15 +++++++ .../compositions/functions/fnc_buttonEdit.sqf | 6 +-- .../functions/fnc_initDisplayCurator.sqf | 10 ++++- .../functions/fnc_openDisplay.sqf | 42 +++++++++---------- .../functions/fnc_removeFromTree.sqf | 10 +++-- addons/compositions/script_component.hpp | 6 +-- 6 files changed, 55 insertions(+), 34 deletions(-) diff --git a/addons/compositions/XEH_preInit.sqf b/addons/compositions/XEH_preInit.sqf index b8d1cc3e5..d33532e92 100644 --- a/addons/compositions/XEH_preInit.sqf +++ b/addons/compositions/XEH_preInit.sqf @@ -8,4 +8,19 @@ PREP_RECOMPILE_END; GVAR(randomize) = false; +private _compositions = GET_COMPOSITIONS; + +if (_compositions isEqualType []) then { + private _newCompositions = createHashMap; + + { + _x params ["_category", "_name", "_data"]; + + private _categoryHash = _newCompositions getOrDefault [_category, createHashMap, true]; + _categoryHash set [_name, _data]; + } forEach _compositions; + + SET_COMPOSITIONS(_newCompositions); +}; + ADDON = true; diff --git a/addons/compositions/functions/fnc_buttonEdit.sqf b/addons/compositions/functions/fnc_buttonEdit.sqf index 138976da2..9e4ec66cb 100644 --- a/addons/compositions/functions/fnc_buttonEdit.sqf +++ b/addons/compositions/functions/fnc_buttonEdit.sqf @@ -24,8 +24,8 @@ private _path = tvCurSel _ctrlTree; private _category = _ctrlTree tvText GET_PARENT_PATH(_path); private _name = _ctrlTree tvText _path; -private _index = FIND_COMPOSITION(_category,_name); +private _composition = GET_COMPOSITION(_category,_name); -if (_index != -1) then { - ["edit", GET_COMPOSITIONS select _index] call FUNC(openDisplay); +if (!isNil "_composition") then { + ["edit", [_category, _name, _composition]] call FUNC(openDisplay); }; diff --git a/addons/compositions/functions/fnc_initDisplayCurator.sqf b/addons/compositions/functions/fnc_initDisplayCurator.sqf index ade8ed492..a61af62c6 100644 --- a/addons/compositions/functions/fnc_initDisplayCurator.sqf +++ b/addons/compositions/functions/fnc_initDisplayCurator.sqf @@ -64,7 +64,15 @@ _ctrlTree tvSetPictureRightColorSelected [[0, _index], [1, 1, 1, 1]]; // Initially add all compositions to the tree - GVAR(treeAdditions) = +GET_COMPOSITIONS; + GVAR(treeAdditions) = []; + + { + private _category = _x; + { + GVAR(treeAdditions) pushBack [_category, _x, _y]; + } forEach _y; + } forEach GET_COMPOSITIONS; + [_display] call FUNC(processTreeAdditions); _ctrlTree ctrlAddEventHandler ["TreeSelChanged", {call FUNC(handleTreeSelect)}]; diff --git a/addons/compositions/functions/fnc_openDisplay.sqf b/addons/compositions/functions/fnc_openDisplay.sqf index 3fe1d2a3d..16cd60bf1 100644 --- a/addons/compositions/functions/fnc_openDisplay.sqf +++ b/addons/compositions/functions/fnc_openDisplay.sqf @@ -33,15 +33,9 @@ _ctrlList ctrlAddEventHandler ["LBSelChanged", { _display call (_display getVariable QFUNC(verify)); }]; -private _categories = []; - { - _x params ["_category"]; - - if (_categories pushBackUnique _category != -1) then { - _ctrlList lbAdd _category; - }; -} forEach GET_COMPOSITIONS; + _ctrlList lbAdd _x; +} forEach keys GET_COMPOSITIONS; // Verify entered values (not empty, unique category and name combination) _display setVariable [QFUNC(verify), { @@ -64,7 +58,7 @@ _display setVariable [QFUNC(verify), { _ctrlButtonOK ctrlSetTooltip localize LSTRING(NameCannotBeEmpty); }; - private _enabled = FIND_COMPOSITION(_category,_name) == -1; + private _enabled = isNil {GET_COMPOSITION(_category,_name)}; private _tooltip = if (_enabled) then {""} else {localize LSTRING(CompositionAlreadyExists)}; _ctrlButtonOK ctrlEnable _enabled; @@ -93,7 +87,7 @@ private _ctrlTitle = _display displayCtrl IDC_DISPLAY_TITLE; _ctrlTitle ctrlSetText localize _title; // Set the current composition category and name -_composition params ["_category", "_name"]; +_composition params ["_category", "_name", "_data"]; _ctrlCategory ctrlSetText _category; _ctrlName ctrlSetText _name; @@ -104,29 +98,31 @@ private _ctrlButtonOK = _display displayCtrl IDC_OK; [_ctrlButtonOK, "ButtonClick", { params ["_ctrlButtonOK"]; - _thisArgs params ["_mode", "_composition"]; + _thisArgs params ["_mode", "_data"]; private _display = ctrlParent _ctrlButtonOK; private _ctrlCategory = _display displayCtrl IDC_DISPLAY_CATEGORY; private _ctrlName = _display displayCtrl IDC_DISPLAY_NAME; // Set the new composition category and name - _composition set [0, ctrlText _ctrlCategory]; - _composition set [1, ctrlText _ctrlName]; - - if (_mode == "create") then { - // In create mode, add the composition to saved data - private _compositions = GET_COMPOSITIONS; - _compositions pushBack _composition; - SET_COMPOSITIONS(_compositions); - } else { + private _category = ctrlText _ctrlCategory; + private _name = ctrlText _ctrlName; + + // Add the composition to saved data + private _compositions = GET_COMPOSITIONS; + private _categoryHash = _compositions getOrDefault [_category, createHashMap, true]; + _categoryHash set [_name, _data]; + + if (_mode == "edit") then { // In edit mode, remove the old composition from the tree - [false] call FUNC(removeFromTree); + [true] call FUNC(removeFromTree); }; + SET_COMPOSITIONS(_compositions); + // Add the new/updated composition to the tree - GVAR(treeAdditions) pushBack +_composition; + GVAR(treeAdditions) pushBack [_category, _name, +_data]; [findDisplay IDD_RSCDISPLAYCURATOR] call FUNC(processTreeAdditions); saveProfileNamespace; -}, _this] call CBA_fnc_addBISEventHandler; +}, [_mode, _data]] call CBA_fnc_addBISEventHandler; diff --git a/addons/compositions/functions/fnc_removeFromTree.sqf b/addons/compositions/functions/fnc_removeFromTree.sqf index d0012b13a..14d000502 100644 --- a/addons/compositions/functions/fnc_removeFromTree.sqf +++ b/addons/compositions/functions/fnc_removeFromTree.sqf @@ -31,16 +31,20 @@ private _name = _ctrlTree tvText _path; _ctrlTree tvDelete _path; // Delete the composition from saved data if needed +private _compositions = GET_COMPOSITIONS; +private _categoryHash = _compositions get _category; + if (_deleteFromData) then { - GET_COMPOSITIONS deleteAt FIND_COMPOSITION(_category,_name); + _categoryHash deleteAt _name; }; // Delete the category from the tree if no more compositions exist under it -if (!CATEGORY_EXISTS(_category)) then { +if (keys _categoryHash isEqualTo []) then { private _categories = _ctrlTree getVariable [QGVAR(categories), []]; _categories deleteAt (_categories find _category); - _ctrlTree tvDelete GET_PARENT_PATH(_path); + + _compositions deleteAt _category; }; // Manually trigger tree selection change diff --git a/addons/compositions/script_component.hpp b/addons/compositions/script_component.hpp index 332da76cf..e9460d946 100644 --- a/addons/compositions/script_component.hpp +++ b/addons/compositions/script_component.hpp @@ -48,12 +48,10 @@ #define COMPOSITION_STR QUOTE(COMPOSITION) #define VAR_COMPOSITIONS QGVAR(data) -#define GET_COMPOSITIONS (profileNamespace getVariable [VAR_COMPOSITIONS, []]) +#define GET_COMPOSITIONS (profileNamespace getVariable [VAR_COMPOSITIONS, createHashMap]) #define SET_COMPOSITIONS(value) (profileNamespace setVariable [VAR_COMPOSITIONS, value]) -#define CATEGORY_EXISTS(category) (GET_COMPOSITIONS findIf {category isEqualTo (_x select 0)} != -1) - -#define FIND_COMPOSITION(category,name) (GET_COMPOSITIONS findIf {category isEqualTo (_x select 0) && {name isEqualTo (_x select 1)}}) +#define GET_COMPOSITION(category,name) (GET_COMPOSITIONS get category get name) #define OBJECT_DATA_VAR(category,name) format [QGVAR(%1:%2), category, name]