1 /* Copyright (C) 2017 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #include "precompiled.h"
20 #include "scriptinterface/ScriptConversions.h"
22 #include "gui/IGUIObject.h"
23 #include "lib/external_libraries/libsdl.h"
24 #include "ps/Hotkey.h"
25 #include "maths/Vector2D.h"
27 #define SET(obj, name, value) STMT(JS::RootedValue v_(cx); AssignOrToJSVal(cx, &v_, (value)); JS_SetProperty(cx, obj, (name), v_))
28 // ignore JS_SetProperty return value, because errors should be impossible
29 // and we can't do anything useful in the case of errors anyway
31 template<> void ScriptInterface::ToJSVal
<SDL_Event_
>(JSContext
* cx
, JS::MutableHandleValue ret
, SDL_Event_
const& val
)
38 case SDL_WINDOWEVENT
: typeName
= "windowevent"; break;
39 case SDL_KEYDOWN
: typeName
= "keydown"; break;
40 case SDL_KEYUP
: typeName
= "keyup"; break;
41 case SDL_MOUSEMOTION
: typeName
= "mousemotion"; break;
42 case SDL_MOUSEBUTTONDOWN
: typeName
= "mousebuttondown"; break;
43 case SDL_MOUSEBUTTONUP
: typeName
= "mousebuttonup"; break;
44 case SDL_QUIT
: typeName
= "quit"; break;
45 case SDL_HOTKEYDOWN
: typeName
= "hotkeydown"; break;
46 case SDL_HOTKEYUP
: typeName
= "hotkeyup"; break;
47 default: typeName
= "(unknown)"; break;
50 JS::RootedObject
obj(cx
, JS_NewPlainObject(cx
));
57 SET(obj
, "type", typeName
);
64 // SET(obj, "which", (int)val.ev.key.which); // (not in wsdl.h)
65 // SET(obj, "state", (int)val.ev.key.state); // (not in wsdl.h)
67 JS::RootedObject
keysym(cx
, JS_NewPlainObject(cx
));
73 JS::RootedValue
keysymVal(cx
, JS::ObjectValue(*keysym
));
74 JS_SetProperty(cx
, obj
, "keysym", keysymVal
);
76 // SET(keysym, "scancode", (int)val.ev.key.keysym.scancode); // (not in wsdl.h)
77 SET(keysym
, "sym", (int)val
.ev
.key
.keysym
.sym
);
78 // SET(keysym, "mod", (int)val.ev.key.keysym.mod); // (not in wsdl.h)
80 SET(keysym
, "unicode", JS::UndefinedHandleValue
);
82 // TODO: scripts have no idea what all the key/mod enum values are;
83 // we should probably expose them as constants if we expect scripts to use them
89 // SET(obj, "which", (int)val.ev.motion.which); // (not in wsdl.h)
90 // SET(obj, "state", (int)val.ev.motion.state); // (not in wsdl.h)
91 SET(obj
, "x", (int)val
.ev
.motion
.x
);
92 SET(obj
, "y", (int)val
.ev
.motion
.y
);
93 // SET(obj, "xrel", (int)val.ev.motion.xrel); // (not in wsdl.h)
94 // SET(obj, "yrel", (int)val.ev.motion.yrel); // (not in wsdl.h)
97 case SDL_MOUSEBUTTONDOWN
:
98 case SDL_MOUSEBUTTONUP
:
100 // SET(obj, "which", (int)val.ev.button.which); // (not in wsdl.h)
101 SET(obj
, "button", (int)val
.ev
.button
.button
);
102 SET(obj
, "state", (int)val
.ev
.button
.state
);
103 SET(obj
, "x", (int)val
.ev
.button
.x
);
104 SET(obj
, "y", (int)val
.ev
.button
.y
);
105 SET(obj
, "clicks", (int)val
.ev
.button
.clicks
);
111 SET(obj
, "hotkey", static_cast<const char*>(val
.ev
.user
.data1
));
119 template<> void ScriptInterface::ToJSVal
<IGUIObject
*>(JSContext
* UNUSED(cx
), JS::MutableHandleValue ret
, IGUIObject
* const& val
)
124 ret
.setObject(*val
->GetJSObject());
127 template<> void ScriptInterface::ToJSVal
<CGUIString
>(JSContext
* cx
, JS::MutableHandleValue ret
, const CGUIString
& val
)
129 ScriptInterface::ToJSVal(cx
, ret
, val
.GetOriginalString());
132 template<> bool ScriptInterface::FromJSVal
<CGUIString
>(JSContext
* cx
, JS::HandleValue v
, CGUIString
& out
)
135 if (!ScriptInterface::FromJSVal(cx
, v
, val
))
141 // define some vectors
142 JSVAL_VECTOR(CVector2D
)
143 JSVAL_VECTOR(std::vector
<CVector2D
>)
144 JSVAL_VECTOR(CGUIString
)