Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSInterface_GUITypes.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 
20 #include "JSInterface_GUITypes.h"
21 #include "ps/CStr.h"
22 
23 /**** GUISize ****/
25  "GUISize", 0,
26  JS_PropertyStub, JS_PropertyStub,
27  JS_PropertyStub, JS_StrictPropertyStub,
28  JS_EnumerateStub, JS_ResolveStub,
29  JS_ConvertStub, JS_FinalizeStub,
30  NULL, NULL, NULL, JSI_GUISize::construct
31 };
32 
33 JSPropertySpec JSI_GUISize::JSI_props[] =
34 {
35  { "left", 0, JSPROP_ENUMERATE},
36  { "top", 1, JSPROP_ENUMERATE},
37  { "right", 2, JSPROP_ENUMERATE},
38  { "bottom", 3, JSPROP_ENUMERATE},
39  { "rleft", 4, JSPROP_ENUMERATE},
40  { "rtop", 5, JSPROP_ENUMERATE},
41  { "rright", 6, JSPROP_ENUMERATE},
42  { "rbottom", 7, JSPROP_ENUMERATE},
43  { 0 }
44 };
45 
46 JSFunctionSpec JSI_GUISize::JSI_methods[] =
47 {
48  { "toString", JSI_GUISize::toString, 0, 0 },
49  { 0 }
50 };
51 
52 JSBool JSI_GUISize::construct(JSContext* cx, uintN argc, jsval* vp)
53 {
54  JSObject* obj = JS_NewObject(cx, &JSI_GUISize::JSI_class, NULL, NULL);
55 
56  if (argc == 8)
57  {
58  JS_SetProperty(cx, obj, "left", &JS_ARGV(cx, vp)[0]);
59  JS_SetProperty(cx, obj, "top", &JS_ARGV(cx, vp)[1]);
60  JS_SetProperty(cx, obj, "right", &JS_ARGV(cx, vp)[2]);
61  JS_SetProperty(cx, obj, "bottom", &JS_ARGV(cx, vp)[3]);
62  JS_SetProperty(cx, obj, "rleft", &JS_ARGV(cx, vp)[4]);
63  JS_SetProperty(cx, obj, "rtop", &JS_ARGV(cx, vp)[5]);
64  JS_SetProperty(cx, obj, "rright", &JS_ARGV(cx, vp)[6]);
65  JS_SetProperty(cx, obj, "rbottom", &JS_ARGV(cx, vp)[7]);
66  }
67  else if (argc == 4)
68  {
69  jsval zero = JSVAL_ZERO;
70  JS_SetProperty(cx, obj, "left", &JS_ARGV(cx, vp)[0]);
71  JS_SetProperty(cx, obj, "top", &JS_ARGV(cx, vp)[1]);
72  JS_SetProperty(cx, obj, "right", &JS_ARGV(cx, vp)[2]);
73  JS_SetProperty(cx, obj, "bottom", &JS_ARGV(cx, vp)[3]);
74  JS_SetProperty(cx, obj, "rleft", &zero);
75  JS_SetProperty(cx, obj, "rtop", &zero);
76  JS_SetProperty(cx, obj, "rright", &zero);
77  JS_SetProperty(cx, obj, "rbottom", &zero);
78  }
79  else
80  {
81  jsval zero = JSVAL_ZERO;
82  JS_SetProperty(cx, obj, "left", &zero);
83  JS_SetProperty(cx, obj, "top", &zero);
84  JS_SetProperty(cx, obj, "right", &zero);
85  JS_SetProperty(cx, obj, "bottom", &zero);
86  JS_SetProperty(cx, obj, "rleft", &zero);
87  JS_SetProperty(cx, obj, "rtop", &zero);
88  JS_SetProperty(cx, obj, "rright", &zero);
89  JS_SetProperty(cx, obj, "rbottom", &zero);
90  }
91 
92  JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
93  return JS_TRUE;
94 }
95 
96 // Produces "10", "-10", "50%", "50%-10", "50%+10", etc
97 CStr ToPercentString(double pix, double per)
98 {
99  if (per == 0)
100  return CStr::FromDouble(pix);
101  else
102  return CStr::FromDouble(per)+"%"+( pix == 0.0 ? CStr() : pix > 0.0 ? CStr("+")+CStr::FromDouble(pix) : CStr::FromDouble(pix) );
103 }
104 
105 JSBool JSI_GUISize::toString(JSContext* cx, uintN argc, jsval* vp)
106 {
107  UNUSED2(argc);
108 
109  CStr buffer;
110 
111  try
112  {
113 #define SIDE(side) buffer += ToPercentString(g_ScriptingHost.GetObjectProperty_Double(JS_THIS_OBJECT(cx, vp), #side), g_ScriptingHost.GetObjectProperty_Double(JS_THIS_OBJECT(cx, vp), "r"#side));
114  SIDE(left);
115  buffer += " ";
116  SIDE(top);
117  buffer += " ";
118  SIDE(right);
119  buffer += " ";
120  SIDE(bottom);
121 #undef SIDE
122  }
124  {
125  JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, "<Error converting value to numbers>")));
126  return JS_TRUE;
127  }
128 
129  JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, buffer.c_str())));
130  return JS_TRUE;
131 }
132 
133 
134 /**** GUIColor ****/
135 
136 
138  "GUIColor", 0,
139  JS_PropertyStub, JS_PropertyStub,
140  JS_PropertyStub, JS_StrictPropertyStub,
141  JS_EnumerateStub, JS_ResolveStub,
142  JS_ConvertStub, JS_FinalizeStub,
143  NULL, NULL, NULL, JSI_GUIColor::construct
144 };
145 
146 JSPropertySpec JSI_GUIColor::JSI_props[] =
147 {
148  { "r", 0, JSPROP_ENUMERATE},
149  { "g", 1, JSPROP_ENUMERATE},
150  { "b", 2, JSPROP_ENUMERATE},
151  { "a", 3, JSPROP_ENUMERATE},
152  { 0 }
153 };
154 
155 JSFunctionSpec JSI_GUIColor::JSI_methods[] =
156 {
157  { "toString", JSI_GUIColor::toString, 0, 0 },
158  { 0 }
159 };
160 
161 JSBool JSI_GUIColor::construct(JSContext* cx, uintN argc, jsval* vp)
162 {
163  JSObject* obj = JS_NewObject(cx, &JSI_GUIColor::JSI_class, NULL, NULL);
164 
165  if (argc == 4)
166  {
167  JS_SetProperty(cx, obj, "r", &JS_ARGV(cx, vp)[0]);
168  JS_SetProperty(cx, obj, "g", &JS_ARGV(cx, vp)[1]);
169  JS_SetProperty(cx, obj, "b", &JS_ARGV(cx, vp)[2]);
170  JS_SetProperty(cx, obj, "a", &JS_ARGV(cx, vp)[3]);
171  }
172  else
173  {
174  // Nice magenta:
175  jsval c;
176  if (!JS_NewNumberValue(cx, 1.0, &c))
177  return JS_FALSE;
178  JS_SetProperty(cx, obj, "r", &c);
179  JS_SetProperty(cx, obj, "b", &c);
180  JS_SetProperty(cx, obj, "a", &c);
181  if (!JS_NewNumberValue(cx, 0.0, &c))
182  return JS_FALSE;
183  JS_SetProperty(cx, obj, "g", &c);
184  }
185 
186  JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
187  return JS_TRUE;
188 }
189 
190 JSBool JSI_GUIColor::toString(JSContext* cx, uintN argc, jsval* vp)
191 {
192  UNUSED2(argc);
193 
194  jsdouble r, g, b, a;
195  if (!JS_ValueToNumber(cx, g_ScriptingHost.GetObjectProperty(JS_THIS_OBJECT(cx, vp), "r"), &r)) return JS_FALSE;
196  if (!JS_ValueToNumber(cx, g_ScriptingHost.GetObjectProperty(JS_THIS_OBJECT(cx, vp), "g"), &g)) return JS_FALSE;
197  if (!JS_ValueToNumber(cx, g_ScriptingHost.GetObjectProperty(JS_THIS_OBJECT(cx, vp), "b"), &b)) return JS_FALSE;
198  if (!JS_ValueToNumber(cx, g_ScriptingHost.GetObjectProperty(JS_THIS_OBJECT(cx, vp), "a"), &a)) return JS_FALSE;
199 
200  char buffer[256];
201  // Convert to integers, to be compatible with the GUI's string SetSetting
202  snprintf(buffer, 256, "%d %d %d %d",
203  (int)(255.0 * r),
204  (int)(255.0 * g),
205  (int)(255.0 * b),
206  (int)(255.0 * a));
207  JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, buffer)));
208  return JS_TRUE;
209 }
210 
211 /**** GUIMouse ****/
212 
213 
215  "GUIMouse", 0,
216  JS_PropertyStub, JS_PropertyStub,
217  JS_PropertyStub, JS_StrictPropertyStub,
218  JS_EnumerateStub, JS_ResolveStub,
219  JS_ConvertStub, JS_FinalizeStub,
220  NULL, NULL, NULL, JSI_GUIMouse::construct
221 };
222 
223 JSPropertySpec JSI_GUIMouse::JSI_props[] =
224 {
225  { "x", 0, JSPROP_ENUMERATE},
226  { "y", 1, JSPROP_ENUMERATE},
227  { "buttons", 2, JSPROP_ENUMERATE},
228  { 0 }
229 };
230 
231 JSFunctionSpec JSI_GUIMouse::JSI_methods[] =
232 {
233  { "toString", JSI_GUIMouse::toString, 0, 0 },
234  { 0 }
235 };
236 
237 JSBool JSI_GUIMouse::construct(JSContext* cx, uintN argc, jsval* vp)
238 {
239  JSObject* obj = JS_NewObject(cx, &JSI_GUIMouse::JSI_class, NULL, NULL);
240 
241  if (argc == 3)
242  {
243  JS_SetProperty(cx, obj, "x", &JS_ARGV(cx, vp)[0]);
244  JS_SetProperty(cx, obj, "y", &JS_ARGV(cx, vp)[1]);
245  JS_SetProperty(cx, obj, "buttons", &JS_ARGV(cx, vp)[2]);
246  }
247  else
248  {
249  jsval zero = JSVAL_ZERO;
250  JS_SetProperty(cx, obj, "x", &zero);
251  JS_SetProperty(cx, obj, "y", &zero);
252  JS_SetProperty(cx, obj, "buttons", &zero);
253  }
254 
255  JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
256  return JS_TRUE;
257 }
258 
259 JSBool JSI_GUIMouse::toString(JSContext* cx, uintN argc, jsval* vp)
260 {
261  UNUSED2(argc);
262 
263  int32 x, y, buttons;
264  if (!JS_ValueToECMAInt32(cx, g_ScriptingHost.GetObjectProperty(JS_THIS_OBJECT(cx, vp), "x"), &x)) return JS_FALSE;
265  if (!JS_ValueToECMAInt32(cx, g_ScriptingHost.GetObjectProperty(JS_THIS_OBJECT(cx, vp), "y"), &y)) return JS_FALSE;
266  if (!JS_ValueToECMAInt32(cx, g_ScriptingHost.GetObjectProperty(JS_THIS_OBJECT(cx, vp), "buttons"), &buttons)) return JS_FALSE;
267 
268  char buffer[256];
269  snprintf(buffer, 256, "%d %d %d", x, y, buttons);
270  JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, buffer)));
271  return JS_TRUE;
272 }
273 
274 
275 // Initialise all the types at once:
277 {
281 }
JSBool toString(JSContext *cx, uintN argc, jsval *vp)
CStr ToPercentString(double pix, double per)
JSBool construct(JSContext *cx, uintN argc, jsval *vp)
JSPropertySpec JSI_props[]
JSFunctionSpec JSI_methods[]
JSPropertySpec JSI_props[]
JSBool toString(JSContext *cx, uintN argc, jsval *vp)
#define UNUSED2(param)
mark a function local variable or parameter as unused and avoid the corresponding compiler warning...
#define SIDE(side)
#define g_ScriptingHost
JSFunctionSpec JSI_methods[]
JSBool construct(JSContext *cx, uintN argc, jsval *vp)
JSBool construct(JSContext *cx, uintN argc, jsval *vp)
JSBool toString(JSContext *cx, uintN argc, jsval *vp)
JSPropertySpec JSI_props[]
JSFunctionSpec JSI_methods[]