Fire and forget programming
BlitzMax Forums/BlitzMax Programming/Fire and forget programming
| ||
Is there a name for this style of programming? It's a kind of, "set up once and forget about it" method where you can have a function called automatically when an event occurs The example here being the CloseDown() function gets called when the user clicks the button. As defined by the .OnClick event: window=CreateWindow("Test",x,y,w,h,) button=CreateButton("click me to end",x,y,w,h,window) button.OnClick = CloseDown GUI.Run ' runs until an internal QUIT state is reached End Function CloseDown() window.Free End Function I am not sure what you would call it in terms of its style of operation? What do you guys think of this method? The basic pro's and cons for me are: PROS Once the .OnAction event has been set up you pretty much forget about it. The background control automatically calls the function when the event occurs CONS - You can end up with lots of Function..End Function sections in your code. - If gadgets share the same function, the function does not know who called it. Therefore an internal 'Source' variable is required so that the function can query who the 'sender' is |
| ||
Could you not pass CloseDown and object identifier? |
| ||
I am not sure what you would call it in terms of its style of operation? Event Driven. wxMax and QtMax both work in this way, funnily enough. eg. SuperStrict Framework wx.wxApp Import wx.wxFrame Import wx.wxButton Import wx.wxPanel New MyApp.Run() Type MyApp Extends wxApp Method OnInit:Int() Local window:Frame = Frame(New Frame.Create(Null, wxID_ANY, "Test", -1, -1, 250, 150)) window.Show(True) Return True End Method End Type Type Frame Extends wxFrame Method OnInit() Local panel:wxPanel = New wxPanel.Create(Self) Local button:wxButton = New wxButton.Create(panel, 100, "click me to end", 20, 20) Connect(100, wxEVT_COMMAND_BUTTON_CLICKED, OnClose) End Method Function OnClose(event:wxEvent) wxWindow(event.parent).Close() End Function End Type |
| ||
Well you're half way to an Immediate Mode GUI there, but I think for an IMGUI, you have to do everything in one call and not return a gadget to work on any further. I guess you can't parent gadgets to other gadgets in an IMGUI, but perhaps some of these "rules" are flexible. There's an article about IMGUI's here if you're interested: http://sol.gfxile.net/imgui/index.html What you have there is a callback rather than a polled event system. I prefer a callback system to a polled input system, but I'm not a fan of IMGUI's as they're a little too simplistic for my needs. If you're making a simpler game, they're probably fine. |
| ||
Qt is done slightly differently... rather than Function callbacks it uses method callbacks in a signal/slot (observer) pattern :-)SuperStrict Framework Qt.QApplication Import Qt.QWidget Import Qt.QPushButton Import Qt.QVBoxLayout Local app:QApplication = New QApplication.Create() Local window:TFrame = New TFrame.Create() window.show() app.exec() End Type TFrame Extends QWidget Method Create:TFrame() Return TFrame(Super._Create()) End Method Method OnInit() Local button:QPushButton = New QPushButton.Create(tr("click me to end")) button.show() connect(button, "clicked", Self, "onClose") Local layout:QVBoxLayout = New QVBoxLayout.Create() layout.addWidget(button, Qt_AlignTop) setLayout(layout) setWindowTitle(tr("Test")) End Method Method onClose() close() End Method End Type Which I think is a rather cool use of reflection. |
| ||
My ifsoGUI has that capability as well. You set a callback function, then when a gadget generates an event, it calls your callback function rather than placing the event in the event queue (it can actually do both if you want). |
| ||
A couple of things I am wrestling with this 'Event' method is how best to handle keyboard / mouse events. Should these be global regardless of what gadget is active? Or, local only to the gadget? Example #1 - Mouse move events 'local' to a gadget Example #2 - Mouse move events 'global' Going local means you would keep having to decare .OnMouseMove = thisFunction for all the gadgets you want to handle mouse move events Another thing, there are hundreds of possible events which occur. For example: 1) the user IS ABOUT TO resize the window 2) the user IS resizing the window 3) the user has STOPPED resizing the window Do you have .OnXXX events for all of these or just one main .OnResize with a sub variable to dertime what type of resize is occuring? Brucey, How is this sort of thing in WxMax? Marcus, In a similar question, how many events have you catered for? Gabriel, Thanks. I am reading through the IMGUI right now |
| ||
How is this sort of thing in WxMax Brucey? There are a myriad of event types to which you choose to connect. Some are quite general, but others are more finely grained, like your "ABOUT TO" example. The actual event raised contains a reference to object which raised it. But also, the whole structure is object-oriented, rather than the flat, procedural model you seem to be using. Being object-oriented makes things much simpler, I feel. The design itself tends towards sectioning off different events with their gadgets. <EDIT> wxCodeGen supports about 180 unique event types. I'm sure there are still some missing. |
| ||
Now I do like the idea of imGUI's especially for the simple games I do then all I need is a quick, layout screen editor and I should be able to develope a game a lot faster! Hmm but if my layout editor is building the code how can I run the game within the editor? |
| ||
Which I think is a rather cool use of reflection. Indeed! I thought wxWidgets had this down, but it seems Qt goes one better. Makes me want to switch to it. Dangerous thoughts... |
| ||
Marcus, In a similar question, how many events have you catered for? Missed this previously. This info can be found in the ifsoGUI Wiki at http://wiki.ifsogui.com . ifsoGUI supports the following events at the moment (from this wiki page http://wiki.ifsogui.com/index.php/Events ): ifsoGUI_EVENT_CLICK – Gadget has been clicked with the mouse. ifsoGUI_EVENT_DOUBLE_CLICK – Gadget has been double clicked with the mouse. ifsoGUI_EVENT_RIGHT_CLICK – Gadget has been right clicked. ifsoGUI_EVENT_MIDDLE_CLICK – Gadget has been middle clicked. ifsoGUI_EVENT_MOUSE_ENTER – Mouse has moved over the gadget. Only occurs once, the first time the mouse enters the gadget. ifsoGUI_EVENT_MOUSE_EXIT – Mouse has moved away from the gadget. ifsoGUI_EVENT_MOUSE_DOWN – Mouse button has been pressed down on the gadget. ifsoGUI_EVENT_MOUSE_UP – Mouse button has been released on the gadget. ifsoGUI_EVENT_MOUSE_MOVE – Mouse has been moved over the gadget. This event is generated every time the mouse moves while over the gadget. ifsoGUI_EVENT_CHANGE – Gadget’s value has changed. ifsoGUI_EVENT_KEYHIT – Key was pressed while the gadget has focus. ifsoGUI_EVENT_GAIN_FOCUS – Gadget has gained the focus. ifsoGUI_EVENT_LOST_FOCUS – Gadget has lost the focus. ifsoGUI_EVENT_RESIZE – Gadget has been resized by the user. ifsoGUI_EVENT_CELL_CHANGE - The value in a cell has changed. Obviously all gadgets do not emit all events. Each gadgets wiki page epxlains what events it emits. |