blob: a6b5e4cae551117fd54e2293c32463fa3fe1dc46 [file] [log] [blame]
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 *
8 */
9
10#ifndef SkV8Example_JsContext_DEFINED
11#define SkV8Example_JsContext_DEFINED
12
13#include <v8.h>
14
15#include "SkPaint.h"
jcgregorio5e44b002014-10-27 10:27:01 -070016#include "DrawingMethods.h"
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000017
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000018class SkCanvas;
19class Global;
20
21// Provides the canvas context implementation in JS, and the OnDraw() method in
22// C++ that's used to bridge from C++ to JS. Should be used in JS as:
23//
24// function onDraw(context) {
25// context.fillStyle="#FF0000";
26// context.fillRect(x, y, w, h);
27// }
jcgregorio5e44b002014-10-27 10:27:01 -070028class JsContext : public DrawingMethods {
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000029public:
30 JsContext(Global* global)
commit-bot@chromium.org24e04962014-03-04 20:44:32 +000031 : INHERITED(global)
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000032 , fCanvas(NULL)
33 {
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000034 }
commit-bot@chromium.org24e04962014-03-04 20:44:32 +000035 virtual ~JsContext() {}
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000036
37 // Parse the script.
38 bool initialize();
39
40 // Call this with the SkCanvas you want onDraw to draw on.
41 void onDraw(SkCanvas* canvas);
42
commit-bot@chromium.org24e04962014-03-04 20:44:32 +000043 virtual SkCanvas* getCanvas() { return fCanvas; };
44
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000045private:
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000046
47 // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap.
jcgregorioe22f45f2014-10-24 12:49:17 -070048 v8::Handle<v8::Object> wrap();
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000049
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000050 // A handle to the onDraw function defined in the script.
jcgregorioe22f45f2014-10-24 12:49:17 -070051 v8::Persistent<v8::Function> fOnDraw;
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000052
53 // The template for what a canvas context object looks like. The canvas
54 // context object is what's passed into the JS onDraw() function.
jcgregorioe22f45f2014-10-24 12:49:17 -070055 static v8::Persistent<v8::ObjectTemplate> gContextTemplate;
commit-bot@chromium.org24e04962014-03-04 20:44:32 +000056
57 // Only valid when inside OnDraw().
58 SkCanvas* fCanvas;
59
jcgregorio5e44b002014-10-27 10:27:01 -070060 typedef DrawingMethods INHERITED;
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000061};
62
63#endif