Oolite
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 "OOJSFlasher.h"
26#import "OOFlasherEntity.h"
27#import "OOJSEntity.h"
28#import "OOJSVector.h"
31#import "ShipEntity.h"
32
33static JSObject* sFlasherPrototype;
34
35static BOOL JSFlasherGetFlasherEntity(JSContext* context, JSObject* jsobj, OOFlasherEntity** outEntity);
36
37static JSBool FlasherGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value);
38static JSBool FlasherSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value);
39
40static JSBool FlasherRemove(JSContext* context, uintN argc, jsval* vp);
41
42static JSClass sFlasherClass = {
43 "Flasher",
44 JSCLASS_HAS_PRIVATE,
45
46 JS_PropertyStub, // addProperty
47 JS_PropertyStub, // delProperty
48 FlasherGetProperty, // getProperty
49 FlasherSetProperty, // setProperty
50 JS_EnumerateStub, // enumerate
51 JS_ResolveStub, // resolve
52 JS_ConvertStub, // convert
53 OOJSObjectWrapperFinalize, // finalize
54 JSCLASS_NO_OPTIONAL_MEMBERS
55};
56
57enum {
58 // Property IDs
65};
66
67static JSPropertySpec sFlasherProperties[] = {
68 // JS name ID flags
75 { 0 }
76};
77
78static JSFunctionSpec sFlasherMethods[] = {
79 // JS name Function min args
80 { "remove", FlasherRemove, 0 },
81
82 { 0 }
83};
84
91
92static BOOL JSFlasherGetFlasherEntity(JSContext* context, JSObject* jsobj, OOFlasherEntity** outEntity)
93{
95
96 BOOL result;
97 Entity* entity = nil;
98
99 if (outEntity == NULL)
100 return NO;
101 *outEntity = nil;
102
103 result = OOJSEntityGetEntity(context, jsobj, &entity);
104 if (!result)
105 return NO;
106
107 if (![entity isKindOfClass:[OOFlasherEntity class]])
108 return NO;
109
110 *outEntity = (OOFlasherEntity*)entity;
111 return YES;
112
114}
115
116@implementation OOFlasherEntity (OOJavaScriptExtensions)
117
118- (void)getJSClass:(JSClass**)outClass andPrototype:(JSObject**)outPrototype
119{
120 *outClass = &sFlasherClass;
121 *outPrototype = sFlasherPrototype;
122}
123
124- (NSString*)oo_jsClassName
125{
126 return @"Flasher";
127}
128
129- (BOOL)isVisibleToScripts
130{
131 return YES;
132}
133
134@end
135
136static JSBool FlasherGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
137{
138 if (!JSID_IS_INT(propID))
139 return YES;
140
141 OOJS_NATIVE_ENTER(context)
142
143 OOFlasherEntity* entity = nil;
144 id result = nil;
145
146 if (!JSFlasherGetFlasherEntity(context, this, &entity))
147 return NO;
148 if (entity == nil) {
149 *value = JSVAL_VOID;
150 return YES;
151 }
152
153 switch (JSID_TO_INT(propID)) {
154 case kFlasher_active:
155 *value = OOJSValueFromBOOL([entity isActive]);
156 return YES;
157
158 case kFlasher_color:
159 result = [[entity color] normalizedArray];
160 break;
161
163 return JS_NewNumberValue(context, [entity frequency], value);
164
166 return JS_NewNumberValue(context, [entity fraction], value);
167
168 case kFlasher_phase:
169 return JS_NewNumberValue(context, [entity phase], value);
170
171 case kFlasher_size:
172 return JS_NewNumberValue(context, [entity diameter], value);
173
174 default:
176 return NO;
177 }
178
179 *value = OOJSValueFromNativeObject(context, result);
180 return YES;
181
183}
184
185static JSBool FlasherSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value)
186{
187 if (!JSID_IS_INT(propID))
188 return YES;
189
190 OOJS_NATIVE_ENTER(context)
191
192 OOFlasherEntity* entity = nil;
193 jsdouble fValue;
194 JSBool bValue;
195 OOColor* colorForScript = nil;
196
197 if (!JSFlasherGetFlasherEntity(context, this, &entity))
198 return NO;
199 if (entity == nil)
200 return YES;
201
202 switch (JSID_TO_INT(propID)) {
203 case kFlasher_active:
204 if (JS_ValueToBoolean(context, *value, &bValue)) {
205 [entity setActive:bValue];
206 return YES;
207 }
208 break;
209
210 case kFlasher_color:
211 colorForScript = [OOColor colorWithDescription:OOJSNativeObjectFromJSValue(context, *value)];
212 if (colorForScript != nil || JSVAL_IS_NULL(*value)) {
213 [entity setColor:colorForScript];
214 return YES;
215 }
216 break;
217
219 if (JS_ValueToNumber(context, *value, &fValue)) {
220 if (fValue >= 0.0) {
221 [entity setFrequency:fValue];
222 return YES;
223 }
224 }
225 break;
226
228 if (JS_ValueToNumber(context, *value, &fValue)) {
229 if (fValue > 0.0 && fValue <= 1.0) {
230 [entity setFraction:fValue];
231 return YES;
232 }
233 }
234 break;
235
236 case kFlasher_phase:
237 if (JS_ValueToNumber(context, *value, &fValue)) {
238 [entity setPhase:fValue];
239 return YES;
240 }
241 break;
242
243 case kFlasher_size:
244 if (JS_ValueToNumber(context, *value, &fValue)) {
245 if (fValue > 0.0) {
246 [entity setDiameter:fValue];
247 return YES;
248 }
249 }
250 break;
251
252 default:
254 return NO;
255 }
256
257 OOJSReportBadPropertyValue(context, this, propID, sFlasherProperties, *value);
258 return NO;
259
261}
262
263// *** Methods ***
264
265#define GET_THIS_FLASHER(THISENT) \
266 do { \
267 if (EXPECT_NOT(!JSFlasherGetFlasherEntity(context, OOJS_THIS, &THISENT))) \
268 return NO; /* Exception */ \
269 if (OOIsStaleEntity(THISENT)) \
270 OOJS_RETURN_VOID; \
271 } while (0)
272
273static JSBool FlasherRemove(JSContext* context, uintN argc, jsval* vp)
274{
275 OOJS_NATIVE_ENTER(context)
276
277 OOFlasherEntity* thisEnt = nil;
278 GET_THIS_FLASHER(thisEnt);
279
280 Entity* parent = [thisEnt owner];
281 if ([parent isShip]) {
282 [(ShipEntity*)parent removeFlasher:thisEnt];
283 } else {
285 }
286
288
290}
#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:67
@ kFlasher_size
Definition OOJSFlasher.m:64
@ kFlasher_phase
Definition OOJSFlasher.m:63
@ kFlasher_active
Definition OOJSFlasher.m:59
@ kFlasher_fraction
Definition OOJSFlasher.m:61
@ kFlasher_frequency
Definition OOJSFlasher.m:62
@ kFlasher_color
Definition OOJSFlasher.m:60
static JSObject * sFlasherPrototype
Definition OOJSFlasher.m:33
static JSFunctionSpec sFlasherMethods[]
Definition OOJSFlasher.m:78
static JSBool FlasherSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static BOOL JSFlasherGetFlasherEntity(JSContext *context, JSObject *jsobj, OOFlasherEntity **outEntity)
Definition OOJSFlasher.m:92
static JSBool FlasherRemove(JSContext *context, uintN argc, jsval *vp)
#define GET_THIS_FLASHER(THISENT)
static JSClass sFlasherClass
Definition OOJSFlasher.m:42
void InitOOJSFlasher(JSContext *context, JSObject *global)
Definition OOJSFlasher.m:85
#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:548
OOColor * colorWithDescription:(id description)
Definition OOColor.m:135
NSArray * normalizedArray()
Definition OOColor.m:465
void setPhase:(float phase)
void setFrequency:(float frequency)
void setActive:(BOOL active)
void setFraction:(float fraction)
void setColor:(OOColor *color)
void setDiameter:(float diameter)
void removeSubEntity:(Entity< OOSubEntity > *sub)
void removeFlasher:(OOFlasherEntity *flasher)