forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtogglebutton.lua
More file actions
95 lines (81 loc) · 3.61 KB
/
togglebutton.lua
File metadata and controls
95 lines (81 loc) · 3.61 KB
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
local Group = import('/lua/maui/group.lua').Group
local UIUtil = import('/lua/ui/uiutil.lua')
local LayoutHelpers = import('/lua/maui/layouthelpers.lua')
local Tooltip = import('/lua/ui/game/tooltip.lua')
--- Represents a button with multiple states.
-- A ToggleButton is a button that, when clicked, moves to the next state (or back to the first state
-- if the end of the state list has been reached) and dispatches an event.
ToggleButton = Class(Group) {
--- Create a new ToggleButton
--
-- @param parent The UI control to parent this control from.
-- @param texturePath The path, relative to the source root, to look for textures. A directory
-- for each key in the keyset is expected to be located there, each of which
-- should contain a set of textures suitable for creating a Button.
-- @param states A list of tables representing the states of this control. States are traversed
-- in the order they are given in this list. Valid keys for each state object are:
-- key: A value to uniquely identify this state. This will be passed to
-- OnStateChanged when the control transitions to this state.
-- label (optional): The string label to put on the button when in this state.
-- tooltip (optional): A tooltip id to use when the button is in this state.
-- @param defaultKey The key of the state the control should start in.
__init = function(self, parent, texturePath, states, defaultKey)
Group.__init(self, parent)
local buttonmap = {}
self.buttonmap = buttonmap
self.activeState = defaultKey
for k, v in states do
-- Closure copy
local btnKey = v.key
local btn = UIUtil.CreateButtonStd(self, texturePath .. btnKey .. "/", v.label)
if v.tooltip then
Tooltip.AddButtonTooltip(btn, v.tooltip)
end
LayoutHelpers.AtLeftTopIn(btn, self)
-- Hide this button if it is not the default.
if btnKey ~= defaultKey then
btn:Hide()
btn:DisableHitTest()
end
buttonmap[btnKey] = btn
local nextState = states[k + 1].key or states[1].key
btn.OnClick = function(btn, modifiers)
self:SetState(nextState)
end
end
self.Width:Set(buttonmap[defaultKey].Width)
self.Height:Set(buttonmap[defaultKey].Height)
end,
--- Set the control to the state with the given key.
--
-- @param stateKey The key of the state to transition to
-- @param ignoreEvent If truthy, OnStateChanged will not be called as a result of this call.
SetState = function(self, stateKey, ignoreEvent)
-- Show the appropriate button for this state.
for k, v in self.buttonmap do
v:Hide()
end
local newBtn = self.buttonmap[stateKey]
newBtn:Show()
newBtn:EnableHitTest()
-- Synthesie a mouse
Tooltip.DestroyMouseoverDisplay()
newBtn:HandleEvent({Type = "MouseEnter"})
-- Possibly call the event listener.
if not ignoreEvent then
self:OnStateChanged(stateKey)
end
end,
Disable = function(self)
for k, v in self.buttonmap do
v:Disable()
end
end,
Enable = function(self)
for k, v in self.buttonmap do
v:Enable()
end
end,
--- Called when the button is moved to a new state, via clicking or a call to SetState.
OnStateChanged = function(self, newState) end
}