blob: c1466a08dda05e4cb7dc3b99dcc68865e6493c36 [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"
16
17using namespace v8;
18
19class SkCanvas;
20class Global;
21
22// Provides the canvas context implementation in JS, and the OnDraw() method in
23// C++ that's used to bridge from C++ to JS. Should be used in JS as:
24//
25// function onDraw(context) {
26// context.fillStyle="#FF0000";
27// context.fillRect(x, y, w, h);
28// }
29class JsContext {
30public:
31 JsContext(Global* global)
32 : fGlobal(global)
33 , fCanvas(NULL)
34 {
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000035 fFillStyle.setColor(SK_ColorBLACK);
36 fFillStyle.setAntiAlias(true);
37 fFillStyle.setStyle(SkPaint::kFill_Style);
38 fStrokeStyle.setColor(SK_ColorBLACK);
39 fStrokeStyle.setAntiAlias(true);
40 fStrokeStyle.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000041 }
42 ~JsContext();
43
44 // Parse the script.
45 bool initialize();
46
47 // Call this with the SkCanvas you want onDraw to draw on.
48 void onDraw(SkCanvas* canvas);
49
50private:
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000051 static void GetStyle(Local<String> name,
52 const PropertyCallbackInfo<Value>& info,
53 const SkPaint& style);
54 static void SetStyle(Local<String> name, Local<Value> value,
55 const PropertyCallbackInfo<void>& info,
56 SkPaint& style);
57 // JS Attributes
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000058 static void GetFillStyle(Local<String> name,
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000059 const PropertyCallbackInfo<Value>& info);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000060 static void SetFillStyle(Local<String> name, Local<Value> value,
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000061 const PropertyCallbackInfo<void>& info);
62 static void GetStrokeStyle(Local<String> name,
63 const PropertyCallbackInfo<Value>& info);
64 static void SetStrokeStyle(Local<String> name, Local<Value> value,
65 const PropertyCallbackInfo<void>& info);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000066 static void GetWidth(Local<String> name,
67 const PropertyCallbackInfo<Value>& info);
68 static void GetHeight(Local<String> name,
69 const PropertyCallbackInfo<Value>& info);
70
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000071 // JS Methods
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000072 static void FillRect(const v8::FunctionCallbackInfo<Value>& args);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000073 static void Stroke(const v8::FunctionCallbackInfo<Value>& args);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000074 static void Fill(const v8::FunctionCallbackInfo<Value>& args);
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000075 static void Rotate(const v8::FunctionCallbackInfo<Value>& args);
76 static void Save(const v8::FunctionCallbackInfo<Value>& args);
77 static void Restore(const v8::FunctionCallbackInfo<Value>& args);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000078 static void Translate(const v8::FunctionCallbackInfo<Value>& args);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000079 static void ResetTransform(const v8::FunctionCallbackInfo<Value>& args);
80
81 // Get the pointer out of obj.
82 static JsContext* Unwrap(Handle<Object> obj);
83
84 // Create a template for JS object associated with JsContext, called lazily
85 // by Wrap() and the results are stored in gContextTemplate;
86 Handle<ObjectTemplate> makeContextTemplate();
87
88 // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap.
89 Handle<Object> wrap();
90
91 Global* fGlobal;
92
93 // Only valid when inside OnDraw().
94 SkCanvas* fCanvas;
95
96 SkPaint fFillStyle;
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000097 SkPaint fStrokeStyle;
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000098
99 // A handle to the onDraw function defined in the script.
100 Persistent<Function> fOnDraw;
101
102 // The template for what a canvas context object looks like. The canvas
103 // context object is what's passed into the JS onDraw() function.
104 static Persistent<ObjectTemplate> gContextTemplate;
105};
106
107#endif