Skip to content

Commit 15009d9

Browse files
committed
polygon, fill polygon, and optimize a bit color handling in example pizarra
1 parent 2bb11c5 commit 15009d9

File tree

1 file changed

+176
-17
lines changed

1 file changed

+176
-17
lines changed

examples/pizarra.winxed

Lines changed: 176 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,22 @@ class ColorSelector : ChildWindow, BoxedSelector
145145
class ToolSelector : ChildWindow, BoxedSelector
146146
{
147147
var pizarra;
148-
var pixmap;
148+
var pmline;
149+
var pmpoly;
150+
var pmfillpoly;
149151
function ToolSelector(pizarra, int x, int y)
150152
{
151-
self.BoxedSelector(pizarra.display, 2, 2);
153+
self.BoxedSelector(pizarra.display, 2, 3);
152154
self.pizarra = pizarra;
153155
self.ChildWindow(pizarra, x, y, self.width, self.height);
154156
self.OnDestroy += evhandler(self, "ondestroy");
155157
self.OnExpose += evhandler(self, "onexpose");
156158
self.OnButtonPress += evhandler(self, "onbuttonpress");
157159
self.Map();
158160
// Ugly icon for hand drawn lines
159-
self.pixmap = self.CreatePixmapFromBuffer(<<:
161+
self.pmline = self.CreatePixmapFromBuffer(<<:
160162
/* XPM */
161-
static char *test[] = {
163+
static char *line[] = {
162164
"16 16 2 1",
163165
" c #FFFFFF",
164166
"+ c #000000",
@@ -179,16 +181,68 @@ static char *test[] = {
179181
" ++++ ",
180182
" ",
181183
};
184+
:>>
185+
);
186+
self.pmpoly = self.CreatePixmapFromBuffer(<<:
187+
/* XPM */
188+
static char *poly[] = {
189+
"16 16 2 1",
190+
" c #FFFFFF",
191+
"+ c #000000",
192+
" ",
193+
" + ",
194+
" ++ ",
195+
" + + ",
196+
" + + ",
197+
" + + ",
198+
" + + ",
199+
" + + ",
200+
" + + ",
201+
" + + ",
202+
" + ++ ",
203+
" + ++ ",
204+
" + ++ ",
205+
" + ++ ",
206+
" ++ ",
207+
" ",
208+
};
209+
:>>
210+
);
211+
self.pmfillpoly = self.CreatePixmapFromBuffer(<<:
212+
/* XPM */
213+
static char *fillpoly[] = {
214+
"16 16 2 1",
215+
" c #FFFFFF",
216+
"+ c #000000",
217+
" ",
218+
" + ",
219+
" ++ ",
220+
" +++ ",
221+
" ++++ ",
222+
" +++++ ",
223+
" ++++++ ",
224+
" ++++++++ ",
225+
" ++++++++++ ",
226+
" ++++++++++++ ",
227+
" ++++++++++++ ",
228+
" +++++++++++ ",
229+
" ++++++++ ",
230+
" +++++ ",
231+
" ++ ",
232+
" ",
233+
};
182234
:>>
183235
);
184236
}
185237
function ondestroy(event)
186238
{
187-
var pixmap = self.pixmap;
188-
if (pixmap != null) {
189-
pixmap.Free();
190-
self.pixmap = null;
191-
}
239+
for (var pixmap in
240+
[ self.pmline, self.pmpoly, self.pmfillpoly ] )
241+
if (pixmap != null)
242+
pixmap.Free();
243+
self.pmline = null;
244+
self.pmpoly = null;
245+
self.pmfillpoly = null;
192246
}
193247
function onexpose(event)
194248
{
@@ -198,14 +252,14 @@ static char *test[] = {
198252
int boxheight = self.boxheight;
199253
self.SetForeground("black");
200254
self.DrawRectangle(0, 0, width - 1, height - 1);
201-
for (int i = 0; i < 2; ++i)
255+
for (int i = 0; i < 3; ++i)
202256
for (int j = 0; j < 2; ++j) {
203257
int x = 3 + j * (boxwidth + 2);
204258
int y = 3 + i * (boxheight + 2);
205259
self.DrawRectangle(x, y, boxwidth - 1, boxheight - 1);
206260
switch (i * 2 + j) {
207261
case 0:
208-
self.CopyArea(self.pixmap, 0, 0, 16, 16, x + 1, y + 1);
262+
self.CopyArea(self.pmline, 0, 0, 16, 16, x + 1, y + 1);
209263
break;
210264
case 1:
211265
self.DrawLine(x + 2, y + 3, x + 10, y + 14);
@@ -216,6 +270,12 @@ static char *test[] = {
216270
case 3:
217271
self.FillRectangle(x + 3, y + 3, boxwidth - 6, boxheight - 6);
218272
break;
273+
case 4:
274+
self.CopyArea(self.pmpoly, 0, 0, 16, 16, x + 1, y + 1);
275+
break;
276+
case 5:
277+
self.CopyArea(self.pmfillpoly, 0, 0, 16, 16, x + 1, y + 1);
278+
break;
219279
}
220280
}
221281
}
@@ -238,13 +298,25 @@ static char *test[] = {
238298
class Thing
239299
{
240300
var colorspec;
301+
var _color;
241302
function Thing(string colorspec)
242303
{
243304
self.colorspec = colorspec;
244305
}
306+
function _getcolor(drawable)
307+
{
308+
var color = self._color;
309+
if (color == null)
310+
self._color = color = drawable.display.ParseColor(self.colorspec);
311+
return color;
312+
}
245313
function setforeground(drawable)
246314
{
247-
drawable.SetForeground(self.colorspec);
315+
drawable.SetForeground(self._getcolor(drawable));
316+
}
317+
function setbackground(drawable)
318+
{
319+
drawable.SetBackground(self._getcolor(drawable));
248320
}
249321
}
250322

@@ -290,6 +362,35 @@ class Line : Thing
290362
}
291363
}
292364

365+
class Poly : Line
366+
{
367+
function Poly(string colorspec, int x0, int y0, int x1, int y1)
368+
{
369+
self.Line(colorspec, x0, y0, x1, y1);
370+
}
371+
}
372+
373+
class FillPoly : Poly
374+
{
375+
function FillPoly(string colorspec, int x0, int y0, int x1, int y1)
376+
{
377+
self.Poly(colorspec, x0, y0, x1, y1);
378+
}
379+
function draw(drawable)
380+
{
381+
self.setforeground(drawable);
382+
self.setbackground(drawable);
383+
drawable.FillPolygon(self.x, self.y);
384+
}
385+
function hint(drawable)
386+
{
387+
self.setforeground(drawable);
388+
drawable.SetFunction(GXinvert);
389+
drawable.DrawLines(self.x, self.y);
390+
drawable.SetFunction(GXcopy);
391+
}
392+
}
393+
293394
class HandLine : Thing
294395
{
295396
var xcoord;
@@ -489,7 +590,10 @@ class Pizarra : TopLevelWindow
489590

490591
class Board : ChildWindow
491592
{
492-
const int HANDLINE = 0, LINE = 1, RECTANGLE = 2, FILLRECT = 3;
593+
const int
594+
HANDLINE = 0, LINE = 1,
595+
RECTANGLE = 2, FILLRECT = 3,
596+
POLY = 4, FILLPOLY = 5;
493597

494598
var parent;
495599
var mode;
@@ -567,23 +671,49 @@ class Board : ChildWindow
567671
break;
568672
case "Shift_L":
569673
case "Shift_R":
570-
if (self.pressed && self.mode == LINE) {
674+
int mode = self.mode;
675+
if (self.pressed && (mode == LINE || mode == POLY || mode == FILLPOLY)) {
571676
int initx = self.initx;
572677
int inity = self.inity;
573678
int oldx = self.oldx;
574679
int oldy = self.oldy;
575680
self.hintline(initx, inity, oldx, oldy);
576681
var l = self.line;
577682
if (l == null) {
578-
l = new Line(self.colorspec, initx, inity, oldx, oldy);
683+
switch (mode) {
684+
case LINE:
685+
l = new Line(self.colorspec, initx, inity, oldx, oldy);
686+
break;
687+
case POLY:
688+
l = new Poly(self.colorspec, initx, inity, oldx, oldy);
689+
break;
690+
case FILLPOLY:
691+
l = new FillPoly(self.colorspec, initx, inity, oldx, oldy);
692+
break;
693+
}
579694
self.line = l;
580695
self.listline.push(l);
581696
}
582-
else
697+
else {
698+
if (mode == FILLPOLY)
699+
l.hint(self);
583700
l.addsegment(oldx - initx, oldy - inity);
584-
l.draw(self);
701+
}
702+
switch (mode) {
703+
case LINE:
704+
l.draw(self);
705+
break;
706+
case POLY:
707+
self.DrawLine(initx, inity, oldx, oldy);
708+
break;
709+
case FILLPOLY:
710+
l.hint(self);
711+
break;
712+
}
585713
self.initx =: oldx;
586714
self.inity =: oldy;
715+
self.oldx =: oldx;
716+
self.oldy =: oldy;
587717
}
588718
break;
589719
default:
@@ -648,6 +778,33 @@ class Board : ChildWindow
648778
}
649779
l.draw(self);
650780
break;
781+
case POLY:
782+
self.hintline(initx, inity, oldx, oldy);
783+
var p = self.line;
784+
if (p == null) {
785+
p = new Poly(colorspec, initx, inity, oldx, oldy);
786+
self.listline.push(p);
787+
}
788+
else {
789+
p.addsegment(oldx - initx, oldy - inity);
790+
p.close();
791+
}
792+
p.draw(self);
793+
break;
794+
case FILLPOLY:
795+
self.hintline(initx, inity, oldx, oldy);
796+
var fp = self.line;
797+
fp.hint(self);
798+
if (fp == null) {
799+
fp = new FillPoly(colorspec, initx, inity, oldx, oldy);
800+
self.listline.push(fp);
801+
}
802+
else {
803+
fp.addsegment(oldx - initx, oldy - inity);
804+
fp.close();
805+
}
806+
fp.draw(self);
807+
break;
651808
}
652809
self.line = null;
653810
}
@@ -680,6 +837,8 @@ class Board : ChildWindow
680837
self.DrawLine(oldx, oldy, x, y);
681838
break;
682839
case LINE:
840+
case POLY:
841+
case FILLPOLY:
683842
self.hintline(self.initx, self.inity, oldx, oldy);
684843
self.hintline(self.initx, self.inity, x, y);
685844
break;

0 commit comments

Comments
 (0)