From 27d85180f111a5b38519d9b787d829cc7ae41640 Mon Sep 17 00:00:00 2001 From: SilentSpike Date: Fri, 17 Jul 2015 02:30:00 +0100 Subject: [PATCH] Improve handling of unit tree --- addons/spectator/UI/interface.hpp | 2 +- .../functions/fnc_handleInterface.sqf | 2 +- addons/spectator/functions/fnc_handleTree.sqf | 87 +++++++------------ .../spectator/functions/fnc_updateUnits.sqf | 11 +-- 4 files changed, 38 insertions(+), 64 deletions(-) diff --git a/addons/spectator/UI/interface.hpp b/addons/spectator/UI/interface.hpp index 897f5d4a6a..3114196e0a 100644 --- a/addons/spectator/UI/interface.hpp +++ b/addons/spectator/UI/interface.hpp @@ -174,7 +174,7 @@ class GVAR(interface) { h = TOOL_H; sizeEx = TOOL_H; text = CSTRING(RefreshList); - action = QUOTE([] call FUNC(handleTree)); + action = QUOTE([allUnits] call FUNC(updateUnits)); }; }; }; diff --git a/addons/spectator/functions/fnc_handleInterface.sqf b/addons/spectator/functions/fnc_handleInterface.sqf index 3cd598419e..377bce9124 100644 --- a/addons/spectator/functions/fnc_handleInterface.sqf +++ b/addons/spectator/functions/fnc_handleInterface.sqf @@ -138,7 +138,7 @@ switch (toLower _mode) do { (_display displayCtrl IDC_TOOL_VIEW) ctrlSetText (["FREE","FIRST","THIRD"] select GVAR(camMode)); // Keep unit tree up to date - [] call FUNC(handleTree); + [FUNC(handleTree), 10] call CBA_fnc_addPerFrameHandler; // Hacky way to enable keybindings //_display displayAddEventHandler ["KeyUp", {[_this,'keyup'] call CBA_events_fnc_keyHandler}]; diff --git a/addons/spectator/functions/fnc_handleTree.sqf b/addons/spectator/functions/fnc_handleTree.sqf index fc647f5a9a..69225d2c06 100644 --- a/addons/spectator/functions/fnc_handleTree.sqf +++ b/addons/spectator/functions/fnc_handleTree.sqf @@ -1,6 +1,6 @@ /* * Author: SilentSpike - * Tracks the units in the unit tree and updates it according to their status + * Tracks the the unit list and updates the unit tree according to its status * * Arguments: * None @@ -16,11 +16,8 @@ #include "script_component.hpp" -// Remove any existing PFH -if !(isNil QGVAR(treePFH)) then { - [GVAR(treePFH)] call CBA_fnc_removePerFrameHandler; - GVAR(treePFH) = nil; -}; +// Kill PFH when display is closed +if (isNull (GETUVAR(GVAR(display),displayNull))) exitWith { [_this select 1] call CBA_fnc_removePerFrameHandler; }; // Fetch tree disableSerialization; @@ -33,63 +30,39 @@ _curSelData = _ctrl tvData (tvCurSel _ctrl); // Clear the tree tvClear _ctrl; -// Update the tree +// Update the tree from the unit list _cachedGrps = []; { - _grp = group _x; - _node = 0; - // If group already exists, find existing node - if !(_grp in _cachedGrps) then { - _side = [west,east,resistance,civilian,sideLogic] find (side _grp); - _node = _ctrl tvCount []; + // Exclude any currently dead units + if ((alive _x) && !(_x getVariable [QGVAR(isSpectator), false])) then { + _grp = group _x; + _node = 0; + // If group already exists, find existing node + if !(_grp in _cachedGrps) then { + _side = [west,east,resistance,civilian,sideLogic] find (side _grp); + _node = _ctrl tvCount []; - _ctrl tvAdd [[], groupID _grp]; - _ctrl tvSetValue [[_node], _side]; + _ctrl tvAdd [[], groupID _grp]; + _ctrl tvSetValue [[_node], _side]; - _cachedGrps pushBack _grp; - } else { - _node = _cachedGrps find _grp; + _cachedGrps pushBack _grp; + } else { + _node = _cachedGrps find _grp; + }; + + _index = _ctrl tvCount [_node]; + + _ctrl tvAdd [[_node], name _x]; + _ctrl tvSetData [[_node,_index], netID _x]; + + // Preserve the previous selection + if (_curSelData == (_ctrl tvData [_node,_index])) then { + _ctrl tvSetCurSel [_node,_index]; + }; + + _ctrl tvSort [[_node],false]; }; - - _index = _ctrl tvCount [_node]; - - _ctrl tvAdd [[_node], name _x]; - _ctrl tvSetData [[_node,_index], netID _x]; - - // Preserve the previous selection - if (_curSelData == (_ctrl tvData [_node,_index])) then { - _ctrl tvSetCurSel [_node,_index]; - }; - - _ctrl tvSort [[_node],false]; } forEach GVAR(unitList); // Sort group nodes by side _ctrl tvSortByValue [[],false]; - -// Keep tree up to date -GVAR(treePFH) = [{ - // Kill PFH when display is closed - if (isNull (GETUVAR(GVAR(display),displayNull))) exitWith { - [_this select 1] call CBA_fnc_removePerFrameHandler; - GVAR(treePFH) = nil; - }; - - disableSerialization; - _display = GETUVAR(GVAR(display),displayNull); - _ctrl = (_display displayCtrl IDC_UNIT) controlsGroupCtrl IDC_UNIT_TREE; - - _nodes = (_ctrl tvCount []) - 1; - for "_n" from 0 to _nodes do { - _units = (_ctrl tvCount [_n]) - 1; - for "_i" from 0 to _units do { - _netID = _ctrl tvData [_n,_i]; - _unit = objectFromNetId _netID; - - if (isNull _unit || !alive _unit) then { - _ctrl tvDelete [_n,_i]; - [[_unit],false] call FUNC(updateUnits); - }; - }; - }; -}, 5] call CBA_fnc_addPerFrameHandler; diff --git a/addons/spectator/functions/fnc_updateUnits.sqf b/addons/spectator/functions/fnc_updateUnits.sqf index 1fb29eb6a5..9586e41535 100644 --- a/addons/spectator/functions/fnc_updateUnits.sqf +++ b/addons/spectator/functions/fnc_updateUnits.sqf @@ -32,15 +32,14 @@ if (_permanent) then { }; if (_append) then { - _newUnits = _newUnits - GVAR(unitList); - // Append only valid units to the list { if ( - (_x isKindOf "CAManBase") && - {(side _x) in [west,east,resistance,civilian]} && + (alive _x) && + {(_x isKindOf "CAManBase")} && + {(side _x) in [west,east,resistance,civilian]} && // Side filter {(isPlayer _x) || GVAR(allowAI)} && // AI restriction - {(simulationEnabled _x)} && //!isObjectHidden _unit} && // (currently dev branch only) + {(simulationEnabled _x) && !isObjectHidden _unit} && {!(_x getVariable [QGVAR(isSpectator), false])} // Who watches the watchmen? ) then { GVAR(unitList) pushBack _x; @@ -53,3 +52,5 @@ if (_append) then { // Apply whitelist and blacklist GVAR(unitList) append GVAR(unitWhitelist); GVAR(unitList) = GVAR(unitList) - GVAR(unitBlacklist); + +GVAR(unitList) arrayIntersect GVAR(unitList);