Line data Source code
1 0 : /*
2 : OOJSExhaustPlume.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 "OOExhaustPlumeEntity.h"
25 : #import "OOJSExhaustPlume.h"
26 : #import "OOJSEntity.h"
27 : #import "OOJSVector.h"
28 : #import "OOJavaScriptEngine.h"
29 : #import "EntityOOJavaScriptExtensions.h"
30 : #import "ShipEntity.h"
31 :
32 :
33 0 : static JSObject *sExhaustPlumePrototype;
34 :
35 : static BOOL JSExhaustPlumeGetExhaustPlumeEntity(JSContext *context, JSObject *jsobj, OOExhaustPlumeEntity **outEntity);
36 :
37 :
38 : static JSBool ExhaustPlumeGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
39 : static JSBool ExhaustPlumeSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
40 :
41 : static JSBool ExhaustPlumeRemove(JSContext *context, uintN argc, jsval *vp);
42 :
43 :
44 0 : static JSClass sExhaustPlumeClass =
45 : {
46 : "ExhaustPlume",
47 : JSCLASS_HAS_PRIVATE,
48 :
49 : JS_PropertyStub, // addProperty
50 : JS_PropertyStub, // delProperty
51 : ExhaustPlumeGetProperty, // getProperty
52 : ExhaustPlumeSetProperty, // setProperty
53 : JS_EnumerateStub, // enumerate
54 : JS_ResolveStub, // resolve
55 : JS_ConvertStub, // convert
56 : OOJSObjectWrapperFinalize,// finalize
57 : JSCLASS_NO_OPTIONAL_MEMBERS
58 : };
59 :
60 :
61 0 : enum
62 : {
63 : // Property IDs
64 : kExhaustPlume_size
65 : };
66 :
67 :
68 0 : static JSPropertySpec sExhaustPlumeProperties[] =
69 : {
70 : // JS name ID flags
71 : { "size", kExhaustPlume_size, OOJS_PROP_READWRITE_CB },
72 : { 0 }
73 : };
74 :
75 :
76 0 : static JSFunctionSpec sExhaustPlumeMethods[] =
77 : {
78 : // JS name Function min args
79 : { "remove", ExhaustPlumeRemove, 0 },
80 :
81 : { 0 }
82 : };
83 :
84 :
85 0 : void InitOOJSExhaustPlume(JSContext *context, JSObject *global)
86 : {
87 : sExhaustPlumePrototype = JS_InitClass(context, global, JSEntityPrototype(), &sExhaustPlumeClass, OOJSUnconstructableConstruct, 0, sExhaustPlumeProperties, sExhaustPlumeMethods, NULL, NULL);
88 : OOJSRegisterObjectConverter(&sExhaustPlumeClass, OOJSBasicPrivateObjectConverter);
89 : OOJSRegisterSubclass(&sExhaustPlumeClass, JSEntityClass());
90 : }
91 :
92 :
93 0 : static BOOL JSExhaustPlumeGetExhaustPlumeEntity(JSContext *context, JSObject *jsobj, OOExhaustPlumeEntity **outEntity)
94 : {
95 : OOJS_PROFILE_ENTER
96 :
97 : BOOL result;
98 : Entity *entity = nil;
99 :
100 : if (outEntity == NULL) return NO;
101 : *outEntity = nil;
102 :
103 : result = OOJSEntityGetEntity(context, jsobj, &entity);
104 : if (!result) return NO;
105 :
106 : if (![entity isKindOfClass:[OOExhaustPlumeEntity class]]) return NO;
107 :
108 : *outEntity = (OOExhaustPlumeEntity *)entity;
109 : return YES;
110 :
111 : OOJS_PROFILE_EXIT
112 : }
113 :
114 :
115 : @implementation OOExhaustPlumeEntity (OOJavaScriptExtensions)
116 :
117 : - (void)getJSClass:(JSClass **)outClass andPrototype:(JSObject **)outPrototype
118 : {
119 : *outClass = &sExhaustPlumeClass;
120 : *outPrototype = sExhaustPlumePrototype;
121 : }
122 :
123 :
124 : - (NSString *) oo_jsClassName
125 : {
126 : return @"ExhaustPlume";
127 : }
128 :
129 : - (BOOL) isVisibleToScripts
130 : {
131 : return YES;
132 : }
133 :
134 : @end
135 :
136 :
137 0 : static JSBool ExhaustPlumeGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
138 : {
139 : if (!JSID_IS_INT(propID)) return YES;
140 :
141 : OOJS_NATIVE_ENTER(context)
142 :
143 : OOExhaustPlumeEntity *entity = nil;
144 : id result = nil;
145 :
146 : if (!JSExhaustPlumeGetExhaustPlumeEntity(context, this, &entity)) return NO;
147 : if (entity == nil) { *value = JSVAL_VOID; return YES; }
148 :
149 : switch (JSID_TO_INT(propID))
150 : {
151 : case kExhaustPlume_size:
152 : return VectorToJSValue(context, [entity scale], value);
153 :
154 : default:
155 : OOJSReportBadPropertySelector(context, this, propID, sExhaustPlumeProperties);
156 : return NO;
157 : }
158 :
159 : *value = OOJSValueFromNativeObject(context, result);
160 : return YES;
161 :
162 : OOJS_NATIVE_EXIT
163 : }
164 :
165 :
166 0 : static JSBool ExhaustPlumeSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
167 : {
168 : if (!JSID_IS_INT(propID)) return YES;
169 :
170 : OOJS_NATIVE_ENTER(context)
171 :
172 : OOExhaustPlumeEntity *entity = nil;
173 : Vector vValue;
174 :
175 : if (!JSExhaustPlumeGetExhaustPlumeEntity(context, this, &entity)) return NO;
176 : if (entity == nil) return YES;
177 :
178 : switch (JSID_TO_INT(propID))
179 : {
180 : case kExhaustPlume_size:
181 : if (JSValueToVector(context, *value, &vValue))
182 : {
183 : [entity setScale:vValue];
184 : return YES;
185 : }
186 : break;
187 :
188 : default:
189 : OOJSReportBadPropertySelector(context, this, propID, sExhaustPlumeProperties);
190 : return NO;
191 : }
192 :
193 : OOJSReportBadPropertyValue(context, this, propID, sExhaustPlumeProperties, *value);
194 : return NO;
195 :
196 : OOJS_NATIVE_EXIT
197 : }
198 :
199 :
200 : // *** Methods ***
201 :
202 0 : #define GET_THIS_EXHAUSTPLUME(THISENT) do { \
203 : if (EXPECT_NOT(!JSExhaustPlumeGetExhaustPlumeEntity(context, OOJS_THIS, &THISENT))) return NO; /* Exception */ \
204 : if (OOIsStaleEntity(THISENT)) OOJS_RETURN_VOID; \
205 : } while (0)
206 :
207 :
208 0 : static JSBool ExhaustPlumeRemove(JSContext *context, uintN argc, jsval *vp)
209 : {
210 : OOJS_NATIVE_ENTER(context)
211 :
212 : OOExhaustPlumeEntity *thisEnt = nil;
213 : GET_THIS_EXHAUSTPLUME(thisEnt);
214 :
215 : ShipEntity *parent = [thisEnt owner];
216 : [parent removeExhaust:thisEnt];
217 :
218 : OOJS_RETURN_VOID;
219 :
220 : OOJS_NATIVE_EXIT
221 : }
222 :
223 :
|