Class: Core::GUI::Button

Inherits:
Element show all
Defined in:
lib/gui/button.rb

Overview

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

Direct Known Subclasses

ImageButton

Constant Summary collapse

@@sound =
Core::Sample.new("click1")
@@error =
Core::Sample.new("error1")

Instance Attribute Summary collapse

Attributes inherited from Element

#h, #w, #x, #xoff, #y, #yoff, #zoff

Instance Method Summary collapse

Methods inherited from Element

#remove?

Constructor Details

#initialize(x, y, w, h, text, proc, bg = true, align = :center, text_height = h) ⇒ Button

Returns a new instance of Button.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#backgroundObject

Returns the value of attribute background.



6
7
8
# File 'lib/gui/button.rb', line 6

def background
  @background
end

#procObject

Returns the value of attribute proc.



6
7
8
# File 'lib/gui/button.rb', line 6

def proc
  @proc
end

Instance Method Details

#disableObject



30
31
32
# File 'lib/gui/button.rb', line 30

def disable
  @enabled = false
end

#drawObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

#enableObject



33
34
35
# File 'lib/gui/button.rb', line 33

def enable
  @enabled = true
end

#hovered?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/gui/button.rb', line 24

def hovered?
  return @selected
end

#toggleObject



27
28
29
# File 'lib/gui/button.rb', line 27

def toggle
  @enabled = !@enabled
end

#updateObject



36
37
38
39
40
41
42
43
44
45
46
# 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