blob: cfd1172d67457243b2453cb1cf8e67a2aa697926 [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.comde2e7fb2009-09-02 02:07:32 +0000153void Forth_test_stdwords(bool verbose);
reed@android.come50025a2009-09-01 21:00:44 +0000154
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.comde2e7fb2009-09-02 02:07:32 +0000163 Forth_test_stdwords(false);
reed@android.come50025a2009-09-01 21:00:44 +0000164
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.comf56e2952009-08-29 21:30:25 +0000189 }
190
191protected:
192 // overrides from SkEventSink
193 virtual bool onQuery(SkEvent* evt) {
194 if (SampleCode::TitleQ(*evt)) {
195 SampleCode::TitleR(evt, "Forth");
196 return true;
197 }
198 return this->INHERITED::onQuery(evt);
199 }
200
201 void drawBG(SkCanvas* canvas) {
202 canvas->drawColor(0xFFDDDDDD);
203 }
204
205 void test_onClick(ForthEnv* env) {
206 ForthWord* word = env->findWord("view.onClick");
207 if (word) {
208 const intptr_t idata[2] = { 40, 2 };
209 intptr_t odata[1] = { -1 };
210 ForthCallBlock block;
211 block.in_data = idata;
212 block.in_count = 2;
213 block.out_data = odata;
214 block.out_count = 1;
215 word->call(&block);
216 SkDebugf("after view.onClick depth %d count %d top %d\n",
217 block.out_depth, block.out_count, odata[0]);
218 } else {
219 SkDebugf("------ view.onClick not found\n");
220 }
221 }
222
223 virtual void onDraw(SkCanvas* canvas) {
224 drawBG(canvas);
225 canvas->drawBitmap(fBM, 0, 0, NULL);
226 }
227
228 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
229 return fOnClickWord ? new Click(this) : NULL;
230 }
231
232 virtual bool onClick(Click* click) {
233 intptr_t idata[2] = {
234 f2i_bits(click->fCurr.fX), f2i_bits(click->fCurr.fY)
235 };
236 ForthCallBlock block;
237 block.in_data = idata;
238 block.in_count = 2;
239 block.out_count = 0;
240 fOnClickWord->call(&block);
241 this->inval(NULL);
242 return true;
243 }
244
245private:
246 typedef SkView INHERITED;
247};
248
249//////////////////////////////////////////////////////////////////////////////
250
251static SkView* MyFactory() { return new ForthView; }
252static SkViewRegister reg(MyFactory);
253