comparison nodejs/paints.cc @ 687:da12923a789a

Migrate paints.cc to use gen_v8_binding.m4
author Thinker K.F. Li <thinker@branda.to>
date Sat, 07 Aug 2010 21:37:57 +0800
parents b346e4699e55
children 0b98bdc53215
comparison
equal deleted inserted replaced
686:ddce24029561 687:da12923a789a
16 * \ingroup xnjsmb 16 * \ingroup xnjsmb
17 * 17 *
18 * @{ 18 * @{
19 */ 19 */
20 20
21 /*! \brief Fill a shape with the paint. 21 static void
22 */ 22 xnjsmb_paint_fill(paint_t *paint, Handle<Object> self, shape_t *sh) {
23 static Handle<Value> 23 Handle<Value> rt_v;
24 xnjsmb_paint_fill(const Arguments &args) { 24 Handle<Object> rt_o;
25 int argc = args.Length();
26 Handle<Object> self = args.This();
27 Handle<Object> sh_obj;
28 Handle<Object> rt;
29 Handle<Value> rt_val;
30 paint_t *paint;
31 shape_t *sh;
32 redraw_man_t *rdman; 25 redraw_man_t *rdman;
33 26
34 if(argc != 1) 27 rt_v = GET(self, "mbrt");
35 THROW("Invalid number of arguments (!= 1)"); 28 rt_o = rt_v->ToObject();
36 if(!args[0]->IsObject()) 29 rdman = xnjsmb_rt_rdman(rt_o);
37 THROW("Invalid argument type (shape)");
38
39 paint = (paint_t *)UNWRAP(self);
40
41 sh_obj = args[0]->ToObject();
42 sh = (shape_t *)UNWRAP(sh_obj);
43
44 rt_val = GET(self, "mbrt");
45 rt = rt_val->ToObject();
46 rdman = xnjsmb_rt_rdman(rt);
47 30
48 rdman_paint_fill(rdman, paint, sh); 31 rdman_paint_fill(rdman, paint, sh);
49 32
50 if(sh_get_coord(sh)) 33 if(sh_get_coord(sh))
51 rdman_shape_changed(rdman, sh); 34 rdman_shape_changed(rdman, sh);
52
53 return Null();
54 } 35 }
55 36
56 /*! \brief Stroke a shape with the paint. 37 static void
57 */ 38 xnjsmb_paint_stroke(paint_t *paint, Handle<Object> self, shape_t *sh) {
58 static Handle<Value> 39 Handle<Value> rt_v;
59 xnjsmb_paint_stroke(const Arguments &args) { 40 Handle<Object> rt_o;
60 int argc = args.Length();
61 Handle<Object> self = args.This();
62 Handle<Object> sh_obj;
63 Handle<Object> rt;
64 Handle<Value> rt_val, sh_val;
65 paint_t *paint;
66 shape_t *sh;
67 redraw_man_t *rdman; 41 redraw_man_t *rdman;
68 42
69 if(argc != 1) 43 rt_v = GET(self, "mbrt");
70 THROW("Invalid number of arguments (!= 1)"); 44 rt_o = rt_v->ToObject();
71 if(!args[0]->IsObject()) 45 rdman = xnjsmb_rt_rdman(rt_o);
72 THROW("Invalid argument type (shape)");
73
74 paint = (paint_t *)UNWRAP(self);
75
76 sh_val = args[0];
77 sh_obj = sh_val->ToObject();
78 sh = (shape_t *)UNWRAP(sh_obj);
79
80 rt_val = GET(self, "mbrt");
81 rt = rt_val->ToObject();
82 rdman = xnjsmb_rt_rdman(rt);
83 46
84 rdman_paint_stroke(rdman, paint, sh); 47 rdman_paint_stroke(rdman, paint, sh);
85 48
86 if(sh_get_coord(sh)) 49 if(sh_get_coord(sh))
87 rdman_shape_changed(rdman, sh); 50 rdman_shape_changed(rdman, sh);
88
89 return Null();
90 } 51 }
91 52
92 /*! \brief Constructor of color paint_color_t object for Javascript. 53 #include "paints-inc.h"
54
55 /*! \defgroup xnjsmb_paints_cons Constructor of paints
56 *
57 * @{
93 */ 58 */
94 static Handle<Value> 59 paint_t *
95 xnjsmb_paint_color(const Arguments &args) { 60 xnjsmb_paint_color_new(njs_runtime_t *rt,
96 int argc = args.Length(); 61 float r, float g, float b, float a,
97 Handle<Object> self = args.This(); 62 const char **err) {
98 Handle<Object> rt; 63 paint_t *paint;
99 redraw_man_t *rdman; 64 redraw_man_t *rdman;
100 paint_t *paint;
101 float r, g, b, a;
102 65
103 if(argc != 5) 66 rdman = X_njs_MB_rdman(rt);
104 THROW("Invalid number of arguments (!= 5)"); 67 paint = rdman_paint_color_new(rdman, r, g, b, a);
105 if(!args[0]->IsObject() || !args[1]->IsNumber() || 68 if(paint == NULL) {
106 !args[2]->IsNumber() || !args[3]->IsNumber() || 69 *err = "can not allocate a paint_color_t";
107 !args[4]->IsNumber()) 70 return NULL;
108 THROW("Invalid argument type"); 71 }
109 72
110 rt = args[0]->ToObject(); 73 return paint;
111 r = args[1]->ToNumber()->Value();
112 g = args[2]->ToNumber()->Value();
113 b = args[3]->ToNumber()->Value();
114 a = args[4]->ToNumber()->Value();
115
116 rdman = xnjsmb_rt_rdman(rt);
117 paint = rdman_paint_color_new(rdman, r, g, b, a);
118 ASSERT(sh != NULL);
119
120 WRAP(self, paint);
121 SET(self, "mbrt", rt);
122
123 return Null();
124 } 74 }
125 75
126 static Persistent<FunctionTemplate> xnjsmb_paint_temp; 76 paint_t *
127 static Persistent<FunctionTemplate> xnjsmb_paint_color_temp; 77 xnjsmb_paint_image_new(njs_runtime_t *rt, mb_img_data_t *img,
78 const char **err) {
79 paint_t *paint;
80 redraw_man_t *rdman;
128 81
129 /*! \brief Create and return a paint_color object. 82 rdman = X_njs_MB_rdman(rt);
130 */ 83 paint = rdman_paint_image_new(rdman, img);
131 static Handle<Value> 84 if(paint == NULL) {
132 xnjsmb_paint_color_new(const Arguments &args) { 85 *err = "can not allocate a paint_image_t";
133 HandleScope scope; 86 return NULL;
134 Handle<Object> rt = args.This(); 87 }
135 Handle<Object> paint_color_obj;
136 Handle<Function> paint_color_func;
137 Handle<Value> pc_args[5];
138 int argc;
139 int i;
140 88
141 argc = args.Length(); 89 return paint;
142 if(argc != 4)
143 THROW("Invalid number of arguments (r, g, b, a)");
144 for(i = 0; i < 4; i++)
145 if(!args[i]->IsNumber())
146 THROW("Invalid argument type");
147
148 pc_args[0] = rt;
149 pc_args[1] = args[0]; // r
150 pc_args[2] = args[1]; // g
151 pc_args[3] = args[2]; // b
152 pc_args[4] = args[3]; // a
153 paint_color_func = xnjsmb_paint_color_temp->GetFunction();
154 paint_color_obj = paint_color_func->NewInstance(5, pc_args);
155
156 scope.Close(paint_color_obj);
157 return paint_color_obj;
158 } 90 }
159 91
160 static Persistent<FunctionTemplate> xnjsmb_paint_color_new_temp; 92 /* @} */
161 93
162 /*! \brief Constructor of paint_image_t objects for Javascript. 94 /*! \defgroup xnjsmb_paints_export Exported wrapper maker for paints
95 *
96 * These functions are used by MB runtime to wrap C paints to JS
97 * objects.
98 *
99 * @{
163 */ 100 */
164 static Handle<Value> 101 Handle<Value>
165 xnjsmb_paint_image(const Arguments &args) { 102 export_xnjsmb_auto_paint_color_new(paint_t *paint) {
166 int argc = args.Length(); 103 Handle<Value> ret;
167 Handle<Object> rt;
168 Handle<Object> self = args.This();
169 Handle<Object> img_obj;
170 redraw_man_t *rdman;
171 mb_img_data_t *img;
172 paint_t *paint;
173
174 if(argc != 2)
175 THROW("Invalid number of arguments (!= 2)");
176 if(!args[0]->IsObject() || !args[1]->IsObject())
177 THROW("Invalid argument type");
178 104
179 rt = args[0]->ToObject(); 105 ret = xnjsmb_auto_paint_color_new(paint);
180 img_obj = args[1]->ToObject();
181 106
182 rdman = xnjsmb_rt_rdman(rt); 107 return ret;
183 img = (mb_img_data_t *)UNWRAP(img_obj);
184
185 paint = rdman_paint_image_new(rdman, img);
186 ASSERT(paint != NULL);
187
188 WRAP(self, paint);
189 SET(self, "mbrt", rt);
190
191 return Null();
192 } 108 }
193 109
194 static Persistent<FunctionTemplate> xnjsmb_paint_image_temp; 110 Handle<Value>
195 111 export_xnjsmb_auto_paint_image_new(paint_t *paint) {
196 /*! \brief Create and return a paint_image object. 112 Handle<Value> ret;
197 */ 113
198 static Handle<Value> 114 ret = xnjsmb_auto_paint_image_new(paint);
199 xnjsmb_paint_image_new(const Arguments &args) { 115
200 int argc = args.Length(); 116 return ret;
201 HandleScope scope;
202 Handle<Object> rt = args.This();
203 Handle<Object> paint_image_obj;
204 Handle<Value> pi_args[2];
205 Handle<Function> paint_image_func;
206
207 if(argc != 1)
208 THROW("Invalid number of arguments (!= 2)");
209 if(!args[0]->IsObject())
210 THROW("Invalid argument type");
211
212 pi_args[0] = rt;
213 pi_args[1] = args[0]; // image
214 paint_image_func = xnjsmb_paint_image_temp->GetFunction();
215 paint_image_obj = paint_image_func->NewInstance(2, pi_args);
216
217 scope.Close(paint_image_obj);
218 return paint_image_obj;
219 } 117 }
220 118 /* @} */
221 static Persistent<FunctionTemplate> xnjsmb_paint_image_new_temp;
222
223 /*! \brief Create templates for paint types.
224 *
225 * This function is only called one time for every execution.
226 */
227 static void
228 xnjsmb_init_paints(void) {
229 Handle<FunctionTemplate> temp, meth;
230 Handle<ObjectTemplate> inst_temp;
231 Handle<ObjectTemplate> proto_temp;
232
233 /*
234 * Base type of paint types.
235 */
236 temp = FunctionTemplate::New();
237 xnjsmb_paint_temp = Persistent<FunctionTemplate>::New(temp);
238 xnjsmb_paint_temp->SetClassName(String::New("paint"));
239
240 meth = FunctionTemplate::New(xnjsmb_paint_fill);
241 proto_temp = xnjsmb_paint_temp->PrototypeTemplate();
242 SET(proto_temp, "fill", meth);
243
244 meth = FunctionTemplate::New(xnjsmb_paint_stroke);
245 proto_temp = xnjsmb_paint_temp->PrototypeTemplate();
246 SET(proto_temp, "stroke", meth);
247
248 /*
249 * Paint color
250 */
251 temp = FunctionTemplate::New(xnjsmb_paint_color);
252 xnjsmb_paint_color_temp = Persistent<FunctionTemplate>::New(temp);
253 xnjsmb_paint_color_temp->SetClassName(String::New("paint_color"));
254 xnjsmb_paint_color_temp->Inherit(xnjsmb_paint_temp);
255
256 inst_temp = xnjsmb_paint_color_temp->InstanceTemplate();
257 inst_temp->SetInternalFieldCount(1);
258
259 temp = FunctionTemplate::New(xnjsmb_paint_color_new);
260 xnjsmb_paint_color_new_temp = Persistent<FunctionTemplate>::New(temp);
261
262 /*
263 * Paint image
264 */
265 temp = FunctionTemplate::New(xnjsmb_paint_image);
266 xnjsmb_paint_image_temp = Persistent<FunctionTemplate>::New(temp);
267 xnjsmb_paint_image_temp->SetClassName(String::New("paint_image"));
268 xnjsmb_paint_image_temp->Inherit(xnjsmb_paint_temp);
269
270 inst_temp = xnjsmb_paint_image_temp->InstanceTemplate();
271 inst_temp->SetInternalFieldCount(1);
272
273 temp = FunctionTemplate::New(xnjsmb_paint_image_new);
274 xnjsmb_paint_image_new_temp = Persistent<FunctionTemplate>::New(temp);
275 }
276 119
277 /*! \brief Initialize paints for mbfly. 120 /*! \brief Initialize paints for mbfly.
278 * 121 *
279 * This function is called by init() in mbfly_njs.cc when the module 122 * This function is called by init() in mbfly_njs.cc when the module
280 * being loaded. 123 * being loaded.
282 void xnjsmb_paints_init_mb_rt_temp(Handle<FunctionTemplate> rt_temp) { 125 void xnjsmb_paints_init_mb_rt_temp(Handle<FunctionTemplate> rt_temp) {
283 static int init_flag = 0; 126 static int init_flag = 0;
284 Handle<ObjectTemplate> rt_proto_temp; 127 Handle<ObjectTemplate> rt_proto_temp;
285 128
286 if(!init_flag) { 129 if(!init_flag) {
287 xnjsmb_init_paints(); 130 xnjsmb_auto_paint_init();
131 xnjsmb_auto_paint_color_init();
132 xnjsmb_auto_paint_image_init();
133
134 /* xnjsmb_init_paints(); */
288 init_flag = 1; 135 init_flag = 1;
289 } 136 }
290 137 /*
291 rt_proto_temp = rt_temp->PrototypeTemplate(); 138 rt_proto_temp = rt_temp->PrototypeTemplate();
292 SET(rt_proto_temp, "paint_color_new", xnjsmb_paint_color_new_temp); 139 SET(rt_proto_temp, "paint_color_new", xnjsmb_paint_color_new_temp);
293 SET(rt_proto_temp, "paint_image_new", xnjsmb_paint_image_new_temp); 140 SET(rt_proto_temp, "paint_image_new", xnjsmb_paint_image_new_temp);
141 */
294 } 142 }
295 143
296 /* @} */ 144 /* @} */