home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / e / EasyGUI_v33b2.lha / Src / EasyGUI / led_test3.e < prev    next >
Text File  |  1997-02-13  |  5KB  |  172 lines

  1. -> This one shows the use of multi-window GUIs with PLUGINs, and multiple,
  2. -> real asynchronous activities (by creating tasks).
  3. MODULE 'tools/easygui', 'tools/exceptions',
  4.        'graphics/text', 'intuition/intuition',
  5.        'plugins/led',
  6.        'amigalib/tasks', 'amigalib/time', 'other/ecode',
  7.        'devices/timer', 'exec/tasks'
  8.  
  9. -> Global for multiexists().
  10. DEF gh:PTR TO guihandle
  11.  
  12. CONST NUM_VALUES=2
  13.  
  14. -> Store all the GUI data in one place.
  15. OBJECT mygui
  16.   -> In particular, keep PLUGIN references here.
  17.   ledplug:PTR TO led
  18.   gh:PTR TO guihandle
  19.   gui
  20.   title
  21.   going
  22.   values[NUM_VALUES]:ARRAY OF INT
  23.   task
  24. ENDOBJECT
  25.  
  26. -> The main loop: create one window to start with.
  27. PROC main() HANDLE
  28.   DEF mh=NIL
  29.   mh:=multiinit()
  30.   create(mh)
  31.   multiloop(mh)
  32. EXCEPT DO
  33.   cleanmulti(mh)
  34.   report_exception()
  35. ENDPROC
  36.  
  37. -> Create a new GUI.
  38. PROC create(mh) HANDLE
  39.   DEF t, g, gui=NIL:PTR TO mygui
  40.   NEW gui
  41.   -> Allocate all PLUGINs for the GUI like this.
  42.   NEW gui.ledplug.led(NUM_VALUES,gui.values,TRUE)
  43.   -> Now we can try opening a GUI.
  44.   gui.gh:=addmultiA(mh,'BOOPSI in EasyGUI!',
  45.                    g:=NEW [ROWS,
  46.                      t:=NEW [TEXT,'LED Boopsi image tester...',NIL,TRUE,16],
  47.                      NEW [COLS,
  48.                         NEW [EQROWS,
  49.                           NEW [BUTTON,{runaction},'Run/Stop'],
  50.                           NEW [BUTTON,{spawn},'Spawn'],
  51.                           NEW [BUTTON,{quit},'Quit']
  52.                         ],
  53.                         NEW [PLUGIN,0,gui.ledplug]
  54.                      ]
  55.                    ],
  56.                    [EG_INFO,gui, EG_LEFT,Rnd(400), EG_TOP,Rnd(400),
  57.                     -> The cleanup routine will deallocate the PLUGINs used.
  58.                     EG_CLEAN,{cleanmygui}, EG_CLOSE,{close}, NIL])
  59.   gui.gui:=g
  60.   gui.title:=t
  61. EXCEPT
  62.   -> If there was any problem then it may have been the creation of PLUGINs
  63.   -> or addmultiA().  Luckily, addmultiA() (and guiinitA()) will *not* call
  64.   -> the EG_CLEAN function if they caused the problem, so we can (safely).
  65.   cleanmygui(gui)
  66.   ReThrow()
  67. ENDPROC
  68.  
  69. PROC quit(info) IS quitgui(0)
  70.  
  71. -> The custom clean up code for each GUI.
  72. PROC cleanmygui(gui:PTR TO mygui)
  73.   IF gui
  74.     -> Stop and destroy task, if necessary.
  75.     stop(gui)
  76.     disposegui(gui.gui)
  77.     END gui.ledplug
  78.     END gui
  79.   ENDIF
  80. ENDPROC
  81.  
  82. -> The action function creates a new GUI in the group.
  83. PROC spawn(info:PTR TO mygui) IS create(info.gh.mh)
  84.  
  85. -> The run/stop action.
  86. PROC runaction(info:PTR TO mygui)
  87.   IF info.going
  88.     settext(info.gh,info.title,'You stopped me!')
  89.     stop(info)
  90.   ELSE
  91.     settext(info.gh,info.title,'Started counting...')
  92.     Delay(10)
  93.     settext(info.gh,info.title,'Counting...')
  94.     run(info)
  95.   ENDIF
  96. ENDPROC
  97.  
  98. PROC stop(info:PTR TO mygui)
  99.   -> Going, so stop.
  100.   info.going:=FALSE
  101.   -> Temporarily make it high priority to die quicker.
  102.   Forbid()
  103.   IF info.task THEN SetTaskPri(info.task,5)
  104.   Permit()
  105.   -> Wait for task to die.
  106.   WHILE info.task DO Delay(1)
  107. ENDPROC
  108.  
  109. PROC run(info:PTR TO mygui)
  110.   DEF taskcode
  111.   IF info.task=NIL
  112.     IF taskcode:=eCodeTask({taskloop})
  113.       -> Make new Counter task, low priority.
  114.       info.task:=createTask('Counter',-5,taskcode,1000,info)
  115.     ENDIF
  116.   ENDIF
  117. ENDPROC
  118.  
  119. -> The loop the task will execute.
  120. PROC taskloop()
  121.   DEF task:PTR TO tc, info:PTR TO mygui, error=FALSE
  122.   task:=FindTask(NIL)
  123.   info:=task.userdata
  124.   info.going:=TRUE
  125.   -> While there is something to do.
  126.   WHILE next(info)
  127.     -> Cannot Delay() since this code is run by a Task (not a Process).
  128.     -> (200000 is a fifth of a second, or 10 ticks)
  129.   EXIT error:=timeDelay(UNIT_MICROHZ,0,200000)
  130.   ENDWHILE
  131.   -> Kill ourself safely.
  132.   Forbid()
  133.   -> This GUI update is safe, since within Forbid()/Permit().
  134.   IF error THEN settext(info.gh,info.title,'Timer error!')
  135.   info.task:=NIL
  136.   deleteTask(task)
  137.   Permit()
  138. ENDPROC
  139.  
  140. -> Next count.
  141. PROC next(info:PTR TO mygui)
  142.   DEF l:PTR TO led,h,m
  143.   IF info.going
  144.     l:=info.ledplug
  145.     l.colon:=(l.colon=FALSE)
  146.     m:=info.values[1]+1
  147.     IF m=60
  148.       m:=0
  149.       h:=info.values[]+1
  150.       IF h=13
  151.         h:=0
  152.         info.going:=FALSE
  153.         -> Must Forbid()/Permit() since we're a different task to the GUI.
  154.         Forbid()
  155.         settext(info.gh,info.title,'Finished!')
  156.         Permit()
  157.         RETURN FALSE
  158.       ENDIF
  159.       l.values[]:=h
  160.     ENDIF
  161.     l.values[1]:=m
  162.     -> Must Forbid()/Permit() since we're a different task to the GUI.
  163.     Forbid()
  164.     l.redisplay()
  165.     Permit()
  166.     RETURN TRUE
  167.   ENDIF
  168. ENDPROC FALSE
  169.  
  170. -> Close function.
  171. PROC close(info:PTR TO mygui) IS cleangui(info.gh)
  172.