Class: BTetrisKp::MenuItem

Inherits:
Object
  • Object
show all
Defined in:
lib/btetris_kp/gui/menuitem.rb

Overview

class representing menu item

Instance Method Summary collapse

Constructor Details

#initialize(window, text, id, callback, font, x, y) ⇒ MenuItem

Returns a new instance of MenuItem.



6
7
8
9
10
11
12
13
14
# File 'lib/btetris_kp/gui/menuitem.rb', line 6

def initialize(window, text, id, callback, font, x, y)
  @window = window
  @text = text
  @callback = callback
  @font = font
  @color = Const::MENU_ITEM_CLR
  @x = x
  @y = y + id * (@font.height + Const::FONT_GAP)
end

Instance Method Details

#clickedObject

returns true menu item is clicked (click + mouse_over)



35
36
37
# File 'lib/btetris_kp/gui/menuitem.rb', line 35

def clicked
  @callback.call if mouse_over?
end

#drawObject

draws menuitem on window (gosu)



40
41
42
# File 'lib/btetris_kp/gui/menuitem.rb', line 40

def draw
  @font.draw(@text, @x, @y, 0, 1, 1, @color)
end

#mouse_over?Boolean

returns true if mouse is over menu item

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/btetris_kp/gui/menuitem.rb', line 26

def mouse_over?
  mx = @window.mouse_x
  my = @window.mouse_y
  (mx >= @x && my >= @y) &&
  (mx <= @x + @font.text_width(@text)) &&
  (my <= @y + @font.height)
end

#updateObject

updates menu item, changes item color depending on mouse_over?



17
18
19
20
21
22
23
# File 'lib/btetris_kp/gui/menuitem.rb', line 17

def update
  if mouse_over?
    @color = Const::MENU_ITEM_MO_CLR
  else
    @color = Const::MENU_ITEM_CLR
  end
end