File: option.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (268 lines) | stat: -rw-r--r-- 8,476 bytes parent folder | download
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
//
//

#include "option.h"
#include "enums.h"

namespace scripting {
namespace api {

ADE_OBJ(l_ValueDescription, options::ValueDescription, "ValueDescription",
        "An option value that contains a displayable string and the serialized value.");

ADE_FUNC(__tostring, l_ValueDescription, nullptr, "Value display string", "string",
         "The display string or nil on error")
{
	options::ValueDescription* desc;
	if (!ade_get_args(L, "o", l_ValueDescription.GetPtr(&desc))) {
		return ADE_RETURN_NIL;
	}
	return ade_set_args(L, "s", desc->display.c_str());
}
ADE_FUNC(__eq, l_ValueDescription, "ValueDescription other", "Compares two value descriptions", "string",
         "True if equal, false otherwise")
{
	options::ValueDescription* desc;
	options::ValueDescription* other;
	if (!ade_get_args(L, "oo", l_ValueDescription.GetPtr(&desc), l_ValueDescription.GetPtr(&other))) {
		return ADE_RETURN_NIL;
	}
	return ade_set_args(L, "b", desc->serialized == other->serialized);
}
ADE_VIRTVAR(Display, l_ValueDescription, nullptr, "Value display string", "string",
            "The display string or nil on error")
{
	options::ValueDescription* desc;
	if (!ade_get_args(L, "o", l_ValueDescription.GetPtr(&desc))) {
		return ADE_RETURN_NIL;
	}
	return ade_set_args(L, "s", desc->display.c_str());
}
ADE_VIRTVAR(Serialized, l_ValueDescription, nullptr, "Serialized string value of the contained value", "string",
            "The serialized string or nil on error")
{
	options::ValueDescription* desc;
	if (!ade_get_args(L, "o", l_ValueDescription.GetPtr(&desc))) {
		return ADE_RETURN_NIL;
	}
	return ade_set_args(L, "s", desc->serialized.c_str());
}

option_h::option_h(const options::OptionBase* opt) : _opt(opt) {}
bool option_h::isValid() const { return _opt != nullptr; }
const options::OptionBase* option_h::get() { return _opt; }

//**********HANDLE: Object
ADE_OBJ(l_Option, option_h, "option", "Option handle");

ADE_VIRTVAR(Title, l_Option, nullptr, "The title of this option (read-only)", "string", "The title or nil on error")
{
	option_h* opt;
	if (!ade_get_args(L, "o", l_Option.GetPtr(&opt))) {
		return ADE_RETURN_NIL;
	}

	if (!opt->isValid()) {
		return ADE_RETURN_NIL;
	}

	return ade_set_args(L, "s", opt->get()->getTitle().c_str());
}
ADE_VIRTVAR(Description, l_Option, nullptr, "The description of this option (read-only)", "string",
            "The description or nil on error")
{
	option_h* opt;
	if (!ade_get_args(L, "o", l_Option.GetPtr(&opt))) {
		return ADE_RETURN_NIL;
	}

	if (!opt->isValid()) {
		return ADE_RETURN_NIL;
	}

	return ade_set_args(L, "s", opt->get()->getDescription().c_str());
}
ADE_VIRTVAR(Key, l_Option, nullptr, "The configuration key of this option. This will be a unique string. (read-only)",
            "string", "The key or nil on error")
{
	option_h* opt;
	if (!ade_get_args(L, "o", l_Option.GetPtr(&opt))) {
		return ADE_RETURN_NIL;
	}

	if (!opt->isValid()) {
		return ADE_RETURN_NIL;
	}

	return ade_set_args(L, "s", opt->get()->getConfigKey().c_str());
}
ADE_VIRTVAR(Category, l_Option, nullptr, "The category of this option. (read-only)", "string",
            "The category or nil on error")
{
	option_h* opt;
	if (!ade_get_args(L, "o", l_Option.GetPtr(&opt))) {
		return ADE_RETURN_NIL;
	}

	if (!opt->isValid()) {
		return ADE_RETURN_NIL;
	}

	return ade_set_args(L, "s", opt->get()->getCategory().c_str());
}
ADE_VIRTVAR(Type, l_Option, nullptr, "The type of this option. One of the OPTION_TYPE_* values. (read-only)",
            "enumeration", "The enum or nil on error")
{
	option_h* opt;
	if (!ade_get_args(L, "o", l_Option.GetPtr(&opt))) {
		return ADE_RETURN_NIL;
	}

	if (!opt->isValid()) {
		return ADE_RETURN_NIL;
	}

	lua_enum enum_val = ENUM_INVALID;
	switch (opt->get()->getType()) {
	case options::OptionType::Selection:
		enum_val = LE_OPTION_TYPE_SELECTION;
		break;
	case options::OptionType::Range:
		enum_val = LE_OPTION_TYPE_RANGE;
		break;
	}

	return ade_set_args(L, "o", l_Enum.Set(enum_h(enum_val)));
}
ADE_VIRTVAR(Value, l_Option, "ValueDescription", "The current value of this option.", "ValueDescription",
            "The current value or nil on error")
{
	option_h* opt;
	options::ValueDescription* new_val = nullptr;
	if (!ade_get_args(L, "o|o", l_Option.GetPtr(&opt), l_ValueDescription.GetPtr(&new_val))) {
		return ADE_RETURN_NIL;
	}
	if (!opt->isValid()) {
		return ADE_RETURN_NIL;
	}
	if (ADE_SETTING_VAR && new_val != nullptr) {
		opt->get()->setValueDescription(*new_val);
	}
	return ade_set_args(L, "o", l_ValueDescription.Set(opt->get()->getCurrentValueDescription()));
}
ADE_VIRTVAR(Flags,
	l_Option,
	nullptr,
	"Contains a list mapping a flag name to its value. Possible names are:"
	"<ul>"
	"<li><b>ForceMultiValueSelection:</b> If true, a selection option with two values should be displayed the "
	"same as an option with more possible values</li>"
	"<li><b>RetailBuiltinOption:</b> If true, the option is one of the original retail options</li>"
	"<li><b>RangeTypeInteger:</b> If true, this range option requires an integer for the range value rather than a float</li>"
	"</ul>",
	"{ string => boolean ... }",
	"The table of flags values.")
{
	option_h* opt;
	options::ValueDescription* new_val = nullptr;
	if (!ade_get_args(L, "o|o", l_Option.GetPtr(&opt), l_ValueDescription.GetPtr(&new_val))) {
		return ADE_RETURN_NIL;
	}
	if (!opt->isValid()) {
		return ADE_RETURN_NIL;
	}

	luacpp::LuaTable t = luacpp::LuaTable::create(L);

	t.addValue("ForceMultiValueSelection", opt->get()->getFlags()[options::OptionFlags::ForceMultiValueSelection]);
	t.addValue("RetailBuiltinOption", opt->get()->getFlags()[options::OptionFlags::RetailBuiltinOption]);
	t.addValue("RangeTypeInteger", opt->get()->getFlags()[options::OptionFlags::RangeTypeInteger]);

	return ade_set_args(L, "t", &t);
}
ADE_FUNC(getValueFromRange, l_Option, "number interpolant",
         "Gets a value from an option range. The specified value must be between 0 and 1.", "ValueDescription",
         "The value at the specifiedposition")
{
	option_h* opt;
	float f;
	if (!ade_get_args(L, "of", l_Option.GetPtr(&opt), &f)) {
		return ADE_RETURN_NIL;
	}
	if (!opt->isValid()) {
		return ADE_RETURN_NIL;
	}
	if (opt->get()->getType() != options::OptionType::Range) {
		LuaError(L, "This option is not a range option!");
		return ADE_RETURN_NIL;
	}
	if (f < 0.0f || f > 1.0f) {
		LuaError(L, "Invalid interpolant value %f!", f);
		return ADE_RETURN_NIL;
	}
	return ade_set_args(L, "o", l_ValueDescription.Set(opt->get()->getValueFromRange(f)));
}
ADE_FUNC(getInterpolantFromValue, l_Option, "ValueDescription value",
         "From a value description of this option, determines the range value.", "number",
         "The range value or 0 on error.")
{
	option_h* opt;
	options::ValueDescription* value = nullptr;
	if (!ade_get_args(L, "oo", l_Option.GetPtr(&opt), l_ValueDescription.GetPtr(&value))) {
		return ADE_RETURN_NIL;
	}
	if (!opt->isValid()) {
		return ADE_RETURN_NIL;
	}
	if (opt->get()->getType() != options::OptionType::Range) {
		LuaError(L, "This option is not a range option!");
		return ADE_RETURN_NIL;
	}
	return ade_set_args(L, "f", opt->get()->getInterpolantFromValue(*value));
}
ADE_FUNC(getValidValues,
	l_Option,
	nullptr,
	"Gets the valid values of this option. The order or the returned values must be maintained in the UI. This is "
	"only valid for selection or boolean options.",
	"ValueDescription[]",
	"A table containing the possible values or nil on error.")
{
	option_h* opt;
	if (!ade_get_args(L, "o", l_Option.GetPtr(&opt))) {
		return ADE_RETURN_NIL;
	}
	if (!opt->isValid()) {
		return ADE_RETURN_NIL;
	}
	if (opt->get()->getType() != options::OptionType::Selection) {
		LuaError(L, "This option is not a selection option!");
		return ADE_RETURN_NIL;
	}
	auto values = opt->get()->getValidValues();

	auto table = luacpp::LuaTable::create(L);
	auto i     = 1;
	for (auto& value : values) {
		table.addValue(i, l_ValueDescription.Set(value));
		++i;
	}

	return ade_set_args(L, "t", &table);
}
ADE_FUNC(persistChanges, l_Option, nullptr,
         "Immediately persists any changes made to this specific option.", "boolean",
         "true if the change was applied successfully, false otherwise. nil on error.")
{
	option_h* opt;
	if (!ade_get_args(L, "o", l_Option.GetPtr(&opt))) {
		return ADE_RETURN_NIL;
	}
	if (!opt->isValid()) {
		return ADE_RETURN_NIL;
	}
	return ade_set_args(L, "b", opt->get()->persistChanges());
}

} // namespace api
} // namespace scripting