File: hudgauge.cpp

package info (click to toggle)
freespace2 25.0.0~rc11%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 47,232 kB
  • sloc: cpp: 657,500; ansic: 22,305; sh: 293; python: 200; makefile: 198; xml: 181
file content (426 lines) | stat: -rw-r--r-- 12,827 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
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
//
//

#include "hudgauge.h"
#include "hud/hudscripting.h"
#include "scripting/api/objs/color.h"
#include "font.h"
#include "texture.h"

namespace scripting {
namespace api {

ADE_OBJ(l_HudGauge, HudGauge*, "HudGauge", "HUD Gauge handle");

ADE_FUNC(isCustom, l_HudGauge, nullptr, "Custom HUD Gauge status", "boolean", "Returns true if this is a custom HUD gauge, or false if it is a non-custom (default) HUD gauge")
{
	HudGauge* gauge;

	if (!ade_get_args(L, "o", l_HudGauge.Get(&gauge)))
		return ADE_RETURN_NIL;

	return ade_set_args(L, "b", gauge->isCustom());
}

ADE_VIRTVAR(Name, l_HudGauge, "string", "Custom HUD Gauge name", "string", "Custom HUD Gauge name, or nil if this is a default gauge or the handle is invalid")
{
	HudGauge* gauge;

	if (!ade_get_args(L, "o", l_HudGauge.Get(&gauge)))
		return ADE_RETURN_NIL;

	if (gauge->getObjectType() != HUD_OBJECT_CUSTOM)
		return ADE_RETURN_NIL;

	if (ADE_SETTING_VAR)
		LuaError(L, "Setting the hud gauge name is not supported");

	return ade_set_args(L, "s", gauge->getCustomGaugeName());
}

ADE_VIRTVAR(Text, l_HudGauge, "string", "Custom HUD Gauge text", "string", "Custom HUD Gauge text, or nil if this is a default gauge or the handle is invalid")
{
	HudGauge* gauge;
	const char* text = nullptr;

	if (!ade_get_args(L, "o|s", l_HudGauge.Get(&gauge), &text))
		return ADE_RETURN_NIL;

	if (gauge->getObjectType() != HUD_OBJECT_CUSTOM)
		return ADE_RETURN_NIL;

	if (ADE_SETTING_VAR && text != NULL)
	{
		gauge->updateCustomGaugeText(text);
	}

	return ade_set_args(L, "s", gauge->getCustomGaugeText());
}

ADE_VIRTVAR(ConfigType, l_HudGauge, "string", "The config type (such as \"LEAD_INDICATOR\") of this HUD Gauge", "string", "HUD Gauge config type, or nil if this gauge does not have a config type (custom gauges and some default gauges do not) or if the handle is invalid")
{
	HudGauge* gauge;

	if (!ade_get_args(L, "o", l_HudGauge.Get(&gauge)))
		return ADE_RETURN_NIL;

	if (ADE_SETTING_VAR)
		LuaError(L, "Setting the hud gauge config type is not supported");

	int type = gauge->getConfigType();
	if (type >= 0 && type < NUM_HUD_GAUGES)
		return ade_set_args(L, "s", Legacy_HUD_gauges[type].hud_gauge_text);

	return ADE_RETURN_NIL;
}

ADE_VIRTVAR(ObjectType, l_HudGauge, "string", "The object type (such as \"Lead indicator\") of this HUD Gauge", "string", "HUD Gauge object type, or nil if this gauge does not have an object type or if the handle is invalid")
{
	HudGauge* gauge;

	if (!ade_get_args(L, "o", l_HudGauge.Get(&gauge)))
		return ADE_RETURN_NIL;

	if (ADE_SETTING_VAR)
		LuaError(L, "Setting the hud gauge object type is not supported");

	int type = gauge->getObjectType();
	if (type == HUD_OBJECT_CUSTOM)
		return ade_set_args(L, "s", "Custom");

	for (int i = 0; i < Num_hud_gauge_types; i++)
	{
		if (type == Hud_gauge_types[i].def)
			return ade_set_args(L, "s", Hud_gauge_types[i].name);
	}

	return ADE_RETURN_NIL;
}

ADE_FUNC(getBaseResolution, l_HudGauge, nullptr, "Returns the base width and base height (which may be different from the screen width and height) used by the specified HUD gauge.",
	"number, number", "Base width and height")
{
	HudGauge* gauge;
	if (!ade_get_args(L, "o", l_HudGauge.Get(&gauge)))
		return ADE_RETURN_NIL;

	return ade_set_args(L, "ii", gauge->getBaseWidth(), gauge->getBaseHeight());
}

ADE_FUNC(getAspectQuotient, l_HudGauge, nullptr, "Returns the aspect quotient (ratio between the current aspect ratio and the HUD's native aspect ratio) used by the specified HUD gauge.",
	"number", "Aspect quotient")
{
	HudGauge* gauge;
	if (!ade_get_args(L, "o", l_HudGauge.Get(&gauge)))
		return ADE_RETURN_NIL;

	return ade_set_args(L, "f", gauge->getAspectQuotient());
}

ADE_FUNC(getPosition, l_HudGauge, nullptr, "Returns the position of the specified HUD gauge.",
	"number, number", "X and Y coordinates")
{
	HudGauge* gauge;
	if (!ade_get_args(L, "o", l_HudGauge.Get(&gauge)))
		return ADE_RETURN_NIL;

	int x, y;
	gauge->getPosition(&x, &y);
	return ade_set_args(L, "ii", x, y);
}

ADE_FUNC(setPosition, l_HudGauge, "number, number", "Sets the position of the specified HUD gauge.", nullptr, nullptr)
{
	HudGauge* gauge;
	int x, y;
	if (!ade_get_args(L, "oii", l_HudGauge.Get(&gauge), &x, &y))
		return ADE_RETURN_NIL;

	gauge->setGaugeCoords(x, y);
	return ADE_RETURN_NIL;
}

ADE_FUNC(getFont, l_HudGauge, nullptr, "Returns the font used by the specified HUD gauge.",
	"font", "The font handle")
{
	HudGauge* gauge;
	if (!ade_get_args(L, "o", l_HudGauge.Get(&gauge)))
		return ADE_RETURN_NIL;

	int font_num = gauge->getFont();

	if (font_num < 0 || font_num >= font::FontManager::numberOfFonts())
		return ade_set_error(L, "o", l_Font.Set(font_h()));

	return ade_set_args(L, "o", l_Font.Set(font_h(font_num)));
}

ADE_FUNC(getOriginAndOffset, l_HudGauge, nullptr, "Returns the origin and offset of the specified HUD gauge as specified in the table.",
	"number, number, number, number", "Origin X, Origin Y, Offset X, Offset Y")
{
	HudGauge* gauge;
	if (!ade_get_args(L, "o", l_HudGauge.Get(&gauge)))
		return ADE_RETURN_NIL;

	float originX, originY;
	int offsetX, offsetY;
	gauge->getOriginAndOffset(&originX, &originY, &offsetX, &offsetY);
	return ade_set_args(L, "ffii", originX, originY, offsetX, offsetY);
}

ADE_FUNC(getCoords, l_HudGauge, nullptr, "Returns the coordinates of the specified HUD gauge as specified in the table.",
	"boolean, number, number", "Coordinates flag (whether coordinates are used), X, Y")
{
	HudGauge* gauge;
	if (!ade_get_args(L, "o", l_HudGauge.Get(&gauge)))
		return ADE_RETURN_NIL;

	bool useCoords;
	int x, y;
	gauge->getCoords(&useCoords, &x, &y);
	return ade_set_args(L, "bii", useCoords, x, y);
}

ADE_FUNC(isHiRes, l_HudGauge, nullptr, "Returns whether this is a hi-res HUD gauge, determined by whether the +Filename property is prefaced with \"2_\".  Not all gauges have such a filename.",
	"boolean", "Whether the HUD gauge is known to be hi-res")
{
	HudGauge* gauge;
	if (!ade_get_args(L, "o", l_HudGauge.Get(&gauge)))
		return ADE_RETURN_NIL;

	return ade_set_args(L, "b", gauge->isHiRes());
}

ADE_FUNC(getColor,
	l_HudGauge,
	nullptr,
	"Gets the active 2D drawing color. False to return raw rgb, true to return a color object. Defaults to false.",
	"number, number, number, number | color",
	"The current color, in red, green, blue, and alpha components from 0 to 255")
{
	HudGauge* gauge;
	bool rc = false;
	if (!ade_get_args(L, "o|b", l_HudGauge.Get(&gauge), &rc))
		return ADE_RETURN_NIL;

	auto &c = gauge->getColor();

	if (rc) {
		return ade_set_args(L, "o", l_Color.Set(c));
	}else{
		return ade_set_args(L, "iiii", static_cast<int>(c.red), static_cast<int>(c.green), static_cast<int>(c.blue), static_cast<int>(c.alpha));
	}
}

ADE_FUNC(setColor,
	l_HudGauge,
	"number|color /* red value or color object */, [number Green, number Blue, number Alpha]",
	"Sets the current color used by this HUD gauge.  Numbers must be 0-255 in red/green/blue/alpha components; alpha is optional.",
	nullptr, nullptr)
{
	HudGauge* gauge = nullptr;
	int r, g, b, a = -1;

	if (lua_isnumber(L, 2)) {
		if (!ade_get_args(L, "oiii|i", l_HudGauge.Get(&gauge) , &r, &g, &b, &a))
			return ADE_RETURN_NIL;

		if (a < 0)
			a = (HUD_color_alpha + 1) * 16; // from sexp_hud_set_color()
		else
			CLAMP(a, 0, 255);

		CLAMP(r, 0, 255);
		CLAMP(g, 0, 255);
		CLAMP(b, 0, 255);
	} else {
		color col;
		gr_init_alphacolor(&col, 0, 0, 0, 255);

		ade_get_args(L, "oo", l_HudGauge.Get(&gauge), l_Color.Get(&col));

		r = col.red;
		g = col.green;
		b = col.blue;
		a = col.alpha;
	}

	if (gauge == nullptr) {
		return ADE_RETURN_NIL;
	}

	gauge->sexpLockConfigColor(false);
	gauge->updateColor(static_cast<ubyte>(r), static_cast<ubyte>(g), static_cast<ubyte>(b), static_cast<ubyte>(a));
	gauge->sexpLockConfigColor(true);

	return ADE_RETURN_NIL;
}

ADE_FUNC(setRenderOverride,
	l_HudGauge,
	"boolean",
	"Sets a rendering override to enable or disable rendering of this gauge. This takes precedence over all other gauge toggles!",
	"boolean",
	"True to enable the override and stop the gauge from rendering, false otherwise")
{
	HudGauge* gauge;
	bool override = false;
	if (!ade_get_args(L, "o|b", l_HudGauge.Get(&gauge), &override))
		return ADE_RETURN_NIL;

	gauge->updateScriptingOverride(override);

	return ade_set_args(L, "b", gauge->getScriptingOverride());
}

ADE_VIRTVAR(RenderFunction,
            l_HudGauge,
            "function (HudGaugeDrawFunctions gauge_handle) => void",
            "For scripted HUD gauges, the function that will be called for rendering the HUD gauge",
            "function (HudGaugeDrawFunctions gauge_handle) => void",
            "Render function or nil if no action is set or handle is invalid") {
	HudGauge* gauge;
	luacpp::LuaFunction func;

	if (!ade_get_args(L, "o|u", l_HudGauge.Get(&gauge), &func)) {
		return ADE_RETURN_NIL;
	}

	if (gauge->getObjectType() != HUD_OBJECT_SCRIPTING) {
		return ADE_RETURN_NIL;
	}

	auto scriptedGauge = static_cast<HudGaugeScripting*>(gauge);

	if (ADE_SETTING_VAR && func.isValid()) {
		scriptedGauge->setRenderFunction(func);
	}

	return ade_set_args(L, "u", scriptedGauge->getRenderFunction());
}

ADE_OBJ(l_HudGaugeDrawFuncs,
        HudGauge*,
        "HudGaugeDrawFunctions",
        "Handle to the rendering functions used for HUD gauges. Do not keep a reference to this since these are only useful inside the rendering callback of a HUD gauge.");

ADE_FUNC(drawString,
         l_HudGaugeDrawFuncs,
         "string text, number x, number y",
         "Draws a string in the context of the HUD gauge.",
         "boolean",
         "true on success, false otherwise") {
	HudGauge* gauge;
	float x;
	float y;
	const char* text;

	if (!ade_get_args(L, "osff", l_HudGaugeDrawFuncs.Get(&gauge), &text, &x, &y)) {
		return ADE_RETURN_FALSE;
	}

	int gauge_x, gauge_y;
	gauge->getPosition(&gauge_x, &gauge_y);

	gauge->renderString(fl2i(gauge_x + x), fl2i(gauge_y + y), text);

	return ADE_RETURN_TRUE;
}

ADE_FUNC(drawLine, l_HudGaugeDrawFuncs, "number X1, number Y1, number X2, number Y2",
         "Draws a line in the context of the HUD gauge.", "boolean", "true on success, false otherwise")
{
	HudGauge* gauge;
	float x1;
	float y1;
	float x2;
	float y2;

	if (!ade_get_args(L, "offff", l_HudGaugeDrawFuncs.Get(&gauge), &x1, &y1, &x2, &y2)) {
		return ADE_RETURN_FALSE;
	}

	int gauge_x, gauge_y;
	gauge->getPosition(&gauge_x, &gauge_y);

	gauge->renderLine(fl2i(gauge_x + x1), fl2i(gauge_y + y1), fl2i(gauge_x + x2), fl2i(gauge_y + y2));

	return ADE_RETURN_TRUE;
}

ADE_FUNC(drawCircle, l_HudGaugeDrawFuncs, "number radius, number X, number Y, [boolean filled=true]",
         "Draws a circle in the context of the HUD gauge.", "boolean", "true on success, false otherwise")
{
	HudGauge* gauge;
	float x;
	float y;
	float radius;
	bool filled=true;

	if (!ade_get_args(L, "offf|b", l_HudGaugeDrawFuncs.Get(&gauge), &radius, &x, &y, &filled)) {
		return ADE_RETURN_FALSE;
	}

	int gauge_x, gauge_y;
	gauge->getPosition(&gauge_x, &gauge_y);

	gauge->renderCircle(fl2i(gauge_x + x), fl2i(gauge_y + y), fl2i(radius*2), filled);

	return ADE_RETURN_TRUE;
}

ADE_FUNC(drawRectangle, l_HudGaugeDrawFuncs, "number X1, number Y1, number X2, number Y2, [boolean Filled=true]",
         "Draws a rectangle in the context of the HUD gauge.", "boolean", "true on success, false otherwise")
{
	HudGauge* gauge;
	float x1;
	float y1;
	float x2;
	float y2;
	bool filled = true;

	if (!ade_get_args(L, "offff|b", l_HudGaugeDrawFuncs.Get(&gauge), &x1, &y1, &x2, &y2, &filled)) {
		return ADE_RETURN_FALSE;
	}

	int gauge_x, gauge_y;
	gauge->getPosition(&gauge_x, &gauge_y);

	if (filled) {
		gauge->renderRect(fl2i(gauge_x + x1), fl2i(gauge_y + y1), fl2i(x2-x1), fl2i(y2-y1));
	} else {
		gauge->renderLine(fl2i(gauge_x + x1), fl2i(gauge_y + y1), fl2i(gauge_x + x2), fl2i(gauge_y + y1));
		gauge->renderLine(fl2i(gauge_x + x1), fl2i(gauge_y + y2), fl2i(gauge_x + x2), fl2i(gauge_y + y2));
		gauge->renderLine(fl2i(gauge_x + x1), fl2i(gauge_y + y1), fl2i(gauge_x + x1), fl2i(gauge_y + y2));
		gauge->renderLine(fl2i(gauge_x + x2), fl2i(gauge_y + y1), fl2i(gauge_x + x2), fl2i(gauge_y + y2));
	}

	return ADE_RETURN_TRUE;
}

ADE_FUNC(drawImage, l_HudGaugeDrawFuncs, "texture Texture, [number X=0, number Y=0]",
         "Draws an image in the context of the HUD gauge.", "boolean",
         "true on success, false otherwise")
{
	HudGauge* gauge;
	texture_h* texture = nullptr;
	float x1           = 0;
	float y1           = 0;

	if (!ade_get_args(L, "oo|ff", l_HudGaugeDrawFuncs.Get(&gauge), l_Texture.GetPtr(&texture), &x1, &y1)) {
		return ADE_RETURN_FALSE;
	}
	if (!texture->isValid()) {
		return ADE_RETURN_FALSE;
	}

	int gauge_x, gauge_y;
	gauge->getPosition(&gauge_x, &gauge_y);

	gauge->renderBitmapColor(texture->handle, fl2i(gauge_x + x1), fl2i(gauge_y + y1));

	return ADE_RETURN_TRUE;
}

}
}