Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOJSFlasher.m
Go to the documentation of this file.
1/*
2OOJSFlasher.m
3
4Oolite
5Copyright (C) 2004-2013 Giles C Williams and contributors
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; either version 2
10of the License, or (at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20MA 02110-1301, USA.
21
22 */
23
24#import "OOFlasherEntity.h"
25#import "OOJSFlasher.h"
26#import "OOJSEntity.h"
27#import "OOJSVector.h"
30#import "ShipEntity.h"
32
33
34static JSObject *sFlasherPrototype;
35
36static BOOL JSFlasherGetFlasherEntity(JSContext *context, JSObject *jsobj, OOFlasherEntity **outEntity);
37
38
39static JSBool FlasherGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
40static JSBool FlasherSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
41
42static JSBool FlasherRemove(JSContext *context, uintN argc, jsval *vp);
43
44
45static 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
58 JSCLASS_NO_OPTIONAL_MEMBERS
59};
60
61
62enum
63{
64 // Property IDs
71};
72
73
74static JSPropertySpec sFlasherProperties[] =
75{
76 // JS name ID flags
83 { 0 }
84};
85
86
87static JSFunctionSpec sFlasherMethods[] =
88{
89 // JS name Function min args
90 { "remove", FlasherRemove, 0 },
91
92 { 0 }
93};
94
95
102
103
104static BOOL JSFlasherGetFlasherEntity(JSContext *context, JSObject *jsobj, OOFlasherEntity **outEntity)
105{
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
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
148static 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
171 return JS_NewNumberValue(context, [entity frequency], value);
172
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:
184 return NO;
185 }
186
187 *value = OOJSValueFromNativeObject(context, result);
188 return YES;
189
191}
192
193
194static 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
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
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:
270 return NO;
271 }
272
273 OOJSReportBadPropertyValue(context, this, propID, sFlasherProperties, *value);
274 return NO;
275
277}
278
279
280// *** Methods ***
281
282#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
288static 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
306
308}
309
310
#define OOJS_PROFILE_EXIT
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
OOINLINE JSClass * JSEntityClass(void)
Definition OOJSEntity.h:42
OOINLINE JSObject * JSEntityPrototype(void)
Definition OOJSEntity.h:43
static JSBool FlasherGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSPropertySpec sFlasherProperties[]
Definition OOJSFlasher.m:74
static JSObject * sFlasherPrototype
Definition OOJSFlasher.m:34
static JSFunctionSpec sFlasherMethods[]
Definition OOJSFlasher.m:87
static JSBool FlasherSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static BOOL JSFlasherGetFlasherEntity(JSContext *context, JSObject *jsobj, OOFlasherEntity **outEntity)
static JSBool FlasherRemove(JSContext *context, uintN argc, jsval *vp)
#define GET_THIS_FLASHER(THISENT)
static JSClass sFlasherClass
Definition OOJSFlasher.m:45
void InitOOJSFlasher(JSContext *context, JSObject *global)
Definition OOJSFlasher.m:96
@ kFlasher_size
Definition OOJSFlasher.m:70
@ kFlasher_phase
Definition OOJSFlasher.m:69
@ kFlasher_active
Definition OOJSFlasher.m:65
@ kFlasher_fraction
Definition OOJSFlasher.m:67
@ kFlasher_frequency
Definition OOJSFlasher.m:68
@ kFlasher_color
Definition OOJSFlasher.m:66
#define OOJS_PROP_READWRITE_CB
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
void OOJSRegisterSubclass(JSClass *subclass, JSClass *superclass)
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
#define OOJS_RETURN_VOID
return nil
id owner()
Definition Entity.m:583
OOColor * colorWithDescription:(id description)
Definition OOColor.m:127
NSArray * normalizedArray()
Definition OOColor.m:511
void setPhase:(float phase)
void setFrequency:(float frequency)
void setActive:(BOOL active)
void setFraction:(float fraction)
void setColor:(OOColor *color)
void setDiameter:(float diameter)