reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkLua.h" |
| 11 | #include "SkCanvas.h" |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame^] | 12 | #include "Resources.h" |
| 13 | #include "SkData.h" |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 14 | |
| 15 | extern "C" { |
| 16 | #include "lua.h" |
| 17 | #include "lualib.h" |
| 18 | #include "lauxlib.h" |
| 19 | } |
| 20 | |
| 21 | static const char gDrawName[] = "onDrawContent"; |
| 22 | |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame^] | 23 | static const char gMissingCode[] = "" |
| 24 | "local paint = Sk.newPaint()" |
| 25 | "paint:setAntiAlias(true)" |
| 26 | "paint:setTextSize(30)" |
| 27 | "" |
| 28 | "function onDrawContent(canvas)" |
| 29 | " canvas:drawText('missing \"test.lua\"', 20, 50, paint)" |
| 30 | "end" |
| 31 | ; |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 32 | |
| 33 | class LuaView : public SampleView { |
| 34 | public: |
| 35 | LuaView() : fLua(NULL) {} |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 36 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 37 | virtual ~LuaView() { |
| 38 | SkDELETE(fLua); |
| 39 | } |
| 40 | |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame^] | 41 | void setImageFilename(lua_State* L) { |
| 42 | SkString str = GetResourcePath("mandrill_256.png"); |
| 43 | |
| 44 | lua_getglobal(L, "setImageFilename"); |
| 45 | if (lua_isfunction(L, -1)) { |
| 46 | fLua->pushString(str.c_str()); |
| 47 | if (lua_pcall(L, 1, 0, 0) != LUA_OK) { |
| 48 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 53 | lua_State* ensureLua() { |
| 54 | if (NULL == fLua) { |
| 55 | fLua = SkNEW(SkLua); |
reed | 09445a4 | 2014-10-10 20:31:24 -0700 | [diff] [blame^] | 56 | |
| 57 | SkString str = GetResourcePath("test.lua"); |
| 58 | SkData* data = SkData::NewFromFileName(str.c_str()); |
| 59 | if (data) { |
| 60 | fLua->runCode(data->data(), data->size()); |
| 61 | data->unref(); |
| 62 | this->setImageFilename(fLua->get()); |
| 63 | } else { |
| 64 | fLua->runCode(gMissingCode); |
| 65 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 66 | } |
| 67 | return fLua->get(); |
| 68 | } |
| 69 | |
| 70 | protected: |
| 71 | virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { |
| 72 | if (SampleCode::TitleQ(*evt)) { |
| 73 | SampleCode::TitleR(evt, "Lua"); |
| 74 | return true; |
| 75 | } |
| 76 | SkUnichar uni; |
| 77 | if (SampleCode::CharQ(*evt, &uni)) { |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 78 | } |
| 79 | return this->INHERITED::onQuery(evt); |
| 80 | } |
| 81 | |
| 82 | virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { |
| 83 | lua_State* L = this->ensureLua(); |
| 84 | |
| 85 | lua_getglobal(L, gDrawName); |
| 86 | if (!lua_isfunction(L, -1)) { |
| 87 | int t = lua_type(L, -1); |
| 88 | SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t); |
| 89 | lua_pop(L, 1); |
| 90 | } else { |
| 91 | // does it make sense to try to "cache" the lua version of this |
| 92 | // canvas between draws? |
| 93 | fLua->pushCanvas(canvas); |
| 94 | if (lua_pcall(L, 1, 0, 0) != LUA_OK) { |
| 95 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 96 | } |
| 97 | } |
| 98 | // need a way for the lua-sample to tell us if they want animations... |
| 99 | // hard-code it ON for now. |
| 100 | this->inval(NULL); |
| 101 | } |
| 102 | |
| 103 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, |
| 104 | unsigned modi) SK_OVERRIDE { |
| 105 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 106 | } |
| 107 | |
| 108 | virtual bool onClick(Click* click) SK_OVERRIDE { |
| 109 | return this->INHERITED::onClick(click); |
| 110 | } |
| 111 | |
| 112 | private: |
| 113 | SkLua* fLua; |
| 114 | |
| 115 | typedef SampleView INHERITED; |
| 116 | }; |
| 117 | |
| 118 | ////////////////////////////////////////////////////////////////////////////// |
| 119 | |
| 120 | static SkView* MyFactory() { return new LuaView; } |
| 121 | static SkViewRegister reg(MyFactory); |