-
Notifications
You must be signed in to change notification settings - Fork 97
/
GroupHandler.cpp
219 lines (169 loc) · 4.96 KB
/
GroupHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include <SDL_keycode.h>
#include "GroupHandler.h"
#include "Group.h"
#include "Game/SelectedUnitsHandler.h"
#include "Game/CameraHandler.h"
#include "Sim/Units/UnitHandler.h"
#include "System/Input/KeyInput.h"
#include "System/Log/ILog.h"
#include "System/ContainerUtil.h"
#include "System/EventHandler.h"
#include "System/StringHash.h"
#include "System/Misc/TracyDefs.h"
std::vector<CGroupHandler> uiGroupHandlers;
CR_BIND(CGroupHandler, (0))
CR_REG_METADATA(CGroupHandler, (
CR_MEMBER(groups),
CR_MEMBER(freeGroups),
CR_MEMBER(changedGroups),
CR_MEMBER(unitGroups),
CR_MEMBER(team),
CR_MEMBER(firstUnusedGroup)
))
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGroupHandler::CGroupHandler(int teamId): team(teamId)
{
RECOIL_DETAILED_TRACY_ZONE;
groups.reserve(FIRST_SPECIAL_GROUP);
for (int groupId = 0; groupId < FIRST_SPECIAL_GROUP; ++groupId) {
groups.emplace_back(groupId, teamId);
}
}
void CGroupHandler::Update()
{
RECOIL_DETAILED_TRACY_ZONE;
{
for (CGroup& g: groups) {
// may invoke RemoveGroup, but can not cause iterator invalidation
g.Update();
}
}
{
if (changedGroups.empty())
return;
std::vector<int> grpChg;
// swap containers to prevent recursion through lua
changedGroups.swap(grpChg);
for (int i: grpChg) {
eventHandler.GroupChanged(i);
}
}
}
bool CGroupHandler::GroupCommand(int num)
{
RECOIL_DETAILED_TRACY_ZONE;
bool error;
if (KeyInput::GetKeyModState(KMOD_CTRL))
return GroupCommand(num, (!KeyInput::GetKeyModState(KMOD_SHIFT))? "set": "add", error);
if (KeyInput::GetKeyModState(KMOD_SHIFT))
return GroupCommand(num, "selectadd", error);
if (KeyInput::GetKeyModState(KMOD_ALT))
return GroupCommand(num, "selecttoggle", error);
return GroupCommand(num, "", error);
}
bool CGroupHandler::GroupCommand(int num, const std::string& cmd, bool& error)
{
RECOIL_DETAILED_TRACY_ZONE;
error = false;
CGroup* group = GetGroup(num);
switch (hashString(cmd.c_str())) {
case hashString("set"):
case hashString("add"): {
if (cmd[0] == 's')
group->ClearUnits();
for (const int unitID: selectedUnitsHandler.selectedUnits) {
CUnit* u = unitHandler.GetUnit(unitID);
if (u == nullptr) {
assert(false);
continue;
}
// change group, but do not call SUH::AddUnit while iterating
u->SetGroup(group, false, false);
}
} break;
case hashString("select"): {
if (group->units.empty())
return false;
selectedUnitsHandler.SelectGroup(num);
return true;
} break;
case hashString("focus"): {
if (group->units.empty())
return false;
camHandler->CameraTransition(0.5f);
camHandler->GetCurrentController().SetPos(group->CalculateCenter());
return true;
} break;
case hashString("selectadd"): {
// do not select the group, just add its members to the current selection
for (const int unitID: group->units) {
selectedUnitsHandler.AddUnit(unitHandler.GetUnit(unitID));
}
return true;
} break;
case hashString("selectclear"): {
// do not select the group, just remove its members from the current selection
for (const int unitID: group->units) {
selectedUnitsHandler.RemoveUnit(unitHandler.GetUnit(unitID));
}
return true;
} break;
case hashString("selecttoggle"): {
// do not select the group, just toggle its members with the current selection
const auto& selUnits = selectedUnitsHandler.selectedUnits;
for (const int unitID: group->units) {
CUnit* unit = unitHandler.GetUnit(unitID);
if (selUnits.find(unitID) == selUnits.end()) {
selectedUnitsHandler.AddUnit(unit);
} else {
selectedUnitsHandler.RemoveUnit(unit);
}
}
return true;
} break;
// Selects or focuses camera on group.
case hashString(""): break;
// Unrecognized command
default: {
error = true;
return false;
}
}
if (group->units.empty())
return false;
if (selectedUnitsHandler.IsGroupSelected(num)) {
camHandler->CameraTransition(0.5f);
camHandler->GetCurrentController().SetPos(group->CalculateCenter());
}
selectedUnitsHandler.SelectGroup(num);
return true;
}
CGroup* CGroupHandler::CreateNewGroup()
{
RECOIL_DETAILED_TRACY_ZONE;
if (freeGroups.empty()) {
groups.emplace_back(firstUnusedGroup++, team);
return &groups[groups.size() - 1];
}
return &groups[ spring::VectorBackPop(freeGroups) ];
}
void CGroupHandler::RemoveGroup(CGroup* group)
{
RECOIL_DETAILED_TRACY_ZONE;
if (group->id < FIRST_SPECIAL_GROUP) {
LOG_L(L_WARNING, "[GroupHandler::%s] trying to remove hot-key group %i", __func__, group->id);
return;
}
if (selectedUnitsHandler.IsGroupSelected(group->id))
selectedUnitsHandler.ClearSelected();
group->ClearUnits();
freeGroups.push_back(group->id);
}
void CGroupHandler::PushGroupChange(int id)
{
RECOIL_DETAILED_TRACY_ZONE;
spring::VectorInsertUnique(changedGroups, id, true);
}