Skip to content

Commit

Permalink
Convert compositions to hashes (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilzar authored Sep 3, 2021
1 parent 75c62a0 commit a22ac29
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 34 deletions.
15 changes: 15 additions & 0 deletions addons/compositions/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 3 additions & 3 deletions addons/compositions/functions/fnc_buttonEdit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
10 changes: 9 additions & 1 deletion addons/compositions/functions/fnc_initDisplayCurator.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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)}];
Expand Down
42 changes: 19 additions & 23 deletions addons/compositions/functions/fnc_openDisplay.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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), {
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;
10 changes: 7 additions & 3 deletions addons/compositions/functions/fnc_removeFromTree.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions addons/compositions/script_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit a22ac29

Please sign in to comment.