2014-01-06 18:17:24 +00:00
|
|
|
/*
|
|
|
|
* 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 <v8.h>
|
|
|
|
|
|
|
|
#include "SkPaint.h"
|
2014-10-27 17:27:01 +00:00
|
|
|
#include "DrawingMethods.h"
|
2014-01-06 18:17:24 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
// }
|
2014-10-27 17:27:01 +00:00
|
|
|
class JsContext : public DrawingMethods {
|
2014-01-06 18:17:24 +00:00
|
|
|
public:
|
|
|
|
JsContext(Global* global)
|
2014-03-04 20:44:32 +00:00
|
|
|
: INHERITED(global)
|
2014-01-06 18:17:24 +00:00
|
|
|
, fCanvas(NULL)
|
|
|
|
{
|
|
|
|
}
|
2014-03-04 20:44:32 +00:00
|
|
|
virtual ~JsContext() {}
|
2014-01-06 18:17:24 +00:00
|
|
|
|
|
|
|
// Parse the script.
|
|
|
|
bool initialize();
|
|
|
|
|
|
|
|
// Call this with the SkCanvas you want onDraw to draw on.
|
|
|
|
void onDraw(SkCanvas* canvas);
|
|
|
|
|
2014-03-04 20:44:32 +00:00
|
|
|
virtual SkCanvas* getCanvas() { return fCanvas; };
|
|
|
|
|
2014-01-06 18:17:24 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
// Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap.
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::Handle<v8::Object> wrap();
|
2014-01-06 18:17:24 +00:00
|
|
|
|
|
|
|
// A handle to the onDraw function defined in the script.
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::Persistent<v8::Function> fOnDraw;
|
2014-01-06 18:17:24 +00:00
|
|
|
|
|
|
|
// The template for what a canvas context object looks like. The canvas
|
|
|
|
// context object is what's passed into the JS onDraw() function.
|
2014-10-24 19:49:17 +00:00
|
|
|
static v8::Persistent<v8::ObjectTemplate> gContextTemplate;
|
2014-03-04 20:44:32 +00:00
|
|
|
|
|
|
|
// Only valid when inside OnDraw().
|
|
|
|
SkCanvas* fCanvas;
|
|
|
|
|
2014-10-27 17:27:01 +00:00
|
|
|
typedef DrawingMethods INHERITED;
|
2014-01-06 18:17:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|