/ld26-basecode

To get this branch, use:
bzr branch http://9ix.org/bzr/ld26-basecode
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
-- Class: Button
-- A button is a composite sprite made of two things: a label (e.g.
-- some text like "OK") and a background (e.g. a graphic or fill).
-- A button's dimensions, e.g. its clickable area, is determined by
-- its background's dimensions.
--
-- The x and y position of the label sprite is relative to the button's
-- top-left corner. The background sprite is always drawn at the button's
-- top-left corner. The label is always drawn on top of the background.
--
-- Extends:
--		<Sprite>
--
-- Event: onMouseOver
-- Called each frame the mouse is over the button.
--
-- Event: onMouseEnter
-- Called during the first frame the user moves the mouse over the button.
--
-- Event: onMouseExit
-- Called during the first frame the user moves the mouse off of the button.
--
-- Event: onMouseDown
-- Called during the first frame the user is clicking the mouse when it is over the button.
--
-- Event: onMouseUp
-- Called when the user releases the mouse over the button.

Button = Sprite:extend
{
	-- Property: background
	-- The background <Sprite> of the button.

	-- Propetrty: label
	-- The label <Sprite> of the button.

	-- Property: enabled
	-- If false, then no events will fire for this object, even just
	-- mouse enter/exit ones. It also forces the <mouseOver> property to
	-- false. This does not change the appearance of the button
	-- on its own; that's up to you.
	enabled = true,

	-- Property: mouseOver
	-- Tracks whether the user's mouse is over the button this frame.
	mouseOver = false,

	-- Property: beingClicked
	-- Tracks whether the user has the mouse button down and started
	-- clicking the mouse inside the button.
	beingClicked = false,

	draw = function (self, x, y)
		local bg = self.background
		local label = self.label

		x = x or self.x
		y = y or self.y

		if bg then bg:draw(x + bg.x, y + bg.y) end
		if label then label:draw(x + label.x, y + label.y) end
		Sprite.draw(self, x, y)
	end,

	update = function (self, elapsed)
		local bg = self.background
		local label = self.label

		-- keep dimensions in sync with background

		if bg then
			self.width = bg.width
			self.height = bg.height
		end

		-- call hooks for mouse movement events

		if self.enabled then
			local overBefore = self.mouseOver
			self.mouseOver = self:intersects(the.mouse.x, the.mouse.y)

			if self.mouseOver then self:callHook('onMouseOver') end
			if self.mouseOver and not overBefore then self:callHook('onMouseEnter') end
			if not self.mouseOver and overBefore then self:callHook('onMouseExit') end

			-- check for clicks

			if self.mouseOver and the.mouse:justPressed() then
				self.beingClicked = true
				self:callHook('onMouseDown')
			end

			if self.beingClicked and the.mouse:justReleased() then
				if self.mouseOver then
					self:callHook('onMouseUp')
				else
					if label and label.onMouseUp then label:onMouseUp() end
					if bg and bg.onMouseUp then label:onMouseUp() end
					if self.onMouseUp then self:onMouseUp() end
				end

				self.beingClicked = false
			end
		else
			self.mouseOver = false
		end

		-- let label and background update

		if bg then bg:update(elapsed) end
		if label then label:update(elapsed) end

		Sprite.update(self, elapsed)
	end,

	callHook = function (self, name)
		local label = self.label
		local bg = self.background

		if label and label[name] then label[name](label) end
		if bg and bg[name] then bg[name](bg) end
		if self[name] then self[name](self) end
	end
}