Module: Okami::Keyboard

Defined in:
lib/okami/keyboard.rb

Constant Summary collapse

DefaultKeySymbols =
{
  Gosu::KbUp    => :up,
  Gosu::KbDown  => :down,
  Gosu::KbLeft  => :left,
  Gosu::KbRight => :right,

  Gosu::KbSpace         => :space,
  Gosu::KbReturn        => :return,
  Gosu::KbBackspace     => :backspace,
  Gosu::KbLeftShift     => :left_shift,
  Gosu::KbRightShift    => :right_shift,
  Gosu::KbLeftControl   => :left_ctrl,
  Gosu::KbRightControl  => :right_ctrl,
  Gosu::KbEscape        => :escape,
  Gosu::KbLeftAlt       => :left_alt,
  Gosu::KbRightAlt      => :right_alt,
  Gosu::KbTab           => :tab,

  Gosu::Kb0 => 0,
  Gosu::Kb1 => 1,
  Gosu::Kb2 => 2,
  Gosu::Kb3 => 3,
  Gosu::Kb4 => 4,
  Gosu::Kb5 => 5,
  Gosu::Kb6 => 6,
  Gosu::Kb7 => 7,
  Gosu::Kb8 => 8,
  Gosu::Kb9 => 9,

  Gosu::KbF1  => :f1,
  Gosu::KbF2  => :f2,
  Gosu::KbF3  => :f3,
  Gosu::KbF4  => :f4,
  Gosu::KbF5  => :f5,
  Gosu::KbF6  => :f6,
  Gosu::KbF7  => :f7,
  Gosu::KbF8  => :f8,
  Gosu::KbF9  => :f9,
  Gosu::KbF10 => :f10,
  Gosu::KbF11 => :f11,
  Gosu::KbF12 => :f12,

  Gosu::KbA => :a,
  Gosu::KbB => :b,
  Gosu::KbC => :c,
  Gosu::KbD => :d,
  Gosu::KbE => :e,
  Gosu::KbF => :f,
  Gosu::KbG => :g,
  Gosu::KbH => :h,
  Gosu::KbI => :i,
  Gosu::KbJ => :j,
  Gosu::KbK => :k,
  Gosu::KbL => :l,
  Gosu::KbM => :m,
  Gosu::KbN => :n,
  Gosu::KbO => :o,
  Gosu::KbP => :p,
  Gosu::KbQ => :q,
  Gosu::KbR => :r,
  Gosu::KbS => :s,
  Gosu::KbT => :t,
  Gosu::KbU => :u,
  Gosu::KbV => :v,
  Gosu::KbW => :w,
  Gosu::KbX => :x,
  Gosu::KbY => :y,
  Gosu::KbZ => :z
}
@@key_symbols =
DefaultKeySymbols.dup
@@key_down_listeners =
{}
@@key_up_listeners =
{}

Class Method Summary collapse

Class Method Details

.add_key_down_listener(listener_method) ⇒ Object



90
91
92
# File 'lib/okami/keyboard.rb', line 90

def add_key_down_listener listener_method
  @@key_down_listeners[ listener_method.receiver ] = listener_method
end

.add_key_up_listener(listener_method) ⇒ Object



94
95
96
# File 'lib/okami/keyboard.rb', line 94

def add_key_up_listener listener_method
  @@key_up_listeners[ listener_method.receiver ] = listener_method
end

.any_key_down?(*key_symbols) ⇒ Boolean Also known as: any_of_keys_down?

Returns true if one of the key_symbol arguments is down

Returns:

  • (Boolean)


133
134
135
136
137
138
139
# File 'lib/okami/keyboard.rb', line 133

def any_key_down? *key_symbols
  key_symbols << :any_key if key_symbols.empty?
  key_symbols.each do |key_symbol|
    return true if key_down? key_symbol
  end
  return false
end

.button_down(id) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/okami/keyboard.rb', line 142

def button_down id
  key = @@key_symbols[id]
  case key
  when :left_alt,   :right_alt   then call_key_down_listeners :alt
  when :left_shift, :right_shift then call_key_down_listeners :shift
  when :left_ctrl,  :right_ctrl  then call_key_down_listeners :ctrl
  when :left_cmd,   :right_cmd   then call_key_down_listeners :cmd
  end
  call_key_down_listeners key
end

.button_up(id) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/okami/keyboard.rb', line 153

def button_up id
  key = @@key_symbols[id]
  case key
  when :left_alt,   :right_alt;   call_key_up_listeners :alt
  when :left_shift, :right_shift; call_key_up_listeners :shift
  when :left_ctrl,  :right_ctrl;  call_key_up_listeners :ctrl
  when :left_cmd,   :right_cmd;   call_key_up_listeners :cmd
  end
  call_key_up_listeners key
end

.key_down?(key_symbol) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
# File 'lib/okami/keyboard.rb', line 112

def key_down? key_symbol
  case key_symbol
  when :any_key; any_key_down? *@@key_symbols.values
  when :alt;     any_key_down? :left_alt,   :right_alt
  when :shift;   any_key_down? :left_shift, :right_shift
  when :ctrl;    any_key_down? :left_ctrl,  :right_ctrl
  when :cmd;     any_key_down? :left_cmd,   :right_cmd
  else
    $window.button_down? @@key_symbols.key( key_symbol )
  end
end

.key_symbolsObject



87
# File 'lib/okami/keyboard.rb', line 87

def key_symbols; @@key_symbols end

.key_symbols=(hash) ⇒ Object



88
# File 'lib/okami/keyboard.rb', line 88

def key_symbols= hash; @@key_symbols = hash end

.keys_down?(*key_symbols) ⇒ Boolean

Returns true if all key_symbol arguments is down

Returns:

  • (Boolean)


125
126
127
128
129
130
# File 'lib/okami/keyboard.rb', line 125

def keys_down? *key_symbols
  key_symbols.each do |key_symbol|
    return false unless key_down? key_symbol
  end
  return true
end

.remove_key_down_listener(listener) ⇒ Object



105
106
107
108
109
110
# File 'lib/okami/keyboard.rb', line 105

def remove_key_down_listener listener
  case listener
  when Method; @@key_down_listeners.delete @@key_down_listeners.key(listener)
  else;        @@key_down_listeners.delete listener
  end
end

.remove_key_up_listener(listener) ⇒ Object



98
99
100
101
102
103
# File 'lib/okami/keyboard.rb', line 98

def remove_key_up_listener listener
  case listener
  when Method; @@key_up_listeners.delete @@key_up_listeners.key(listener)
  else;        @@key_up_listeners.delete listener
  end
end