I have been making extensive use of hooks and emitted events in BlitzMax.
For example, every window in my program has an EmitEventHook. This evaluates all events that are relevant to that window or groups of gadgets. I find this approach much easier to break events up this way, because otherwise my event evaluation source file gets way too big. It also makes it very easy to mix and match various GUI elements. For example, I could include a new browser control in my main program with one source file, without having to modify the main source:
The other interesting thing I have been doing is replacing redundant event evalaluation with an EmitEvent() call. For example in the above code, both the menu action and the window close will end the program. When the window is closed, I just emit a menu event that makes the program think the Exit menu was pressed. This makes it so I only have to write the code to process that event once.
My main program loop then looks like this:
Repeat
WaitEvent()
Forever
Everything is handled in callbacks! This approach is very different than what I am used to, but it seems to work well, and it compartmentalizes the code very easily.
Are there any potential problems with this approach?
For example, every window in my program has an EmitEventHook. This evaluates all events that are relevant to that window or groups of gadgets. I find this approach much easier to break events up this way, because otherwise my event evaluation source file gets way too big. It also makes it very easy to mix and match various GUI elements. For example, I could include a new browser control in my main program with one source file, without having to modify the main source:
Function MainWindowHook:Object(id,data:Object,context:Object) Local editorviewport:TEditorViewport Local event:TEvent=TEvent(data) If Not event Return data Select event.id Case EVENT_WINDOWCLOSE Select event.source Case MainWindow EmitEvent CreateEvent(EVENT_MENUACTION,Null,MENU_EXIT) Return Null EndSelect Case EVENT_MENUACTION Select event.data Case MENU_EXIT HideGadget MainWindow StopEngine() End Return Null EndSelect EndSelect Return data EndFunction
The other interesting thing I have been doing is replacing redundant event evalaluation with an EmitEvent() call. For example in the above code, both the menu action and the window close will end the program. When the window is closed, I just emit a menu event that makes the program think the Exit menu was pressed. This makes it so I only have to write the code to process that event once.
My main program loop then looks like this:
Repeat
WaitEvent()
Forever
Everything is handled in callbacks! This approach is very different than what I am used to, but it seems to work well, and it compartmentalizes the code very easily.
Are there any potential problems with this approach?