| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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_JsContext_DEFINED | 10 #ifndef SkV8Example_JsContext_DEFINED |
| 11 #define SkV8Example_JsContext_DEFINED | 11 #define SkV8Example_JsContext_DEFINED |
| 12 | 12 |
| 13 #include <v8.h> | 13 #include <v8.h> |
| 14 | 14 |
| 15 #include "SkPaint.h" | 15 #include "SkPaint.h" |
| 16 #include "BaseContext.h" |
| 16 | 17 |
| 17 using namespace v8; | 18 using namespace v8; |
| 18 | 19 |
| 19 class SkCanvas; | 20 class SkCanvas; |
| 20 class Global; | 21 class Global; |
| 21 | 22 |
| 22 // Provides the canvas context implementation in JS, and the OnDraw() method in | 23 // 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 // C++ that's used to bridge from C++ to JS. Should be used in JS as: |
| 24 // | 25 // |
| 25 // function onDraw(context) { | 26 // function onDraw(context) { |
| 26 // context.fillStyle="#FF0000"; | 27 // context.fillStyle="#FF0000"; |
| 27 // context.fillRect(x, y, w, h); | 28 // context.fillRect(x, y, w, h); |
| 28 // } | 29 // } |
| 29 class JsContext { | 30 class JsContext : public BaseContext { |
| 30 public: | 31 public: |
| 31 JsContext(Global* global) | 32 JsContext(Global* global) |
| 32 : fGlobal(global) | 33 : INHERITED(global) |
| 33 , fCanvas(NULL) | 34 , fCanvas(NULL) |
| 34 { | 35 { |
| 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); | |
| 41 } | 36 } |
| 42 ~JsContext(); | 37 virtual ~JsContext() {} |
| 43 | 38 |
| 44 // Parse the script. | 39 // Parse the script. |
| 45 bool initialize(); | 40 bool initialize(); |
| 46 | 41 |
| 47 // Call this with the SkCanvas you want onDraw to draw on. | 42 // Call this with the SkCanvas you want onDraw to draw on. |
| 48 void onDraw(SkCanvas* canvas); | 43 void onDraw(SkCanvas* canvas); |
| 49 | 44 |
| 45 virtual SkCanvas* getCanvas() { return fCanvas; }; |
| 46 |
| 50 private: | 47 private: |
| 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 | |
| 58 static void GetFillStyle(Local<String> name, | |
| 59 const PropertyCallbackInfo<Value>& info); | |
| 60 static void SetFillStyle(Local<String> name, Local<Value> value, | |
| 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); | |
| 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 | |
| 71 // JS Methods | |
| 72 static void FillRect(const v8::FunctionCallbackInfo<Value>& args); | |
| 73 static void Stroke(const v8::FunctionCallbackInfo<Value>& args); | |
| 74 static void Fill(const v8::FunctionCallbackInfo<Value>& args); | |
| 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); | |
| 78 static void Translate(const v8::FunctionCallbackInfo<Value>& args); | |
| 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 | 48 |
| 88 // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap. | 49 // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap. |
| 89 Handle<Object> wrap(); | 50 Handle<Object> wrap(); |
| 90 | 51 |
| 91 Global* fGlobal; | |
| 92 | |
| 93 // Only valid when inside OnDraw(). | |
| 94 SkCanvas* fCanvas; | |
| 95 | |
| 96 SkPaint fFillStyle; | |
| 97 SkPaint fStrokeStyle; | |
| 98 | |
| 99 // A handle to the onDraw function defined in the script. | 52 // A handle to the onDraw function defined in the script. |
| 100 Persistent<Function> fOnDraw; | 53 Persistent<Function> fOnDraw; |
| 101 | 54 |
| 102 // The template for what a canvas context object looks like. The canvas | 55 // The template for what a canvas context object looks like. The canvas |
| 103 // context object is what's passed into the JS onDraw() function. | 56 // context object is what's passed into the JS onDraw() function. |
| 104 static Persistent<ObjectTemplate> gContextTemplate; | 57 static Persistent<ObjectTemplate> gContextTemplate; |
| 58 |
| 59 // Only valid when inside OnDraw(). |
| 60 SkCanvas* fCanvas; |
| 61 |
| 62 typedef BaseContext INHERITED; |
| 105 }; | 63 }; |
| 106 | 64 |
| 107 #endif | 65 #endif |
| OLD | NEW |