| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
from fltk import * |
| 28 |
import sys |
| 29 |
|
| 30 |
window = None |
| 31 |
point_test_ix = None |
| 32 |
line_test_ix = None |
| 33 |
rect_test_ix = None |
| 34 |
viewport_test_ix = None |
| 35 |
circle_test_ix = None |
| 36 |
|
| 37 |
|
| 38 |
def changePageCB(ptr, ixvp): |
| 39 |
i = 0 |
| 40 |
ix = ixvp |
| 41 |
n = window.children() |
| 42 |
for i in range(n): |
| 43 |
window.child(i).hide() |
| 44 |
if ix >= n or ix < 0: |
| 45 |
ix = n-1 |
| 46 |
window.child(ix).show() |
| 47 |
|
| 48 |
def newButton(x, y, w, h, l, ix, tt): |
| 49 |
b = Fl_Button(x, y, w, h, l) |
| 50 |
b.tooltip(tt) |
| 51 |
b.callback(changePageCB, ix) |
| 52 |
return b |
| 53 |
|
| 54 |
def createMenuPage(): |
| 55 |
page = Fl_Group(0, 0, 600, 600) |
| 56 |
g = Fl_Group(100, 20, 460, 26, "drawing:") |
| 57 |
g.align(FL_ALIGN_LEFT) |
| 58 |
b1 = newButton(100+2, 22, 22, 22, "1", point_test_ix, "Testing pixel drawing") |
| 59 |
b2 = newButton(125+2, 22, 22, 22, "2", line_test_ix, "Testing fl_line") |
| 60 |
b3 = newButton(150+2, 22, 22, 22, "3", rect_test_ix, "Testing fl_rect") |
| 61 |
b4 = newButton(175+2, 22, 22, 22, "4", viewport_test_ix, "Testing viewport alignment") |
| 62 |
b5 = newButton(200+2, 22, 22, 22, "5", circle_test_ix, "Testing circle and oval drawing") |
| 63 |
g.end() |
| 64 |
page.end() |
| 65 |
|
| 66 |
def beginTestPage(l): |
| 67 |
ix = window.children() |
| 68 |
g = Fl_Group(0, 0, window.w(), window.h()) |
| 69 |
g.box(FL_FLAT_BOX) |
| 70 |
g.hide() |
| 71 |
newButton(20, 20, 20, 20, "M", -1, "Return to main menu") |
| 72 |
newButton(20, 40, 20, 20, "@<", ix-1, "previous test") |
| 73 |
newButton(20, 60, 20, 20, "@>", ix+1, "next test") |
| 74 |
bx = Fl_Box(60, 20, window.w()-80, 100, l) |
| 75 |
bx.box(FL_ENGRAVED_BOX) |
| 76 |
bx.align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP) |
| 77 |
return g |
| 78 |
|
| 79 |
|
| 80 |
class PointTest(Fl_Widget): |
| 81 |
def __init__(self, x, y, w, h): |
| 82 |
Fl_Widget.__init__(self, x, y, w, h) |
| 83 |
|
| 84 |
def draw(self): |
| 85 |
a = self.x() |
| 86 |
b = self.y() |
| 87 |
fl_color(FL_BLACK) |
| 88 |
fl_rect(self.x(), self.y(), self.w(), self.h()) |
| 89 |
fl_point(a+10, b+10) |
| 90 |
fl_point(a+20, b+20) |
| 91 |
fl_point(a+10, b+20) |
| 92 |
fl_point(a+20, b+10) |
| 93 |
fl_color(FL_RED) |
| 94 |
a = self.x()+70 |
| 95 |
fl_point(a+10, b+10) |
| 96 |
fl_point(a+20, b+20) |
| 97 |
fl_point(a+10, b+20) |
| 98 |
fl_point(a+20, b+10) |
| 99 |
fl_color(FL_GREEN) |
| 100 |
a = self.x() |
| 101 |
b = self.y()+70 |
| 102 |
fl_point(a+10, b+10) |
| 103 |
fl_point(a+20, b+20) |
| 104 |
fl_point(a+10, b+20) |
| 105 |
fl_point(a+20, b+10) |
| 106 |
fl_color(FL_BLUE) |
| 107 |
a = self.x()+70 |
| 108 |
fl_point(a+10, b+10) |
| 109 |
fl_point(a+20, b+20) |
| 110 |
fl_point(a+10, b+20) |
| 111 |
fl_point(a+20, b+10) |
| 112 |
|
| 113 |
def fl_point_test(): |
| 114 |
global point_test_ix |
| 115 |
point_test_ix = window.children() |
| 116 |
page = beginTestPage( |
| 117 |
"""testing the fl_point call\n |
| 118 |
You should see four pixels each in black, red, green and blue. |
| 119 |
Make sure that pixels are not anti-aliased (blured across multiple pixels)!""" ) |
| 120 |
pt = PointTest(20, 140, 100, 100) |
| 121 |
page.end() |
| 122 |
return pt |
| 123 |
|
| 124 |
|
| 125 |
class LineTest(Fl_Widget): |
| 126 |
def __init__(self, x, y, w, h): |
| 127 |
Fl_Widget.__init__(self, x, y, w, h) |
| 128 |
|
| 129 |
def draw(self): |
| 130 |
a = self.x() |
| 131 |
b = self.y() |
| 132 |
fl_color(FL_BLACK) |
| 133 |
fl_rect(a, b, self.w(), self.h()) |
| 134 |
|
| 135 |
fl_color(FL_RED) |
| 136 |
fl_point(a+10, b+10) |
| 137 |
fl_point(a+20, b+10) |
| 138 |
fl_color(FL_BLACK) |
| 139 |
fl_xyline(a+10, b+10, a+20) |
| 140 |
|
| 141 |
fl_color(FL_RED) |
| 142 |
fl_point(a+10, b+20) |
| 143 |
fl_point(a+20, b+20) |
| 144 |
fl_point(a+20, b+30) |
| 145 |
fl_color(FL_BLACK) |
| 146 |
fl_xyline(a+10, b+20, a+20, b+30) |
| 147 |
|
| 148 |
fl_color(FL_RED) |
| 149 |
fl_point(a+10, b+40) |
| 150 |
fl_point(a+20, b+40) |
| 151 |
fl_point(a+20, b+50) |
| 152 |
fl_point(a+30, b+50) |
| 153 |
fl_color(FL_BLACK) |
| 154 |
fl_xyline(a+10, b+40, a+20, b+50, a+30) |
| 155 |
|
| 156 |
|
| 157 |
fl_color(FL_RED) |
| 158 |
fl_point(a+60, b+60) |
| 159 |
fl_point(a+90, b+60) |
| 160 |
fl_point(a+60, b+90) |
| 161 |
fl_point(a+90, b+90) |
| 162 |
fl_color(FL_BLACK) |
| 163 |
fl_loop(a+60, b+60, a+90, b+60, a+90, b+90, a+60, b+90) |
| 164 |
|
| 165 |
def fl_line_test(): |
| 166 |
global line_test_ix |
| 167 |
line_test_ix = window.children() |
| 168 |
page = beginTestPage( |
| 169 |
"""testing the integer based fl_line calls |
| 170 |
No red pixels should be visible. |
| 171 |
If you see bright red pixels, the line drawing alignment is off, |
| 172 |
or the last pixel in a line does not get drawn. |
| 173 |
If you see dark red pixels, anti-aliasing must be switched off.""" |
| 174 |
) |
| 175 |
lt = LineTest(20, 140, 100, 100) |
| 176 |
page.end() |
| 177 |
return lt |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
class RectTest(Fl_Widget): |
| 182 |
def __init__(self, x, y, w, h): |
| 183 |
Fl_Widget.__init__(self, x, y, w, h) |
| 184 |
|
| 185 |
def draw(self): |
| 186 |
a = self.x() |
| 187 |
b = self.y() |
| 188 |
fl_color(FL_BLACK) |
| 189 |
fl_rect(a, b, self.w(), self.h()) |
| 190 |
|
| 191 |
fl_color(FL_RED) |
| 192 |
fl_loop(a+10, b+10, a+40, b+10, a+40, b+40, a+10, b+40) |
| 193 |
fl_color(FL_GREEN) |
| 194 |
fl_loop(a+ 9, b+ 9, a+41, b+ 9, a+41, b+41, a+ 9, b+41) |
| 195 |
fl_color(FL_GREEN) |
| 196 |
fl_loop(a+11, b+11, a+39, b+11, a+39, b+39, a+11, b+39) |
| 197 |
fl_color(FL_BLACK) |
| 198 |
fl_rect(a+10, b+10, 31, 31) |
| 199 |
|
| 200 |
fl_color(FL_RED) |
| 201 |
fl_loop(a+60, b+60, a+90, b+60, a+90, b+90, a+60, b+90) |
| 202 |
fl_color(FL_GREEN) |
| 203 |
fl_loop(a+59, b+59, a+91, b+59, a+91, b+91, a+59, b+91) |
| 204 |
fl_color(FL_BLACK) |
| 205 |
fl_rectf(a+60, b+60, 31, 31) |
| 206 |
|
| 207 |
|
| 208 |
def fl_rect_test(): |
| 209 |
global rect_test_ix |
| 210 |
rect_test_ix = window.children() |
| 211 |
page = beginTestPage( |
| 212 |
"testing the fl_rect call\n" |
| 213 |
"No red pixels should be visible. " |
| 214 |
"If you see bright red lines, or if parts of the green frames are hidden, " |
| 215 |
"the rect drawing alignment is off. " |
| 216 |
) |
| 217 |
rt = RectTest(20, 140, 100, 100) |
| 218 |
page.end() |
| 219 |
return rt |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
class ViewportTest(Fl_Widget): |
| 224 |
def __init__(self, x, y, w, h, p): |
| 225 |
Fl_Widget.__init__(self, x, y, w, h), |
| 226 |
self.pos = p |
| 227 |
|
| 228 |
def draw(self): |
| 229 |
if self.pos&1: |
| 230 |
fl_color(FL_RED) |
| 231 |
fl_yxline(self.x()+self.w(), self.y(), self.y()+self.h()) |
| 232 |
fl_color(FL_GREEN) |
| 233 |
fl_yxline(self.x()+self.w()-1, self.y(), self.y()+self.h()) |
| 234 |
else: |
| 235 |
fl_color(FL_RED) |
| 236 |
fl_yxline(self.x()-1, self.y(), self.y()+self.h()) |
| 237 |
fl_color(FL_GREEN) |
| 238 |
fl_yxline(self.x(), self.y(), self.y()+self.h()) |
| 239 |
|
| 240 |
if self.pos&2: |
| 241 |
fl_color(FL_RED) |
| 242 |
fl_xyline(self.x(), self.y()+self.h(), self.x()+self.w()) |
| 243 |
fl_color(FL_GREEN) |
| 244 |
fl_xyline(self.x(), self.y()+self.h()-1, self.x()+self.w()) |
| 245 |
else: |
| 246 |
fl_color(FL_RED) |
| 247 |
fl_xyline(self.x(), self.y()-1, self.x()+self.w()) |
| 248 |
fl_color(FL_GREEN) |
| 249 |
fl_xyline(self.x(), self.y(), self.x()+self.w()) |
| 250 |
|
| 251 |
fl_color(FL_BLACK) |
| 252 |
fl_loop(self.x()+3, self.y()+3, self.x()+self.w()-4, self.y()+3, self.x()+self.w()-4, self.y()+self.h()-4, self.x()+3, self.y()+self.h()-4) |
| 253 |
|
| 254 |
|
| 255 |
def fl_viewport_test(): |
| 256 |
global viewport_test_ix |
| 257 |
viewport_test_ix = window.children() |
| 258 |
page = beginTestPage( |
| 259 |
"testing viewport alignment\n" |
| 260 |
"Only green lines should be visible. " |
| 261 |
"If red lines are visible in the corners of this window, " |
| 262 |
"your viewport alignment and clipping is off. " |
| 263 |
"If there is a space between the green lines and the window border, " |
| 264 |
"the viewport is off, but some clipping may be working. " |
| 265 |
"Also, your window size may be off to begin with." |
| 266 |
) |
| 267 |
vt1 = ViewportTest(0, 0, 20, 20, 0) |
| 268 |
vt2 = ViewportTest(page.w()-20, 0, 20, 20, 1) |
| 269 |
vt3 = ViewportTest(0, page.h()-20, 20, 20, 2) |
| 270 |
vt4 = ViewportTest(page.w()-20,page.h()-20, 20, 20, 3) |
| 271 |
page.end() |
| 272 |
return (vt1, vt2, vt3, vt4) |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
class CircleTest(Fl_Widget): |
| 277 |
def __init__(self, x, y, w, h): |
| 278 |
Fl_Widget.__init__(self, x, y, w, h) |
| 279 |
|
| 280 |
def draw(self): |
| 281 |
a = self.x() |
| 282 |
b = self.y() |
| 283 |
fl_color(FL_BLACK) |
| 284 |
fl_rect(a, b, 100, 100) |
| 285 |
|
| 286 |
fl_color(FL_GREEN) |
| 287 |
fl_rect(a+ 9, b+ 9, 33, 33) |
| 288 |
fl_color(FL_RED) |
| 289 |
fl_xyline(a+24, b+10, a+27) |
| 290 |
fl_xyline(a+24, b+40, a+27) |
| 291 |
fl_yxline(a+10, b+24, b+27) |
| 292 |
fl_yxline(a+40, b+24, b+27) |
| 293 |
fl_color(FL_BLACK) |
| 294 |
fl_arc(a+10, b+10, 31, 31, 0.0, 360.0) |
| 295 |
|
| 296 |
fl_color(FL_GREEN) |
| 297 |
fl_rect(a+54, b+ 4, 43, 43) |
| 298 |
fl_rect(a+54, b+4, 18, 18) |
| 299 |
fl_rect(a+79, b+29, 18, 18) |
| 300 |
fl_color(FL_RED) |
| 301 |
fl_point(a+55, b+30) |
| 302 |
fl_point(a+70, b+45) |
| 303 |
fl_point(a+80, b+5) |
| 304 |
fl_point(a+95, b+20) |
| 305 |
fl_color(FL_BLACK) |
| 306 |
fl_arc(a+65, b+ 5, 31, 31, -35.0, 125.0) |
| 307 |
|
| 308 |
fl_color(FL_BLACK) |
| 309 |
fl_arc(a+55, b+15, 31, 31, 145.0, 305.0) |
| 310 |
|
| 311 |
fl_color(FL_RED) |
| 312 |
fl_xyline(a+24, b+60, a+27) |
| 313 |
fl_xyline(a+24, b+90, a+27) |
| 314 |
fl_yxline(a+10, b+74, b+77) |
| 315 |
fl_yxline(a+40, b+74, b+77) |
| 316 |
fl_color(FL_GREEN) |
| 317 |
fl_rect(a+ 9, b+59, 33, 33) |
| 318 |
fl_color(FL_BLACK) |
| 319 |
fl_pie(a+10, b+60, 31, 31, 0.0, 360.0) |
| 320 |
|
| 321 |
fl_color(FL_GREEN) |
| 322 |
fl_rect(a+54, b+54, 43, 43) |
| 323 |
fl_rect(a+54, b+54, 18, 18) |
| 324 |
fl_rect(a+79, b+79, 18, 18) |
| 325 |
fl_point(a+79, b+71) |
| 326 |
fl_point(a+71, b+79) |
| 327 |
fl_color(FL_RED) |
| 328 |
fl_point(a+55, b+80) |
| 329 |
fl_point(a+70, b+95) |
| 330 |
fl_point(a+80, b+55) |
| 331 |
fl_point(a+95, b+70) |
| 332 |
fl_point(a+81, b+69) |
| 333 |
fl_point(a+69, b+81) |
| 334 |
fl_color(FL_BLACK) |
| 335 |
fl_pie(a+65, b+55, 31, 31, -30.0, 120.0) |
| 336 |
|
| 337 |
fl_color(FL_BLACK) |
| 338 |
fl_pie(a+55, b+65, 31, 31, 150.0, 300.0) |
| 339 |
|
| 340 |
a = self.x()+120 |
| 341 |
b = self.y() |
| 342 |
fl_color(FL_BLACK) |
| 343 |
fl_rect(a, b, 100, 100) |
| 344 |
fl_color(FL_GREEN) |
| 345 |
fl_rect(a+19, b+9, 63, 33) |
| 346 |
fl_rect(a+19, b+59, 63, 33) |
| 347 |
fl_color(FL_BLACK) |
| 348 |
fl_arc(a+20, b+10, 61, 31, 0, 360) |
| 349 |
fl_pie(a+20, b+60, 61, 31, 0, 360) |
| 350 |
|
| 351 |
a = self.x()+240 |
| 352 |
b = self.y() |
| 353 |
fl_color(FL_BLACK) |
| 354 |
fl_rect(a, b, 100, 100) |
| 355 |
fl_color(FL_GREEN) |
| 356 |
fl_rect(a+9, b+19, 33, 63) |
| 357 |
fl_rect(a+59, b+19, 33, 63) |
| 358 |
fl_color(FL_BLACK) |
| 359 |
fl_arc(a+10, b+20, 31, 61, 0, 360) |
| 360 |
fl_pie(a+60, b+20, 31, 61, 0, 360) |
| 361 |
|
| 362 |
|
| 363 |
def fl_circle_test(): |
| 364 |
global circle_test_ix |
| 365 |
circle_test_ix = window.children() |
| 366 |
page = beginTestPage( |
| 367 |
"testing int drawing of circles and ovals (fl_arc, fl_pie)\n" |
| 368 |
"No red lines should be visible. " |
| 369 |
"If you see bright red pixels, the circle drawing alignment is off. " |
| 370 |
"If you see dark red pixels, your syste supports anti-aliasing " |
| 371 |
"which should be of no concern. " |
| 372 |
"The green rectangles should not be touched by circle drawings." |
| 373 |
) |
| 374 |
ct = CircleTest(20, 140, 340, 100) |
| 375 |
page.end() |
| 376 |
return ct |
| 377 |
|
| 378 |
|
| 379 |
if __name__=='__main__': |
| 380 |
window = Fl_Window(600, 600, "Unit Tests for FLTK") |
| 381 |
|
| 382 |
pt = fl_point_test() |
| 383 |
lt = fl_line_test() |
| 384 |
rt = fl_rect_test() |
| 385 |
(vt1, vt2, vt3, vt4) = fl_viewport_test() |
| 386 |
ct = fl_circle_test() |
| 387 |
|
| 388 |
createMenuPage() |
| 389 |
window.end() |
| 390 |
window.show(sys.argv) |
| 391 |
Fl.run() |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|