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" |
| 16 | |
| 17 | using namespace v8; |
| 18 | |
| 19 | class SkCanvas; |
| 20 | class Global; |
| 21 | |
| 22 | // Provides the canvas context implementation in JS, and the OnDraw() method in |
| 23 | // C++ that's used to bridge from C++ to JS. Should be used in JS as: |
| 24 | // |
| 25 | // function onDraw(context) { |
| 26 | // context.fillStyle="#FF0000"; |
| 27 | // context.fillRect(x, y, w, h); |
| 28 | // } |
| 29 | class JsContext { |
| 30 | public: |
| 31 | JsContext(Global* global) |
| 32 | : fGlobal(global) |
| 33 | , fCanvas(NULL) |
| 34 | { |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 35 | fFillStyle.setColor(SK_ColorBLACK); |
| 36 | fFillStyle.setAntiAlias(true); |
| 37 | fFillStyle.setStyle(SkPaint::kFill_Style); |
| 38 | fStrokeStyle.setColor(SK_ColorBLACK); |
| 39 | fStrokeStyle.setAntiAlias(true); |
| 40 | fStrokeStyle.setStyle(SkPaint::kStroke_Style); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 41 | } |
| 42 | ~JsContext(); |
| 43 | |
| 44 | // Parse the script. |
| 45 | bool initialize(); |
| 46 | |
| 47 | // Call this with the SkCanvas you want onDraw to draw on. |
| 48 | void onDraw(SkCanvas* canvas); |
| 49 | |
| 50 | private: |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 51 | static void GetStyle(Local<String> name, |
| 52 | const PropertyCallbackInfo<Value>& info, |
| 53 | const SkPaint& style); |
| 54 | static void SetStyle(Local<String> name, Local<Value> value, |
| 55 | const PropertyCallbackInfo<void>& info, |
| 56 | SkPaint& style); |
| 57 | // JS Attributes |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 58 | static void GetFillStyle(Local<String> name, |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 59 | const PropertyCallbackInfo<Value>& info); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 60 | static void SetFillStyle(Local<String> name, Local<Value> value, |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 61 | const PropertyCallbackInfo<void>& info); |
| 62 | static void GetStrokeStyle(Local<String> name, |
| 63 | const PropertyCallbackInfo<Value>& info); |
| 64 | static void SetStrokeStyle(Local<String> name, Local<Value> value, |
| 65 | const PropertyCallbackInfo<void>& info); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 66 | static void GetWidth(Local<String> name, |
| 67 | const PropertyCallbackInfo<Value>& info); |
| 68 | static void GetHeight(Local<String> name, |
| 69 | const PropertyCallbackInfo<Value>& info); |
| 70 | |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 71 | // JS Methods |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 72 | static void FillRect(const v8::FunctionCallbackInfo<Value>& args); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 73 | static void Stroke(const v8::FunctionCallbackInfo<Value>& args); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 74 | static void Fill(const v8::FunctionCallbackInfo<Value>& args); |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 75 | static void Rotate(const v8::FunctionCallbackInfo<Value>& args); |
| 76 | static void Save(const v8::FunctionCallbackInfo<Value>& args); |
| 77 | static void Restore(const v8::FunctionCallbackInfo<Value>& args); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 78 | static void Translate(const v8::FunctionCallbackInfo<Value>& args); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 79 | static void ResetTransform(const v8::FunctionCallbackInfo<Value>& args); |
| 80 | |
| 81 | // Get the pointer out of obj. |
| 82 | static JsContext* Unwrap(Handle<Object> obj); |
| 83 | |
| 84 | // Create a template for JS object associated with JsContext, called lazily |
| 85 | // by Wrap() and the results are stored in gContextTemplate; |
| 86 | Handle<ObjectTemplate> makeContextTemplate(); |
| 87 | |
| 88 | // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap. |
| 89 | Handle<Object> wrap(); |
| 90 | |
| 91 | Global* fGlobal; |
| 92 | |
| 93 | // Only valid when inside OnDraw(). |
| 94 | SkCanvas* fCanvas; |
| 95 | |
| 96 | SkPaint fFillStyle; |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 97 | SkPaint fStrokeStyle; |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 98 | |
| 99 | // A handle to the onDraw function defined in the script. |
| 100 | Persistent<Function> fOnDraw; |
| 101 | |
| 102 | // The template for what a canvas context object looks like. The canvas |
| 103 | // context object is what's passed into the JS onDraw() function. |
| 104 | static Persistent<ObjectTemplate> gContextTemplate; |
| 105 | }; |
| 106 | |
| 107 | #endif |