Chromium Code Reviews| Index: samplecode/SampleLua.cpp |
| diff --git a/samplecode/SampleLua.cpp b/samplecode/SampleLua.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3e55482e3b9086bcac53bd612399225787c3e2f9 |
| --- /dev/null |
| +++ b/samplecode/SampleLua.cpp |
| @@ -0,0 +1,115 @@ |
| +/* |
| + * Copyright 2013 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SampleCode.h" |
| +#include "SkView.h" |
| +#include "SkLua.h" |
| +#include "SkCanvas.h" |
| +#include "SkPath.h" |
|
robertphillips
2013/05/22 19:25:06
Why include SkRegion.h?
reed1
2013/05/22 19:30:59
Done.
|
| +#include "SkRegion.h" |
| +#include "SkShader.h" |
| +#include "SkUtils.h" |
| +#include "SkImage.h" |
| +#include "SkSurface.h" |
| + |
| +extern "C" { |
| +#include "lua.h" |
| +#include "lualib.h" |
| +#include "lauxlib.h" |
| +} |
| + |
| +static const char gDrawName[] = "onDrawContent"; |
| + |
| +static const char gCode[] = "" |
| + "local r = { left = 10, top = 10, right = 100, bottom = 80 } " |
| + "local x = 0;" |
| + "" |
| + "local paint = Sk.newPaint();" |
| + "paint:setAntiAlias(true);" |
| + "" |
| + "local color = {a = 1, r = 1, g = 0, b = 0};" |
| + "" |
| + "function onDrawContent(canvas) " |
| + " color.g = x / 100;" |
| + " paint:setColor(color) " |
| + " canvas:translate(x, 0);" |
| + " canvas:drawOval(r, paint) " |
| + " x = x + 1;" |
| + " if x > 100 then x = 0 end;" |
| + "end"; |
| + |
| +class LuaView : public SampleView { |
| +public: |
| + LuaView() { |
|
robertphillips
2013/05/22 19:25:06
Defer this?
reed1
2013/05/22 19:30:59
Marked as a todo for now
|
| + fLua.runCode(gCode); |
| + } |
| + |
| +protected: |
| + virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { |
| + if (SampleCode::TitleQ(*evt)) { |
| + SampleCode::TitleR(evt, "Lua"); |
| + return true; |
| + } |
| + SkUnichar uni; |
| + if (SampleCode::CharQ(*evt, &uni)) { |
| + switch (uni) { |
| + case 'c': |
| + this->inval(NULL); |
| + return true; |
| + default: |
| + break; |
| + } |
| + } |
| + return this->INHERITED::onQuery(evt); |
| + } |
| + |
|
robertphillips
2013/05/22 19:25:06
override?
reed1
2013/05/22 19:30:59
Done.
|
| + virtual void onDrawContent(SkCanvas* canvas) { |
| + SkPaint paint; |
| + paint.setAntiAlias(true); |
| + |
| + lua_State* L = fLua.get(); |
| + |
| + lua_getglobal(L, gDrawName); |
| + if (!lua_isfunction(L, -1)) { |
| + int t = lua_type(L, -1); |
| + SkDebugf("--- expected %s function %d, ignoring.\n", gDrawName, t); |
| + lua_pop(L, 1); |
| + } else { |
| + // does it make sense to try to "cache" the lua version of this |
| + // canvas between draws? |
| + fLua.pushCanvas(canvas); |
| + // hack: when we can create new Paints in lua, we won't need to |
| + // push one up. |
|
robertphillips
2013/05/22 19:25:06
remove
reed1
2013/05/22 19:30:59
Done.
|
| + fLua.pushPaint(paint); |
| + if (lua_pcall(L, 2, 0, 0) != LUA_OK) { |
| + SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| + } |
| + } |
| + // need a way for the lua-sample to tell us if they want animations... |
| + // hard-code it ON for now. |
| + this->inval(NULL); |
| + } |
| + |
| + virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, |
| + unsigned modi) SK_OVERRIDE { |
| + return this->INHERITED::onFindClickHandler(x, y, modi); |
| + } |
| + |
| + virtual bool onClick(Click* click) SK_OVERRIDE { |
| + return this->INHERITED::onClick(click); |
| + } |
| + |
| +private: |
| + SkLua fLua; |
| + |
| + typedef SampleView INHERITED; |
| +}; |
| + |
| +////////////////////////////////////////////////////////////////////////////// |
| + |
| +static SkView* MyFactory() { return new LuaView; } |
| +static SkViewRegister reg(MyFactory); |