Mousey Mousey
April 25th, 2009 by Boon
A small update on sagaEngine (an increasingly appropriate name, mind you). I realised I was handling input wrong for the editor. Each entity which required input (each button, palette swatch, scroll bar, draggable object, etc.) was individually checking for input, and passing around a whole bunch of global variables through a singleton to avoid registering multiple input events – for example, if a button is in a window and that window is over the stage, and I click my mouse, I don’t want the editor to think I clicked the button, the window, AND the stage. Unfortunately, my editor doesn’t understand common english and no amount of screaming abuse at it to “JUST F***ING WORK” seemed to have any effect.
The global variables worked well enough – I simply set a global “MouseLocked” flag to true from the first object that detected a usable click (ie. a click within it’s bounds) allowing all other potential click-targets to ignore the event.
That was fine enough, but having each entity handle its own input meant pawing through individual classes to implement new mouse/keyboard events or modify the handling of existing events. So I decided to create an InputManager singleton to wrap all game/editor input into a single location, with public events to which interested entities can subscribe.
So now, the InputManager recognises a CLICK, and sends off an OnClick event to all objects that would be interested in a click. The first object to receive the callback still has to set the global MouseLock flag to true so that objects further down the subscription tree ignore the click, but the real advantage of this system is in being able to manage all keyboard/mouse input from one location, and consequently being able to implement input-response in any entity through a very simple subscription setup.
Where before if I wanted to add a LeftClick response to an entity I would have to get the mouse state, check the button states, compare against previous states, etc., now I can simply subscribe MyEntityResponse() to InputManager.LeftClick and it is automatically called from the InputManager when the user clicks the left button. Event better, because input handling is disconnected from the main program, I could safely implement handling for every mouse button/scroll wheel/key, and just subscribe to the events I’m interested in, giving myself a reusable InputManager that is easily adaptable to any project I wish to work on, and any input-related event I could want.
I also painted a swampy thing tonight but it turned out horrible which is why I busted out the code so I could post something creative without having to post that. Sly, no?
- No Comments »
- Posted in sagaEngine
