Class: Vedeu::Input::Mapper
- Inherits:
-
Object
- Object
- Vedeu::Input::Mapper
- Defined in:
- lib/vedeu/input/mapper.rb
Overview
Maps keys to keymaps.
Instance Attribute Summary collapse
- #key ⇒ String|Symbol readonly protected
- #repository ⇒ Vedeu::Repositories::Repository readonly protected
Class Method Summary collapse
-
.keypress(key = nil, name = nil) ⇒ Boolean
Takes a key as a keypress and sends it to registered keymaps.
-
.valid?(key = nil, name = nil) ⇒ Boolean
Checks a key is valid; i.e.
Instance Method Summary collapse
-
#global_key? ⇒ Boolean
private
Is the key a global key?.
-
#initialize(key = nil, name = nil, repository = nil) ⇒ Vedeu::Input::Mapper
constructor
Returns a new instance of Vedeu::Input::Mapper.
-
#key_defined?(named = name) ⇒ Boolean
private
Is the key defined in the named keymap?.
-
#keymap(named = name) ⇒ Vedeu::Input::Keymap
private
Fetch the named keymap from the repository.
-
#keymap?(named = name) ⇒ Boolean
private
Does the keymaps repository have the named keymap already registered?.
-
#keypress ⇒ Boolean
Returns a boolean indicating that the key is registered to the current keymap, or the global keymap.
-
#name ⇒ String|NilClass
(also: #interface)
private
With a name, we check the keymap with that name, otherwise we use the name of the interface currently in focus.
-
#valid? ⇒ Boolean
Returns a boolean indicating that the key is not registered to the current keymap, or the global keymap.
Constructor Details
#initialize(key = nil, name = nil, repository = nil) ⇒ Vedeu::Input::Mapper
Returns a new instance of Vedeu::Input::Mapper.
44 45 46 47 48 |
# File 'lib/vedeu/input/mapper.rb', line 44 def initialize(key = nil, name = nil, repository = nil) @key = key @name = name @repository = repository || Vedeu.keymaps end |
Instance Attribute Details
#key ⇒ String|Symbol (readonly, protected)
80 81 82 |
# File 'lib/vedeu/input/mapper.rb', line 80 def key @key end |
#repository ⇒ Vedeu::Repositories::Repository (readonly, protected)
84 85 86 |
# File 'lib/vedeu/input/mapper.rb', line 84 def repository @repository end |
Class Method Details
.keypress(key = nil, name = nil) ⇒ Boolean
Takes a key as a keypress and sends it to registered keymaps. If found, the associated action is fired, otherwise, we move to the next keymap or return false.
18 19 20 21 22 23 24 |
# File 'lib/vedeu/input/mapper.rb', line 18 def self.keypress(key = nil, name = nil) Vedeu.trigger(:key, key) return false unless key new(key, name).keypress end |
.valid?(key = nil, name = nil) ⇒ Boolean
Checks a key is valid; i.e. not already registered to a keymap. When the key is registered, then the key is invalid and cannot be used again.
32 33 34 35 36 |
# File 'lib/vedeu/input/mapper.rb', line 32 def self.valid?(key = nil, name = nil) return false unless key new(key, name).valid? end |
Instance Method Details
#global_key? ⇒ Boolean (private)
Is the key a global key?
91 92 93 |
# File 'lib/vedeu/input/mapper.rb', line 91 def global_key? key_defined?('_global_') end |
#key_defined?(named = name) ⇒ Boolean (private)
Is the key defined in the named keymap?
99 100 101 |
# File 'lib/vedeu/input/mapper.rb', line 99 def key_defined?(named = name) keymap?(named) && keymap(named).key_defined?(key) end |
#keymap(named = name) ⇒ Vedeu::Input::Keymap (private)
Fetch the named keymap from the repository.
107 108 109 |
# File 'lib/vedeu/input/mapper.rb', line 107 def keymap(named = name) repository.find(named) end |
#keymap?(named = name) ⇒ Boolean (private)
Does the keymaps repository have the named keymap already registered?
116 117 118 |
# File 'lib/vedeu/input/mapper.rb', line 116 def keymap?(named = name) repository.registered?(named) end |
#keypress ⇒ Boolean
Returns a boolean indicating that the key is registered to the current keymap, or the global keymap.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/vedeu/input/mapper.rb', line 54 def keypress return false unless key return true if key_defined? && keymap.use(key) return true if global_key? && keymap('_global_').use(key) Vedeu.log(type: :input, message: "Key detected: #{key.inspect}".freeze) false end |
#name ⇒ String|NilClass (private) Also known as: interface
With a name, we check the keymap with that name, otherwise we use the name of the interface currently in focus.
124 125 126 |
# File 'lib/vedeu/input/mapper.rb', line 124 def name @name || Vedeu.focus end |
#valid? ⇒ Boolean
Returns a boolean indicating that the key is not registered to the current keymap, or the global keymap.
70 71 72 73 74 |
# File 'lib/vedeu/input/mapper.rb', line 70 def valid? return false if !key || key_defined? || global_key? true end |