I haven't seen anyone sharing code for something like this, so I figured once I got one built for myself I'd share.
This is a manager for high level input based on three types of game input;
simple actions
- simple key binding
chords
- multiple keys pressed at once for an action
sequences
- multiple keys pressed in order for an action
Each of these are presented in a way such that you provide a named action, and this class will poll Gosu's input methods to determine the state of it. Simple actions are pretty straight forward, if any of the buttons are pressed, the action will fire. Chords and sequences are a little bit more complicated. Chords allow the user some amount of time (here it is hard coded to 10 frames, but you can change that however you please) to press down all of the buttons. Sequences permit the user some threshold between keystrokes to fire (this threshold is determined when creating the action). All three of these input types will have one of five states;
idle - not active
process - chord or sequence has started
begin - the key has just been pressed
active - the key(s) is/are held down
finish - the/a key has just been released
I did not implement any event triggering code, and instead opted for a query interface. This was simply just because it was easier to write (plus, for my projects I don't have much need for anything more complicated :D ).
At any rate, I started out mocking the code in Ruby then created the C++ code I needed. Since I'm a nice guy (so I'm told), and I know most of you enjoy using the Ruby end of Gosu, I finished the Ruby version and wrote a simple test case (instead of proper documentation). If any of you want to use the C++ code, hopefully you can figure out how to use it from the header file, if not I'll be happy to answer questions here. As for the Ruby, you should be able to gather everything from the test case. But I'll still answer any questions you have if I can.
Happy coding!
http://dl.dropbox.com/u/6366/ChadyG_InputHandler.zipnote: This code is fresh as of today, so it may have some bugs. Also I realize the Ruby version could be cleaner, but it should still be usable.