blob: df4110db6e3c87919cf6a1085df64d17999645dc [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comf56e2952009-08-29 21:30:25 +00008#include "SampleCode.h"
9#include "SkColorPriv.h"
10#include "SkGradientShader.h"
11#include "SkView.h"
12#include "SkCanvas.h"
13#include "SkUtils.h"
14#include "Forth.h"
15
16class MyOutput : public ForthOutput {
17public:
18 SkString fStr;
19
20 virtual void show(const char text[]) {
21 fStr.set(text);
22 }
23};
24
25class SkForthCtx {
26public:
27 SkCanvas fCanvas;
28 SkPaint fPaint;
29
30 void init(const SkBitmap& bm) {
31 fCanvas.setBitmapDevice(bm);
32 fPaint.setAntiAlias(true);
33 }
34};
35
36class SkForthCtx_FW : public ForthWord {
37public:
38 SkForthCtx_FW() : fCtx(NULL) {}
39
40 void setCtx(SkForthCtx* ctx) { fCtx = ctx; }
41
42 SkCanvas* canvas() const { return &fCtx->fCanvas; }
43 SkPaint* paint() const { return &fCtx->fPaint; }
44
45private:
46 SkForthCtx* fCtx;
47};
48
49class setColor_FW : public SkForthCtx_FW {
50public:
51 virtual void exec(ForthEngine* fe) {
52 paint()->setColor(fe->pop());
53 }
54
55 static SkForthCtx_FW* New() { return new setColor_FW; }
56};
57
58class setStyle_FW : public SkForthCtx_FW {
59public:
60 virtual void exec(ForthEngine* fe) {
61 paint()->setStyle((SkPaint::Style)fe->pop());
62 }
63
64 static SkForthCtx_FW* New() { return new setStyle_FW; }
65};
66
67class setStrokeWidth_FW : public SkForthCtx_FW {
68public:
69 virtual void exec(ForthEngine* fe) {
70 paint()->setStrokeWidth(fe->fpop());
71 }
72
73 static SkForthCtx_FW* New() { return new setStrokeWidth_FW; }
74};
75
76class translate_FW : public SkForthCtx_FW {
77public:
78 virtual void exec(ForthEngine* fe) {
79 SkScalar dy = fe->fpop();
80 SkScalar dx = fe->fpop();
81 canvas()->translate(dx, dy);
82 }
83
84 static SkForthCtx_FW* New() { return new translate_FW; }
85};
86
87class drawColor_FW : public SkForthCtx_FW {
88public:
89 virtual void exec(ForthEngine* fe) {
90 canvas()->drawColor(fe->pop());
91 }
92
93 static SkForthCtx_FW* New() { return new drawColor_FW; }
94};
95
96class drawRect_FW : public SkForthCtx_FW {
97public:
98 virtual void exec(ForthEngine* fe) {
99 SkRect r;
100 r.fBottom = fe->fpop();
101 r.fRight = fe->fpop();
102 r.fTop = fe->fpop();
103 r.fLeft = fe->fpop();
104 canvas()->drawRect(r, *paint());
105 }
106
107 static SkForthCtx_FW* New() { return new drawRect_FW; }
108};
109
110class drawCircle_FW : public SkForthCtx_FW {
111public:
112 virtual void exec(ForthEngine* fe) {
113 SkScalar radius = fe->fpop();
114 SkScalar y = fe->fpop();
115 SkScalar x = fe->fpop();
116 canvas()->drawCircle(x, y, radius, *paint());
117 }
118
119 static SkForthCtx_FW* New() { return new drawCircle_FW; }
120};
121
122class drawLine_FW : public SkForthCtx_FW {
123public:
124 virtual void exec(ForthEngine* fe) {
125 SkScalar x0, y0, x1, y1;
126 y1 = fe->fpop();
127 x1 = fe->fpop();
128 y0 = fe->fpop();
129 x0 = fe->fpop();
130 canvas()->drawLine(x0, y0, x1, y1, *paint());
131 }
132
133 static SkForthCtx_FW* New() { return new drawLine_FW; }
134};
135
136static const struct {
137 const char* fName;
138 SkForthCtx_FW* (*fProc)();
139} gWordRecs[] = {
140 { "setColor", setColor_FW::New },
141 { "setStyle", setStyle_FW::New },
142 { "setStrokeWidth", setStrokeWidth_FW::New },
143 { "translate", translate_FW::New },
144 { "drawColor", drawColor_FW::New },
145 { "drawRect", drawRect_FW::New },
146 { "drawCircle", drawCircle_FW::New },
147 { "drawLine", drawLine_FW::New },
148};
149
150static void load_words(ForthEnv* env, SkForthCtx* ctx) {
151 for (size_t i = 0; i < SK_ARRAY_COUNT(gWordRecs); i++) {
152 SkForthCtx_FW* word = gWordRecs[i].fProc();
153 word->setCtx(ctx);
154 env->addWord(gWordRecs[i].fName, word);
155 }
156}
157
158///////////////////////////////////////////////////////////////////////////////
159
reed@android.comde2e7fb2009-09-02 02:07:32 +0000160void Forth_test_stdwords(bool verbose);
reed@android.come50025a2009-09-01 21:00:44 +0000161
reed@android.comf56e2952009-08-29 21:30:25 +0000162class ForthView : public SkView {
163 ForthEnv fEnv;
164 ForthWord* fOnClickWord;
165
166 SkBitmap fBM;
167 SkForthCtx fContext;
168public:
169 ForthView() {
reed@android.comde2e7fb2009-09-02 02:07:32 +0000170 Forth_test_stdwords(false);
reed@android.come50025a2009-09-01 21:00:44 +0000171
reed@android.comf56e2952009-08-29 21:30:25 +0000172 load_words(&fEnv, &fContext);
173
174 fBM.setConfig(SkBitmap::kARGB_8888_Config, 640, 480);
175 fBM.allocPixels();
176 fBM.eraseColor(0);
177 fContext.init(fBM);
178
reed@android.come50025a2009-09-01 21:00:44 +0000179 fEnv.parse(": mycolor ( x. y. -- x. y. ) OVER OVER f< IF #FFFF0000 ELSE #FF0000FF THEN setColor ;");
reed@android.com19a89f22009-08-30 03:24:51 +0000180 fEnv.parse(": view.onClick ( x. y. -- ) mycolor 10. drawCircle ;");
reed@android.comf56e2952009-08-29 21:30:25 +0000181 fOnClickWord = fEnv.findWord("view.onClick");
182#if 0
183 env.parse(
184 ": draw1 "
185 "10. setStrokeWidth 1 setStyle #FF000000 setColor "
186 "10. 20. 200. 100. drawLine "
187 "0 setStyle #80FF0000 setColor "
188 "50. 50. 250. 150. drawRect "
189 ";");
190 env.parse("#FF0000FF drawColor "
191 "draw1 "
192 "150. 120. translate "
193 "draw1 "
194 );
195#endif
reed@android.comf56e2952009-08-29 21:30:25 +0000196 }
197
reed@google.com1d5aaa82011-05-31 15:35:54 +0000198 virtual ~ForthView() {
reed@google.com1d5aaa82011-05-31 15:35:54 +0000199 }
200
reed@android.comf56e2952009-08-29 21:30:25 +0000201protected:
202 // overrides from SkEventSink
203 virtual bool onQuery(SkEvent* evt) {
204 if (SampleCode::TitleQ(*evt)) {
205 SampleCode::TitleR(evt, "Forth");
206 return true;
207 }
208 return this->INHERITED::onQuery(evt);
209 }
210
211 void drawBG(SkCanvas* canvas) {
212 canvas->drawColor(0xFFDDDDDD);
213 }
214
215 void test_onClick(ForthEnv* env) {
216 ForthWord* word = env->findWord("view.onClick");
217 if (word) {
218 const intptr_t idata[2] = { 40, 2 };
219 intptr_t odata[1] = { -1 };
220 ForthCallBlock block;
221 block.in_data = idata;
222 block.in_count = 2;
223 block.out_data = odata;
224 block.out_count = 1;
225 word->call(&block);
226 SkDebugf("after view.onClick depth %d count %d top %d\n",
227 block.out_depth, block.out_count, odata[0]);
228 } else {
229 SkDebugf("------ view.onClick not found\n");
230 }
231 }
232
233 virtual void onDraw(SkCanvas* canvas) {
234 drawBG(canvas);
235 canvas->drawBitmap(fBM, 0, 0, NULL);
236 }
237
238 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
239 return fOnClickWord ? new Click(this) : NULL;
240 }
241
242 virtual bool onClick(Click* click) {
243 intptr_t idata[2] = {
244 f2i_bits(click->fCurr.fX), f2i_bits(click->fCurr.fY)
245 };
246 ForthCallBlock block;
247 block.in_data = idata;
248 block.in_count = 2;
249 block.out_count = 0;
250 fOnClickWord->call(&block);
251 this->inval(NULL);
252 return true;
253 }
254
255private:
256 typedef SkView INHERITED;
257};
258
259//////////////////////////////////////////////////////////////////////////////
260
261static SkView* MyFactory() { return new ForthView; }
262static SkViewRegister reg(MyFactory);
263