Class: SFML::Event
- Inherits:
-
Object
- Object
- SFML::Event
- Defined in:
- lib/sfml/window/event.rb
Overview
A window event. Holds a type symbol and a Hash of type-specific data. Pattern-match it directly; that’s the intended interface:
case event
in {type: :closed} then ...
in {type: :key_pressed, code: :escape} then ...
in {type: :resized, size:} then ...
in {type: :mouse_moved, position: {x:, y:}} then ...
in {type: :mouse_wheel_scrolled, delta:} then ...
end
Type-specific fields are also reachable as plain methods (‘event.code`, `event.position`) for the cases when pattern matching is overkill.
Constant Summary collapse
- MOUSE_BUTTONS =
Order matches sfMouseButton in CSFML 3: left, right, middle, extra1, extra2. (CSFML 2 called the last two x_button1/x_button2, but SFML 3 dropped the X-button terminology.)
%i[left right middle extra1 extra2].freeze
- MOUSE_WHEELS =
%i[vertical horizontal].freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .decode(type_sym, ptr) ⇒ Object
-
.from_native(buffer) ⇒ Object
Reads the sfEvent union buffer, decodes the discriminator, and reads the type-specific variant.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #fetch(*args) ⇒ Object
-
#initialize(type, data = {}) ⇒ Event
constructor
A new instance of Event.
- #method_missing(name, *args) ⇒ Object
- #respond_to_missing?(name, _private = false) ⇒ Boolean
- #to_s ⇒ Object (also: #inspect)
Constructor Details
#initialize(type, data = {}) ⇒ Event
Returns a new instance of Event.
24 25 26 27 28 |
# File 'lib/sfml/window/event.rb', line 24 def initialize(type, data = {}) @type = type @data = data.freeze freeze end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
41 42 43 44 |
# File 'lib/sfml/window/event.rb', line 41 def method_missing(name, *args) return @data[name] if args.empty? && @data.key?(name) super end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
22 23 24 |
# File 'lib/sfml/window/event.rb', line 22 def data @data end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
22 23 24 |
# File 'lib/sfml/window/event.rb', line 22 def type @type end |
Class Method Details
.decode(type_sym, ptr) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/sfml/window/event.rb', line 61 def self.decode(type_sym, ptr) case type_sym when :closed, :focus_lost, :focus_gained, :mouse_entered, :mouse_left EMPTY when :resized s = C::Window::SizeEvent.new(ptr) { size: Vector2.new(s[:size][:x], s[:size][:y]) } when :key_pressed, :key_released k = C::Window::KeyEvent.new(ptr) { code: Keyboard.code_to_symbol(k[:code]), alt: k[:alt], control: k[:control], shift: k[:shift], system: k[:system], } when :text_entered t = C::Window::TextEvent.new(ptr) { unicode: t[:unicode], char: [t[:unicode]].pack("U*") } when :mouse_moved m = C::Window::MouseMoveEvent.new(ptr) { position: Vector2.new(m[:position][:x], m[:position][:y]) } when :mouse_moved_raw m = C::Window::MouseMoveEvent.new(ptr) # Raw event re-uses the same struct shape but the field is a delta. { delta: Vector2.new(m[:position][:x], m[:position][:y]) } when :mouse_button_pressed, :mouse_button_released b = C::Window::MouseButtonEvent.new(ptr) { button: MOUSE_BUTTONS[b[:button]] || :unknown, position: Vector2.new(b[:position][:x], b[:position][:y]), } when :mouse_wheel_scrolled w = C::Window::MouseWheelScrollEvent.new(ptr) { wheel: MOUSE_WHEELS[w[:wheel]] || :unknown, delta: w[:delta], position: Vector2.new(w[:position][:x], w[:position][:y]), } when :joystick_button_pressed, :joystick_button_released b = C::Window::JoystickButtonEvent.new(ptr) { joystick_id: b[:joystick_id], button: b[:button] } when :joystick_moved m = C::Window::JoystickMoveEvent.new(ptr) { joystick_id: m[:joystick_id], axis: Joystick::AXES[m[:axis]] || :unknown, position: m[:position], } when :joystick_connected, :joystick_disconnected c = C::Window::JoystickConnectEvent.new(ptr) { joystick_id: c[:joystick_id] } else EMPTY end end |
.from_native(buffer) ⇒ Object
Reads the sfEvent union buffer, decodes the discriminator, and reads the type-specific variant. CSFML lays each variant out with the sfEventType as the first field, so we can re-interpret the same memory as the right struct.
53 54 55 56 57 58 59 |
# File 'lib/sfml/window/event.rb', line 53 def self.from_native(buffer) type_index = buffer[:type] type_sym = C::Window::EVENT_TYPES[type_index] || :unknown data = decode(type_sym, buffer.to_ptr) new(type_sym, data) end |
Instance Method Details
#[](key) ⇒ Object
30 |
# File 'lib/sfml/window/event.rb', line 30 def [](key) = @data[key] |
#deconstruct_keys(_keys) ⇒ Object
33 34 35 |
# File 'lib/sfml/window/event.rb', line 33 def deconstruct_keys(_keys) { type: @type, **@data } end |
#fetch(*args) ⇒ Object
31 |
# File 'lib/sfml/window/event.rb', line 31 def fetch(*args, &) = @data.fetch(*args, &) |
#respond_to_missing?(name, _private = false) ⇒ Boolean
37 38 39 |
# File 'lib/sfml/window/event.rb', line 37 def respond_to_missing?(name, _private = false) @data.key?(name) || super end |
#to_s ⇒ Object Also known as: inspect
46 |
# File 'lib/sfml/window/event.rb', line 46 def to_s = "#<SFML::Event #{@type} #{@data.inspect}>" |