| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 * | 7 * |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 #ifndef SkV8Example_DEFINED | 10 #ifndef SkV8Example_DEFINED |
| 11 #define SkV8Example_DEFINED | 11 #define SkV8Example_DEFINED |
| 12 | 12 |
| 13 #include <v8.h> | 13 #include "SkWindow.h" |
| 14 | 14 |
| 15 #include "SkWindow.h" | 15 class JsContext; |
| 16 #include "SkPaint.h" | |
| 17 | |
| 18 using namespace v8; | |
| 19 | |
| 20 class SkCanvas; | |
| 21 class JsCanvas; | |
| 22 class Global; | |
| 23 | 16 |
| 24 class SkV8ExampleWindow : public SkOSWindow { | 17 class SkV8ExampleWindow : public SkOSWindow { |
| 25 public: | 18 public: |
| 26 SkV8ExampleWindow(void* hwnd, JsCanvas* canvas); | 19 SkV8ExampleWindow(void* hwnd, JsContext* canvas); |
| 27 | 20 |
| 28 protected: | 21 protected: |
| 29 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE; | 22 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE; |
| 30 | 23 |
| 31 #ifdef SK_BUILD_FOR_WIN | 24 #ifdef SK_BUILD_FOR_WIN |
| 32 virtual void onHandleInval(const SkIRect&) SK_OVERRIDE; | 25 virtual void onHandleInval(const SkIRect&) SK_OVERRIDE; |
| 33 #endif | 26 #endif |
| 34 | 27 |
| 35 private: | 28 private: |
| 36 typedef SkOSWindow INHERITED; | 29 typedef SkOSWindow INHERITED; |
| 37 JsCanvas* fJsCanvas; | 30 JsContext* fJsContext; |
| 38 }; | |
| 39 | |
| 40 | |
| 41 // Provides the canvas implementation in JS, and the OnDraw() method in C++ | |
| 42 // that's used to bridge from C++ to JS. Should be used in JS as: | |
| 43 // | |
| 44 // function onDraw(canvas) { | |
| 45 // canvas.fillStyle="#FF0000"; | |
| 46 // canvas.fillRect(x, y, w, h); | |
| 47 // } | |
| 48 class JsCanvas { | |
| 49 public: | |
| 50 JsCanvas(Global* global) | |
| 51 : fGlobal(global) | |
| 52 , fCanvas(NULL) | |
| 53 { | |
| 54 fFillStyle.setColor(SK_ColorRED); | |
| 55 } | |
| 56 ~JsCanvas(); | |
| 57 | |
| 58 // Parse the script. | |
| 59 bool initialize(); | |
| 60 | |
| 61 // Call this with the SkCanvas you want onDraw to draw on. | |
| 62 void onDraw(SkCanvas* canvas); | |
| 63 | |
| 64 private: | |
| 65 // Implementation of the canvas.fillStyle field. | |
| 66 static void GetFillStyle(Local<String> name, | |
| 67 const PropertyCallbackInfo<Value>& info); | |
| 68 static void SetFillStyle(Local<String> name, Local<Value> value, | |
| 69 const PropertyCallbackInfo<void>& info); | |
| 70 | |
| 71 // Implementation of the canvas.fillRect() JS function. | |
| 72 static void FillRect(const v8::FunctionCallbackInfo<Value>& args); | |
| 73 | |
| 74 // Get the pointer out of obj. | |
| 75 static JsCanvas* Unwrap(Handle<Object> obj); | |
| 76 | |
| 77 // Create a template for JS object associated with JsCanvas, called lazily | |
| 78 // by Wrap() and the results are stored in fCanvasTemplate; | |
| 79 Handle<ObjectTemplate> makeCanvasTemplate(); | |
| 80 | |
| 81 // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap. | |
| 82 Handle<Object> wrap(); | |
| 83 | |
| 84 Global* fGlobal; | |
| 85 | |
| 86 // Only valid when inside OnDraw(). | |
| 87 SkCanvas* fCanvas; | |
| 88 | |
| 89 SkPaint fFillStyle; | |
| 90 | |
| 91 // A handle to the onDraw function defined in the script. | |
| 92 Persistent<Function> fOnDraw; | |
| 93 | |
| 94 // The template for what a canvas object looks like. The canvas object is | |
| 95 // what's passed into the JS onDraw() function. | |
| 96 static Persistent<ObjectTemplate> fCanvasTemplate; | |
| 97 }; | 31 }; |
| 98 | 32 |
| 99 #endif | 33 #endif |
| OLD | NEW |