File: GuiScriptConversions.cpp

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (384 lines) | stat: -rw-r--r-- 10,369 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
/* Copyright (C) 2021 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. 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.
 *
 * 0 A.D. 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 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "gui/ObjectBases/IGUIObject.h"
#include "gui/SettingTypes/CGUIColor.h"
#include "gui/SettingTypes/CGUIList.h"
#include "gui/SettingTypes/CGUISeries.h"
#include "gui/SettingTypes/CGUISize.h"
#include "gui/Scripting/JSInterface_GUIProxy.h"
#include "lib/external_libraries/libsdl.h"
#include "maths/Size2D.h"
#include "maths/Vector2D.h"
#include "ps/Hotkey.h"
#include "ps/CLogger.h"
#include "scriptinterface/Object.h"
#include "scriptinterface/ScriptConversions.h"

#include <string>

#define SET(obj, name, value) STMT(JS::RootedValue v_(rq.cx); Script::ToJSVal(rq, &v_, (value)); JS_SetProperty(rq.cx, obj, (name), v_))
	// ignore JS_SetProperty return value, because errors should be impossible
	// and we can't do anything useful in the case of errors anyway

template<> void Script::ToJSVal<SDL_Event_>(const ScriptRequest& rq, JS::MutableHandleValue ret, SDL_Event_ const& val)
{
	const char* typeName;

	switch (val.ev.type)
	{
	case SDL_WINDOWEVENT: typeName = "windowevent"; break;
	case SDL_KEYDOWN: typeName = "keydown"; break;
	case SDL_KEYUP: typeName = "keyup"; break;
	case SDL_MOUSEMOTION: typeName = "mousemotion"; break;
	case SDL_MOUSEBUTTONDOWN: typeName = "mousebuttondown"; break;
	case SDL_MOUSEBUTTONUP: typeName = "mousebuttonup"; break;
	case SDL_QUIT: typeName = "quit"; break;
	case SDL_HOTKEYPRESS: typeName = "hotkeypress"; break;
	case SDL_HOTKEYDOWN: typeName = "hotkeydown"; break;
	case SDL_HOTKEYUP: typeName = "hotkeyup"; break;
	case SDL_HOTKEYPRESS_SILENT: typeName = "hotkeypresssilent"; break;
	case SDL_HOTKEYUP_SILENT: typeName = "hotkeyupsilent"; break;
	default: typeName = "(unknown)"; break;
	}

	JS::RootedObject obj(rq.cx, JS_NewPlainObject(rq.cx));
	if (!obj)
	{
		ret.setUndefined();
		return;
	}

	SET(obj, "type", typeName);

	switch (val.ev.type)
	{
	case SDL_KEYDOWN:
	case SDL_KEYUP:
	{
		// SET(obj, "which", (int)val.ev.key.which); // (not in wsdl.h)
		// SET(obj, "state", (int)val.ev.key.state); // (not in wsdl.h)

		JS::RootedObject keysym(rq.cx, JS_NewPlainObject(rq.cx));
		if (!keysym)
		{
			ret.setUndefined();
			return;
		}
		JS::RootedValue keysymVal(rq.cx, JS::ObjectValue(*keysym));
		JS_SetProperty(rq.cx, obj, "keysym", keysymVal);

		// SET(keysym, "scancode", (int)val.ev.key.keysym.scancode); // (not in wsdl.h)
		SET(keysym, "sym", (int)val.ev.key.keysym.sym);
		// SET(keysym, "mod", (int)val.ev.key.keysym.mod); // (not in wsdl.h)
		{
			SET(keysym, "unicode", JS::UndefinedHandleValue);
		}
		// TODO: scripts have no idea what all the key/mod enum values are;
		// we should probably expose them as constants if we expect scripts to use them

		break;
	}
	case SDL_MOUSEMOTION:
	{
		// SET(obj, "which", (int)val.ev.motion.which); // (not in wsdl.h)
		// SET(obj, "state", (int)val.ev.motion.state); // (not in wsdl.h)
		SET(obj, "x", (int)val.ev.motion.x);
		SET(obj, "y", (int)val.ev.motion.y);
		// SET(obj, "xrel", (int)val.ev.motion.xrel); // (not in wsdl.h)
		// SET(obj, "yrel", (int)val.ev.motion.yrel); // (not in wsdl.h)
		break;
	}
	case SDL_MOUSEBUTTONDOWN:
	case SDL_MOUSEBUTTONUP:
	{
		// SET(obj, "which", (int)val.ev.button.which); // (not in wsdl.h)
		SET(obj, "button", (int)val.ev.button.button);
		SET(obj, "state", (int)val.ev.button.state);
		SET(obj, "x", (int)val.ev.button.x);
		SET(obj, "y", (int)val.ev.button.y);
		SET(obj, "clicks", (int)val.ev.button.clicks);
		break;
	}
	case SDL_HOTKEYPRESS:
	case SDL_HOTKEYDOWN:
	case SDL_HOTKEYUP:
	case SDL_HOTKEYPRESS_SILENT:
	case SDL_HOTKEYUP_SILENT:
	{
		SET(obj, "hotkey", static_cast<const char*>(val.ev.user.data1));
		break;
	}
	}

	ret.setObject(*obj);
}

template<> void Script::ToJSVal<IGUIObject*>(const ScriptRequest& UNUSED(rq), JS::MutableHandleValue ret, IGUIObject* const& val)
{
	if (val == nullptr)
		ret.setNull();
	else
		ret.setObject(*val->GetJSObject());
}

template<> bool Script::FromJSVal<IGUIObject*>(const ScriptRequest& rq, JS::HandleValue v, IGUIObject*& out)
{
	if (!v.isObject())
	{
		ScriptException::Raise(rq, "Value is not an IGUIObject.");
		return false;
	}
	out = IGUIProxyObject::FromPrivateSlot<IGUIObject>(v.toObjectOrNull());
	if (!out)
	{
		ScriptException::Raise(rq, "Value is not an IGUIObject.");
		return false;
	}
	return true;
}

template<> void Script::ToJSVal<CGUIString>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CGUIString& val)
{
	Script::ToJSVal(rq, ret, val.GetOriginalString());
}

template<> bool Script::FromJSVal<CGUIString>(const ScriptRequest& rq, JS::HandleValue v, CGUIString& out)
{
	std::wstring val;
	if (!FromJSVal(rq, v, val))
		return false;
	out.SetValue(val);
	return true;
}

JSVAL_VECTOR(CVector2D)
JSVAL_VECTOR(std::vector<CVector2D>)
JSVAL_VECTOR(CGUIString)

template<> void Script::ToJSVal<CGUIColor>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CGUIColor& val)
{
	ToJSVal<CColor>(rq, ret, val);
}

/**
 * The color depends on the predefined color database stored in the current GUI page.
 */
template<> bool Script::FromJSVal<CGUIColor>(const ScriptRequest& rq, JS::HandleValue v, CGUIColor& out) = delete;

template<> void Script::ToJSVal<CRect>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CRect& val)
{
	Script::CreateObject(
		rq,
		ret,
		"left", val.left,
		"right", val.right,
		"top", val.top,
		"bottom", val.bottom);
}

template<> void Script::ToJSVal<CGUISize>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CGUISize& val)
{
	val.ToJSVal(rq, ret);
}

template<> bool Script::FromJSVal<CGUISize>(const ScriptRequest& rq, JS::HandleValue v, CGUISize& out)
{
	return out.FromJSVal(rq, v);
}

template<> void Script::ToJSVal<CGUIList>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CGUIList& val)
{
	ToJSVal(rq, ret, val.m_Items);
}

template<> bool Script::FromJSVal<CGUIList>(const ScriptRequest& rq, JS::HandleValue v, CGUIList& out)
{
	return FromJSVal(rq, v, out.m_Items);
}

template<> void Script::ToJSVal<CGUISeries>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CGUISeries& val)
{
	ToJSVal(rq, ret, val.m_Series);
}

template<> bool Script::FromJSVal<CGUISeries>(const ScriptRequest& rq, JS::HandleValue v, CGUISeries& out)
{
	return FromJSVal(rq, v, out.m_Series);
}

template<> void Script::ToJSVal<EVAlign>(const ScriptRequest& rq, JS::MutableHandleValue ret, const EVAlign& val)
{
	std::string word;
	switch (val)
	{
	case EVAlign::TOP:
		word = "top";
		break;

	case EVAlign::BOTTOM:
		word = "bottom";
		break;

	case EVAlign::CENTER:
		word = "center";
		break;

	default:
		word = "error";
		ScriptException::Raise(rq, "Invalid EVAlign");
		break;
	}
	ToJSVal(rq, ret, word);
}

template<> bool Script::FromJSVal<EVAlign>(const ScriptRequest& rq, JS::HandleValue v, EVAlign& out)
{
	std::string word;
	FromJSVal(rq, v, word);

	if (word == "top")
		out = EVAlign::TOP;
	else if (word == "bottom")
		out = EVAlign::BOTTOM;
	else if (word == "center")
		out = EVAlign::CENTER;
	else
	{
		out = EVAlign::TOP;
		LOGERROR("Invalid alignment (should be 'left', 'right' or 'center')");
		return false;
	}
	return true;
}

template<> void Script::ToJSVal<EAlign>(const ScriptRequest& rq, JS::MutableHandleValue ret, const EAlign& val)
{
	std::string word;
	switch (val)
	{
	case EAlign::LEFT:
		word = "left";
		break;
	case EAlign::RIGHT:
		word = "right";
		break;
	case EAlign::CENTER:
		word = "center";
		break;
	default:
		word = "error";
		ScriptException::Raise(rq, "Invalid alignment (should be 'left', 'right' or 'center')");
		break;
	}
	ToJSVal(rq, ret, word);
}

template<> bool Script::FromJSVal<EAlign>(const ScriptRequest& rq, JS::HandleValue v, EAlign& out)
{
	std::string word;
	FromJSVal(rq, v, word);

	if (word == "left")
		out = EAlign::LEFT;
	else if (word == "right")
		out = EAlign::RIGHT;
	else if (word == "center")
		out = EAlign::CENTER;
	else
	{
		out = EAlign::LEFT;
		LOGERROR("Invalid alignment (should be 'left', 'right' or 'center')");
		return false;
	}
	return true;
}

template<> void Script::ToJSVal<CGUISpriteInstance>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CGUISpriteInstance& val)
{
	ToJSVal(rq, ret, val.GetName());
}

template<> bool Script::FromJSVal<CGUISpriteInstance>(const ScriptRequest& rq, JS::HandleValue v, CGUISpriteInstance& out)
{
	std::string name;
	if (!FromJSVal(rq, v, name))
		return false;

	out.SetName(name);
	return true;
}

template<> void Script::ToJSVal<CSize2D>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CSize2D& val)
{
	Script::CreateObject(rq, ret, "width", val.Width, "height", val.Height);
}

template<> bool Script::FromJSVal<CSize2D>(const ScriptRequest& rq, JS::HandleValue v, CSize2D& out)
{
	if (!v.isObject())
	{
		LOGERROR("CSize2D value must be an object!");
		return false;
	}

	if (!FromJSProperty(rq, v, "width", out.Width))
	{
		LOGERROR("Failed to get CSize2D.Width property");
		return false;
	}

	if (!FromJSProperty(rq, v, "height", out.Height))
	{
		LOGERROR("Failed to get CSize2D.Height property");
		return false;
	}

	return true;
}

template<> void Script::ToJSVal<CVector2D>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CVector2D& val)
{
	Script::CreateObject(rq, ret, "x", val.X, "y", val.Y);
}

template<> bool Script::FromJSVal<CVector2D>(const ScriptRequest& rq, JS::HandleValue v, CVector2D& out)
{
	if (!v.isObject())
	{
		LOGERROR("CVector2D value must be an object!");
		return false;
	}

	if (!FromJSProperty(rq, v, "x", out.X))
	{
		LOGERROR("Failed to get CVector2D.X property");
		return false;
	}

	if (!FromJSProperty(rq, v, "y", out.Y))
	{
		LOGERROR("Failed to get CVector2D.Y property");
		return false;
	}

	return true;
}

#undef SET