Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GuiScriptConversions.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 #include "precompiled.h"
19 
21 
22 #include "gui/IGUIObject.h"
24 #include "ps/Hotkey.h"
25 
26 #include "js/jsapi.h"
27 
28 #define SET(obj, name, value) STMT(jsval v_ = ToJSVal(cx, (value)); JS_SetProperty(cx, (obj), (name), &v_))
29  // ignore JS_SetProperty return value, because errors should be impossible
30  // and we can't do anything useful in the case of errors anyway
31 
32 template<> jsval ScriptInterface::ToJSVal<SDL_Event_>(JSContext* cx, SDL_Event_ const& val)
33 {
34  const char* typeName;
35 
36  switch (val.ev.type)
37  {
38 #if SDL_VERSION_ATLEAST(2, 0, 0)
39  case SDL_WINDOWEVENT: typeName = "windowevent"; break;
40 #else
41  case SDL_ACTIVEEVENT: typeName = "activeevent"; break;
42  case SDL_VIDEOEXPOSE: typeName = "videoexpose"; break;
43  case SDL_VIDEORESIZE: typeName = "videoresize"; break;
44 #endif
45  case SDL_KEYDOWN: typeName = "keydown"; break;
46  case SDL_KEYUP: typeName = "keyup"; break;
47  case SDL_MOUSEMOTION: typeName = "mousemotion"; break;
48  case SDL_MOUSEBUTTONDOWN: typeName = "mousebuttondown"; break;
49  case SDL_MOUSEBUTTONUP: typeName = "mousebuttonup"; break;
50  case SDL_QUIT: typeName = "quit"; break;
51  case SDL_HOTKEYDOWN: typeName = "hotkeydown"; break;
52  case SDL_HOTKEYUP: typeName = "hotkeyup"; break;
53  default: typeName = "(unknown)"; break;
54  }
55 
56  JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);
57  if (! obj)
58  return JSVAL_VOID;
59 
60  SET(obj, "type", typeName);
61 
62  switch (val.ev.type)
63  {
64 #if !SDL_VERSION_ATLEAST(2, 0, 0)
65  case SDL_ACTIVEEVENT:
66  {
67  SET(obj, "gain", (int)val.ev.active.gain);
68  SET(obj, "state", (int)val.ev.active.state);
69  break;
70  }
71 #endif
72  case SDL_KEYDOWN:
73  case SDL_KEYUP:
74  {
75  // SET(obj, "which", (int)val.ev.key.which); // (not in wsdl.h)
76  // SET(obj, "state", (int)val.ev.key.state); // (not in wsdl.h)
77 
78  JSObject* keysym = JS_NewObject(cx, NULL, NULL, NULL);
79  if (! keysym)
80  return JSVAL_VOID;
81  jsval keysymVal = OBJECT_TO_JSVAL(keysym);
82  JS_SetProperty(cx, obj, "keysym", &keysymVal);
83 
84  // SET(keysym, "scancode", (int)val.ev.key.keysym.scancode); // (not in wsdl.h)
85  SET(keysym, "sym", (int)val.ev.key.keysym.sym);
86  // SET(keysym, "mod", (int)val.ev.key.keysym.mod); // (not in wsdl.h)
87  if (val.ev.key.keysym.unicode)
88  {
89  std::wstring unicode(1, (wchar_t)val.ev.key.keysym.unicode);
90  SET(keysym, "unicode", unicode);
91  }
92  else
93  {
94  SET(keysym, "unicode", CScriptVal(JSVAL_VOID));
95  }
96  // TODO: scripts have no idea what all the key/mod enum values are;
97  // we should probably expose them as constants if we expect scripts to use them
98 
99  break;
100  }
101  case SDL_MOUSEMOTION:
102  {
103  // SET(obj, "which", (int)val.ev.motion.which); // (not in wsdl.h)
104  // SET(obj, "state", (int)val.ev.motion.state); // (not in wsdl.h)
105  SET(obj, "x", (int)val.ev.motion.x);
106  SET(obj, "y", (int)val.ev.motion.y);
107  // SET(obj, "xrel", (int)val.ev.motion.xrel); // (not in wsdl.h)
108  // SET(obj, "yrel", (int)val.ev.motion.yrel); // (not in wsdl.h)
109  break;
110  }
111  case SDL_MOUSEBUTTONDOWN:
112  case SDL_MOUSEBUTTONUP:
113  {
114  // SET(obj, "which", (int)val.ev.button.which); // (not in wsdl.h)
115  SET(obj, "button", (int)val.ev.button.button);
116  SET(obj, "state", (int)val.ev.button.state);
117  SET(obj, "x", (int)val.ev.button.x);
118  SET(obj, "y", (int)val.ev.button.y);
119  break;
120  }
121  case SDL_HOTKEYDOWN:
122  case SDL_HOTKEYUP:
123  {
124  SET(obj, "hotkey", static_cast<const char*>(val.ev.user.data1));
125  break;
126  }
127  }
128 
129  jsval rval = OBJECT_TO_JSVAL(obj);
130 
131  return rval;
132 }
133 
134 template<> jsval ScriptInterface::ToJSVal<IGUIObject*>(JSContext* UNUSED(cx), IGUIObject* const& val)
135 {
136  if (val == NULL)
137  return JSVAL_NULL;
138 
139  return OBJECT_TO_JSVAL(val->GetJSObject());
140 }
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
Definition: wsdl.h:294
#define SET(obj, name, value)
A trivial wrapper around a jsval.
Definition: ScriptVal.h:29
Base settings, all objects possess these settings in their m_BaseSettings Instructions can be found i...
Definition: IGUIObject.h:140
Hotkey system.
const int SDL_HOTKEYUP
Definition: Hotkey.h:42
const int SDL_HOTKEYDOWN
Definition: Hotkey.h:41