blob: 3de2fad2c152ab814bb82a3286ee14106ad7ba6d [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
153class ForthView : public SkView {
154 ForthEnv fEnv;
155 ForthWord* fOnClickWord;
156
157 SkBitmap fBM;
158 SkForthCtx fContext;
159public:
160 ForthView() {
161 load_words(&fEnv, &fContext);
162
163 fBM.setConfig(SkBitmap::kARGB_8888_Config, 640, 480);
164 fBM.allocPixels();
165 fBM.eraseColor(0);
166 fContext.init(fBM);
167
reed@android.com19a89f22009-08-30 03:24:51 +0000168 fEnv.parse(": mycolor ( x. y. -- x. y. ) rot rot f< IF #FFFF0000 ELSE #FF0000FF THEN setColor ;");
169 fEnv.parse(": view.onClick ( x. y. -- ) mycolor 10. drawCircle ;");
reed@android.comf56e2952009-08-29 21:30:25 +0000170 fOnClickWord = fEnv.findWord("view.onClick");
171#if 0
172 env.parse(
173 ": draw1 "
174 "10. setStrokeWidth 1 setStyle #FF000000 setColor "
175 "10. 20. 200. 100. drawLine "
176 "0 setStyle #80FF0000 setColor "
177 "50. 50. 250. 150. drawRect "
178 ";");
179 env.parse("#FF0000FF drawColor "
180 "draw1 "
181 "150. 120. translate "
182 "draw1 "
183 );
184#endif
reed@android.com19a89f22009-08-30 03:24:51 +0000185 ForthEnv env;
186 env.parse("3 5 = IF 42 . ELSE -42 . THEN 99 .");
187 env.run();
reed@android.comf56e2952009-08-29 21:30:25 +0000188 }
189
190protected:
191 // overrides from SkEventSink
192 virtual bool onQuery(SkEvent* evt) {
193 if (SampleCode::TitleQ(*evt)) {
194 SampleCode::TitleR(evt, "Forth");
195 return true;
196 }
197 return this->INHERITED::onQuery(evt);
198 }
199
200 void drawBG(SkCanvas* canvas) {
201 canvas->drawColor(0xFFDDDDDD);
202 }
203
204 void test_onClick(ForthEnv* env) {
205 ForthWord* word = env->findWord("view.onClick");
206 if (word) {
207 const intptr_t idata[2] = { 40, 2 };
208 intptr_t odata[1] = { -1 };
209 ForthCallBlock block;
210 block.in_data = idata;
211 block.in_count = 2;
212 block.out_data = odata;
213 block.out_count = 1;
214 word->call(&block);
215 SkDebugf("after view.onClick depth %d count %d top %d\n",
216 block.out_depth, block.out_count, odata[0]);
217 } else {
218 SkDebugf("------ view.onClick not found\n");
219 }
220 }
221
222 virtual void onDraw(SkCanvas* canvas) {
223 drawBG(canvas);
224 canvas->drawBitmap(fBM, 0, 0, NULL);
225 }
226
227 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
228 return fOnClickWord ? new Click(this) : NULL;
229 }
230
231 virtual bool onClick(Click* click) {
232 intptr_t idata[2] = {
233 f2i_bits(click->fCurr.fX), f2i_bits(click->fCurr.fY)
234 };
235 ForthCallBlock block;
236 block.in_data = idata;
237 block.in_count = 2;
238 block.out_count = 0;
239 fOnClickWord->call(&block);
240 this->inval(NULL);
241 return true;
242 }
243
244private:
245 typedef SkView INHERITED;
246};
247
248//////////////////////////////////////////////////////////////////////////////
249
250static SkView* MyFactory() { return new ForthView; }
251static SkViewRegister reg(MyFactory);
252