Class: RubyGL::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygl/event.rb

Constant Summary collapse

@@callbacks =
{}

Class Method Summary collapse

Class Method Details

.register(event_type, &block) ⇒ Object

Accepts an event_type symbol (see native/all_enums.rb) and a block that will be passed a struct containing event information (see native/input.rb)



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubygl/event.rb', line 10

def self.register(event_type, &block)
    callback = FFI::Function.new(:int, [:pointer, :pointer]) do |_, event_ptr|
        event_union = Native::Event.new(event_ptr)
        
        # Makes Our Case Statement Less Verbose
        events = Native::ALL_EVENTS
        
        # Map Multiple Events To The Same Struct
        if (event_union[:common][:type] == events[event_type]) then
            case events[event_type]
            when events[:quit]
                block.call(event_union[:quit])
            when events[:window_event]
                block.call(event_union[:window])
            when events[:key_down], events[:key_up]
                block.call(event_union[:key])
            when events[:text_editing]
                block.call(event_union[:edit])
            when events[:text_input]
                block.call(event_union[:text])
            when events[:mouse_motion]
                block.call(event_union[:motion])
            when events[:mouse_button_down], events[:mouse_button_up]
                block.call(event_union[:button])
            when events[:mouse_wheel]
                block.call(event_union[:wheel])
            when events[:joy_axis_motion]
                block.call(event_union[:jaxis])
            when events[:joy_ball_motion]
                block.call(event_union[:jball])
            when events[:joy_hat_motion]
                block.call(event_union[:jhat])
            when events[:joy_button_down], events[:joy_button_up]
                block.call(event_union[:jbutton])
            when events[:joy_device_added], events[:joy_device_removed]
                block.call(event_union[:jdevice])
            when events[:controller_axis_motion]
                block.call(event_union[:caxis])
            when events[:controller_button_down], events[:controller_button_up]
                block.call(event_union[:cbutton])
            when events[:controller_device_added], events[:controller_device_removed], events[:controller_device_remapped]
                block.call(event_union[:cdevice])
            when events[:drop_file]
                block.call(event_union[:drop])
            end
        end
    end
    @@callbacks[event_type] = callback
    
    Native.addEventWatch(callback, nil)
end