Oolite 1.91.0.7646-241128-10e222e
Loading...
Searching...
No Matches
OOJSPlanet.m
Go to the documentation of this file.
1/*
2
3OOJSPlanet.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
26#import "OOJSPlanet.h"
27#import "OOJSEntity.h"
29#import "OOJSQuaternion.h"
30#import "OOJSVector.h"
31
32#import "OOPlanetEntity.h"
33
34
35static JSObject *sPlanetPrototype;
36
37
38static JSBool PlanetGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
39static JSBool PlanetSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
40
41
42static JSClass sPlanetClass =
43{
44 "Planet",
45 JSCLASS_HAS_PRIVATE,
46
47 JS_PropertyStub, // addProperty
48 JS_PropertyStub, // delProperty
49 PlanetGetProperty, // getProperty
50 PlanetSetProperty, // setProperty
51 JS_EnumerateStub, // enumerate
52 JS_ResolveStub, // resolve
53 JS_ConvertStub, // convert
55 JSCLASS_NO_OPTIONAL_MEMBERS
56};
57
58
59enum
60{
61 // Property IDs
62 kPlanet_airColor, // air color, read/write
63 kPlanet_airColorMixRatio, // air color mix ratio, float, read/write
64 kPlanet_airDensity, // air density, float, read/write
66 kPlanet_illuminationColor, // illumination color, read/write
67 kPlanet_isMainPlanet, // Is [UNIVERSE planet], boolean, read-only
68 kPlanet_name, // Name of planet, string, read/write
69 kPlanet_radius, // Radius of planet in metres, read-only
70 kPlanet_texture, // Planet texture read / write
71 kPlanet_orientation, // orientation, quaternion, read/write
74};
75
76
77static JSPropertySpec sPlanetProperties[] =
78{
79 // JS name ID flags
81 { "airColorMixRatio", kPlanet_airColorMixRatio, OOJS_PROP_READWRITE_CB },
83 { "hasAtmosphere", kPlanet_hasAtmosphere, OOJS_PROP_READONLY_CB },
84 { "illuminationColor", kPlanet_illuminationColor, OOJS_PROP_READWRITE_CB },
88 { "rotationalVelocity", kPlanet_rotationalVelocity, OOJS_PROP_READWRITE_CB },
90 { "orientation", kPlanet_orientation, OOJS_PROP_READWRITE_CB }, // Not documented since it's inherited from Entity
91 { "terminatorThresholdVector", kPlanet_terminatorThresholdVector, OOJS_PROP_READWRITE_CB },
92 { 0 }
93};
94
95
96DEFINE_JS_OBJECT_GETTER(JSPlanetGetPlanetEntity, &sPlanetClass, sPlanetPrototype, OOPlanetEntity)
97
98
99void InitOOJSPlanet(JSContext *context, JSObject *global)
100{
101 sPlanetPrototype = JS_InitClass(context, global, JSEntityPrototype(), &sPlanetClass, OOJSUnconstructableConstruct, 0, sPlanetProperties, NULL, NULL, NULL);
104}
105
106
107@implementation OOPlanetEntity (OOJavaScriptExtensions)
108
109- (BOOL) isVisibleToScripts
110{
111 OOStellarBodyType type = [self planetType];
112 return type == STELLAR_TYPE_NORMAL_PLANET || type == STELLAR_TYPE_MOON;
113}
114
115
116- (void)getJSClass:(JSClass **)outClass andPrototype:(JSObject **)outPrototype
117{
118 *outClass = &sPlanetClass;
119 *outPrototype = sPlanetPrototype;
120}
121
122
123- (NSString *) oo_jsClassName
124{
125 switch ([self planetType])
126 {
128 return @"Planet";
130 return @"Moon";
131 default:
132 return @"Unknown";
133 }
134}
135
136@end
137
138
139static JSBool PlanetGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
140{
141 if (!JSID_IS_INT(propID)) return YES;
142
143 OOJS_NATIVE_ENTER(context)
144
145 OOPlanetEntity *planet = nil;
146 if (!JSPlanetGetPlanetEntity(context, this, &planet)) return NO;
147
148 switch (JSID_TO_INT(propID))
149 {
150 case kPlanet_airColor:
151 *value = OOJSValueFromNativeObject(context, [[planet airColor] normalizedArray]);
152 return YES;
153
155 return JS_NewNumberValue(context, [planet airColorMixRatio], value);
156
158 return JS_NewNumberValue(context, [planet airDensity], value);
159
161 *value = OOJSValueFromNativeObject(context, [[planet illuminationColor] normalizedArray]);
162 return YES;
163
165 *value = OOJSValueFromBOOL(planet == (id)[UNIVERSE planet]);
166 return YES;
167
168 case kPlanet_radius:
169 return JS_NewNumberValue(context, [planet radius], value);
170
172 *value = OOJSValueFromBOOL([planet hasAtmosphere]);
173 return YES;
174
175 case kPlanet_texture:
176 *value = OOJSValueFromNativeObject(context, [planet textureFileName]);
177 return YES;
178
179 case kPlanet_name:
180 *value = OOJSValueFromNativeObject(context, [planet name]);
181 return YES;
182
184 return QuaternionToJSValue(context, [planet normalOrientation], value);
185
187 return JS_NewNumberValue(context, [planet rotationalVelocity], value);
188
190 return VectorToJSValue(context, [planet terminatorThresholdVector], value);
191
192 default:
193 OOJSReportBadPropertySelector(context, this, propID, sPlanetProperties);
194 return NO;
195 }
196
198}
199
200
201static JSBool PlanetSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
202{
203 if (!JSID_IS_INT(propID)) return YES;
204
205 OOJS_NATIVE_ENTER(context)
206
207 OOPlanetEntity *planet = nil;
208 NSString *sValue = nil;
209 Quaternion qValue;
210 Vector vValue;
211 jsdouble dValue;
212 OOColor *colorForScript = nil;
213
214 if (!JSPlanetGetPlanetEntity(context, this, &planet)) return NO;
215
216 switch (JSID_TO_INT(propID))
217 {
218 case kPlanet_airColor:
219 colorForScript = [OOColor colorWithDescription:OOJSNativeObjectFromJSValue(context, *value)];
220 if (colorForScript != nil || JSVAL_IS_NULL(*value))
221 {
222 [planet setAirColor:colorForScript];
223 return YES;
224 }
225 break;
226
228 if (JS_ValueToNumber(context, *value, &dValue))
229 {
230 [planet setAirColorMixRatio:dValue];
231 return YES;
232 }
233 break;
234
236 if (JS_ValueToNumber(context, *value, &dValue))
237 {
238 [planet setAirDensity:dValue];
239 return YES;
240 }
241 break;
242
244 colorForScript = [OOColor colorWithDescription:OOJSNativeObjectFromJSValue(context, *value)];
245 if (colorForScript != nil || JSVAL_IS_NULL(*value))
246 {
247 [planet setIlluminationColor:colorForScript];
248 return YES;
249 }
250 break;
251
252 case kPlanet_name:
253 sValue = OOStringFromJSValue(context, *value);
254 [planet setName:sValue];
255 return YES;
256
257 case kPlanet_texture:
258 {
259 BOOL OK = NO;
260 sValue = OOStringFromJSValue(context, *value);
261
263
264 if ([planet isKindOfClass:[OOPlanetEntity class]])
265 {
266 if (sValue == nil)
267 {
268 OOJSReportWarning(context, @"Expected texture string. Value not set.");
269 }
270 else
271 {
272 OK = YES;
273 }
274 }
275
276 if (OK)
277 {
278 OK = [planet setUpPlanetFromTexture:sValue];
279 if (!OK) OOJSReportWarning(context, @"Cannot find texture \"%@\". Value not set.", sValue);
280 }
281
283
284 return YES; // Even if !OK, no exception was raised.
285 }
286
288 if (JSValueToQuaternion(context, *value, &qValue))
289 {
290 quaternion_normalize(&qValue);
291 [planet setOrientation:qValue];
292 return YES;
293 }
294 break;
295
297 if (JS_ValueToNumber(context, *value, &dValue))
298 {
299 [planet setRotationalVelocity:dValue];
300 return YES;
301 }
302 break;
303
305 if (JSValueToVector(context, *value, &vValue))
306 {
307 [planet setTerminatorThresholdVector:vValue];
308 return YES;
309 }
310 break;
311
312 default:
313 OOJSReportBadPropertySelector(context, this, propID, sPlanetProperties);
314 return NO;
315 }
316
317 OOJSReportBadPropertyValue(context, this, propID, sPlanetProperties, *value);
318 return NO;
319
321}
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
OOINLINE JSClass * JSEntityClass(void)
Definition OOJSEntity.h:42
OOINLINE JSObject * JSEntityPrototype(void)
Definition OOJSEntity.h:43
void InitOOJSPlanet(JSContext *context, JSObject *global)
Definition OOJSPlanet.m:99
static JSClass sPlanetClass
Definition OOJSPlanet.m:42
static JSObject * sPlanetPrototype
Definition OOJSPlanet.m:35
static JSPropertySpec sPlanetProperties[]
Definition OOJSPlanet.m:77
static JSBool PlanetGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSPlanet.m:139
static JSBool PlanetSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
Definition OOJSPlanet.m:201
@ kPlanet_airColor
Definition OOJSPlanet.m:62
@ kPlanet_orientation
Definition OOJSPlanet.m:71
@ kPlanet_radius
Definition OOJSPlanet.m:69
@ kPlanet_airColorMixRatio
Definition OOJSPlanet.m:63
@ kPlanet_isMainPlanet
Definition OOJSPlanet.m:67
@ kPlanet_terminatorThresholdVector
Definition OOJSPlanet.m:73
@ kPlanet_airDensity
Definition OOJSPlanet.m:64
@ kPlanet_name
Definition OOJSPlanet.m:68
@ kPlanet_rotationalVelocity
Definition OOJSPlanet.m:72
@ kPlanet_hasAtmosphere
Definition OOJSPlanet.m:65
@ kPlanet_illuminationColor
Definition OOJSPlanet.m:66
@ kPlanet_texture
Definition OOJSPlanet.m:70
BOOL JSValueToQuaternion(JSContext *context, jsval value, Quaternion *outQuaternion) NONNULL_FUNC
BOOL QuaternionToJSValue(JSContext *context, Quaternion quaternion, jsval *outValue) NONNULL_FUNC
BOOL JSValueToVector(JSContext *context, jsval value, Vector *outVector) NONNULL_FUNC
Definition OOJSVector.m:259
BOOL VectorToJSValue(JSContext *context, Vector vector, jsval *outValue) NONNULL_FUNC
Definition OOJSVector.m:185
void OOJSPauseTimeLimiter(void)
void OOJSReportWarning(JSContext *context, NSString *format,...)
#define OOJS_PROP_READWRITE_CB
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
#define DEFINE_JS_OBJECT_GETTER(NAME, JSCLASS, JSPROTO, OBJCCLASSNAME)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
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_PROP_READONLY_CB
void OOJSResumeTimeLimiter(void)
return nil
OOStellarBodyType
@ STELLAR_TYPE_MOON
@ STELLAR_TYPE_NORMAL_PLANET
#define UNIVERSE
Definition Universe.h:840
OOColor * colorWithDescription:(id description)
Definition OOColor.m:127