Module: SFML::Keybindings

Included in:
App, Scene
Defined in:
lib/sfml/keybindings.rb

Overview

Class-level on_key DSL — shared by SFML::App and SFML::Scene. Each class that extends this module gets:

* `on_key(code, :method_name)` — bind a key to an instance method
* `on_key(code, ->(receiver) { ... })` — bind to a Proc
* `on_key(code) { |receiver| ... }` — bind to a block

Inheritance: a subclass's bindings layer on top of the parent's, so class Sub < Parent; on_key :x, :foo; end keeps the parent's other bindings while overriding (or adding) :x.

Instance Method Summary collapse

Instance Method Details

#key_handlers ⇒ Object

Hash{key_code => handler} — own bindings layered over the parent's (own keys win).



23
24
25
26
27
# File 'lib/sfml/keybindings.rb', line 23

def key_handlers
  own    = (@key_handlers ||= {})
  parent = superclass.respond_to?(:key_handlers) ? superclass.key_handlers : {}
  parent.merge(own)
end

#on_key(code, target = nil, &block) ⇒ Object

Returns the on key.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
# File 'lib/sfml/keybindings.rb', line 14

def on_key(code, target = nil, &block)
  @key_handlers ||= {}
  handler = block || target
  raise ArgumentError, "on_key needs a target Symbol/Proc or a block" unless handler
  @key_handlers[code.to_sym] = handler
end