Class: Conjuration::Button

Inherits:
GameObject show all
Defined in:
lib/conjuration/ui/button.rb

Instance Method Summary collapse

Methods inherited from GameObject

create, #in_bounds?, #mouse_down, #mouse_entered, #mouse_exited, #mouse_position, #mouse_up, #on_mouse_down, #on_mouse_up

Methods included from Attributes

included, #inspect

Constructor Details

#initialize(x:, y:, width: 200, height: 40, text:, font_size: 20, color: Gosu::Color::BLACK, background_color: Gosu::Color::WHITE, hover: { color: Gosu::Color::BLACK, background_color: Gosu::Color::GRAY, }, &action) ⇒ Button

Returns a new instance of Button.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/conjuration/ui/button.rb', line 3

def initialize(
  x:,
  y:,
  width: 200,
  height: 40,
  text:,
  font_size: 20,
  color: Gosu::Color::BLACK,
  background_color: Gosu::Color::WHITE,
  hover: {
    color: Gosu::Color::BLACK,
    background_color: Gosu::Color::GRAY,
  },
  &action
)
  super(x:, y:, width:, height:)

  @text = text
  @font = Gosu::Font.new(font_size)
  @color = color
  @background_color = background_color
  @hover = hover
  @action = action
end

Instance Method Details

#draw(window, scene) ⇒ Object



28
29
30
31
32
# File 'lib/conjuration/ui/button.rb', line 28

def draw(window, scene)
  window.draw_rect(x, y, width, height, hovering? ? @hover[:background_color] : @background_color)

  @font.draw_text(@text, x + width/2 - @font.text_width(@text)/2, y + height/2 - @font.height/2, 1, 1, 1, hovering? ? @hover[:color] : @color)
end

#on_click(scene, key) ⇒ Object



34
35
36
# File 'lib/conjuration/ui/button.rb', line 34

def on_click(scene, key)
  @action.call(scene) if key == InputManager::LEFT_CLICK
end