Oolite
Loading...
Searching...
No Matches
OOJSWaypoint.m
Go to the documentation of this file.
1/*
2OOJSWaypoint.m (QuickJS-ng)
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#import "OOWaypointEntity.h"
32
33static JSClassID sWaypointClassID;
34
35static BOOL JSWaypointGetWaypointEntity(JSContext* context, JSValueConst waypointObj, OOWaypointEntity** outEntity);
36
39
40static JSClassDef sWaypointClass = {
41 .class_name = "Waypoint",
42 .finalizer = OOJSObjectWrapperFinalize,
43};
44
45enum {
46 // Property IDs
49 kWaypoint_orientation, // overrides entity as waypoints can be unoriented
51};
52
60
61// No Waypoint methods.
62
63void InitOOJSWaypoint(JSContext* context, JSValueConst global)
64{
66
67 JSValue parentPrototype = JS_GetClassProto(context, JSEntityClassID());
68 NSCAssert(JS_IsObject(parentPrototype), @"Entity must be initialized before Waypoint.");
69 OOJSInitClass(context, global, sWaypointClassID, parentPrototype, OOJSUnconstructableConstruct, 0,
71 JS_FreeValue(context, parentPrototype);
72
75}
76
77JSClassID JSWaypointClassID(void)
78{
79 return sWaypointClassID;
80}
81
82static BOOL JSWaypointGetWaypointEntity(JSContext* context, JSValueConst waypointObj, OOWaypointEntity** outEntity)
83{
85
86 BOOL result;
87 Entity* entity = nil;
88
89 if (outEntity == NULL)
90 return NO;
91 *outEntity = nil;
92
93 result = OOJSEntityGetEntity(context, waypointObj, &entity);
94 if (!result)
95 return NO;
96
97 if (![entity isKindOfClass:[OOWaypointEntity class]])
98 return NO;
99
100 *outEntity = (OOWaypointEntity*)entity;
101 return YES;
102
104}
105
106@implementation OOWaypointEntity (OOJavaScriptExtensions)
107
108- (JSClassID)oo_jsClassID
109{
110 return sWaypointClassID;
111}
112
113- (NSString*)oo_jsClassName
114{
115 return @"Waypoint";
116}
117
119{
120 return YES;
121}
122
123@end
124
126{
127 OOJS_NATIVE_ENTER(context)
128
129 OOWaypointEntity* entity = nil;
130 id result = nil;
132
133 if (!JSWaypointGetWaypointEntity(context, this, &entity))
134 return OOJSNativeFailure(context);
135 if (entity == nil) {
136 return JS_UNDEFINED;
137 }
138
139 switch (magic) {
141 result = [entity beaconCode];
142 break;
143
145 result = [entity beaconLabel];
146 break;
147
149 q = [entity orientation];
150 if (![entity oriented]) {
151 q = kZeroQuaternion;
152 }
154
155 case kWaypoint_size:
156 return JS_NewNumber(context, [entity size]);
157
158 default:
160 return OOJSNativeFailure(context);
161 }
162
163 return OOJSValueFromNativeObject(context, result);
164
166}
167
169{
170 OOJS_NATIVE_ENTER(context)
171
172 OOWaypointEntity* entity = nil;
173 double fValue;
174 NSString* sValue = nil;
175 Quaternion qValue;
176
177 if (!JSWaypointGetWaypointEntity(context, this, &entity))
178 return OOJSNativeFailure(context);
179 if (entity == nil)
180 return JS_UNDEFINED;
181
182 switch (magic) {
184 sValue = OOStringFromJSValue(context, value);
185 if (sValue == nil || [sValue length] == 0) {
186 if ([entity isBeacon]) {
187 [UNIVERSE clearBeacon:entity];
188 if ([PLAYER nextBeacon] == entity) {
189 [PLAYER setCompassMode:COMPASS_MODE_PLANET];
190 }
191 }
192 } else {
193 if ([entity isBeacon]) {
194 [entity setBeaconCode:sValue];
195 } else // Universe needs to update beacon lists in this case only
196 {
197 [entity setBeaconCode:sValue];
198 [UNIVERSE setNextBeacon:entity];
199 }
200 }
201 return JS_UNDEFINED;
202 break;
203
205 sValue = OOStringFromJSValue(context, value);
206 if (sValue != nil) {
207 [entity setBeaconLabel:sValue];
208 return JS_UNDEFINED;
209 }
210 break;
211
213 if (JSValueToQuaternion(context, value, &qValue)) {
214 [entity setNormalOrientation:qValue];
215 return JS_UNDEFINED;
216 }
217 break;
218
219 case kWaypoint_size:
220 if (JS_ToFloat64(context, &fValue, value) == 0) {
221 if (fValue > 0.0) {
222 [entity setSize:fValue];
223 return JS_UNDEFINED;
224 }
225 }
226 break;
227
228 default:
230 return OOJSNativeFailure(context);
231 }
232
234 return OOJSNativeFailure(context);
235
237}
#define OOJS_PROFILE_EXIT
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
BOOL JSValueToQuaternion(JSContext *context, jsval value, Quaternion *outQuaternion) NONNULL_FUNC
static JSPropertySpec sWaypointProperties[]
static JSClass sWaypointClass
static JSBool WaypointSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
@ 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)
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)
#define OOJS_RETURN_QUATERNION(value)
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
JSClassID JSEntityClassID(void)
Definition OOJSEntity.m:115
JSClassID JSWaypointClassID(void)
static JSClassID sWaypointClassID
#define OOJS_PROPERTY_RW(name, getter, setter, magic)
JSValue OOJSNativeFailure(JSContext *context)
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
#define OOJS_SETTER_ARGS
JSValueConst OOJSInitClass(JSContext *context, JSValueConst global, JSClassID classID, JSValueConst parentPrototype, JSCFunction *constructor, int constructorArgCount, const JSCFunctionListEntry *properties, size_t propertyCount, const JSCFunctionListEntry *methods, size_t methodCount, const JSCFunctionListEntry *staticMethods, size_t staticMethodCount)
#define OOJS_GETTER_ARGS
#define OOJS_COUNT(array)
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