class Core::GUI::Button

Button to be used in menus. Executes the given lambda when clicked

Attributes

background[RW]
proc[RW]

Public Class Methods

new(x, y, w, h, text, proc, bg=true, align=:center, text_height=h) click to toggle source
Calls superclass method Core::GUI::Element::new
# File lib/gui/button.rb, line 9
def initialize(x, y, w, h, text, proc, bg=true, align=:center, text_height=h)
  super(x, y, w, h)
  if bg
    @bg = Core.sprite("gui/button_background")
  end
  @hi = Core.sprite("gui/button_highlight", true)
  @font = Core.font(Core::DEFAULT_FONT, text_height)
  @text = text
  @proc = proc
  @selected = false
  @background = bg
  @align = align
  @enabled = true
  @fw = @font.text_width(@text)
end

Public Instance Methods

disable() click to toggle source
# File lib/gui/button.rb, line 30
def disable
  @enabled = false
end
draw() click to toggle source
# File lib/gui/button.rb, line 47
def draw
  case @align
  when :center
    @font.draw(@text, @x + @xoff + (@w/2) - (@fw/2), @y + @yoff + (@h/9), Core::GUI_Z + 10 + @zoff, 1, 1, Gosu::Color::BLACK)
  when :left
    @font.draw(@text, @x + @xoff + 4, @y + @yoff + (@h/9), Core::GUI_Z + 10 + @zoff, 1, 1, Gosu::Color::BLACK)
  when :right
    @font.draw(@text, @x + @xoff + (@w-@fw), @y + @yoff + (@h/9), Core::GUI_Z + 10 + @zoff, 1, 1, Gosu::Color::BLACK)
  end
  if @background
    color = Gosu::Color.new(255, 255, 255, 255)
    if !@enabled
      color.saturation = 125
    end
    @bg.draw(@x + @xoff, @y + @yoff, Core::GUI_Z + 9 + @zoff, @w/@bg.width.to_f, @h/@bg.height.to_f, color)
  end
  if @selected and @enabled
    @hi.draw(@x + @xoff, @y + @yoff, Core::GUI_Z + 11 + @zoff, @w/@hi.width.to_f, @h/@hi.height.to_f, 0xff999999, :additive)
  end
end
enable() click to toggle source
# File lib/gui/button.rb, line 33
def enable
  @enabled = true
end
hovered?() click to toggle source
# File lib/gui/button.rb, line 24
def hovered?
  return @selected
end
toggle() click to toggle source
# File lib/gui/button.rb, line 27
def toggle
  @enabled = !@enabled
end
update() click to toggle source
# File lib/gui/button.rb, line 36
def update
  @selected = Core.inside?(Core.window.mouse_x, Core.window.mouse_y, @x+@xoff, @y+@yoff, @x+@w+@xoff, @y+@h+@yoff)
  if @selected and Core.window.pressed?(Gosu::MsLeft)
    if @enabled
      @@sound.play(0.25)
      @proc.call
    else
      @@error.play
    end
  end
end