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