Class: InputManager
- Extended by:
- Publisher
- Defined in:
- lib/gamebox/core/input_manager.rb
Overview
InputManager handles the pumping for events and distributing of said events. You can gain access to these events by registering for all events, or just the ones you care about. All events: input_manager.when :event_received do |evt|
...
end
Some events: input_manager.reg KeyPressed, :space do
...
end
Don’t forget to unreg for these things between stages, since the InputManager is shared across stages.
Constant Summary collapse
- DOWN_EVENTS =
[:mouse_down, :keyboard_down, :game_pad_down]
- UP_EVENTS =
[:mouse_up, :keyboard_up, :game_pad_up]
Instance Attribute Summary collapse
-
#down_ids ⇒ Object
Returns the value of attribute down_ids.
-
#window ⇒ Object
Returns the value of attribute window.
Instance Method Summary collapse
- #_register_hook(listener, event_class, *event_ids, &block) ⇒ Object
-
#clear_hooks(listener = nil) ⇒ Object
removes all blocks that are in the scope of listener’s instance.
- #fire_event(event) ⇒ Object
-
#initialize ⇒ InputManager
constructor
Sets up the clock and main event loop.
- #pause ⇒ Object
- #register(game) ⇒ Object
-
#register_hook(event_class, *event_ids, &block) ⇒ Object
(also: #reg)
registers a block to be called when matching events are pulled from the SDL queue.
- #setup ⇒ Object
- #show ⇒ Object
- #unpause ⇒ Object
-
#unregister_hook(event_class, *event_ids, &block) ⇒ Object
(also: #unreg)
unregisters a block to be called when matching events are pulled from the SDL queue.
-
#while_pressed(id_or_ids, target, accessor) ⇒ Object
autohook a boolean to be set to true while any of the keys are pressed.
Constructor Details
#initialize ⇒ InputManager
Sets up the clock and main event loop. You should never call this method, as this class should be initialized by conject.
28 29 30 31 32 33 34 35 |
# File 'lib/gamebox/core/input_manager.rb', line 28 def initialize @hooks = {} @non_id_hooks = {} @down_ids = {} @block_self_lookup = {} setup end |
Instance Attribute Details
#down_ids ⇒ Object
Returns the value of attribute down_ids.
23 24 25 |
# File 'lib/gamebox/core/input_manager.rb', line 23 def down_ids @down_ids end |
#window ⇒ Object
Returns the value of attribute window.
23 24 25 |
# File 'lib/gamebox/core/input_manager.rb', line 23 def window @window end |
Instance Method Details
#_register_hook(listener, event_class, *event_ids, &block) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/gamebox/core/input_manager.rb', line 94 def _register_hook(listener, event_class, *event_ids, &block) return unless block_given? @hooks[event_class] ||= {} for event_id in event_ids @uses_mouse = true if event_id >= MsRangeBegin && event_id <= MsRangeEnd @hooks[event_class][event_id] ||= [] @hooks[event_class][event_id] << block end @non_id_hooks[event_class] ||= [] if event_ids.empty? @uses_mouse = true @non_id_hooks[event_class] << block end if listener.respond_to?(:can_fire?) && listener.can_fire?(:remove_me) listener.when :remove_me do unregister_hook event_class, *event_ids, &block end end end |
#clear_hooks(listener = nil) ⇒ Object
removes all blocks that are in the scope of listener’s instance. clears all listeners if listener is nil
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/gamebox/core/input_manager.rb', line 136 def clear_hooks(listener=nil) if listener for event_klass, id_listeners in @hooks for key in id_listeners.keys.dup id_listeners[key].delete_if do |block| block_self = @block_self_lookup.delete block block_self.equal?(listener) end end end for key in @non_id_hooks.keys.dup @non_id_hooks[key].delete_if do |block| eval('self',block.binding).equal?(listener) end end else @hooks = {} @non_id_hooks = {} end end |
#fire_event(event) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/gamebox/core/input_manager.rb', line 49 def fire_event(event) fire :event_received, event # fix for pause bug? @hooks ||= {} @non_id_hooks ||= {} event_hooks = @hooks[event[:callback_key]] gosu_id = event[:id] unless gosu_id.nil? event_action_hooks = event_hooks[gosu_id] if event_hooks if event_action_hooks for callback in event_action_hooks callback.call event end end end non_id_event_hooks = @non_id_hooks[event[:callback_key]] if non_id_event_hooks for callback in non_id_event_hooks callback.call event end end end |
#pause ⇒ Object
172 173 174 175 176 177 |
# File 'lib/gamebox/core/input_manager.rb', line 172 def pause @paused_hooks = @hooks @paused_non_id_hooks = @non_id_hooks @hooks = {} @non_id_hooks = {} end |
#register(game) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/gamebox/core/input_manager.rb', line 186 def register(game) @window.when :button_up do || handle_event , :up end @window.when :button_down do || handle_event , :down end @window.when :update do |millis| if @uses_mouse @last_mouse_x ||= mouse_x @last_mouse_y ||= mouse_y x_diff = @last_mouse_x - mouse_x y_diff = @last_mouse_y - mouse_y unless x_diff < 0.1 && x_diff > -0.1 && y_diff < 0.1 && y_diff > -0.1 handle_event nil, :motion @last_mouse_x = mouse_x @last_mouse_y = mouse_y end end game.update millis end @window.when :draw do game.draw end end |
#register_hook(event_class, *event_ids, &block) ⇒ Object Also known as: reg
registers a block to be called when matching events are pulled from the SDL queue. ie input_manager.register_hook KeyPressed do |evt|
# will be called on every key press
end input_manager.register_hook KeyPressed, K_SPACE do |evt|
# will be called on every spacebar key press
end
84 85 86 87 88 89 90 91 |
# File 'lib/gamebox/core/input_manager.rb', line 84 def register_hook(event_class, *event_ids, &block) event_class = :down if DOWN_EVENTS.include? event_class event_class = :up if UP_EVENTS.include? event_class return unless block_given? listener = eval("self", block.binding) @block_self_lookup[block] = listener _register_hook listener, event_class, *event_ids, &block end |
#setup ⇒ Object
37 38 39 40 41 |
# File 'lib/gamebox/core/input_manager.rb', line 37 def setup @window = wrapped_screen.screen auto_quit = config_manager[:auto_quit] @auto_quit = instance_eval(auto_quit) if auto_quit end |
#show ⇒ Object
43 44 45 46 47 |
# File 'lib/gamebox/core/input_manager.rb', line 43 def show # Does not return, Gosu uses this to pop up the window # This must be last in the method @window.show end |
#unpause ⇒ Object
179 180 181 182 183 184 |
# File 'lib/gamebox/core/input_manager.rb', line 179 def unpause @hooks = @paused_hooks @non_id_hooks = @paused_non_id_hooks @paused_hooks = nil @paused_non_id_hooks = nil end |
#unregister_hook(event_class, *event_ids, &block) ⇒ Object Also known as: unreg
unregisters a block to be called when matching events are pulled from the SDL queue. ie input_manager.unregister_hook KeyPressed, :space, registered_block also see InputManager#clear_hooks for clearing many hooks
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/gamebox/core/input_manager.rb', line 118 def unregister_hook(event_class, *event_ids, &block) event_class = :down if DOWN_EVENTS.include? event_class event_class = :up if UP_EVENTS.include? event_class @hooks[event_class] ||= {} for event_id in event_ids @hooks[event_class][event_id] ||= [] @hooks[event_class][event_id].delete block if block_given? end if event_ids.empty? @hooks[event_class] ||= [] @hooks[event_class].delete block if block_given? end end |
#while_pressed(id_or_ids, target, accessor) ⇒ Object
autohook a boolean to be set to true while any of the keys are pressed
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/gamebox/core/input_manager.rb', line 159 def while_pressed(id_or_ids, target, accessor) ids = [id_or_ids].flatten ids.each do |id| _register_hook target, :down, id do target.send "#{accessor}=", true end _register_hook target, :up, id do target.send "#{accessor}=", false if (@down_ids.keys & ids).size == 0 end end end |