class Savio::Button

Attributes

buttonManager[R]
cooldownTime[R]
enforceManager[RW]
height[R]
length[R]
selected[R]
style[R]
timeLastClicked[R]
type[R]
value[RW]

Public Class Methods

buttons() click to toggle source
# File lib/savio/Button.rb, line 9
def self.buttons
  @@buttons
end
new(args = {}) click to toggle source
Calls superclass method Savio::IORenderable::new
# File lib/savio/Button.rb, line 13
def initialize(args = {})
  super(args)

  @@buttons.push(self)

  @baseColor = args[:baseColor] || Savio::Colors::White
  @selectedColor = args[:selectedColor] || Savio::Colors::Blue
  @labelActiveColor = args[:labelActiveColor] || Savio::Colors::White
  @labelInactiveColor = args[:labelInactiveColor] || Savio::Colors::White

  @cooldownTime = args[:cooldownTime] || 0.0
  @timeLastClicked = 0.0

  @selected = args[:selected] || false

  @buttonManager = args[:buttonManager] || nil
  @enforceManager = args[:enforceManager] || true

  @type = args[:type] || 'toggle'
  if @type != 'toggle' && @type != 'clicker'
    @type = 'toggle'
  end

  @style = args[:style] || 'badge'
  if @style != 'box' && @style != 'badge'
    @style = 'badge'
  end

  if @style == 'box'
    @size *= 2
    @labelActiveColor = args[:labelActiveColor] || Savio::Colors::White
    @labelInactiveColor = args[:labelInactiveColor] || Savio::Colors::Gray
  end

  @length = args[:length] || @size * 10
  @height = args[:height] || @size * 2

  @onClick = Proc.new {}

  build()
end

Public Instance Methods

add() click to toggle source
Calls superclass method Savio::IORenderable#add
# File lib/savio/Button.rb, line 168
def add()
  super()
  @nameLabel.add
  @baseCircle.add
  @selectCircle.add
  if @selected
    select()
  else
    deselect()
  end
end
baseColor=(c) click to toggle source
# File lib/savio/Button.rb, line 76
def baseColor=(c)
  @baseColor = c
  rebuild()
end
build() click to toggle source
# File lib/savio/Button.rb, line 184
def build()
  @shown = true
  case @style
  when 'badge'
    @baseCircle = Circle.new(
      x: @x, y: @y,
      radius: @size,
      color: @baseColor,
      z: @z
    )
    @selectCircle = Circle.new(
      x: @x, y: @y,
      radius: @size * 0.8,
      color: @selectedColor,
      z: @z+1
    )
    @nameLabel = Text.new(
      @displayName.to_s,
      x: @x + @size * 2, y: @y - @size * 1.5,
      size: @size * 2,
      color: @labelInactiveColor,
      z: @z
    )
    @nameLabel.y = @baseCircle.y - @baseCircle.radius / 4 - @nameLabel.height / 2
  when 'box'
    @baseCircle = Rectangle.new(
      x: @x, y: @y,
      height: @height, width: @length,
      color: @baseColor,
      z: @z
    )
    @selectCircle = Rectangle.new(
      x: @x + (@height * 0.1), y: @y + (@height * 0.1),
      height: @height - (@height * 0.2), width: @length - (@height * 0.2),
      color: @selectedColor,
      z: @z+1
    )
    @nameLabel = Text.new(
      @displayName.to_s,
      x: @x, y: @y,
      size: @size,
      color: @labelInactiveColor,
      z: @z+2
    )
    @nameLabel.x = @baseCircle.x + @baseCircle.width / 2 - @nameLabel.width / 2
    @nameLabel.y = @baseCircle.y + @baseCircle.height / 2 - @nameLabel.height / 2
  end

  if @buttonManager == nil
    if @selected
      select()
    else
      deselect()
    end
  else
    if @buttonManager.class.name != 'Savio::ButtonManager'
      raise ArgumentError, 'Given object ' + @buttonManager.to_s + ' is not a ButtonManager. Must be of type ButtonManager'
    end
    @buttonManager.addButton(self)

    if @selected
      @buttonManager.select(self)
    else
      @buttonManager.deselect(self)
    end
  end

end
buttonManager=(newManager) click to toggle source
# File lib/savio/Button.rb, line 101
def buttonManager=(newManager)
  if @buttonManager != nil
    if newManager.class.name != 'Savio::ButtonManager'
      raise ArgumentError, 'Given object ' + newManager.to_s + ' is not a ButtonManager. Must be of type ButtonManager'
    end
    @buttonManager.removeButton(self, false)
  end

  if newManager != nil
    @buttonManager = newManager
    if @buttonManager.buttons.include?(self) != true
      @buttonManager.addButton(self)
    end
  else
    @buttonManager = nil
  end
end
click() click to toggle source
# File lib/savio/Button.rb, line 180
def click()
  @onClick.call()
end
cooldownTime=(cooldown) click to toggle source
# File lib/savio/Button.rb, line 72
def cooldownTime=(cooldown)
  @cooldownTime = cooldown.to_f
end
deselect(enforce = @enforceManager) click to toggle source
# File lib/savio/Button.rb, line 143
def deselect(enforce = @enforceManager)
  if enforce == true && @buttonManager != nil
    @buttonManager.deselect(self)
  else
    @selectCircle.remove
    @nameLabel.color = @labelInactiveColor
    @selected = false
  end
end
labelColor=(c) click to toggle source
# File lib/savio/Button.rb, line 84
def labelColor=(c)
  @labelColor = c
  rebuild()
end
onClick(&proc) click to toggle source
# File lib/savio/Button.rb, line 97
def onClick(&proc)
  @onClick = proc
end
remove() click to toggle source
Calls superclass method Savio::IORenderable#remove
# File lib/savio/Button.rb, line 161
def remove()
  super()
  @nameLabel.remove
  @baseCircle.remove
  @selectCircle.remove
end
select(enforce = @enforceManager) click to toggle source
# File lib/savio/Button.rb, line 119
def select(enforce = @enforceManager)
  if Time.now.to_f - @timeLastClicked.to_f >= @cooldownTime.to_f
    @timeLastClicked = Time.now.to_f
    click()
    if enforce == true && @buttonManager != nil
      @buttonManager.select(self)
    else
      @selectCircle.add
      @nameLabel.color = @labelActiveColor
      @selected = true
      if @type == 'clicker'
        fade = Thread.new {
          @selectCircle.add
          @nameLabel.color = @labelActiveColor
          sleep(0.06)
          @selectCircle.remove
          @nameLabel.color = @labelInactiveColor
        }
        deselect(enforce)
      end
    end
  end
end
selected=(bool) click to toggle source
# File lib/savio/Button.rb, line 89
def selected=(bool)
  if bool == true
    select()
  elsif bool == false
    deselect()
  end
end
selectedColor=(c) click to toggle source
# File lib/savio/Button.rb, line 80
def selectedColor=(c)
  @selectedColor = c
  rebuild()
end
size=(size) click to toggle source
Calls superclass method Savio::IORenderable#size=
# File lib/savio/Button.rb, line 55
def size=(size)
  @length = size * 10
  @height = size * 2
  super(size)
end
style=(style) click to toggle source
# File lib/savio/Button.rb, line 65
def style=(style)
  if style == 'box' || style == 'badge'
    @style = style
    rebuild()
  end
end
toggle(enforce = @enforceManager) click to toggle source
# File lib/savio/Button.rb, line 153
def toggle(enforce = @enforceManager)
  if @selected
    deselect(enforce)
  else
    select(enforce)
  end
end
type=(newType) click to toggle source
# File lib/savio/Button.rb, line 60
def type=(newType)
  if newType == 'toggle' || newType == 'clicker'
    @type = newType
  end
end