Class: Rubydraw::EventQueue
Overview
Rubydraw uses an EventQueue to collect SDL events, e.g. mouse movements keystrokes, and window actions. A new EventQueue is automatically created when a Rubydraw::Window is initialized. Every tick, the window asks its event queue (created in Rubydraw::Window#initialize) for the new events
Instance Method Summary collapse
- #exp_get_events ⇒ Object
-
#get_events ⇒ Object
Get all the new events.
-
#initialize ⇒ EventQueue
constructor
A new instance of EventQueue.
- #key_repeat ⇒ Object
Constructor Details
#initialize ⇒ EventQueue
Returns a new instance of EventQueue.
7 8 9 |
# File 'lib/rubydraw/event_queue.rb', line 7 def initialize @keys_being_repeated = [] end |
Instance Method Details
#exp_get_events ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rubydraw/event_queue.rb', line 30 def exp_get_events events = [] until((event = SDL::PollEvent()).nil?) new_event = Events.match(event) events << new_event key = new_event.key if new_event.is_a?(Events::KeyboardEvent) if new_event.is_a?(Events::KeyReleased) and @keys_being_repeated.include?(key) @keys_being_repeated.delete(key) end # Synthesize each key that is being held. @keys_being_repeated.each {|num| events << Events::KeyPressed.new(num)} # Start adding keys that are held, and remove keys that are no longer # being held (whenever a Events::KeyReleased is created). if new_event.is_a?(Events::KeyPressed) @keys_being_repeated << key end end puts @keys_being_repeated.size events end |
#get_events ⇒ Object
Get all the new events
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rubydraw/event_queue.rb', line 14 def get_events events = [] until ((event = SDL.PollEvent) == nil) new_event = Events.match(event) if new_event.is_a?(Events::KeyReleased) @keys_being_repeated.delete(new_event.key) end if new_event.is_a?(Events::KeyPressed) and not @keys_being_repeated.include?(new_event.key) @keys_being_repeated << new_event.key end events << new_event end @keys_being_repeated.each {|num| events << Events::KeyPressed.new(num)} events end |
#key_repeat ⇒ Object
10 11 12 |
# File 'lib/rubydraw/event_queue.rb', line 10 def key_repeat Rubydraw.key_repeat end |