| 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_BaseContext_DEFINED |
| 11 #define SkV8Example_JsContext_DEFINED | 11 #define SkV8Example_BaseContext_DEFINED |
| 12 | 12 |
| 13 #include <v8.h> | 13 #include <v8.h> |
| 14 | 14 |
| 15 #include "SkPaint.h" | 15 #include "SkPaint.h" |
| 16 | 16 |
| 17 using namespace v8; | 17 using namespace v8; |
| 18 | 18 |
| 19 class SkCanvas; | 19 class SkCanvas; |
| 20 class Global; | 20 class Global; |
| 21 | 21 |
| 22 // Provides the canvas context implementation in JS, and the OnDraw() method in | 22 // BaseContext contains common functionality for both JsContext |
| 23 // C++ that's used to bridge from C++ to JS. Should be used in JS as: | 23 // and DisplayList. |
| 24 // | 24 class BaseContext { |
| 25 // function onDraw(context) { | |
| 26 // context.fillStyle="#FF0000"; | |
| 27 // context.fillRect(x, y, w, h); | |
| 28 // } | |
| 29 class JsContext { | |
| 30 public: | 25 public: |
| 31 JsContext(Global* global) | 26 BaseContext(Global* global) |
| 32 : fGlobal(global) | 27 : fGlobal(global) |
| 33 , fCanvas(NULL) | |
| 34 { | 28 { |
| 35 fFillStyle.setColor(SK_ColorBLACK); | 29 fFillStyle.setColor(SK_ColorBLACK); |
| 36 fFillStyle.setAntiAlias(true); | 30 fFillStyle.setAntiAlias(true); |
| 37 fFillStyle.setStyle(SkPaint::kFill_Style); | 31 fFillStyle.setStyle(SkPaint::kFill_Style); |
| 38 fStrokeStyle.setColor(SK_ColorBLACK); | 32 fStrokeStyle.setColor(SK_ColorBLACK); |
| 39 fStrokeStyle.setAntiAlias(true); | 33 fStrokeStyle.setAntiAlias(true); |
| 40 fStrokeStyle.setStyle(SkPaint::kStroke_Style); | 34 fStrokeStyle.setStyle(SkPaint::kStroke_Style); |
| 41 } | 35 } |
| 42 ~JsContext(); | 36 virtual ~BaseContext() {} |
| 43 | 37 |
| 44 // Parse the script. | 38 // Retrieve the SkCanvas to draw on. May return NULL. |
| 45 bool initialize(); | 39 virtual SkCanvas* getCanvas() = 0; |
| 46 | 40 |
| 47 // Call this with the SkCanvas you want onDraw to draw on. | 41 // Add the Javascript attributes and methods that BaseContext implements to
the ObjectTemplate. |
| 48 void onDraw(SkCanvas* canvas); | 42 void addAttributesAndMethods(Handle<ObjectTemplate> tmpl); |
| 43 |
| 44 protected: |
| 45 // Get the pointer out of obj. |
| 46 static BaseContext* Unwrap(Handle<Object> obj); |
| 47 |
| 48 Global* fGlobal; |
| 49 SkPaint fFillStyle; |
| 50 SkPaint fStrokeStyle; |
| 49 | 51 |
| 50 private: | 52 private: |
| 51 static void GetStyle(Local<String> name, | 53 static void GetStyle(Local<String> name, |
| 52 const PropertyCallbackInfo<Value>& info, | 54 const PropertyCallbackInfo<Value>& info, |
| 53 const SkPaint& style); | 55 const SkPaint& style); |
| 54 static void SetStyle(Local<String> name, Local<Value> value, | 56 static void SetStyle(Local<String> name, Local<Value> value, |
| 55 const PropertyCallbackInfo<void>& info, | 57 const PropertyCallbackInfo<void>& info, |
| 56 SkPaint& style); | 58 SkPaint& style); |
| 57 // JS Attributes | 59 // JS Attributes |
| 58 static void GetFillStyle(Local<String> name, | 60 static void GetFillStyle(Local<String> name, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 70 | 72 |
| 71 // JS Methods | 73 // JS Methods |
| 72 static void FillRect(const v8::FunctionCallbackInfo<Value>& args); | 74 static void FillRect(const v8::FunctionCallbackInfo<Value>& args); |
| 73 static void Stroke(const v8::FunctionCallbackInfo<Value>& args); | 75 static void Stroke(const v8::FunctionCallbackInfo<Value>& args); |
| 74 static void Fill(const v8::FunctionCallbackInfo<Value>& args); | 76 static void Fill(const v8::FunctionCallbackInfo<Value>& args); |
| 75 static void Rotate(const v8::FunctionCallbackInfo<Value>& args); | 77 static void Rotate(const v8::FunctionCallbackInfo<Value>& args); |
| 76 static void Save(const v8::FunctionCallbackInfo<Value>& args); | 78 static void Save(const v8::FunctionCallbackInfo<Value>& args); |
| 77 static void Restore(const v8::FunctionCallbackInfo<Value>& args); | 79 static void Restore(const v8::FunctionCallbackInfo<Value>& args); |
| 78 static void Translate(const v8::FunctionCallbackInfo<Value>& args); | 80 static void Translate(const v8::FunctionCallbackInfo<Value>& args); |
| 79 static void ResetTransform(const v8::FunctionCallbackInfo<Value>& args); | 81 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 | |
| 88 // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap. | |
| 89 Handle<Object> wrap(); | |
| 90 | |
| 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. | |
| 100 Persistent<Function> fOnDraw; | |
| 101 | |
| 102 // The template for what a canvas context object looks like. The canvas | |
| 103 // context object is what's passed into the JS onDraw() function. | |
| 104 static Persistent<ObjectTemplate> gContextTemplate; | |
| 105 }; | 82 }; |
| 106 | 83 |
| 107 #endif | 84 #endif |
| OLD | NEW |