class Umbra::ButtonGroup

This is not a visual class or a widget. This class allows us to attach several RadioButtons to it, so it can maintain which one is the selected one. It also allows for assigning of commands to be executed whenever a button is pressed, akin to binding to the fire of the button, except that one would not have to bind to each button, but only once here.

@example

group = ButtonGroup.new
group.add(r1).add(r2).add(r3)
group.command(somelabel) do |grp, label| label.text = grp.value; end

Attributes

elements[R]

Array of buttons that have been added.

name[RW]

name for group, can be used in messages

value[R]

the value of the radio button that is selected. To get the button itself, use selection.

Public Class Methods

new(name="Buttongroup") click to toggle source

@param name [String] a name which is used more for documenting/debugging.

# File lib/umbra/buttongroup.rb, line 35
def initialize name="Buttongroup"
  @elements = []
  @hash     = {}
  @name     = name
end

Public Instance Methods

add(e) click to toggle source

Add a radio button to the group. @note Maybe we should allow adding multiple, and alias to add_widget. @param e [RadioButton] button to add.

# File lib/umbra/buttongroup.rb, line 44
def add e
  @elements << e
  @hash[e.value] = e
  e.button_group=(self)
  self
end
command(*args, &block) click to toggle source

install trigger to call whenever a value is updated @public called by user components

# File lib/umbra/buttongroup.rb, line 74
def command *args, &block
  @commands ||= []
  @args ||= []
  @commands << block
  @args << args
end
remove(e) click to toggle source

remove button from group

# File lib/umbra/buttongroup.rb, line 52
def remove e
  @elements.delete e
  @hash.delete e.value
  self
end
select(button) click to toggle source

select the given button or value. This may be called by user programs to programmatically select a button

# File lib/umbra/buttongroup.rb, line 82
def select button
  if button.is_a? String
    ;
  else
    button = button.value
  end
  self.value = button
end
selected?(val) click to toggle source

@param val [String, RadioButton] value of a button, or Button itself to check if selected. @return [true or false] for whether the given value or button is the selected one

# File lib/umbra/buttongroup.rb, line 65
def selected? val
  if val.is_a? String
    @value == val
  else
    @hash[@value] == val
  end
end
selection() click to toggle source

@return the radiobutton that is selected

# File lib/umbra/buttongroup.rb, line 59
def selection
  @hash[@value]
end
value=(value) click to toggle source

whenever a radio button is pressed, it updates the value of the group with it;s value. since only one is true at a time.

# File lib/umbra/buttongroup.rb, line 92
def value=(value)
  @value = value
  # 2018-04-02 - need to repaint all the radio buttons so they become off
  @elements.each {|e| e.repaint_required = true }

  return unless @commands
  @commands.each_with_index do |comm, ix|
    comm.call(self, *@args[ix]) unless comm.nil?
  end
end