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