/* * Copyright 2014 Google Inc. * * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. * */ #ifndef SkV8Example_JsContext_DEFINED #define SkV8Example_JsContext_DEFINED #include #include "SkPaint.h" using namespace v8; class SkCanvas; class Global; // Provides the canvas context implementation in JS, and the OnDraw() method in // C++ that's used to bridge from C++ to JS. Should be used in JS as: // // function onDraw(context) { // context.fillStyle="#FF0000"; // context.fillRect(x, y, w, h); // } class JsContext { public: JsContext(Global* global) : fGlobal(global) , fCanvas(NULL) { fFillStyle.setColor(SK_ColorBLACK); fFillStyle.setAntiAlias(true); fFillStyle.setStyle(SkPaint::kFill_Style); fStrokeStyle.setColor(SK_ColorBLACK); fStrokeStyle.setAntiAlias(true); fStrokeStyle.setStyle(SkPaint::kStroke_Style); } ~JsContext(); // Parse the script. bool initialize(); // Call this with the SkCanvas you want onDraw to draw on. void onDraw(SkCanvas* canvas); private: static void GetStyle(Local name, const PropertyCallbackInfo& info, const SkPaint& style); static void SetStyle(Local name, Local value, const PropertyCallbackInfo& info, SkPaint& style); // JS Attributes static void GetFillStyle(Local name, const PropertyCallbackInfo& info); static void SetFillStyle(Local name, Local value, const PropertyCallbackInfo& info); static void GetStrokeStyle(Local name, const PropertyCallbackInfo& info); static void SetStrokeStyle(Local name, Local value, const PropertyCallbackInfo& info); static void GetWidth(Local name, const PropertyCallbackInfo& info); static void GetHeight(Local name, const PropertyCallbackInfo& info); // JS Methods static void FillRect(const v8::FunctionCallbackInfo& args); static void Stroke(const v8::FunctionCallbackInfo& args); static void Fill(const v8::FunctionCallbackInfo& args); static void Rotate(const v8::FunctionCallbackInfo& args); static void Save(const v8::FunctionCallbackInfo& args); static void Restore(const v8::FunctionCallbackInfo& args); static void Translate(const v8::FunctionCallbackInfo& args); static void ResetTransform(const v8::FunctionCallbackInfo& args); // Get the pointer out of obj. static JsContext* Unwrap(Handle obj); // Create a template for JS object associated with JsContext, called lazily // by Wrap() and the results are stored in gContextTemplate; Handle makeContextTemplate(); // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap. Handle wrap(); Global* fGlobal; // Only valid when inside OnDraw(). SkCanvas* fCanvas; SkPaint fFillStyle; SkPaint fStrokeStyle; // A handle to the onDraw function defined in the script. Persistent fOnDraw; // The template for what a canvas context object looks like. The canvas // context object is what's passed into the JS onDraw() function. static Persistent gContextTemplate; }; #endif