File: control_config.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 (446 lines) | stat: -rw-r--r-- 10,259 bytes parent folder | download | duplicates (2)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include "control_config.h"

#include "controlconfig/controlsconfig.h"
#include "io/key.h"
#include "controlconfig/presets.h"

namespace scripting {
namespace api {

control_h::control_h() : control(-1) {}
control_h::control_h(int l_control) : control(l_control) {}

CCI* control_h::getControl() const
{
	return &Control_config[control];
}

conflict* control_h::getConflict() const
{
	return &Conflicts[control];
}

int control_h::getIndex() const
{
	return control;
}

preset_h::preset_h() : preset(-1) {}
preset_h::preset_h(int l_preset) : preset(l_preset) {}

CC_preset* preset_h::getPreset() const
{
	return &Control_config_presets[preset];
}

//**********HANDLE: preset
ADE_OBJ(l_Preset, preset_h, "preset", "Control Preset handle");

ADE_VIRTVAR(Name, l_Preset, nullptr, "The name of the preset", "string", "The name")
{
	preset_h current;
	if (!ade_get_args(L, "o", l_Preset.Get(&current))) {
		return ADE_RETURN_NIL;
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "This property is read only.");
	}

	return ade_set_args(L, "s", current.getPreset()->name.c_str());
}

ADE_FUNC(clonePreset,
	l_Preset,
	"string Name",
	"Clones the preset into a new preset with the specified name. Sets it as the active preset",
	"boolean",
	"Returns true if successful, false otherwise")
{
	preset_h current;
	const char* newName;
	if (!ade_get_args(L, "os", l_Preset.Get(&current), &newName)) {
		return ADE_RETURN_FALSE;
	}

	return ade_set_args(L, "b", control_config_clone_preset(*current.getPreset(), newName));
}

ADE_FUNC(deletePreset,
	l_Preset,
	nullptr,
	"Deletes the preset file entirely. Cannot delete a currently active preset.",
	"boolean",
	"Returns true if successful, false otherwise")
{
	preset_h current;
	if (!ade_get_args(L, "o", l_Preset.Get(&current))) {
		return ADE_RETURN_FALSE;
	}

	return ade_set_args(L, "b", control_config_delete_preset(*current.getPreset()));
}

//**********HANDLE: control
ADE_OBJ(l_Control, control_h, "control", "Control handle");

ADE_VIRTVAR(Name, l_Control, nullptr, "The name of the control", "string", "The name")
{
	control_h current;
	if (!ade_get_args(L, "o", l_Control.Get(&current))) {
		return ADE_RETURN_NIL;
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "This property is read only.");
	}

	CCI* item = current.getControl();
	SCP_string name;

	if (item->indexXSTR > 1) {
		name = XSTR(item->text.c_str(), item->indexXSTR, true);
	} else if (item->indexXSTR == 1) {
		name = XSTR(item->text.c_str(), CONTROL_CONFIG_XSTR + current.getIndex(), true);
	} else {
		name = item->text.c_str();
	}

	return ade_set_args(L, "s", name.c_str());
}

ADE_VIRTVAR(Bindings,
	l_Control,
	nullptr,
	"Gets a table of bindings for the control",
	"{ number => string ... }",
	"The keys table")
{
	control_h current;
	if (!ade_get_args(L, "o", l_Control.Get(&current))) {
		return ADE_RETURN_NIL;
	}

	auto table = luacpp::LuaTable::create(L);

	// Using a table so that if the number of bindings is ever increased the API
	// will only need minimal adjustments.
	table.addValue(1, current.getControl()->first.textify().c_str());
	table.addValue(2, current.getControl()->second.textify().c_str());

	return ade_set_args(L, "t", &table);
}

ADE_FUNC(isBindInverted,
	l_Control,
	"number Bind",
	"Returns if the selected bind is inverted. Number is 1 for first bind 2 for second.",
	"boolean",
	"True if inverted, false otherwise. False if the bind is not an axis.")
{
	control_h current;
	int item;
	if (!ade_get_args(L, "oi", l_Control.Get(&current), &item)) {
		return ADE_RETURN_NIL;
	}

	bool inverted = false;

	if (current.getControl()->is_axis()) {
		switch (item) {
		case (1):
			if (current.getControl()->first.is_inverted()) {
				inverted = true;
			}
			break;
		case (2):
			if (current.getControl()->second.is_inverted()) {
				inverted = true;
			}
			break;
		default:
			break;
		}
	}

	return ade_set_args(L, "b", inverted);
}

ADE_VIRTVAR(Shifted,
	l_Control,
	nullptr,
	"Returns whether or not the keybind is Shifted",
	"boolean",
	"True if shifted, false otherwise.")
{
	control_h current;
	if (!ade_get_args(L, "o", l_Control.Get(&current))) {
		return ADE_RETURN_NIL;
	}

	int k = current.getControl()->get_btn(CID_KEYBOARD);
	bool shifted = false;

	if (k >= 0) {
		if (k & KEY_SHIFTED) {
			shifted = true;
		}
	}

	return ade_set_args(L, "b", shifted);
}

ADE_VIRTVAR(Alted,
	l_Control,
	nullptr,
	"Returns whether or not the keybind is Alted",
	"boolean",
	"True if alted, false otherwise.")
{
	control_h current;
	if (!ade_get_args(L, "o", l_Control.Get(&current))) {
		return ADE_RETURN_NIL;
	}

	int k = current.getControl()->get_btn(CID_KEYBOARD);
	bool alted = false;

	if (k >= 0) {
		if (k & KEY_ALTED) {
			alted = true;
		}
	}

	return ade_set_args(L, "b", alted);
}

ADE_VIRTVAR(Tab,
	l_Control,
	nullptr,
	"The tab the control belongs in. 0 = Target Tab, 1 = Ship Tab, 2 = Weapon Tab, 3 = Computer Tab",
	"number",
	"The tab number")
{
	control_h current;
	if (!ade_get_args(L, "o", l_Control.Get(&current))) {
		return ADE_RETURN_NIL;
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "This property is read only.");
	}

	return ade_set_args(L, "i", current.getControl()->tab);
}

ADE_VIRTVAR(Disabled,
	l_Control,
	nullptr,
	"Whether or not the control is disabled and should be hidden.",
	"boolean",
	"True for disabled, false otherwise")
{
	control_h current;
	if (!ade_get_args(L, "o", l_Control.Get(&current))) {
		return ADE_RETURN_NIL;
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "This property is read only.");
	}

	return ade_set_args(L, "b", current.getControl()->disabled);
}

ADE_VIRTVAR(IsAxis,
	l_Control,
	nullptr,
	"Whether or not the bound control is an axis control.",
	"boolean",
	"True for axis, false otherwise")
{
	control_h current;
	if (!ade_get_args(L, "o", l_Control.Get(&current))) {
		return ADE_RETURN_NIL;
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "This property is read only.");
	}

	return ade_set_args(L, "b", current.getControl()->is_axis());
}

ADE_VIRTVAR(IsModifier,
	l_Control,
	nullptr,
	"Whether or not the bound control is a modifier.",
	"boolean",
	"True for modifier, false otherwise")
{
	control_h current;
	if (!ade_get_args(L, "o", l_Control.Get(&current))) {
		return ADE_RETURN_NIL;
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "This property is read only.");
	}

	bool mod = false;

	int k = current.getControl()->get_btn(CID_KEYBOARD);
	if ((k == KEY_LALT) || (k == KEY_RALT) || (k == KEY_LSHIFT) || (k == KEY_RSHIFT))
		mod = true;

	return ade_set_args(L, "b", mod);
}

ADE_VIRTVAR(Conflicted,
	l_Control,
	nullptr,
	"Whether or not the bound control has a conflict.",
	"boolean",
	"Returns the conflict string if true, nil otherwise")
{
	control_h current;
	if (!ade_get_args(L, "o", l_Control.Get(&current))) {
		return ADE_RETURN_NIL;
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "This property is read only.");
	}

	if (!current.getControl()->is_axis() &&
		((current.getConflict()->first >= 0) || (current.getConflict()->second >= 0))) {
		int i = current.getConflict()->first;
		if (i < 0) {
			i = current.getConflict()->second;
		}

		SCP_string str = XSTR("Control conflicts with:", 209);
		str += " ";

		if (Control_config[i].indexXSTR > 1) {
			str += XSTR(Control_config[i].text.c_str(), Control_config[i].indexXSTR, true);
		} else if (Control_config[i].indexXSTR == 1) {
			str += XSTR(Control_config[i].text.c_str(), CONTROL_CONFIG_XSTR + i, true);
		} else {
			str += Control_config[i].text.c_str();
		}

		return ade_set_args(L, "s", str.c_str());
	}

	return ADE_RETURN_NIL;
}

ADE_FUNC(detectKeypress,
	l_Control,
	"number Item",
	"Waits for a keypress to use as a keybind. Binds the key if found. Will need to disable UI input if enabled first. Should run On Frame. Item is first bind (1) or second bind (2)",
	"number",
	"1 if successful or ESC was pressed, 0 otherwise. Returns -1 if the keypress is invalid")
{
	control_h current;
	int item;
	if (!ade_get_args(L, "oi", l_Control.Get(&current), &item)) {
		return ADE_RETURN_FALSE;
	}

	int idx = current.getIndex();

	return ade_set_args(L, "i", control_config_bind_key_on_frame(idx, (selItem)item, true));
}

ADE_FUNC(clearBind,
	l_Control,
	"number Item",
	"Clears the control binding. Item is all controls (1), first control (2), or second control (3)",
	"boolean",
	"Returns true if successful, false otherwise")
{
	control_h current;
	int item;
	if (!ade_get_args(L, "oi", l_Control.Get(&current), &item)) {
		return ADE_RETURN_FALSE;
	}

	int idx = current.getIndex();

	return ade_set_args(L, "b", control_config_remove_binding(idx, (selItem)item, true));
}

ADE_FUNC(clearConflicts,
	l_Control,
	nullptr,
	"Clears all binds that conflict with the selected bind index.",
	"boolean",
	"Returns true if successful, false otherwise")
{
	control_h current;
	if (!ade_get_args(L, "o", l_Control.Get(&current))) {
		return ADE_RETURN_FALSE;
	}

	int idx = current.getIndex();

	return ade_set_args(L, "b", control_config_clear_other(idx, true));
}

ADE_FUNC(toggleShifted,
	l_Control,
	nullptr,
	"Toggles whether or not the current bind uses SHIFT modifier.",
	"boolean",
	"Returns true if successful, false otherwise")
{
	control_h current;
	if (!ade_get_args(L, "o", l_Control.Get(&current))) {
		return ADE_RETURN_FALSE;
	}

	int idx = current.getIndex();

	return ade_set_args(L, "b", control_config_toggle_modifier(KEY_SHIFTED, idx, true));
}

ADE_FUNC(toggleAlted,
	l_Control,
	nullptr,
	"Toggles whether or not the current bind uses ALT modifier.",
	"boolean",
	"Returns true if successful, false otherwise")
{
	control_h current;
	if (!ade_get_args(L, "o", l_Control.Get(&current))) {
		return ADE_RETURN_FALSE;
	}

	int idx = current.getIndex();

	return ade_set_args(L, "b", control_config_toggle_modifier(KEY_ALTED, idx, true));
}

ADE_FUNC(toggleInverted,
	l_Control,
	"number Item",
	"Toggles whether or not the current bind axis is inverted. Item is all controls (1), first control (2), or second "
	"control (3)",
	"boolean",
	"Returns true if successful, false otherwise")
{
	control_h current;
	int item;
	if (!ade_get_args(L, "oi", l_Control.Get(&current), &item)) {
		return ADE_RETURN_FALSE;
	}

	int idx = current.getIndex();

	return ade_set_args(L, "b", control_config_toggle_invert(idx, (selItem)item, true));
}

} // namespace api
} // namespace scripting