blob: 30737963f3b3b07f0ebddd17ba7f4cb49c0529ba [file] [log] [blame]
commit-bot@chromium.org44a38772013-12-05 13:45:19 +00001/*
2 * Copyright 2013 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_DEFINED
11#define SkV8Example_DEFINED
12
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +000013#include <v8.h>
14
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000015#include "SkWindow.h"
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +000016#include "SkPaint.h"
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000017
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +000018using namespace v8;
19
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000020class SkCanvas;
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000021class JsCanvas;
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000022class Global;
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000023
24class SkV8ExampleWindow : public SkOSWindow {
25public:
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000026 SkV8ExampleWindow(void* hwnd, JsCanvas* canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000027
28protected:
29 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE;
30
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000031#ifdef SK_BUILD_FOR_WIN
32 virtual void onHandleInval(const SkIRect&) SK_OVERRIDE;
33#endif
34
35private:
36 typedef SkOSWindow INHERITED;
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000037 JsCanvas* fJsCanvas;
38};
39
40
41// Provides the canvas implementation in JS, and the OnDraw() method in C++
42// that's used to bridge from C++ to JS. Should be used in JS as:
43//
44// function onDraw(canvas) {
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +000045// canvas.fillStyle="#FF0000";
46// canvas.fillRect(x, y, w, h);
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000047// }
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000048class JsCanvas {
49public:
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000050 JsCanvas(Global* global)
51 : fGlobal(global)
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000052 , fCanvas(NULL)
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +000053 {
54 fFillStyle.setColor(SK_ColorRED);
55 }
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000056 ~JsCanvas();
57
58 // Parse the script.
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000059 bool initialize();
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000060
61 // Call this with the SkCanvas you want onDraw to draw on.
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000062 void onDraw(SkCanvas* canvas);
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000063
64private:
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +000065 // Implementation of the canvas.fillStyle field.
66 static void GetFillStyle(Local<String> name,
67 const PropertyCallbackInfo<Value>& info);
68 static void SetFillStyle(Local<String> name, Local<Value> value,
69 const PropertyCallbackInfo<void>& info);
70
71 // Implementation of the canvas.fillRect() JS function.
72 static void FillRect(const v8::FunctionCallbackInfo<Value>& args);
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000073
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000074 // Get the pointer out of obj.
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +000075 static JsCanvas* Unwrap(Handle<Object> obj);
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000076
77 // Create a template for JS object associated with JsCanvas, called lazily
78 // by Wrap() and the results are stored in fCanvasTemplate;
79 Handle<ObjectTemplate> makeCanvasTemplate();
80
81 // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap.
82 Handle<Object> wrap();
83
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000084 Global* fGlobal;
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000085
86 // Only valid when inside OnDraw().
87 SkCanvas* fCanvas;
88
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +000089 SkPaint fFillStyle;
90
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +000091 // A handle to the onDraw function defined in the script.
92 Persistent<Function> fOnDraw;
93
94 // The template for what a canvas object looks like. The canvas object is
95 // what's passed into the JS onDraw() function.
96 static Persistent<ObjectTemplate> fCanvasTemplate;
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000097};
98
99#endif