Class: AdventureRL::EventHandlers::Buttons

Inherits:
EventHandler show all
Defined in:
lib/AdventureRL/EventHandlers/Buttons.rb

Direct Known Subclasses

MouseButtons

Constant Summary collapse

BUTTON_EVENT_HANDLERS =

This constant will be filled with EventHandlers::Buttons and EventHandlers::MouseButtons instances as they are created. It is used by the following class methods EventHandler::Buttons#button_down, EventHandler::Buttons#button_up, and EventHandler::Buttons#update.

[]
DEFAULT_SETTINGS =
Settings.new(
  pressable_buttons: [],
  auto_update:       false
)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EventHandler

#add_event, #subscribe, #trigger, #unsubscribe

Constructor Details

#initialize(settings = {}) ⇒ Buttons

Returns a new instance of Buttons.



32
33
34
35
36
37
38
39
40
# File 'lib/AdventureRL/EventHandlers/Buttons.rb', line 32

def initialize settings = {}
  @settings = DEFAULT_SETTINGS.merge settings
  super
  @pressable_buttons = []
  pressable_buttons = [@settings.get(:pressable_buttons)].flatten
  add_pressable_button pressable_buttons  if (pressable_buttons.any?)
  @events = get_events
  BUTTON_EVENT_HANDLERS << self  if (@settings.get(:auto_update))
end

Class Method Details

.button_down(btnid) ⇒ Object



13
14
15
16
17
# File 'lib/AdventureRL/EventHandlers/Buttons.rb', line 13

def self.button_down btnid
  BUTTON_EVENT_HANDLERS.each do |handler|
    handler.button_down btnid
  end
end

.button_up(btnid) ⇒ Object



18
19
20
21
22
# File 'lib/AdventureRL/EventHandlers/Buttons.rb', line 18

def self.button_up btnid
  BUTTON_EVENT_HANDLERS.each do |handler|
    handler.button_up btnid
  end
end

.updateObject



23
24
25
# File 'lib/AdventureRL/EventHandlers/Buttons.rb', line 23

def self.update
  BUTTON_EVENT_HANDLERS.each &:update
end

Instance Method Details

#add_pressable_button(*btns) ⇒ Object Also known as: add_pressable_buttons

Add one or multiple button character(s) btns, which will trigger the #on_button_press methods on subscribed objects, when the given button is being pressed. Instead of passing single alphanumeric strings / symbols, you can pass hashes, whose keys will be passed to the #on_button_press methods, when any of its values are pressed. Case-sensitive.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/AdventureRL/EventHandlers/Buttons.rb', line 49

def add_pressable_button *btns
  btns.flatten.each do |button|
    if (button.is_a?(Symbol) || button.is_a?(String))
      validate_button button
      btnids = [Gosu.char_to_button_id(button), get_button_constants(button)].flatten.compact
      pressable_button = {
        name: button,
        ids:  btnids
      }
      @pressable_buttons << pressable_button  unless (@pressable_buttons.include? pressable_button)
    elsif (button.is_a?(Hash))
      button.each do |btn_name, btn_buttons|
        pressable_button = {
          name: btn_name,
          ids:  [btn_buttons].flatten.map do |btn|
            validate_button btn
            next [Gosu.char_to_button_id(btn), get_button_constants(btn)].compact
          end .flatten
        }
        @pressable_buttons << pressable_button  unless (@pressable_buttons.include? pressable_button)
      end
    end
  end
end

#button_down(btnid) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/AdventureRL/EventHandlers/Buttons.rb', line 75

def button_down btnid
  trigger(
    :button_down,
    get_semantic_button_name(btnid),
    shift:   shift_button_pressed?,
    control: control_button_pressed?,
    alt:     alt_button_pressed?
  )
end

#button_up(btnid) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/AdventureRL/EventHandlers/Buttons.rb', line 85

def button_up btnid
  trigger(
    :button_up,
    get_semantic_button_name(btnid),
    shift:   shift_button_pressed?,
    control: control_button_pressed?,
    alt:     alt_button_pressed?
  )
end

#get_pressable_buttonsObject



113
114
115
# File 'lib/AdventureRL/EventHandlers/Buttons.rb', line 113

def get_pressable_buttons
  return @pressable_buttons
end

#updateObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/AdventureRL/EventHandlers/Buttons.rb', line 95

def update
  return  unless (get_pressable_buttons.any?)
  pressed_btns = get_pressable_buttons.map do |btn|
    next btn[:name]  if (btn[:ids].any? { |id| Gosu.button_down?(id) })
    next nil
  end .compact.uniq
  return  unless (pressed_btns.any?)
  pressed_btns.each do |btn|
    trigger(
      :button_press,
      btn,
      shift:   shift_button_pressed?,
      control: control_button_pressed?,
      alt:     alt_button_pressed?
    )
  end
end