blob: d3993ee6c7d3ecc3581b4903b919212b844b3371 [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"
commit-bot@chromium.org24e04962014-03-04 20:44:32 +000016#include "BaseContext.h"
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000017
18using namespace v8;
19
20class SkCanvas;
21class Global;
22
23// Provides the canvas context implementation in JS, and the OnDraw() method in
24// C++ that's used to bridge from C++ to JS. Should be used in JS as:
25//
26// function onDraw(context) {
27// context.fillStyle="#FF0000";
28// context.fillRect(x, y, w, h);
29// }
commit-bot@chromium.org24e04962014-03-04 20:44:32 +000030class JsContext : public BaseContext {
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000031public:
32 JsContext(Global* global)
commit-bot@chromium.org24e04962014-03-04 20:44:32 +000033 : INHERITED(global)
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000034 , fCanvas(NULL)
35 {
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000036 }
commit-bot@chromium.org24e04962014-03-04 20:44:32 +000037 virtual ~JsContext() {}
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000038
39 // Parse the script.
40 bool initialize();
41
42 // Call this with the SkCanvas you want onDraw to draw on.
43 void onDraw(SkCanvas* canvas);
44
commit-bot@chromium.org24e04962014-03-04 20:44:32 +000045 virtual SkCanvas* getCanvas() { return fCanvas; };
46
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000047private:
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000048
49 // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap.
50 Handle<Object> wrap();
51
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000052 // A handle to the onDraw function defined in the script.
53 Persistent<Function> fOnDraw;
54
55 // The template for what a canvas context object looks like. The canvas
56 // context object is what's passed into the JS onDraw() function.
57 static Persistent<ObjectTemplate> gContextTemplate;
commit-bot@chromium.org24e04962014-03-04 20:44:32 +000058
59 // Only valid when inside OnDraw().
60 SkCanvas* fCanvas;
61
62 typedef BaseContext INHERITED;
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000063};
64
65#endif