Hello,
I'm writing a simple GUI with SDL (1.2).
It consists of differents classes to represent different widgets (frame, button, label...). Each class extends an abstract base class GUIObject
.
Each widget has a Render
method and methods for event handling : MouseButtonDown
, MouseButtonUp
…
Pseudo-code :
class GUIObject :
//position
Left: int
Top: int
Width: int
Height: int
function IsIn(X, Y) : Boolean //checks if the mouse cursor is on the component
//abstract methogs
procedure Render()
procedure MouseButtonDown(Button, X, Y)
procedure MouseButtonUp(Button, X, Y)
procedure MouseMoved(Button, X, Y, XRel, YRel)
GUIContainer is an element that can maintain a list of child elements (e.g. a panel, a window, a dialog...) :
class GUIContainer
Objects[]
Add(Object)
procedure MouseButtonDown(Button, X, Y)
procedure MouseButtonUp(Button, X, Y)
procedure MouseMoved(Button, X, Y, XRel, YRel)
For event handling I hesitate between two solutions :
1) GUIContainer iterates through each of its child element. GUIContainer is responsible for checking the mouse position and the state of the child component.
class GUIContainer extends GUIObject :
procedure MouseButtonDown(Button, X, Y)
for each Object in Self.Objects :
if Object.IsVisible and Object.IsIn(X, Y) and Object.IsEnabled then
Object.MouseButtonDown(Button, X, Y)
Break // Exit loop
2) GUIContainer simply passes the event to all of its children. The child element is responsible for checking its state and the mouse position.
class GUIContainer :
procedure MouseButtonDown(Button, X, Y)
for each Object in Self.Objects :
Object.MouseButtonDown(Button, X, Y)
class GUIButton extends GUIObject :
procedure MouseButtonDown(Button, X, Y)
if Visible and IsIn(X, Y) then
//...
According to you, what is the best solution ?