commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #ifndef SkV8Example_JsContext_DEFINED |
| 11 | #define SkV8Example_JsContext_DEFINED |
| 12 | |
| 13 | #include <v8.h> |
| 14 | |
| 15 | #include "SkPaint.h" |
commit-bot@chromium.org | 24e0496 | 2014-03-04 20:44:32 +0000 | [diff] [blame] | 16 | #include "BaseContext.h" |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace v8; |
| 19 | |
| 20 | class SkCanvas; |
| 21 | class Global; |
| 22 | |
| 23 | // Provides the canvas context implementation in JS, and the OnDraw() method in |
| 24 | // C++ that's used to bridge from C++ to JS. Should be used in JS as: |
| 25 | // |
| 26 | // function onDraw(context) { |
| 27 | // context.fillStyle="#FF0000"; |
| 28 | // context.fillRect(x, y, w, h); |
| 29 | // } |
commit-bot@chromium.org | 24e0496 | 2014-03-04 20:44:32 +0000 | [diff] [blame] | 30 | class JsContext : public BaseContext { |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 31 | public: |
| 32 | JsContext(Global* global) |
commit-bot@chromium.org | 24e0496 | 2014-03-04 20:44:32 +0000 | [diff] [blame] | 33 | : INHERITED(global) |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 34 | , fCanvas(NULL) |
| 35 | { |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 36 | } |
commit-bot@chromium.org | 24e0496 | 2014-03-04 20:44:32 +0000 | [diff] [blame] | 37 | virtual ~JsContext() {} |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 38 | |
| 39 | // Parse the script. |
| 40 | bool initialize(); |
| 41 | |
| 42 | // Call this with the SkCanvas you want onDraw to draw on. |
| 43 | void onDraw(SkCanvas* canvas); |
| 44 | |
commit-bot@chromium.org | 24e0496 | 2014-03-04 20:44:32 +0000 | [diff] [blame] | 45 | virtual SkCanvas* getCanvas() { return fCanvas; }; |
| 46 | |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 47 | private: |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 48 | |
| 49 | // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap. |
| 50 | Handle<Object> wrap(); |
| 51 | |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 52 | // A handle to the onDraw function defined in the script. |
| 53 | Persistent<Function> fOnDraw; |
| 54 | |
| 55 | // The template for what a canvas context object looks like. The canvas |
| 56 | // context object is what's passed into the JS onDraw() function. |
| 57 | static Persistent<ObjectTemplate> gContextTemplate; |
commit-bot@chromium.org | 24e0496 | 2014-03-04 20:44:32 +0000 | [diff] [blame] | 58 | |
| 59 | // Only valid when inside OnDraw(). |
| 60 | SkCanvas* fCanvas; |
| 61 | |
| 62 | typedef BaseContext INHERITED; |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | #endif |