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 <v8.h> |
14 | 14 |
15 #include "SkWindow.h" | 15 #include "SkWindow.h" |
16 | 16 |
17 | |
18 using namespace v8; | 17 using namespace v8; |
19 | 18 |
20 | |
21 class SkCanvas; | 19 class SkCanvas; |
22 | 20 class JsCanvas; |
23 | 21 |
24 class SkV8ExampleWindow : public SkOSWindow { | 22 class SkV8ExampleWindow : public SkOSWindow { |
25 public: | 23 public: |
26 SkV8ExampleWindow(void* hwnd, | 24 SkV8ExampleWindow(void* hwnd, JsCanvas* canvas); |
27 Isolate* isolate, | |
28 Handle<Context> context, | |
29 Handle<Script> script); | |
30 | |
31 | 25 |
32 protected: | 26 protected: |
33 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE; | 27 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE; |
34 | 28 |
35 | |
36 | |
37 #ifdef SK_BUILD_FOR_WIN | 29 #ifdef SK_BUILD_FOR_WIN |
38 virtual void onHandleInval(const SkIRect&) SK_OVERRIDE; | 30 virtual void onHandleInval(const SkIRect&) SK_OVERRIDE; |
39 #endif | 31 #endif |
40 | 32 |
41 private: | 33 private: |
42 typedef SkOSWindow INHERITED; | 34 typedef SkOSWindow INHERITED; |
| 35 SkScalar fRotationAngle; |
| 36 JsCanvas* fJsCanvas; |
| 37 }; |
| 38 |
| 39 |
| 40 // Provides the canvas implementation in JS, and the OnDraw() method in C++ |
| 41 // that's used to bridge from C++ to JS. Should be used in JS as: |
| 42 // |
| 43 // function onDraw(canvas) { |
| 44 // canvas.drawRect(); |
| 45 // canvas.inval(); |
| 46 // } |
| 47 // |
| 48 class JsCanvas { |
| 49 public: |
| 50 JsCanvas(Isolate* isolate) |
| 51 : fIsolate(isolate) |
| 52 , fCanvas(NULL) |
| 53 , fWindow(NULL) |
| 54 {} |
| 55 ~JsCanvas(); |
| 56 |
| 57 // Parse the script. |
| 58 bool initialize(const char script[]); |
| 59 |
| 60 // Call this with the SkCanvas you want onDraw to draw on. |
| 61 void onDraw(SkCanvas* canvas, SkOSWindow* window); |
| 62 |
| 63 private: |
| 64 // Implementation of the canvas.drawRect() JS function. |
| 65 static void drawRect(const v8::FunctionCallbackInfo<Value>& args); |
| 66 |
| 67 // Implementation of the canvas.inval() JS function. |
| 68 static void inval(const v8::FunctionCallbackInfo<Value>& args); |
| 69 |
| 70 // Get the pointer out of obj. |
| 71 static JsCanvas* unwrap(Handle<Object> obj); |
| 72 |
| 73 // Create a template for JS object associated with JsCanvas, called lazily |
| 74 // by Wrap() and the results are stored in fCanvasTemplate; |
| 75 Handle<ObjectTemplate> makeCanvasTemplate(); |
| 76 |
| 77 // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap. |
| 78 Handle<Object> wrap(); |
| 79 |
43 Isolate* fIsolate; | 80 Isolate* fIsolate; |
| 81 |
| 82 // Only valid when inside OnDraw(). |
| 83 SkCanvas* fCanvas; |
| 84 |
| 85 // Only valid when inside OnDraw(). |
| 86 SkOSWindow* fWindow; |
| 87 |
| 88 // The context that the script will be parsed into. |
44 Persistent<Context> fContext; | 89 Persistent<Context> fContext; |
45 Persistent<Script> fScript; | 90 |
46 SkScalar fRotationAngle; | 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; |
47 }; | 97 }; |
48 | 98 |
49 #endif | 99 #endif |
OLD | NEW |