Oolite
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 "OOJSWaypoint.h"
27#import "OOJSEntity.h"
28#import "OOJSQuaternion.h"
29#import "OOJSVector.h"
31
32static JSObject* sWaypointPrototype;
33
34static BOOL JSWaypointGetWaypointEntity(JSContext* context, JSObject* stationObj, OOWaypointEntity** outEntity);
35
36static JSBool WaypointGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value);
37static JSBool WaypointSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value);
38
39static JSClass sWaypointClass = {
40 "Waypoint",
41 JSCLASS_HAS_PRIVATE,
42
43 JS_PropertyStub, // addProperty
44 JS_PropertyStub, // delProperty
45 WaypointGetProperty, // getProperty
46 WaypointSetProperty, // setProperty
47 JS_EnumerateStub, // enumerate
48 JS_ResolveStub, // resolve
49 JS_ConvertStub, // convert
50 OOJSObjectWrapperFinalize, // finalize
51 JSCLASS_NO_OPTIONAL_MEMBERS
52};
53
54enum {
55 // Property IDs
58 kWaypoint_orientation, // overrides entity as waypoints can be unoriented
60};
61
62static JSPropertySpec sWaypointProperties[] = {
63 // JS name ID flags
68 { 0 }
69};
70
71static JSFunctionSpec sWaypointMethods[] = {
72 // JS name Function min args
73 // { "", WaypointDoStuff, 0 },
74 { 0 }
75};
76
83
84static BOOL JSWaypointGetWaypointEntity(JSContext* context, JSObject* wormholeObj, OOWaypointEntity** outEntity)
85{
87
88 BOOL result;
89 Entity* entity = nil;
90
91 if (outEntity == NULL)
92 return NO;
93 *outEntity = nil;
94
95 result = OOJSEntityGetEntity(context, wormholeObj, &entity);
96 if (!result)
97 return NO;
98
99 if (![entity isKindOfClass:[OOWaypointEntity class]])
100 return NO;
101
102 *outEntity = (OOWaypointEntity*)entity;
103 return YES;
104
106}
107
108@implementation OOWaypointEntity (OOJavaScriptExtensions)
109
110- (void)getJSClass:(JSClass**)outClass andPrototype:(JSObject**)outPrototype
111{
112 *outClass = &sWaypointClass;
113 *outPrototype = sWaypointPrototype;
114}
115
116- (NSString*)oo_jsClassName
117{
118 return @"Waypoint";
119}
120
121- (BOOL)isVisibleToScripts
122{
123 return YES;
124}
125
126@end
127
128static JSBool WaypointGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
129{
130 if (!JSID_IS_INT(propID))
131 return YES;
132
133 OOJS_NATIVE_ENTER(context)
134
135 OOWaypointEntity* entity = nil;
136 id result = nil;
138
139 if (!JSWaypointGetWaypointEntity(context, this, &entity))
140 return NO;
141 if (entity == nil) {
142 *value = JSVAL_VOID;
143 return YES;
144 }
145
146 switch (JSID_TO_INT(propID)) {
148 result = [entity beaconCode];
149 break;
150
152 result = [entity beaconLabel];
153 break;
154
156 q = [entity orientation];
157 if (![entity oriented]) {
158 q = kZeroQuaternion;
159 }
160 return QuaternionToJSValue(context, q, value);
161
162 case kWaypoint_size:
163 return JS_NewNumberValue(context, [entity size], value);
164
165 default:
167 return NO;
168 }
169
170 *value = OOJSValueFromNativeObject(context, result);
171 return YES;
172
174}
175
176static JSBool WaypointSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value)
177{
178 if (!JSID_IS_INT(propID))
179 return YES;
180
181 OOJS_NATIVE_ENTER(context)
182
183 OOWaypointEntity* entity = nil;
184 jsdouble fValue;
185 NSString* sValue = nil;
186 Quaternion qValue;
187
188 if (!JSWaypointGetWaypointEntity(context, this, &entity))
189 return NO;
190 if (entity == nil)
191 return YES;
192
193 switch (JSID_TO_INT(propID)) {
195 sValue = OOStringFromJSValue(context, *value);
196 if (sValue == nil || [sValue length] == 0) {
197 if ([entity isBeacon]) {
198 [UNIVERSE clearBeacon:entity];
199 if ([PLAYER nextBeacon] == entity) {
200 [PLAYER setCompassMode:COMPASS_MODE_PLANET];
201 }
202 }
203 } else {
204 if ([entity isBeacon]) {
205 [entity setBeaconCode:sValue];
206 } else // Universe needs to update beacon lists in this case only
207 {
208 [entity setBeaconCode:sValue];
209 [UNIVERSE setNextBeacon:entity];
210 }
211 }
212 return YES;
213 break;
214
216 sValue = OOStringFromJSValue(context, *value);
217 if (sValue != nil) {
218 [entity setBeaconLabel:sValue];
219 return YES;
220 }
221 break;
222
224 if (JSValueToQuaternion(context, *value, &qValue)) {
225 [entity setNormalOrientation:qValue];
226 return YES;
227 }
228 break;
229
230 case kWaypoint_size:
231 if (JS_ValueToNumber(context, *value, &fValue)) {
232 if (fValue > 0.0) {
233 [entity setSize:fValue];
234 return YES;
235 }
236 }
237 break;
238
239 default:
241 return NO;
242 }
243
244 OOJSReportBadPropertyValue(context, this, propID, sWaypointProperties, *value);
245 return NO;
246
248}
249
250// *** 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.m:682
void setNormalOrientation:(Quaternion quat)
Definition Entity.m:692
void setBeaconLabel:(NSString *blabel)
void setBeaconCode:(NSString *bcode)
void setSize:(OOScalar newSize)
voidpf void uLong size
Definition ioapi.h:126