Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OOJSWaypoint.m
Go to the documentation of this file.
1/*
2OOJSWaypoint.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 "OOWaypointEntity.h"
25#import "OOJSWaypoint.h"
26#import "OOJSEntity.h"
27#import "OOJSVector.h"
28#import "OOJSQuaternion.h"
32
33
34static JSObject *sWaypointPrototype;
35
36static BOOL JSWaypointGetWaypointEntity(JSContext *context, JSObject *stationObj, OOWaypointEntity **outEntity);
37
38
39static JSBool WaypointGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
40static JSBool WaypointSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
41
42
43static JSClass sWaypointClass =
44{
45 "Waypoint",
46 JSCLASS_HAS_PRIVATE,
47
48 JS_PropertyStub, // addProperty
49 JS_PropertyStub, // delProperty
50 WaypointGetProperty, // getProperty
51 WaypointSetProperty, // setProperty
52 JS_EnumerateStub, // enumerate
53 JS_ResolveStub, // resolve
54 JS_ConvertStub, // convert
56 JSCLASS_NO_OPTIONAL_MEMBERS
57};
58
59
60enum
61{
62 // Property IDs
65 kWaypoint_orientation, // overrides entity as waypoints can be unoriented
67};
68
69
70static JSPropertySpec sWaypointProperties[] =
71{
72 // JS name ID flags
77 { 0 }
78};
79
80
81static JSFunctionSpec sWaypointMethods[] =
82{
83 // JS name Function min args
84// { "", WaypointDoStuff, 0 },
85 { 0 }
86};
87
88
95
96
97static BOOL JSWaypointGetWaypointEntity(JSContext *context, JSObject *wormholeObj, OOWaypointEntity **outEntity)
98{
100
101 BOOL result;
102 Entity *entity = nil;
103
104 if (outEntity == NULL) return NO;
105 *outEntity = nil;
106
107 result = OOJSEntityGetEntity(context, wormholeObj, &entity);
108 if (!result) return NO;
109
110 if (![entity isKindOfClass:[OOWaypointEntity class]]) return NO;
111
112 *outEntity = (OOWaypointEntity *)entity;
113 return YES;
114
116}
117
118
119@implementation OOWaypointEntity (OOJavaScriptExtensions)
120
121- (void)getJSClass:(JSClass **)outClass andPrototype:(JSObject **)outPrototype
122{
123 *outClass = &sWaypointClass;
124 *outPrototype = sWaypointPrototype;
125}
126
127
128- (NSString *) oo_jsClassName
129{
130 return @"Waypoint";
131}
132
133- (BOOL) isVisibleToScripts
134{
135 return YES;
136}
137
138@end
139
140
141static JSBool WaypointGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
142{
143 if (!JSID_IS_INT(propID)) return YES;
144
145 OOJS_NATIVE_ENTER(context)
146
147 OOWaypointEntity *entity = nil;
148 id result = nil;
149 Quaternion q = kIdentityQuaternion;
150
151 if (!JSWaypointGetWaypointEntity(context, this, &entity)) return NO;
152 if (entity == nil) { *value = JSVAL_VOID; return YES; }
153
154 switch (JSID_TO_INT(propID))
155 {
157 result = [entity beaconCode];
158 break;
159
161 result = [entity beaconLabel];
162 break;
163
165 q = [entity orientation];
166 if (![entity oriented])
167 {
168 q = kZeroQuaternion;
169 }
170 return QuaternionToJSValue(context, q, value);
171
172 case kWaypoint_size:
173 return JS_NewNumberValue(context, [entity size], value);
174
175 default:
177 return NO;
178 }
179
180 *value = OOJSValueFromNativeObject(context, result);
181 return YES;
182
184}
185
186
187static JSBool WaypointSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
188{
189 if (!JSID_IS_INT(propID)) return YES;
190
191 OOJS_NATIVE_ENTER(context)
192
193 OOWaypointEntity *entity = nil;
194 jsdouble fValue;
195 NSString *sValue = nil;
196 Quaternion qValue;
197
198 if (!JSWaypointGetWaypointEntity(context, this, &entity)) return NO;
199 if (entity == nil) return YES;
200
201 switch (JSID_TO_INT(propID))
202 {
204 sValue = OOStringFromJSValue(context,*value);
205 if (sValue == nil || [sValue length] == 0)
206 {
207 if ([entity isBeacon])
208 {
209 [UNIVERSE clearBeacon:entity];
210 if ([PLAYER nextBeacon] == entity)
211 {
212 [PLAYER setCompassMode:COMPASS_MODE_PLANET];
213 }
214 }
215 }
216 else
217 {
218 if ([entity isBeacon])
219 {
220 [entity setBeaconCode:sValue];
221 }
222 else // Universe needs to update beacon lists in this case only
223 {
224 [entity setBeaconCode:sValue];
225 [UNIVERSE setNextBeacon:entity];
226 }
227 }
228 return YES;
229 break;
230
232 sValue = OOStringFromJSValue(context,*value);
233 if (sValue != nil)
234 {
235 [entity setBeaconLabel:sValue];
236 return YES;
237 }
238 break;
239
241 if (JSValueToQuaternion(context, *value, &qValue))
242 {
243 [entity setNormalOrientation:qValue];
244 return YES;
245 }
246 break;
247
248 case kWaypoint_size:
249 if (JS_ValueToNumber(context, *value, &fValue))
250 {
251 if (fValue > 0.0)
252 {
253 [entity setSize:fValue];
254 return YES;
255 }
256 }
257 break;
258
259 default:
261 return NO;
262 }
263
264 OOJSReportBadPropertyValue(context, this, propID, sWaypointProperties, *value);
265 return NO;
266
268}
269
270
271// *** Methods ***
#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
BOOL JSValueToQuaternion(JSContext *context, jsval value, Quaternion *outQuaternion) NONNULL_FUNC
BOOL QuaternionToJSValue(JSContext *context, Quaternion quaternion, jsval *outValue) NONNULL_FUNC
static JSPropertySpec sWaypointProperties[]
static JSObject * sWaypointPrototype
static JSClass sWaypointClass
static JSBool WaypointSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static JSFunctionSpec sWaypointMethods[]
@ kWaypoint_beaconCode
@ kWaypoint_size
@ kWaypoint_beaconLabel
@ kWaypoint_orientation
static BOOL JSWaypointGetWaypointEntity(JSContext *context, JSObject *stationObj, OOWaypointEntity **outEntity)
void InitOOJSWaypoint(JSContext *context, JSObject *global)
static JSBool WaypointGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
#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)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
void OOJSRegisterSubclass(JSClass *subclass, JSClass *superclass)
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
return nil
const Quaternion kIdentityQuaternion
const Quaternion kZeroQuaternion
#define PLAYER
Quaternion orientation
Definition Entity.h:114
void setNormalOrientation:(Quaternion quat)
Definition Entity.m:744
void setBeaconLabel:(NSString *blabel)
void setBeaconCode:(NSString *bcode)
void setSize:(OOScalar newSize)
voidpf void uLong size
Definition ioapi.h:134