forked from sirjuddington/SLADE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArgs.cpp
307 lines (269 loc) · 8.05 KB
/
Args.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// ----------------------------------------------------------------------------
// SLADE - It's a Doom Editor
// Copyright(C) 2008 - 2017 Simon Judd
//
// Email: [email protected]
// Web: https://slade.mancubus.net
// Filename: Args.cpp
// Description: Arg and related structs for ActionSpecial and ThingType arg
// definitions
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301, USA.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//
// Includes
//
// ----------------------------------------------------------------------------
#include "Main.h"
#include "Args.h"
#include "Utility/Parser.h"
using namespace Game;
// ----------------------------------------------------------------------------
//
// Local Functions
//
// ----------------------------------------------------------------------------
namespace
{
// ------------------------------------------------------------------------
// customFlags
//
// Returns a string representation of [value] for a 'custom flags' type
// arg, given [custom_flags]
// ------------------------------------------------------------------------
string customFlags(int value, const vector<ArgValue>& custom_flags)
{
// This has to go in REVERSE order to correctly handle multi-bit
// enums (so we see 3 before 1 and 2)
vector<string> flags;
size_t final_length = 0;
int last_group = 0;
int original_value = value;
bool has_flag;
for (auto it = custom_flags.rbegin();
it != custom_flags.rend();
++it)
{
if ((it->value & (it->value - 1)) != 0)
// Not a power of two, so must be a group
last_group = value;
if (value == 0)
// Zero is special: it only counts as a flag value if the
// most recent "group" is empty
has_flag = (last_group && (original_value & last_group) == 0);
else
has_flag = ((value & it->value) == it->value);
if (has_flag)
{
value &= ~it->value;
flags.push_back(it->name);
final_length += it->name.size() + 3;
}
}
if (value || !flags.size())
flags.push_back(S_FMT("%d", value));
// Join 'em, in reverse again, to restore the original order
string out;
out.Alloc(final_length);
auto it = flags.rbegin();
while (true)
{
out += *it;
++it;
if (it == flags.rend())
break;
out += " + ";
}
return out;
}
}
// ----------------------------------------------------------------------------
//
// Arg Struct Functions
//
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Arg::valueString
//
// Returns a string representation of [value] depending on the arg's type
// ----------------------------------------------------------------------------
string Arg::valueString(int value) const
{
switch (type)
{
case YesNo:
return (value > 0) ? "Yes" : "No";
case NoYes:
return (value > 0) ? "No" : "Yes";
// Custom list of choices
case Choice:
{
for (auto& cv : custom_values)
if (cv.value == value)
return cv.name;
break;
}
// Custom list of flags
case Flags:
return customFlags(value, custom_flags);
// Angle
case Angle:
return S_FMT("%d Degrees", value); // TODO: E/S/W/N/etc
// Speed
case Speed:
{
string speed_label = speedLabel(value);
if (speed_label.empty())
return S_FMT("%d", value);
else
return S_FMT("%d (%s)", value, speed_label);
}
default:
break;
}
// Any other type
return S_FMT("%d", value);
}
// ----------------------------------------------------------------------------
// Arg::speedLabel
//
// Returns a string representation of speed [value]
// ----------------------------------------------------------------------------
string Arg::speedLabel(int value) const
{
// Speed can optionally have a set of predefined values, most taken
// from the Boom generalized values
if (custom_values.empty())
return "";
if (value == 0)
return "broken";
if (value < custom_values.front().value)
return S_FMT("< %s", custom_values.front().name);
if (value > custom_values.back().value)
return S_FMT("> %s", custom_values.back().name);
for (unsigned a = 0; a < custom_values.size(); a++)
{
if (value == custom_values[a].value)
return custom_values[a].name;
if (a > 0 && value < custom_values[a].value)
return S_FMT("%s ~ %s", custom_values[a - 1].name, custom_values[a].name);
}
return "";
}
// ----------------------------------------------------------------------------
// Arg::parse
//
// Parses an arg definition from [node], using [shared_args] for predeclared
// args if it is given
// ----------------------------------------------------------------------------
void Arg::parse(ParseTreeNode* node, SpecialMap* shared_args)
{
// Check for simple definition
if (node->isLeaf())
{
string name = node->stringValue();
string shared_arg_name;
// Names beginning with a dollar sign are references to predeclared args
if (shared_args && name.StartsWith("$", &shared_arg_name))
{
auto it = shared_args->find(shared_arg_name);
if (it == shared_args->end())
// Totally bogus reference; silently ignore this arg
return;
*this = it->second;
}
else
{
// Set name
this->name = node->stringValue();
// Set description (if specified)
if (node->nValues() > 1) desc = node->stringValue(1);
}
}
else
{
// Extended arg definition
// Name
auto val = node->getChildPTN("name");
if (val) name = val->stringValue();
// Description
val = node->getChildPTN("desc");
if (val) desc = val->stringValue();
// Type
val = node->getChildPTN("type");
string atype;
if (val) atype = val->stringValue();
if (S_CMPNOCASE(atype, "yesno"))
type = YesNo;
else if (S_CMPNOCASE(atype, "noyes"))
type = NoYes;
else if (S_CMPNOCASE(atype, "angle"))
type = Angle;
else if (S_CMPNOCASE(atype, "choice"))
type = Choice;
else if (S_CMPNOCASE(atype, "flags"))
type = Flags;
else if (S_CMPNOCASE(atype, "speed"))
type = Speed;
else
type = Number;
// Customs
val = node->getChildPTN("custom_values");
if (val)
{
for (auto cv : val->allChildren())
custom_values.push_back({ Parser::node(cv)->stringValue(), atoi(CHR(cv->getName())) });
}
val = node->getChildPTN("custom_flags");
if (val)
{
for (auto cf : val->allChildren())
custom_flags.push_back({ Parser::node(cf)->stringValue(), atoi(CHR(cf->getName())) });
}
}
}
// ----------------------------------------------------------------------------
//
// ArgSpec Struct Functions
//
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// ArgSpec::stringDesc
//
// Returns a string representation of [values] depending on the spec arg types
// ----------------------------------------------------------------------------
string ArgSpec::stringDesc(int values[5], string values_str[2]) const
{
string ret;
// Add each arg to the string
for (unsigned a = 0; a < 5; a++)
{
// Skip if the arg name is undefined and the arg value is 0
if (values[a] == 0 && args[a].name.StartsWith("Arg"))
continue;
ret += args[a].name;
ret += ": ";
if (a < 2 && values[a] == 0 && !values_str[a].IsEmpty())
ret += values_str[a];
else
ret += args[a].valueString(values[a]);
ret += ", ";
}
// Cut ending ", "
if (!ret.IsEmpty())
ret.RemoveLast(2);
return ret;
}