/traderous

To get this branch, use:
bzr branch http://66.246.75.32/bzr/traderous
1 by Josh C
zoetrope 1.4
1
-- Class: Button
2
-- A button is a composite sprite made of two things: a label (e.g.
3
-- some text like "OK") and a background (e.g. a graphic or fill).
4
-- A button's dimensions, e.g. its clickable area, is determined by
5
-- its background's dimensions.
6
--
7
-- The x and y position of the label sprite is relative to the button's
8
-- top-left corner. The background sprite is always drawn at the button's
9
-- top-left corner. The label is always drawn on top of the background.
10
--
11
-- Extends:
12
--		<Sprite>
13
--
14
-- Event: onMouseOver
15
-- Called each frame the mouse is over the button.
16
--
17
-- Event: onMouseEnter
18
-- Called during the first frame the user moves the mouse over the button.
19
--
20
-- Event: onMouseExit
21
-- Called during the first frame the user moves the mouse off of the button.
22
--
23
-- Event: onMouseDown
24
-- Called during the first frame the user is clicking the mouse when it is over the button.
25
--
26
-- Event: onMouseUp
27
-- Called when the user releases the mouse over the button.
28
29
Button = Sprite:extend
30
{
31
	-- Property: background
32
	-- The background <Sprite> of the button.
33
34
	-- Propetrty: label
35
	-- The label <Sprite> of the button.
36
37
	-- Property: enabled
38
	-- If false, then no events will fire for this object, even just
39
	-- mouse enter/exit ones. It also forces the <mouseOver> property to
40
	-- false. This does not change the appearance of the button
41
	-- on its own; that's up to you.
42
	enabled = true,
43
44
	-- Property: mouseOver
45
	-- Tracks whether the user's mouse is over the button this frame.
46
	mouseOver = false,
47
48
	-- Property: beingClicked
49
	-- Tracks whether the user has the mouse button down and started
50
	-- clicking the mouse inside the button.
51
	beingClicked = false,
52
53
	draw = function (self, x, y)
54
		local bg = self.background
55
		local label = self.label
56
57
		x = x or self.x
58
		y = y or self.y
59
60
		if bg then bg:draw(x + bg.x, y + bg.y) end
61
		if label then label:draw(x + label.x, y + label.y) end
62
		Sprite.draw(self, x, y)
63
	end,
64
65
	update = function (self, elapsed)
66
		local bg = self.background
67
		local label = self.label
68
69
		-- keep dimensions in sync with background
70
71
		if bg then
72
			self.width = bg.width
73
			self.height = bg.height
74
		end
75
76
		-- call hooks for mouse movement events
77
78
		if self.enabled then
79
			local overBefore = self.mouseOver
80
			self.mouseOver = self:intersects(the.mouse.x, the.mouse.y)
81
82
			if self.mouseOver then self:callHook('onMouseOver') end
83
			if self.mouseOver and not overBefore then self:callHook('onMouseEnter') end
84
			if not self.mouseOver and overBefore then self:callHook('onMouseExit') end
85
86
			-- check for clicks
87
88
			if self.mouseOver and the.mouse:justPressed() then
89
				self.beingClicked = true
90
				self:callHook('onMouseDown')
91
			end
92
93
			if self.beingClicked and the.mouse:justReleased() then
94
				if self.mouseOver then
95
					self:callHook('onMouseUp')
96
				else
97
					if label and label.onMouseUp then label:onMouseUp() end
98
					if bg and bg.onMouseUp then label:onMouseUp() end
99
					if self.onMouseUp then self:onMouseUp() end
100
				end
101
102
				self.beingClicked = false
103
			end
104
		else
105
			self.mouseOver = false
106
		end
107
108
		-- let label and background update
109
110
		if bg then bg:update(elapsed) end
111
		if label then label:update(elapsed) end
112
113
		Sprite.update(self, elapsed)
114
	end,
115
116
	callHook = function (self, name)
117
		local label = self.label
118
		local bg = self.background
119
120
		if label and label[name] then label[name](label) end
121
		if bg and bg[name] then bg[name](bg) end
122
		if self[name] then self[name](self) end
123
	end
124
}