lib/savio/Button.rb in savio-0.1.1 vs lib/savio/Button.rb in savio-0.1.2

- old
+ new

@@ -1,11 +1,11 @@ module Savio class Button include IORenderable attr_accessor :value, :enforceManager - attr_reader :selected, :buttonManager + attr_reader :selected, :buttonManager, :style, :length, :height, :type, :timeLastClicked, :cooldownTime @@buttons = [] def self.buttons @@buttons end @@ -19,32 +19,61 @@ @baseColor = args[:baseColor] || '#F5F5F5' @selectedColor = args[:selectedColor] || '#00B3EC' @labelColor = args[:labelColor] || '#F5F5F5' + @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 + @labelColor = args[:baseColor] || '#01042A' + end + @length = args[:length] || @size * 10 + @height = args[:height] || @size * 1.2 + @onClick = Proc.new {} build() end + def size=(size) + @length = size * 10 + @height = size * 1.2 + super(size) + end def type=(newType) if newType == 'toggle' || newType == 'clicker' @type = newType end end + def style=(style) + if style == 'box' || style == 'badge' + @style = style + rebuild() + end + end + def cooldownTime=(cooldown) + @cooldownTime = cooldown.to_f + end + def baseColor=(c) @baseColor = c rebuild() end def selectedColor=(c) @@ -54,10 +83,18 @@ def labelColor=(c) @labelColor = c rebuild() end + def selected=(bool) + if bool == true + select() + elsif bool == false + deselect() + end + end + def onClick(&proc) @onClick = proc end def buttonManager=(newManager) @@ -77,25 +114,26 @@ @buttonManager = nil end end def select(enforce = @enforceManager) - click() - if enforce == true && @buttonManager != nil - @buttonManager.select(self) - else - @selectCircle.add - @selected = true - if @type == 'clicker' + 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 + @selected = true if @type == 'clicker' fade = Thread.new { @selectCircle.add sleep(0.06) @selectCircle.remove } + deselect(enforce) end - deselect(enforce) end end end def deselect(enforce = @enforceManager) @@ -138,29 +176,51 @@ @onClick.call() end def build() @shown = true - - @nameLabel = Text.new( - @displayName.to_s, - x: @x + @size * 2, y: @y - @size * 1.5, - size: @size * 2, - color: @labelColor, - z: @z - ) - @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 - ) + 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: @labelColor, + z: @z + ) + 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: @labelColor, + z: @z+2 + ) + end if @buttonManager == nil if @selected select() else