File: GTest.hs

package info (click to toggle)
ghc-cvs 20040725-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 68,484 kB
  • ctags: 19,658
  • sloc: haskell: 251,945; ansic: 109,709; asm: 24,961; sh: 12,825; perl: 5,786; makefile: 5,334; xml: 3,884; python: 682; yacc: 650; lisp: 477; cpp: 337; ml: 76; fortran: 24; csh: 18
file content (557 lines) | stat: -rw-r--r-- 15,643 bytes parent folder | download | duplicates (7)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
module Main where

import Graphics.HGL

import Data.Array
import Control.Monad( when )
import Data.IORef

----------------------------------------------------------------

-- withColor  :: Color -> Graphic -> Graphic
-- withColor c = withRGB (colorTable ! c)

----------------------------------------------------------------

-- abbreviation for runGraphics - almost all tests that you run
-- will be of the form "rg w<n>" for some small positive n
rg = runGraphics

main = runGraphics $ parMany [w1,w2,w3,w4,w5,{-w6 p1,-}w7,w8,w9,w10,w13]

w0 = do
	w <- openWindow "My First Graphics Program" (400,400)
	wGetChar w
	closeWindow w

w1 = do
	w <- openWindow "My First Graphics Program" (400,400)
	drawInWindow w $ text (150,200) "Hello Graphics Window 1"
	getLBP w
	drawInWindow w $ text (150,250) "Hit a key" 
	wGetChar w
	clearWindow w 
	drawInWindow w $ text (150,200) "Thank you"
        drawInWindow w $ text (150,250) "Hit another key"
	wGetChar w
	closeWindow w

w2 = do
	w <- openWindow "My Second Graphics Program" (400,400)
	drawInWindow w $ text (150,200) "Hello Graphics Window 2"
	getRBP w
	drawInWindow w $ text (150,250) "Hit a key" 
	wGetChar w
	clearWindow w 
	drawInWindow w $ text (150,200) "Thank you"
        drawInWindow w $ text (150,250) "Hit another key"
	wGetChar w
	closeWindow w

----------------------------------------------------------------
-- Region demo
----------------------------------------------------------------

-- first create some regions

r1,r2,r3,r4 :: MyRegion
r1 = unions
	[ Shape (Rect    (000,000) (300,100))
	, Shape (Ellipse (100,000) (200,500))
	]
r2 = Shape (Rect (050,050) (250,150))
r3 = r1 `Intersect` r2
r4 = pentangle 0

pentangle :: Angle -> MyRegion
pentangle phi = Shape (poly phi (4*pi/5) 5)

triangle :: Angle -> MyRegion
triangle phi = Shape (poly phi (2*pi/3) 3)

-- draw a regular polygon with "n" sides "angle" apart 
poly :: Angle -> Angle -> Int -> Shape
poly init delta n = Polygon vertices
 where
  thetas   = take n [init, init+delta ..]
  vertices = [ (round x, round y)
             | theta <- thetas 
             , let x = 200*(1+cos theta)
	     , let y = 200*(1+sin theta)
             ]

----------------------------------------------------------------

w3 = do
	w <- openWindow "Regions demo" (300,500)
	mapM_ (\ r -> do
                        setGraphic w (drawRegion r)
                  	getLBP w)
              [r1,r2,r3,r4]
	clearWindow w
	drawInWindow w $ text (150,250) "Hit a key" 
	wGetChar w
	closeWindow w

w4 = do 
	wa <- openWindow "Multiwindow demo 1" (300,500)
	wb <- openWindow "Multiwindow demo 2" (300,500)

        drawInWindow wa $ text (100,100) "Hello World 1"
        drawInWindow wb $ text (100,100) "Hello World 2"

	ka <- wGetChar wa
        drawInWindow wb $ text (100,200) ("Got key " ++ [ka])

	kb <- wGetChar wb
        drawInWindow wa $ text (100,200) ("Got key " ++ [kb])

	closeWindow wa
	closeWindow wb

----------------------------------------------------------------
-- Picture demo
----------------------------------------------------------------

type Pic color = [(color, MyRegion)] 

p1 = [ (Red,r3), (Green, r2), (Blue, r1)]

w5 = do
	w <- openWindow "Picture demo" (300,500)
	setGraphic w (drawPic p1)
        getLBP w
	clearWindow w
	drawInWindow w $ text (150,250) "Hit a key" 
	wGetChar w
	closeWindow w

drawPic :: Pic Color -> Graphic
drawPic p = overGraphics [ withColor c $ drawRegion r | (c,r) <- p ]

----------------------------------------------------------------
-- Faster picture drawing
----------------------------------------------------------------

-- This version is more efficient because it allocates colors just once.
--
-- The cost of this performance gain is that we have to expose the
-- allocation and deallocation of brushes to the programmer, which 
-- makes it possible for them to deallocate too early, to deallocate
-- too late (eg never), or to deallocate too often.
--
-- Notice that you must not deallocate brushes (and fonts, etc)
-- until _after_ you clear the screen - otherwise the redraw routine
-- might be called and you'll have a dangling reference to the brush.

-- w6 p = do
--         w <- openWindow "Faster Picture Demo" (300,500)
-- 
--         -- get the "palette"
--         let colors  =  nub [ c | (c,r) <- p ]
--         brushes     <- mapM (\c -> mkBrush (colorTable ! c)) colors
--         let palette =  array (minBound,maxBound) (zip colors brushes)
--         let pic     =  [ (palette!c, r) | (c,r) <- p ]
-- 
--         setGraphic w (drawPic2 pic)
--         getLBP w
-- 
--         clearWindow w
-- 
--         drawInWindow w $ text (150,250) "Hit a key" 
--         wGetChar w
--         closeWindow w

drawPic2 :: Pic Brush -> Graphic
drawPic2 p = overGraphics [ withBrush b (drawRegion r) | (b,r) <- p ]

----------------------------------------------------------------
-- Animation (sort of)
--
-- Simple animations made of lists of pictures
----------------------------------------------------------------

type Frame = Pic Color
type Anim  = [Frame]

a1 :: Anim
a1 = [ [(Red,   pentangle phi)
       ,(Green, triangle  (-phi))
       ]
     | phi <- [0, pi/20 .. 2*pi] 
     ]

-- draw an animation (using user-input to step through animation)
-- Note that this doesn't terminate until the window is closed
-- which has to be done using your window manager.
w7 = do
	w <- openWindow "Animation demo" (400,400)
	mapM_ (drawFrame w) a1
 where
  -- draw a frame and wait for left button press
  drawFrame :: Window -> Frame -> IO ()
  drawFrame w p = do
	  setGraphic w (drawPic p)
	  getLBP w
	  return ()

----------------------------------------------------------------
-- Timer demo
----------------------------------------------------------------

-- draw an animation (using timer to step through animation)
w8 = do
	w <- openWindowEx "Timer demo" Nothing (400,400)
                          DoubleBuffered (Just 50)
	mapM_ (drawFrame w) (cycle a1)
 where
  -- draw a frame and wait for a tick
  drawFrame :: Window -> Frame -> IO ()
  drawFrame w p = do
	setGraphic w (drawPic p)
	getWindowTick w

----------------------------------------------------------------
-- Text demo
----------------------------------------------------------------

-- half-inch wide, red text on a transparent background at a 45 degree angle
-- quarter-inch wide, red text on a green background at a -45 degree angle
w9 = do
	w <- openWindow "Font demo" (500,500)
        font1 <- createFont (50,50)  (pi/4)  False False "helvetica"
        font2 <- createFont (25,50) (-pi/4) True True   "times"
        drawInWindow w
          $ withTextColor (RGB 255 0 0)
          $ withFont font1
          $ withBkMode Transparent
  	  $ text (050,450) "Font Test 1"
        drawInWindow w
          $ withTextColor (RGB 0 0 255)
          $ withFont font2 
          $ withBkMode Opaque
          $ withBkColor (RGB 0 255 0)
          $ text (050,050) "Font Test 2"
	getLBP w
        deleteFont font1
        deleteFont font2
	closeWindow w

----------------------------------------------------------------
-- Error catching demo
----------------------------------------------------------------

-- This program demonstrates that the system doesn't get left in
-- an inconsistent state even if your program hits an error.

w10 = do
	w <- openWindow "Error recovery demo" (300,300)
        drawInWindow w
  	  $ text (10,150) "Click me to test error recovery"
	getLBP w
        drawInWindow w $ error "foo1"
	--error "foo2"
	getLBP w
	clearWindow w
        drawInWindow w
  	  $ text (10,150) "Shouldn't have made it this far"
	getLBP w
	closeWindow w

----------------------------------------------------------------
-- Bitmap demo
----------------------------------------------------------------

{-
w11 = do
	w <- openWindow "Bitmap demo" (300,500)
	setGraphic w $ 
          text (150,200) "Test"
          `overGraphic`
          drawPic p1
	Draw.saveGraphic "bitmaps/Foo.bmp" (300,500) $ 
          text (150,200) "Test"
          `overGraphic`
          drawPic p1
        getLBP w
	closeWindow w

w11b = do
	(bmp,_) <- Bitmap.load "bitmaps/Foo.bmp" 
	w <- openWindow "Bitmap demo" (300,500)
	setGraphic w $ Bitmap.draw (50,50) bmp
	wGetChar w
	closeWindow w

w11c = do
	(bmp,_) <- Bitmap.load "bitmaps/Foo.bmp" 
	w <- openWindow "Bitmap demo" (300,500)
	setGraphic w $ Bitmap.drawStretched (0,400) (400,0) bmp
	wGetChar w
	closeWindow w

w12 = do
	w <- openWindow "Bitmap demo" (400,400)
	mapM_ (drawFrame w) (zip [100..] a1)
 where
  -- draw a frame and wait for left button press
  drawFrame :: Window -> (Int,Frame) -> IO ()
  drawFrame w (i,p) = do
	  setGraphic w $ drawPic p
 	  Draw.saveGraphic name (400,400) $ drawPic p
	  getLBP w
	  return ()
   where
    name = "bitmaps/" ++ "Foo" ++ show i ++ ".bmp"
-}

w13 = do
	w <- openWindow "My First Incremental Graphics Program" (400,400)
        -- start with 2 elements in the list so that polyline doesn't crash
	pointRef <- newIORef [(0,0),(400,400)]
	let
	  -- discrete lines between left clicks, right click to move on
	  loop1 = do
		e <- getWindowEvent w
		case e of
		 Button{pt=pt,isDown=isDown,isLeft=isLeft}
		   | not isLeft -> return ()
                   | isDown -> addPoint pt >> loop1
		 Closed -> return ()
		 _ -> loop1

	  -- continuous lines as long as mouse is held down
	  loop2 down = do
		e <- getWindowEvent w
		case e of
		 Button{isDown=isDown}-> loop2 isDown
		 MouseMove{pt=pt} -> do
		         when down (addPoint pt)
		         loop2 down
		 Closed -> return ()
		 _ -> loop2 down

	  -- code to do a total redraw
	  drawPoints = do
		pts <- ioToDraw $ readIORef pointRef
		withColor Red $ polyline pts

	  -- code to draw the bit that changed and record the change
	  addPoint pt = do
		pts <- readIORef pointRef
		writeIORef pointRef (pt:pts)
		directDraw w $ withColor Red $ line pt (head pts)
		--uncomment the next line to see how bad a total redraw would be
--		redrawWindow w

	setGraphic w drawPoints
	loop1
	loop2 True
	closeWindow w

----------------------------------------------------------------
-- Examples of straight pictures (not regions)
----------------------------------------------------------------

shapeToGraphic :: Shape -> Graphic
shapeToGraphic shape =
  case shape of
   Rect (x1,y1) (x2,y2) -> polygon [(x1,y1),(x2,y1),(x2,y2),(x1,y2)]
   Ellipse p1 p2        -> ellipse p1 p2
   Polygon pts          -> polygon pts

s1 = Rect (100,100) (200,300)
s2 = Ellipse (150,150) (300,200)
s3 = poly 0 (4*pi/5) 5

w14 = do
	w <- openWindow "Pure Picture Demo" (400,400)
        drawInWindow w $ withColor Red    $ shapeToGraphic s1
        drawInWindow w $ withColor Blue   $ shapeToGraphic s2
        drawInWindow w $ withColor Yellow $ shapeToGraphic s3
        suspend w

suspend = loop
  where loop w = getWindowEvent w >> loop w

pic1 = withColor Red  $ 
         ellipse (150,150) (300,200)
pic2 = withColor Blue $ 
         polyline [(100,50),(200,50),(200,250),(100,250),(100,50)]

testGraphics = runGraphics $
  do w <- openWindow "Some Graphics Figures" (300,300)
     drawInWindow w pic1
     drawInWindow w pic2
     suspend w

----------------------------------------------------------------
-- Examples from the documentation
----------------------------------------------------------------

demos :: IO ()
demos = runGraphics $ parMany
  [ helloWorld
  , eg cp
--  , fontDemo
  , lineDemo 0
  , lineDemo 1
  , lineDemo 20
  , keyTest
  , timerDemo
  , ellipseTest
  ]

helloWorld :: IO ()
helloWorld = do
	  w <- openWindow "Hello World Window" (300, 300)
	  drawInWindow w $ text (100, 100) "Hello"
	  drawInWindow w $ text (100, 200) "World"
	  wGetChar w
	  closeWindow w

cp :: Graphic
cp = 
     mkBrush (colorTable!Red)  $ \ red  ->
     mkBrush (colorTable!Blue) $ \ blue ->
     overGraphic
       (withBrush red  $ polygon [(200,200),(400,200),(300,400)])
       (withBrush blue $ polygon [(100,100),(500,100),(500,500),(100,500)])

eg :: Graphic -> IO ()
eg p = do
	  w <- openWindow "Hello World Window" (600,600)
	  drawInWindow w p
	  wGetChar w
	  closeWindow w

-- fontDemo :: IO ()
-- fontDemo = do
--   w <- openWindow "Font Demo Window" (500,500)
--   drawInWindow w $
--     withTextColor (RGB 255 0 0)           $
--     mkFont (50,100) (pi/4) False True "Arial"  $ \ font ->
--     withFont font		          $
--     withBkColor (RGB 0 255 0)             $
--     withBkMode Opaque         	          $
--     text (050,450) "Font Demo"
--   wGetChar w
--   closeWindow w  

-- Note that "width" must be 1 or less for the penstyle to matter

lineDemo :: Int -> IO ()
lineDemo width = do
  w <- openWindow ("Line Demo Window " ++ show width) (500,500)
  drawInWindow w $
    let 
      color = colorTable ! Red 
    in
    mkPen Solid      width color $ \ pen1 ->
    mkPen Dash       width color $ \ pen2 ->
    mkPen Dot        width color $ \ pen3 ->
    mkPen DashDotDot width color $ \ pen4 ->
    overGraphics 
      [ withPen pen1 $ line (100,100) (400,100)
      , withPen pen2 $ line (100,200) (400,200)
      , withPen pen3 $ line (100,300) (400,300)
      , withPen pen4 $ line (100,400) (400,400)
      ]
  wGetChar w
  closeWindow w

-- Just what keys can we see?
keyTest :: IO ()
keyTest= do
  w <- openWindow "Keypress Demo Window" (500,500)
  c <- wGetChar w
  print (fromEnum c)
  closeWindow w


-- Tick counter
timerDemo = do
  w <- openWindowEx 
         "Timer demo"         -- title
         (Just (500,500))     -- initial position of window
         (100,100)            -- initial size of window
         drawFun              -- draw function - see below
         (Just 50)            -- tick rate
  let
    loop x = do
      setGraphic w $ text (0,50) $ show x
      getWindowTick w          -- wait for next tick on window
      loop (x+1)
  loop 0
 where

  -- The possible choices of "drawFun" are
  --
  -- o drawBufferedGraphic - use a double buffer to reduce animation flicker
  -- o drawGraphic         - draw directly to screen (for speed)
  
  useDoubleBuffering = True

  drawFun = if useDoubleBuffering then
               DoubleBuffered
            else
               Unbuffered

ellipseTest :: IO ()
ellipseTest = do
	  w <- openWindow "Ellipse Test" (300, 300)
	  drawInWindow w $ ellipse (0,0) (200, 100)
	  wGetChar w
	  closeWindow w


----------------------------------------------------------------
-- Region code - prototype of the code in SOE
----------------------------------------------------------------

{-
module Region(
	MyRegion(Empty, Union, Intersect, Shape),
	unions,
	Shape(Rect, Ellipse, Polygon),
	drawRegion
	) where

import GraphicsRegion
-}

data MyRegion
  = Empty
  | Union     MyRegion MyRegion
  | Intersect MyRegion MyRegion
  | Shape     Shape

unions :: [MyRegion] -> MyRegion
unions = foldr Union Empty 

--intersects :: [MyRegion] -> MyRegion
--intersects = foldr Intersect Full

drawRegion :: MyRegion -> Graphic
drawRegion r = regionToGraphic (regionToRGN r)

regionToRGN :: MyRegion -> Region

regionToRGN Empty           = rectangleRegion (0,0) (0,0)
regionToRGN (Shape s)       = shapeToRGN s
regionToRGN (r1 `Union` r2) = unionRegion (regionToRGN r1) (regionToRGN r2)
regionToRGN (r1 `Intersect` r2) = intersectRegion (regionToRGN r1) (regionToRGN r2)

data Shape 
  = Rect    Point Point
  | Ellipse Point Point
  | Polygon [Point]

shapeToRGN :: Shape -> Region
shapeToRGN (Rect    p1 p2) = rectangleRegion p1 p2
shapeToRGN (Ellipse p1 p2) = ellipseRegion   p1 p2
shapeToRGN (Polygon ps)    = polygonRegion   ps

----------------------------------------------------------------
-- End
----------------------------------------------------------------