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 =
Mouse wheel axes —
:vertical/:horizontal. %i[vertical horizontal].freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The type, data components.
-
#type ⇒ Object
readonly
The type, data components.
Class Method Summary collapse
-
.decode(type_sym, ptr) ⇒ Object
Returns the self.
-
.from_native(buffer) ⇒ Object
Reads the sfEvent union buffer, decodes the discriminator, and reads the type-specific variant.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Hash-style lookup into the event payload.
-
#deconstruct_keys(_keys) ⇒ Object
Pattern-match hook — flattens
{type:, ...payload}into one hash so callers can writein {type: :key_pressed, code:}. -
#fetch(*args) ⇒ Object
Hash-style fetch with the standard fallback / block behaviour.
-
#initialize(type, data = {}) ⇒ Event
constructor
Build directly (rare —
Event.from_nativeis the usual path). -
#method_missing(name, *args) ⇒ Object
Payload keys are exposed as method calls so
event.codeworks alongsideevent[:code]. -
#respond_to_missing?(name, _private = false) ⇒ Boolean
respond_to?answer for payload keys — keeps#method_missingsafe under introspection. -
#to_s ⇒ Object
(also: #inspect)
String representation for debugging.
Constructor Details
#initialize(type, data = {}) ⇒ Event
Build directly (rare — Event.from_native is the usual path).
27 28 29 30 31 |
# File 'lib/sfml/window/event.rb', line 27 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
Payload keys are exposed as method calls so event.code works
alongside event[:code].
52 53 54 55 |
# File 'lib/sfml/window/event.rb', line 52 def method_missing(name, *args) return @data[name] if args.empty? && @data.key?(name) super end |
Instance Attribute Details
#data ⇒ Object (readonly)
The type, data components.
24 25 26 |
# File 'lib/sfml/window/event.rb', line 24 def data @data end |
#type ⇒ Object (readonly)
The type, data components.
24 25 26 |
# File 'lib/sfml/window/event.rb', line 24 def type @type end |
Class Method Details
.decode(type_sym, ptr) ⇒ Object
Returns the self.
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/sfml/window/event.rb', line 74 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] } when :touch_began, :touch_moved, :touch_ended t = C::Window::TouchEvent.new(ptr) { finger: t[:finger], position: Vector2.new(t[:position][:x], t[:position][:y]), } when :sensor_changed s = C::Window::SensorEvent.new(ptr) { sensor: Sensor::TYPES[s[:sensor]] || :unknown, value: Vector3.new(s[:value][:x], s[:value][:y], s[:value][:z]), } 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.
65 66 67 68 69 70 71 |
# File 'lib/sfml/window/event.rb', line 65 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
Hash-style lookup into the event payload.
34 35 |
# File 'lib/sfml/window/event.rb', line 34 def [](key) = @data[key] # Hash-style fetch with the standard fallback / block behaviour. |
#deconstruct_keys(_keys) ⇒ Object
Pattern-match hook — flattens {type:, ...payload} into one
hash so callers can write in {type: :key_pressed, code:}.
40 41 42 |
# File 'lib/sfml/window/event.rb', line 40 def deconstruct_keys(_keys) { type: @type, **@data } end |
#fetch(*args) ⇒ Object
Hash-style fetch with the standard fallback / block behaviour.
36 |
# File 'lib/sfml/window/event.rb', line 36 def fetch(*args, &) = @data.fetch(*args, &) |
#respond_to_missing?(name, _private = false) ⇒ Boolean
respond_to? answer for payload keys — keeps #method_missing
safe under introspection.
46 47 48 |
# File 'lib/sfml/window/event.rb', line 46 def respond_to_missing?(name, _private = false) @data.key?(name) || super end |
#to_s ⇒ Object Also known as: inspect
String representation for debugging.
58 |
# File 'lib/sfml/window/event.rb', line 58 def to_s = "#<SFML::Event #{@type} #{@data.inspect}>" |