Line data Source code
1 0 : /*
2 : OOJSFlasher.m
3 :
4 : Oolite
5 : Copyright (C) 2004-2013 Giles C Williams and contributors
6 :
7 : This program is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU General Public License
9 : as published by the Free Software Foundation; either version 2
10 : of the License, or (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program; if not, write to the Free Software
19 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 : MA 02110-1301, USA.
21 :
22 : */
23 :
24 : #import "OOFlasherEntity.h"
25 : #import "OOJSFlasher.h"
26 : #import "OOJSEntity.h"
27 : #import "OOJSVector.h"
28 : #import "OOJavaScriptEngine.h"
29 : #import "EntityOOJavaScriptExtensions.h"
30 : #import "ShipEntity.h"
31 : #import "OOVisualEffectEntity.h"
32 :
33 :
34 0 : static JSObject *sFlasherPrototype;
35 :
36 : static BOOL JSFlasherGetFlasherEntity(JSContext *context, JSObject *jsobj, OOFlasherEntity **outEntity);
37 :
38 :
39 : static JSBool FlasherGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
40 : static JSBool FlasherSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
41 :
42 : static JSBool FlasherRemove(JSContext *context, uintN argc, jsval *vp);
43 :
44 :
45 0 : static JSClass sFlasherClass =
46 : {
47 : "Flasher",
48 : JSCLASS_HAS_PRIVATE,
49 :
50 : JS_PropertyStub, // addProperty
51 : JS_PropertyStub, // delProperty
52 : FlasherGetProperty, // getProperty
53 : FlasherSetProperty, // setProperty
54 : JS_EnumerateStub, // enumerate
55 : JS_ResolveStub, // resolve
56 : JS_ConvertStub, // convert
57 : OOJSObjectWrapperFinalize,// finalize
58 : JSCLASS_NO_OPTIONAL_MEMBERS
59 : };
60 :
61 :
62 0 : enum
63 : {
64 : // Property IDs
65 : kFlasher_active,
66 : kFlasher_color,
67 : kFlasher_fraction,
68 : kFlasher_frequency,
69 : kFlasher_phase,
70 : kFlasher_size
71 : };
72 :
73 :
74 0 : static JSPropertySpec sFlasherProperties[] =
75 : {
76 : // JS name ID flags
77 : { "active", kFlasher_active, OOJS_PROP_READWRITE_CB },
78 : { "color", kFlasher_color, OOJS_PROP_READWRITE_CB },
79 : { "fraction", kFlasher_fraction, OOJS_PROP_READWRITE_CB },
80 : { "frequency", kFlasher_frequency, OOJS_PROP_READWRITE_CB },
81 : { "phase", kFlasher_phase, OOJS_PROP_READWRITE_CB },
82 : { "size", kFlasher_size, OOJS_PROP_READWRITE_CB },
83 : { 0 }
84 : };
85 :
86 :
87 0 : static JSFunctionSpec sFlasherMethods[] =
88 : {
89 : // JS name Function min args
90 : { "remove", FlasherRemove, 0 },
91 :
92 : { 0 }
93 : };
94 :
95 :
96 0 : void InitOOJSFlasher(JSContext *context, JSObject *global)
97 : {
98 : sFlasherPrototype = JS_InitClass(context, global, JSEntityPrototype(), &sFlasherClass, OOJSUnconstructableConstruct, 0, sFlasherProperties, sFlasherMethods, NULL, NULL);
99 : OOJSRegisterObjectConverter(&sFlasherClass, OOJSBasicPrivateObjectConverter);
100 : OOJSRegisterSubclass(&sFlasherClass, JSEntityClass());
101 : }
102 :
103 :
104 0 : static BOOL JSFlasherGetFlasherEntity(JSContext *context, JSObject *jsobj, OOFlasherEntity **outEntity)
105 : {
106 : OOJS_PROFILE_ENTER
107 :
108 : BOOL result;
109 : Entity *entity = nil;
110 :
111 : if (outEntity == NULL) return NO;
112 : *outEntity = nil;
113 :
114 : result = OOJSEntityGetEntity(context, jsobj, &entity);
115 : if (!result) return NO;
116 :
117 : if (![entity isKindOfClass:[OOFlasherEntity class]]) return NO;
118 :
119 : *outEntity = (OOFlasherEntity *)entity;
120 : return YES;
121 :
122 : OOJS_PROFILE_EXIT
123 : }
124 :
125 :
126 : @implementation OOFlasherEntity (OOJavaScriptExtensions)
127 :
128 : - (void)getJSClass:(JSClass **)outClass andPrototype:(JSObject **)outPrototype
129 : {
130 : *outClass = &sFlasherClass;
131 : *outPrototype = sFlasherPrototype;
132 : }
133 :
134 :
135 : - (NSString *) oo_jsClassName
136 : {
137 : return @"Flasher";
138 : }
139 :
140 : - (BOOL) isVisibleToScripts
141 : {
142 : return YES;
143 : }
144 :
145 : @end
146 :
147 :
148 0 : static JSBool FlasherGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
149 : {
150 : if (!JSID_IS_INT(propID)) return YES;
151 :
152 : OOJS_NATIVE_ENTER(context)
153 :
154 : OOFlasherEntity *entity = nil;
155 : id result = nil;
156 :
157 : if (!JSFlasherGetFlasherEntity(context, this, &entity)) return NO;
158 : if (entity == nil) { *value = JSVAL_VOID; return YES; }
159 :
160 : switch (JSID_TO_INT(propID))
161 : {
162 : case kFlasher_active:
163 : *value = OOJSValueFromBOOL([entity isActive]);
164 : return YES;
165 :
166 : case kFlasher_color:
167 : result = [[entity color] normalizedArray];
168 : break;
169 :
170 : case kFlasher_frequency:
171 : return JS_NewNumberValue(context, [entity frequency], value);
172 :
173 : case kFlasher_fraction:
174 : return JS_NewNumberValue(context, [entity fraction], value);
175 :
176 : case kFlasher_phase:
177 : return JS_NewNumberValue(context, [entity phase], value);
178 :
179 : case kFlasher_size:
180 : return JS_NewNumberValue(context, [entity diameter], value);
181 :
182 : default:
183 : OOJSReportBadPropertySelector(context, this, propID, sFlasherProperties);
184 : return NO;
185 : }
186 :
187 : *value = OOJSValueFromNativeObject(context, result);
188 : return YES;
189 :
190 : OOJS_NATIVE_EXIT
191 : }
192 :
193 :
194 0 : static JSBool FlasherSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
195 : {
196 : if (!JSID_IS_INT(propID)) return YES;
197 :
198 : OOJS_NATIVE_ENTER(context)
199 :
200 : OOFlasherEntity *entity = nil;
201 : jsdouble fValue;
202 : JSBool bValue;
203 : OOColor *colorForScript = nil;
204 :
205 : if (!JSFlasherGetFlasherEntity(context, this, &entity)) return NO;
206 : if (entity == nil) return YES;
207 :
208 : switch (JSID_TO_INT(propID))
209 : {
210 : case kFlasher_active:
211 : if (JS_ValueToBoolean(context, *value, &bValue))
212 : {
213 : [entity setActive:bValue];
214 : return YES;
215 : }
216 : break;
217 :
218 : case kFlasher_color:
219 : colorForScript = [OOColor colorWithDescription:OOJSNativeObjectFromJSValue(context, *value)];
220 : if (colorForScript != nil || JSVAL_IS_NULL(*value))
221 : {
222 : [entity setColor:colorForScript];
223 : return YES;
224 : }
225 : break;
226 :
227 : case kFlasher_frequency:
228 : if (JS_ValueToNumber(context, *value, &fValue))
229 : {
230 : if (fValue >= 0.0)
231 : {
232 : [entity setFrequency:fValue];
233 : return YES;
234 : }
235 : }
236 : break;
237 :
238 : case kFlasher_fraction:
239 : if (JS_ValueToNumber(context, *value, &fValue))
240 : {
241 : if (fValue > 0.0 && fValue <= 1.0)
242 : {
243 : [entity setFraction:fValue];
244 : return YES;
245 : }
246 : }
247 : break;
248 :
249 : case kFlasher_phase:
250 : if (JS_ValueToNumber(context, *value, &fValue))
251 : {
252 : [entity setPhase:fValue];
253 : return YES;
254 : }
255 : break;
256 :
257 : case kFlasher_size:
258 : if (JS_ValueToNumber(context, *value, &fValue))
259 : {
260 : if (fValue > 0.0)
261 : {
262 : [entity setDiameter:fValue];
263 : return YES;
264 : }
265 : }
266 : break;
267 :
268 : default:
269 : OOJSReportBadPropertySelector(context, this, propID, sFlasherProperties);
270 : return NO;
271 : }
272 :
273 : OOJSReportBadPropertyValue(context, this, propID, sFlasherProperties, *value);
274 : return NO;
275 :
276 : OOJS_NATIVE_EXIT
277 : }
278 :
279 :
280 : // *** Methods ***
281 :
282 0 : #define GET_THIS_FLASHER(THISENT) do { \
283 : if (EXPECT_NOT(!JSFlasherGetFlasherEntity(context, OOJS_THIS, &THISENT))) return NO; /* Exception */ \
284 : if (OOIsStaleEntity(THISENT)) OOJS_RETURN_VOID; \
285 : } while (0)
286 :
287 :
288 0 : static JSBool FlasherRemove(JSContext *context, uintN argc, jsval *vp)
289 : {
290 : OOJS_NATIVE_ENTER(context)
291 :
292 : OOFlasherEntity *thisEnt = nil;
293 : GET_THIS_FLASHER(thisEnt);
294 :
295 : Entity *parent = [thisEnt owner];
296 : if ([parent isShip])
297 : {
298 : [(ShipEntity *)parent removeFlasher:thisEnt];
299 : }
300 : else
301 : {
302 : [(OOVisualEffectEntity *)parent removeSubEntity:thisEnt];
303 : }
304 :
305 : OOJS_RETURN_VOID;
306 :
307 : OOJS_NATIVE_EXIT
308 : }
309 :
310 :
|