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