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.
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.
15
-- Called each frame the mouse is over the button.
17
-- Event: onMouseEnter
18
-- Called during the first frame the user moves the mouse over the button.
21
-- Called during the first frame the user moves the mouse off of the button.
24
-- Called during the first frame the user is clicking the mouse when it is over the button.
27
-- Called when the user releases the mouse over the button.
29
Button = Sprite:extend
31
-- Property: background
32
-- The background <Sprite> of the button.
35
-- The label <Sprite> of the button.
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.
44
-- Property: mouseOver
45
-- Tracks whether the user's mouse is over the button this frame.
48
-- Property: beingClicked
49
-- Tracks whether the user has the mouse button down and started
50
-- clicking the mouse inside the button.
53
draw = function (self, x, y)
54
local bg = self.background
55
local label = self.label
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)
65
update = function (self, elapsed)
66
local bg = self.background
67
local label = self.label
69
-- keep dimensions in sync with background
73
self.height = bg.height
76
-- call hooks for mouse movement events
79
local overBefore = self.mouseOver
80
self.mouseOver = self:intersects(the.mouse.x, the.mouse.y)
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
88
if self.mouseOver and the.mouse:justPressed() then
89
self.beingClicked = true
90
self:callHook('onMouseDown')
93
if self.beingClicked and the.mouse:justReleased() then
94
if self.mouseOver then
95
self:callHook('onMouseUp')
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
102
self.beingClicked = false
105
self.mouseOver = false
108
-- let label and background update
110
if bg then bg:update(elapsed) end
111
if label then label:update(elapsed) end
113
Sprite.update(self, elapsed)
116
callHook = function (self, name)
117
local label = self.label
118
local bg = self.background
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