Class: Gemba::KeyboardMap

Inherits:
Object
  • Object
show all
Defined in:
lib/gemba/keyboard_map.rb

Overview

Manages keyboard keysym → GBA bitmask mappings.

Shares the same interface as GamepadMap so that Player can delegate to either without knowing which device type is active.

Constant Summary collapse

DEFAULT_MAP =
{
  'z'         => KEY_A,
  'x'         => KEY_B,
  'BackSpace' => KEY_SELECT,
  'Return'    => KEY_START,
  'Right'     => KEY_RIGHT,
  'Left'      => KEY_LEFT,
  'Up'        => KEY_UP,
  'Down'      => KEY_DOWN,
  'a'         => KEY_L,
  's'         => KEY_R,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ KeyboardMap

Returns a new instance of KeyboardMap.



22
23
24
25
26
27
# File 'lib/gemba/keyboard_map.rb', line 22

def initialize(config)
  @config = config
  @map = DEFAULT_MAP.dup
  @device = nil
  load_config
end

Instance Attribute Details

#device=(value) ⇒ Object (writeonly)

Sets the attribute device

Parameters:

  • value

    the value to set the attribute device to.



29
30
31
# File 'lib/gemba/keyboard_map.rb', line 29

def device=(value)
  @device = value
end

Instance Method Details

#dead_zone_pctObject



84
# File 'lib/gemba/keyboard_map.rb', line 84

def dead_zone_pct = 0

#labelsObject



67
68
69
70
71
72
73
74
# File 'lib/gemba/keyboard_map.rb', line 67

def labels
  result = {}
  @map.each do |input, bit|
    gba_btn = GBA_BTN_BITS.key(bit)
    result[gba_btn] = input if gba_btn
  end
  result
end

#load_configObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gemba/keyboard_map.rb', line 48

def load_config
  cfg = @config.mappings(Config::KEYBOARD_GUID)
  if cfg.empty?
    @map = DEFAULT_MAP.dup
  else
    @map = {}
    cfg.each do |gba_str, keysym|
      bit = GBA_BTN_BITS[gba_str.to_sym]
      next unless bit
      @map[keysym] = bit
    end
  end
end

#maskObject



31
32
33
34
35
36
# File 'lib/gemba/keyboard_map.rb', line 31

def mask
  return 0 unless @device
  m = 0
  @map.each { |key, bit| m |= bit if @device.button?(key) }
  m
end

#reload!Object



62
63
64
65
# File 'lib/gemba/keyboard_map.rb', line 62

def reload!
  @config.reload!
  load_config
end

#reset!Object



44
45
46
# File 'lib/gemba/keyboard_map.rb', line 44

def reset!
  @map = DEFAULT_MAP.dup
end

#save_to_configObject



76
77
78
79
80
81
# File 'lib/gemba/keyboard_map.rb', line 76

def save_to_config
  @map.each do |input, bit|
    gba_btn = GBA_BTN_BITS.key(bit)
    @config.set_mapping(Config::KEYBOARD_GUID, gba_btn, input) if gba_btn
  end
end

#set(gba_btn, input_key) ⇒ Object



38
39
40
41
42
# File 'lib/gemba/keyboard_map.rb', line 38

def set(gba_btn, input_key)
  bit = GBA_BTN_BITS[gba_btn] or return
  @map.delete_if { |_, v| v == bit }
  @map[input_key.to_s] = bit
end

#set_dead_zone(_) ⇒ Object

Raises:

  • (NotImplementedError)


86
87
88
# File 'lib/gemba/keyboard_map.rb', line 86

def set_dead_zone(_)
  raise NotImplementedError, "keyboard does not support dead zones"
end

#supports_deadzone?Boolean

Returns:

  • (Boolean)


83
# File 'lib/gemba/keyboard_map.rb', line 83

def supports_deadzone? = false