OLD | NEW |
(Empty) | |
| 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 { |
| 35 fFillStyle.setColor(SK_ColorRED); |
| 36 } |
| 37 ~JsContext(); |
| 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 |
| 45 private: |
| 46 // Implementation of the context.fillStyle field. |
| 47 static void GetFillStyle(Local<String> name, |
| 48 const PropertyCallbackInfo<Value>& info); |
| 49 static void SetFillStyle(Local<String> name, Local<Value> value, |
| 50 const PropertyCallbackInfo<void>& info); |
| 51 static void GetWidth(Local<String> name, |
| 52 const PropertyCallbackInfo<Value>& info); |
| 53 static void GetHeight(Local<String> name, |
| 54 const PropertyCallbackInfo<Value>& info); |
| 55 |
| 56 // Implementation of the context.fillRect() JS function. |
| 57 static void FillRect(const v8::FunctionCallbackInfo<Value>& args); |
| 58 |
| 59 // Implementation of the context.stroke(Path path) JS function. |
| 60 static void Stroke(const v8::FunctionCallbackInfo<Value>& args); |
| 61 |
| 62 // Implementation of the context.fill(Path path) JS function. |
| 63 static void Fill(const v8::FunctionCallbackInfo<Value>& args); |
| 64 |
| 65 // Implementation of the context.translate(dx, dy) JS function. |
| 66 static void Translate(const v8::FunctionCallbackInfo<Value>& args); |
| 67 |
| 68 // Implementation of the context.resetTransform() JS function. |
| 69 static void ResetTransform(const v8::FunctionCallbackInfo<Value>& args); |
| 70 |
| 71 // Get the pointer out of obj. |
| 72 static JsContext* Unwrap(Handle<Object> obj); |
| 73 |
| 74 // Create a template for JS object associated with JsContext, called lazily |
| 75 // by Wrap() and the results are stored in gContextTemplate; |
| 76 Handle<ObjectTemplate> makeContextTemplate(); |
| 77 |
| 78 // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap. |
| 79 Handle<Object> wrap(); |
| 80 |
| 81 Global* fGlobal; |
| 82 |
| 83 // Only valid when inside OnDraw(). |
| 84 SkCanvas* fCanvas; |
| 85 |
| 86 SkPaint fFillStyle; |
| 87 |
| 88 // A handle to the onDraw function defined in the script. |
| 89 Persistent<Function> fOnDraw; |
| 90 |
| 91 // The template for what a canvas context object looks like. The canvas |
| 92 // context object is what's passed into the JS onDraw() function. |
| 93 static Persistent<ObjectTemplate> gContextTemplate; |
| 94 }; |
| 95 |
| 96 #endif |
OLD | NEW |