OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkLua.h" | 8 #include "SkLua.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkPaint.h" | 10 #include "SkPaint.h" |
11 #include "SkPath.h" | 11 #include "SkPath.h" |
12 #include "SkMatrix.h" | 12 #include "SkMatrix.h" |
13 #include "SkRRect.h" | 13 #include "SkRRect.h" |
14 #include "SkString.h" | 14 #include "SkString.h" |
15 | 15 |
16 extern "C" { | 16 extern "C" { |
17 #include "lua.h" | 17 #include "lua.h" |
18 #include "lualib.h" | 18 #include "lualib.h" |
19 #include "lauxlib.h" | 19 #include "lauxlib.h" |
20 } | 20 } |
21 | 21 |
| 22 // return the metatable name for a given class |
22 template <typename T> const char* get_mtname(); | 23 template <typename T> const char* get_mtname(); |
23 template <> const char* get_mtname<SkCanvas>() { return "SkCanvas_LuaMetaTableNa
me"; } | 24 #define DEF_MTNAME(T) \ |
24 template <> const char* get_mtname<SkMatrix>() { return "SkMatrix_LuaMetaTableNa
me"; } | 25 template <> const char* get_mtname<T>() { \ |
25 template <> const char* get_mtname<SkRRect>() { return "SkSkRRect_LuaMetaTableNa
me"; } | 26 return #T "_LuaMetaTableName"; \ |
26 template <> const char* get_mtname<SkPath>() { return "SkPath_LuaMetaTableName";
} | 27 } |
27 template <> const char* get_mtname<SkPaint>() { return "SkPaint_LuaMetaTableName
"; } | 28 |
| 29 DEF_MTNAME(SkCanvas) |
| 30 DEF_MTNAME(SkMatrix) |
| 31 DEF_MTNAME(SkRRect) |
| 32 DEF_MTNAME(SkPath) |
| 33 DEF_MTNAME(SkPaint) |
28 | 34 |
29 template <typename T> T* push_new(lua_State* L) { | 35 template <typename T> T* push_new(lua_State* L) { |
30 T* addr = (T*)lua_newuserdata(L, sizeof(T)); | 36 T* addr = (T*)lua_newuserdata(L, sizeof(T)); |
31 new (addr) T; | 37 new (addr) T; |
32 luaL_getmetatable(L, get_mtname<T>()); | 38 luaL_getmetatable(L, get_mtname<T>()); |
33 lua_setmetatable(L, -2); | 39 lua_setmetatable(L, -2); |
34 return addr; | 40 return addr; |
35 } | 41 } |
36 | 42 |
37 template <typename T> void push_obj(lua_State* L, const T& obj) { | 43 template <typename T> void push_obj(lua_State* L, const T& obj) { |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 } | 246 } |
241 | 247 |
242 static int lcanvas_drawCircle(lua_State* L) { | 248 static int lcanvas_drawCircle(lua_State* L) { |
243 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2), | 249 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2), |
244 lua2scalar(L, 3), | 250 lua2scalar(L, 3), |
245 lua2scalar(L, 4), | 251 lua2scalar(L, 4), |
246 *get_obj<SkPaint>(L, 5)); | 252 *get_obj<SkPaint>(L, 5)); |
247 return 0; | 253 return 0; |
248 } | 254 } |
249 | 255 |
| 256 static int lcanvas_drawPath(lua_State* L) { |
| 257 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2), |
| 258 *get_obj<SkPaint>(L, 3)); |
| 259 return 0; |
| 260 } |
| 261 |
250 static int lcanvas_getSaveCount(lua_State* L) { | 262 static int lcanvas_getSaveCount(lua_State* L) { |
251 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount()); | 263 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount()); |
252 return 1; | 264 return 1; |
253 } | 265 } |
254 | 266 |
255 static int lcanvas_getTotalMatrix(lua_State* L) { | 267 static int lcanvas_getTotalMatrix(lua_State* L) { |
256 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix()); | 268 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix()); |
257 return 1; | 269 return 1; |
258 } | 270 } |
259 | 271 |
260 static int lcanvas_translate(lua_State* L) { | 272 static int lcanvas_translate(lua_State* L) { |
261 get_ref<SkCanvas>(L, 1)->translate(lua2scalar(L, 2), lua2scalar(L, 3)); | 273 get_ref<SkCanvas>(L, 1)->translate(lua2scalar(L, 2), lua2scalar(L, 3)); |
262 return 0; | 274 return 0; |
263 } | 275 } |
264 | 276 |
265 static int lcanvas_gc(lua_State* L) { | 277 static int lcanvas_gc(lua_State* L) { |
266 get_ref<SkCanvas>(L, 1)->unref(); | 278 get_ref<SkCanvas>(L, 1)->unref(); |
267 return 0; | 279 return 0; |
268 } | 280 } |
269 | 281 |
270 static const struct luaL_Reg gSkCanvas_Methods[] = { | 282 static const struct luaL_Reg gSkCanvas_Methods[] = { |
271 { "drawColor", lcanvas_drawColor }, | 283 { "drawColor", lcanvas_drawColor }, |
272 { "drawRect", lcanvas_drawRect }, | 284 { "drawRect", lcanvas_drawRect }, |
273 { "drawOval", lcanvas_drawOval }, | 285 { "drawOval", lcanvas_drawOval }, |
274 { "drawCircle", lcanvas_drawCircle }, | 286 { "drawCircle", lcanvas_drawCircle }, |
| 287 { "drawPath", lcanvas_drawPath }, |
275 { "getSaveCount", lcanvas_getSaveCount }, | 288 { "getSaveCount", lcanvas_getSaveCount }, |
276 { "getTotalMatrix", lcanvas_getTotalMatrix }, | 289 { "getTotalMatrix", lcanvas_getTotalMatrix }, |
277 { "translate", lcanvas_translate }, | 290 { "translate", lcanvas_translate }, |
278 { "__gc", lcanvas_gc }, | 291 { "__gc", lcanvas_gc }, |
279 { NULL, NULL } | 292 { NULL, NULL } |
280 }; | 293 }; |
281 | 294 |
282 /////////////////////////////////////////////////////////////////////////////// | 295 /////////////////////////////////////////////////////////////////////////////// |
283 | 296 |
284 static int lpaint_isAntiAlias(lua_State* L) { | 297 static int lpaint_isAntiAlias(lua_State* L) { |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 } while (0) | 557 } while (0) |
545 | 558 |
546 void SkLua::Load(lua_State* L) { | 559 void SkLua::Load(lua_State* L) { |
547 register_Sk(L); | 560 register_Sk(L); |
548 REG_CLASS(L, SkCanvas); | 561 REG_CLASS(L, SkCanvas); |
549 REG_CLASS(L, SkPath); | 562 REG_CLASS(L, SkPath); |
550 REG_CLASS(L, SkPaint); | 563 REG_CLASS(L, SkPaint); |
551 REG_CLASS(L, SkRRect); | 564 REG_CLASS(L, SkRRect); |
552 } | 565 } |
553 | 566 |
OLD | NEW |