Skip to content

Commit a22ac29

Browse files
authored
Convert compositions to hashes (#552)
1 parent 75c62a0 commit a22ac29

File tree

6 files changed

+55
-34
lines changed

6 files changed

+55
-34
lines changed

addons/compositions/XEH_preInit.sqf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,19 @@ PREP_RECOMPILE_END;
88

99
GVAR(randomize) = false;
1010

11+
private _compositions = GET_COMPOSITIONS;
12+
13+
if (_compositions isEqualType []) then {
14+
private _newCompositions = createHashMap;
15+
16+
{
17+
_x params ["_category", "_name", "_data"];
18+
19+
private _categoryHash = _newCompositions getOrDefault [_category, createHashMap, true];
20+
_categoryHash set [_name, _data];
21+
} forEach _compositions;
22+
23+
SET_COMPOSITIONS(_newCompositions);
24+
};
25+
1126
ADDON = true;

addons/compositions/functions/fnc_buttonEdit.sqf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ private _path = tvCurSel _ctrlTree;
2424
private _category = _ctrlTree tvText GET_PARENT_PATH(_path);
2525
private _name = _ctrlTree tvText _path;
2626

27-
private _index = FIND_COMPOSITION(_category,_name);
27+
private _composition = GET_COMPOSITION(_category,_name);
2828

29-
if (_index != -1) then {
30-
["edit", GET_COMPOSITIONS select _index] call FUNC(openDisplay);
29+
if (!isNil "_composition") then {
30+
["edit", [_category, _name, _composition]] call FUNC(openDisplay);
3131
};

addons/compositions/functions/fnc_initDisplayCurator.sqf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@
6464
_ctrlTree tvSetPictureRightColorSelected [[0, _index], [1, 1, 1, 1]];
6565

6666
// Initially add all compositions to the tree
67-
GVAR(treeAdditions) = +GET_COMPOSITIONS;
67+
GVAR(treeAdditions) = [];
68+
69+
{
70+
private _category = _x;
71+
{
72+
GVAR(treeAdditions) pushBack [_category, _x, _y];
73+
} forEach _y;
74+
} forEach GET_COMPOSITIONS;
75+
6876
[_display] call FUNC(processTreeAdditions);
6977

7078
_ctrlTree ctrlAddEventHandler ["TreeSelChanged", {call FUNC(handleTreeSelect)}];

addons/compositions/functions/fnc_openDisplay.sqf

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,9 @@ _ctrlList ctrlAddEventHandler ["LBSelChanged", {
3333
_display call (_display getVariable QFUNC(verify));
3434
}];
3535

36-
private _categories = [];
37-
3836
{
39-
_x params ["_category"];
40-
41-
if (_categories pushBackUnique _category != -1) then {
42-
_ctrlList lbAdd _category;
43-
};
44-
} forEach GET_COMPOSITIONS;
37+
_ctrlList lbAdd _x;
38+
} forEach keys GET_COMPOSITIONS;
4539

4640
// Verify entered values (not empty, unique category and name combination)
4741
_display setVariable [QFUNC(verify), {
@@ -64,7 +58,7 @@ _display setVariable [QFUNC(verify), {
6458
_ctrlButtonOK ctrlSetTooltip localize LSTRING(NameCannotBeEmpty);
6559
};
6660

67-
private _enabled = FIND_COMPOSITION(_category,_name) == -1;
61+
private _enabled = isNil {GET_COMPOSITION(_category,_name)};
6862
private _tooltip = if (_enabled) then {""} else {localize LSTRING(CompositionAlreadyExists)};
6963

7064
_ctrlButtonOK ctrlEnable _enabled;
@@ -93,7 +87,7 @@ private _ctrlTitle = _display displayCtrl IDC_DISPLAY_TITLE;
9387
_ctrlTitle ctrlSetText localize _title;
9488

9589
// Set the current composition category and name
96-
_composition params ["_category", "_name"];
90+
_composition params ["_category", "_name", "_data"];
9791
_ctrlCategory ctrlSetText _category;
9892
_ctrlName ctrlSetText _name;
9993

@@ -104,29 +98,31 @@ private _ctrlButtonOK = _display displayCtrl IDC_OK;
10498

10599
[_ctrlButtonOK, "ButtonClick", {
106100
params ["_ctrlButtonOK"];
107-
_thisArgs params ["_mode", "_composition"];
101+
_thisArgs params ["_mode", "_data"];
108102

109103
private _display = ctrlParent _ctrlButtonOK;
110104
private _ctrlCategory = _display displayCtrl IDC_DISPLAY_CATEGORY;
111105
private _ctrlName = _display displayCtrl IDC_DISPLAY_NAME;
112106

113107
// Set the new composition category and name
114-
_composition set [0, ctrlText _ctrlCategory];
115-
_composition set [1, ctrlText _ctrlName];
116-
117-
if (_mode == "create") then {
118-
// In create mode, add the composition to saved data
119-
private _compositions = GET_COMPOSITIONS;
120-
_compositions pushBack _composition;
121-
SET_COMPOSITIONS(_compositions);
122-
} else {
108+
private _category = ctrlText _ctrlCategory;
109+
private _name = ctrlText _ctrlName;
110+
111+
// Add the composition to saved data
112+
private _compositions = GET_COMPOSITIONS;
113+
private _categoryHash = _compositions getOrDefault [_category, createHashMap, true];
114+
_categoryHash set [_name, _data];
115+
116+
if (_mode == "edit") then {
123117
// In edit mode, remove the old composition from the tree
124-
[false] call FUNC(removeFromTree);
118+
[true] call FUNC(removeFromTree);
125119
};
126120

121+
SET_COMPOSITIONS(_compositions);
122+
127123
// Add the new/updated composition to the tree
128-
GVAR(treeAdditions) pushBack +_composition;
124+
GVAR(treeAdditions) pushBack [_category, _name, +_data];
129125
[findDisplay IDD_RSCDISPLAYCURATOR] call FUNC(processTreeAdditions);
130126

131127
saveProfileNamespace;
132-
}, _this] call CBA_fnc_addBISEventHandler;
128+
}, [_mode, _data]] call CBA_fnc_addBISEventHandler;

addons/compositions/functions/fnc_removeFromTree.sqf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,20 @@ private _name = _ctrlTree tvText _path;
3131
_ctrlTree tvDelete _path;
3232

3333
// Delete the composition from saved data if needed
34+
private _compositions = GET_COMPOSITIONS;
35+
private _categoryHash = _compositions get _category;
36+
3437
if (_deleteFromData) then {
35-
GET_COMPOSITIONS deleteAt FIND_COMPOSITION(_category,_name);
38+
_categoryHash deleteAt _name;
3639
};
3740

3841
// Delete the category from the tree if no more compositions exist under it
39-
if (!CATEGORY_EXISTS(_category)) then {
42+
if (keys _categoryHash isEqualTo []) then {
4043
private _categories = _ctrlTree getVariable [QGVAR(categories), []];
4144
_categories deleteAt (_categories find _category);
42-
4345
_ctrlTree tvDelete GET_PARENT_PATH(_path);
46+
47+
_compositions deleteAt _category;
4448
};
4549

4650
// Manually trigger tree selection change

addons/compositions/script_component.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@
4848
#define COMPOSITION_STR QUOTE(COMPOSITION)
4949

5050
#define VAR_COMPOSITIONS QGVAR(data)
51-
#define GET_COMPOSITIONS (profileNamespace getVariable [VAR_COMPOSITIONS, []])
51+
#define GET_COMPOSITIONS (profileNamespace getVariable [VAR_COMPOSITIONS, createHashMap])
5252
#define SET_COMPOSITIONS(value) (profileNamespace setVariable [VAR_COMPOSITIONS, value])
5353

54-
#define CATEGORY_EXISTS(category) (GET_COMPOSITIONS findIf {category isEqualTo (_x select 0)} != -1)
55-
56-
#define FIND_COMPOSITION(category,name) (GET_COMPOSITIONS findIf {category isEqualTo (_x select 0) && {name isEqualTo (_x select 1)}})
54+
#define GET_COMPOSITION(category,name) (GET_COMPOSITIONS get category get name)
5755

5856
#define OBJECT_DATA_VAR(category,name) format [QGVAR(%1:%2), category, name]
5957

0 commit comments

Comments
 (0)