Line data Source code
1 0 : /*
2 : OOJSWaypoint.m
3 :
4 : Oolite
5 : Copyright (C) 2004-2013 Giles C Williams and contributors
6 :
7 : This program is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU General Public License
9 : as published by the Free Software Foundation; either version 2
10 : of the License, or (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program; if not, write to the Free Software
19 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 : MA 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"
29 : #import "OOJavaScriptEngine.h"
30 : #import "OOCollectionExtractors.h"
31 : #import "EntityOOJavaScriptExtensions.h"
32 :
33 :
34 0 : static JSObject *sWaypointPrototype;
35 :
36 : static BOOL JSWaypointGetWaypointEntity(JSContext *context, JSObject *stationObj, OOWaypointEntity **outEntity);
37 :
38 :
39 : static JSBool WaypointGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
40 : static JSBool WaypointSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
41 :
42 :
43 0 : static 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
55 : OOJSObjectWrapperFinalize,// finalize
56 : JSCLASS_NO_OPTIONAL_MEMBERS
57 : };
58 :
59 :
60 0 : enum
61 : {
62 : // Property IDs
63 : kWaypoint_beaconCode,
64 : kWaypoint_beaconLabel,
65 : kWaypoint_orientation, // overrides entity as waypoints can be unoriented
66 : kWaypoint_size
67 : };
68 :
69 :
70 0 : static JSPropertySpec sWaypointProperties[] =
71 : {
72 : // JS name ID flags
73 : { "beaconCode", kWaypoint_beaconCode, OOJS_PROP_READWRITE_CB },
74 : { "beaconLabel", kWaypoint_beaconLabel, OOJS_PROP_READWRITE_CB },
75 : { "orientation", kWaypoint_orientation, OOJS_PROP_READWRITE_CB },
76 : { "size", kWaypoint_size, OOJS_PROP_READWRITE_CB },
77 : { 0 }
78 : };
79 :
80 :
81 0 : static JSFunctionSpec sWaypointMethods[] =
82 : {
83 : // JS name Function min args
84 : // { "", WaypointDoStuff, 0 },
85 : { 0 }
86 : };
87 :
88 :
89 0 : void InitOOJSWaypoint(JSContext *context, JSObject *global)
90 : {
91 : sWaypointPrototype = JS_InitClass(context, global, JSEntityPrototype(), &sWaypointClass, OOJSUnconstructableConstruct, 0, sWaypointProperties, sWaypointMethods, NULL, NULL);
92 : OOJSRegisterObjectConverter(&sWaypointClass, OOJSBasicPrivateObjectConverter);
93 : OOJSRegisterSubclass(&sWaypointClass, JSEntityClass());
94 : }
95 :
96 :
97 0 : static BOOL JSWaypointGetWaypointEntity(JSContext *context, JSObject *wormholeObj, OOWaypointEntity **outEntity)
98 : {
99 : OOJS_PROFILE_ENTER
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 :
115 : OOJS_PROFILE_EXIT
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 :
141 0 : static 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 : {
156 : case kWaypoint_beaconCode:
157 : result = [entity beaconCode];
158 : break;
159 :
160 : case kWaypoint_beaconLabel:
161 : result = [entity beaconLabel];
162 : break;
163 :
164 : case kWaypoint_orientation:
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:
176 : OOJSReportBadPropertySelector(context, this, propID, sWaypointProperties);
177 : return NO;
178 : }
179 :
180 : *value = OOJSValueFromNativeObject(context, result);
181 : return YES;
182 :
183 : OOJS_NATIVE_EXIT
184 : }
185 :
186 :
187 0 : static 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 : {
203 : case kWaypoint_beaconCode:
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 :
231 : case kWaypoint_beaconLabel:
232 : sValue = OOStringFromJSValue(context,*value);
233 : if (sValue != nil)
234 : {
235 : [entity setBeaconLabel:sValue];
236 : return YES;
237 : }
238 : break;
239 :
240 : case kWaypoint_orientation:
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:
260 : OOJSReportBadPropertySelector(context, this, propID, sWaypointProperties);
261 : return NO;
262 : }
263 :
264 : OOJSReportBadPropertyValue(context, this, propID, sWaypointProperties, *value);
265 : return NO;
266 :
267 : OOJS_NATIVE_EXIT
268 : }
269 :
270 :
271 : // *** Methods ***
|