aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/SkV8Example/JsContext.h
blob: d3993ee6c7d3ecc3581b4903b919212b844b3371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * 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"
#include "BaseContext.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 BaseContext {
public:
    JsContext(Global* global)
            : INHERITED(global)
            , fCanvas(NULL)
    {
    }
    virtual ~JsContext() {}

    // Parse the script.
    bool initialize();

    // Call this with the SkCanvas you want onDraw to draw on.
    void onDraw(SkCanvas* canvas);

    virtual SkCanvas* getCanvas() { return fCanvas; };

private:

    // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap.
    Handle<Object> wrap();

    // A handle to the onDraw function defined in the script.
    Persistent<Function> 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<ObjectTemplate> gContextTemplate;

    // Only valid when inside OnDraw().
    SkCanvas* fCanvas;

    typedef BaseContext INHERITED;
};

#endif