-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.rb
78 lines (65 loc) · 1.28 KB
/
menu.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Menu
class MenuItem
HOVER_OFFSET = 3
def initialize (window, image, x, y, z, callback, hover_image = nil)
@window = window
@main_image = image
@hover_image = hover_image
@original_x = @x = x
@original_y = @y = y
@z = z
@callback = callback
@active_image = @main_image
end
def draw
@active_image.draw(@x, @y, @z)
end
def update
if is_mouse_hovering then
if !@hover_image.nil? then
@active_image = @hover_image
end
@x = @original_x + HOVER_OFFSET
@y = @original_y + HOVER_OFFSET
else
@active_image = @main_image
@x = @original_x
@y = @original_y
end
end
def is_mouse_hovering
mx = @window.mouse_x
my = @window.mouse_y
(mx >= @x and my >= @y) and (mx <= @x + @active_image.width) and (my <= @y + @active_image.height)
end
def clicked
if is_mouse_hovering then
@callback.call
end
end
end
def initialize (window)
@window = window
@items = Array.new
end
def add_item (image, x, y, z, callback, hover_image = nil)
item = MenuItem.new(@window, image, x, y, z, callback, hover_image)
@items << item
self
end
def draw
@items.each do |i|
i.draw
end
end
def update
@items.each do |i|
i.update
end
end
def clicked
@items.each do |i|
i.clicked
end
end
end