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_handlersObject

‘Hash=> handler` — own bindings layered over the parent’s (own keys win).



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

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

Raises:

  • (ArgumentError)


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

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