Class: AGL::Button

Inherits:
Object
  • Object
show all
Defined in:
lib/minigl/forms.rb

Instance Method Summary collapse

Constructor Details

#initialize(x, y, font, text, img, text_color = 0, center = true, margin_x = 0, margin_y = 0, width = nil, height = nil, &action) ⇒ Button

Returns a new instance of Button.



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
# File 'lib/minigl/forms.rb', line 5

def initialize x, y, font, text, img, text_color = 0, center = true, margin_x = 0, margin_y = 0, width = nil, height = nil, &action
	@x = x
	@y = y
	@font = font
	@text = text
	@img =
		if img; Res.imgs img, 1, 3, true
		else; nil; end
	@w =
		if img; @img[0].width
		else; width; end
	@h =
		if img; @img[0].height
		else; height; end
	if center
		@text_x = x + @w / 2
		@text_y = y + @h / 2
	else
		@text_x = x + margin_x
		@text_y = y + margin_y
	end
	@text_color = text_color
	@center = center
	@action = Proc.new &action

	@state = :up
	@img_index = 0
end

Instance Method Details

#clickObject



72
73
74
# File 'lib/minigl/forms.rb', line 72

def click
	@action.call
end

#draw(alpha = 0xff) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/minigl/forms.rb', line 76

def draw alpha = 0xff
	color = (alpha << 24) | 0xffffff
	text_color = (alpha << 24) | @text_color
	@img[@img_index].draw @x, @y, 0, 1, 1, color if @img
	if @text
		if @center
			@font.draw_rel @text, @text_x, @text_y, 0, 0.5, 0.5, 1, 1, text_color
		else
			@font.draw @text, @text_x, @text_y, 0, 1, 1, text_color
		end
	end
end

#updateObject



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
# File 'lib/minigl/forms.rb', line 34

def update
	mouse_over = Mouse.over? @x, @y, @w, @h
	mouse_press = Mouse.button_pressed? :left
	mouse_rel = Mouse.button_released? :left
	
	if @state == :up
		if mouse_over
			@img_index = 1
			@state = :over
		end
	elsif @state == :over
		if not mouse_over
			@img_index = 0
			@state = :up
		elsif mouse_press
			@img_index = 2
			@state = :down
		end
	elsif @state == :down
		if not mouse_over
			@img_index = 0
			@state = :down_out
		elsif mouse_rel
			@img_index = 0
			@state = :up
			click
		end
	elsif @state == :down_out
		if mouse_over
			@img_index = 2
			@state = :down
		elsif mouse_rel
			@img_index = 0
			@state = :up
		end
	end
end