yellowfive@57
|
1 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
2 Keybinding Widget
|
yellowfive@57
|
3 Set Keybindings in the Config UI.
|
yellowfive@57
|
4 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
5 local Type, Version = "Keybinding", 24
|
yellowfive@57
|
6 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
|
yellowfive@57
|
7 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
|
yellowfive@57
|
8
|
yellowfive@57
|
9 -- Lua APIs
|
yellowfive@57
|
10 local pairs = pairs
|
yellowfive@57
|
11
|
yellowfive@57
|
12 -- WoW APIs
|
yellowfive@57
|
13 local IsShiftKeyDown, IsControlKeyDown, IsAltKeyDown = IsShiftKeyDown, IsControlKeyDown, IsAltKeyDown
|
yellowfive@57
|
14 local CreateFrame, UIParent = CreateFrame, UIParent
|
yellowfive@57
|
15
|
yellowfive@57
|
16 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
|
yellowfive@57
|
17 -- List them here for Mikk's FindGlobals script
|
yellowfive@57
|
18 -- GLOBALS: NOT_BOUND
|
yellowfive@57
|
19
|
yellowfive@57
|
20 local wowMoP
|
yellowfive@57
|
21 do
|
yellowfive@57
|
22 local _, _, _, interface = GetBuildInfo()
|
yellowfive@57
|
23 wowMoP = (interface >= 50000)
|
yellowfive@57
|
24 end
|
yellowfive@57
|
25
|
yellowfive@57
|
26 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
27 Scripts
|
yellowfive@57
|
28 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
29
|
yellowfive@57
|
30 local function Control_OnEnter(frame)
|
yellowfive@57
|
31 frame.obj:Fire("OnEnter")
|
yellowfive@57
|
32 end
|
yellowfive@57
|
33
|
yellowfive@57
|
34 local function Control_OnLeave(frame)
|
yellowfive@57
|
35 frame.obj:Fire("OnLeave")
|
yellowfive@57
|
36 end
|
yellowfive@57
|
37
|
yellowfive@57
|
38 local function Keybinding_OnClick(frame, button)
|
yellowfive@57
|
39 if button == "LeftButton" or button == "RightButton" then
|
yellowfive@57
|
40 local self = frame.obj
|
yellowfive@57
|
41 if self.waitingForKey then
|
yellowfive@57
|
42 frame:EnableKeyboard(false)
|
yellowfive@57
|
43 self.msgframe:Hide()
|
yellowfive@57
|
44 frame:UnlockHighlight()
|
yellowfive@57
|
45 self.waitingForKey = nil
|
yellowfive@57
|
46 else
|
yellowfive@57
|
47 frame:EnableKeyboard(true)
|
yellowfive@57
|
48 self.msgframe:Show()
|
yellowfive@57
|
49 frame:LockHighlight()
|
yellowfive@57
|
50 self.waitingForKey = true
|
yellowfive@57
|
51 end
|
yellowfive@57
|
52 end
|
yellowfive@57
|
53 AceGUI:ClearFocus()
|
yellowfive@57
|
54 end
|
yellowfive@57
|
55
|
yellowfive@57
|
56 local ignoreKeys = {
|
yellowfive@57
|
57 ["BUTTON1"] = true, ["BUTTON2"] = true,
|
yellowfive@57
|
58 ["UNKNOWN"] = true,
|
yellowfive@57
|
59 ["LSHIFT"] = true, ["LCTRL"] = true, ["LALT"] = true,
|
yellowfive@57
|
60 ["RSHIFT"] = true, ["RCTRL"] = true, ["RALT"] = true,
|
yellowfive@57
|
61 }
|
yellowfive@57
|
62 local function Keybinding_OnKeyDown(frame, key)
|
yellowfive@57
|
63 local self = frame.obj
|
yellowfive@57
|
64 if self.waitingForKey then
|
yellowfive@57
|
65 local keyPressed = key
|
yellowfive@57
|
66 if keyPressed == "ESCAPE" then
|
yellowfive@57
|
67 keyPressed = ""
|
yellowfive@57
|
68 else
|
yellowfive@57
|
69 if ignoreKeys[keyPressed] then return end
|
yellowfive@57
|
70 if IsShiftKeyDown() then
|
yellowfive@57
|
71 keyPressed = "SHIFT-"..keyPressed
|
yellowfive@57
|
72 end
|
yellowfive@57
|
73 if IsControlKeyDown() then
|
yellowfive@57
|
74 keyPressed = "CTRL-"..keyPressed
|
yellowfive@57
|
75 end
|
yellowfive@57
|
76 if IsAltKeyDown() then
|
yellowfive@57
|
77 keyPressed = "ALT-"..keyPressed
|
yellowfive@57
|
78 end
|
yellowfive@57
|
79 end
|
yellowfive@57
|
80
|
yellowfive@57
|
81 frame:EnableKeyboard(false)
|
yellowfive@57
|
82 self.msgframe:Hide()
|
yellowfive@57
|
83 frame:UnlockHighlight()
|
yellowfive@57
|
84 self.waitingForKey = nil
|
yellowfive@57
|
85
|
yellowfive@57
|
86 if not self.disabled then
|
yellowfive@57
|
87 self:SetKey(keyPressed)
|
yellowfive@57
|
88 self:Fire("OnKeyChanged", keyPressed)
|
yellowfive@57
|
89 end
|
yellowfive@57
|
90 end
|
yellowfive@57
|
91 end
|
yellowfive@57
|
92
|
yellowfive@57
|
93 local function Keybinding_OnMouseDown(frame, button)
|
yellowfive@57
|
94 if button == "LeftButton" or button == "RightButton" then
|
yellowfive@57
|
95 return
|
yellowfive@57
|
96 elseif button == "MiddleButton" then
|
yellowfive@57
|
97 button = "BUTTON3"
|
yellowfive@57
|
98 elseif button == "Button4" then
|
yellowfive@57
|
99 button = "BUTTON4"
|
yellowfive@57
|
100 elseif button == "Button5" then
|
yellowfive@57
|
101 button = "BUTTON5"
|
yellowfive@57
|
102 end
|
yellowfive@57
|
103 Keybinding_OnKeyDown(frame, button)
|
yellowfive@57
|
104 end
|
yellowfive@57
|
105
|
yellowfive@57
|
106 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
107 Methods
|
yellowfive@57
|
108 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
109 local methods = {
|
yellowfive@57
|
110 ["OnAcquire"] = function(self)
|
yellowfive@57
|
111 self:SetWidth(200)
|
yellowfive@57
|
112 self:SetLabel("")
|
yellowfive@57
|
113 self:SetKey("")
|
yellowfive@57
|
114 self.waitingForKey = nil
|
yellowfive@57
|
115 self.msgframe:Hide()
|
yellowfive@57
|
116 self:SetDisabled(false)
|
yellowfive@57
|
117 self.button:EnableKeyboard(false)
|
yellowfive@57
|
118 end,
|
yellowfive@57
|
119
|
yellowfive@57
|
120 -- ["OnRelease"] = nil,
|
yellowfive@57
|
121
|
yellowfive@57
|
122 ["SetDisabled"] = function(self, disabled)
|
yellowfive@57
|
123 self.disabled = disabled
|
yellowfive@57
|
124 if disabled then
|
yellowfive@57
|
125 self.button:Disable()
|
yellowfive@57
|
126 self.label:SetTextColor(0.5,0.5,0.5)
|
yellowfive@57
|
127 else
|
yellowfive@57
|
128 self.button:Enable()
|
yellowfive@57
|
129 self.label:SetTextColor(1,1,1)
|
yellowfive@57
|
130 end
|
yellowfive@57
|
131 end,
|
yellowfive@57
|
132
|
yellowfive@57
|
133 ["SetKey"] = function(self, key)
|
yellowfive@57
|
134 if (key or "") == "" then
|
yellowfive@57
|
135 self.button:SetText(NOT_BOUND)
|
yellowfive@57
|
136 self.button:SetNormalFontObject("GameFontNormal")
|
yellowfive@57
|
137 else
|
yellowfive@57
|
138 self.button:SetText(key)
|
yellowfive@57
|
139 self.button:SetNormalFontObject("GameFontHighlight")
|
yellowfive@57
|
140 end
|
yellowfive@57
|
141 end,
|
yellowfive@57
|
142
|
yellowfive@57
|
143 ["GetKey"] = function(self)
|
yellowfive@57
|
144 local key = self.button:GetText()
|
yellowfive@57
|
145 if key == NOT_BOUND then
|
yellowfive@57
|
146 key = nil
|
yellowfive@57
|
147 end
|
yellowfive@57
|
148 return key
|
yellowfive@57
|
149 end,
|
yellowfive@57
|
150
|
yellowfive@57
|
151 ["SetLabel"] = function(self, label)
|
yellowfive@57
|
152 self.label:SetText(label or "")
|
yellowfive@57
|
153 if (label or "") == "" then
|
yellowfive@57
|
154 self.alignoffset = nil
|
yellowfive@57
|
155 self:SetHeight(24)
|
yellowfive@57
|
156 else
|
yellowfive@57
|
157 self.alignoffset = 30
|
yellowfive@57
|
158 self:SetHeight(44)
|
yellowfive@57
|
159 end
|
yellowfive@57
|
160 end,
|
yellowfive@57
|
161 }
|
yellowfive@57
|
162
|
yellowfive@57
|
163 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
164 Constructor
|
yellowfive@57
|
165 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
166
|
yellowfive@57
|
167 local ControlBackdrop = {
|
yellowfive@57
|
168 bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
|
yellowfive@57
|
169 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
|
yellowfive@57
|
170 tile = true, tileSize = 16, edgeSize = 16,
|
yellowfive@57
|
171 insets = { left = 3, right = 3, top = 3, bottom = 3 }
|
yellowfive@57
|
172 }
|
yellowfive@57
|
173
|
yellowfive@57
|
174 local function keybindingMsgFixWidth(frame)
|
yellowfive@57
|
175 frame:SetWidth(frame.msg:GetWidth() + 10)
|
yellowfive@57
|
176 frame:SetScript("OnUpdate", nil)
|
yellowfive@57
|
177 end
|
yellowfive@57
|
178
|
yellowfive@57
|
179 local function Constructor()
|
yellowfive@57
|
180 local name = "AceGUI30KeybindingButton" .. AceGUI:GetNextWidgetNum(Type)
|
yellowfive@57
|
181
|
yellowfive@57
|
182 local frame = CreateFrame("Frame", nil, UIParent)
|
yellowfive@57
|
183 local button = CreateFrame("Button", name, frame, wowMoP and "UIPanelButtonTemplate" or "UIPanelButtonTemplate2")
|
yellowfive@57
|
184
|
yellowfive@57
|
185 button:EnableMouse(true)
|
yellowfive@57
|
186 button:RegisterForClicks("AnyDown")
|
yellowfive@57
|
187 button:SetScript("OnEnter", Control_OnEnter)
|
yellowfive@57
|
188 button:SetScript("OnLeave", Control_OnLeave)
|
yellowfive@57
|
189 button:SetScript("OnClick", Keybinding_OnClick)
|
yellowfive@57
|
190 button:SetScript("OnKeyDown", Keybinding_OnKeyDown)
|
yellowfive@57
|
191 button:SetScript("OnMouseDown", Keybinding_OnMouseDown)
|
yellowfive@57
|
192 button:SetPoint("BOTTOMLEFT")
|
yellowfive@57
|
193 button:SetPoint("BOTTOMRIGHT")
|
yellowfive@57
|
194 button:SetHeight(24)
|
yellowfive@57
|
195 button:EnableKeyboard(false)
|
yellowfive@57
|
196
|
yellowfive@57
|
197 local text = button:GetFontString()
|
yellowfive@57
|
198 text:SetPoint("LEFT", 7, 0)
|
yellowfive@57
|
199 text:SetPoint("RIGHT", -7, 0)
|
yellowfive@57
|
200
|
yellowfive@57
|
201 local label = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
|
yellowfive@57
|
202 label:SetPoint("TOPLEFT")
|
yellowfive@57
|
203 label:SetPoint("TOPRIGHT")
|
yellowfive@57
|
204 label:SetJustifyH("CENTER")
|
yellowfive@57
|
205 label:SetHeight(18)
|
yellowfive@57
|
206
|
yellowfive@57
|
207 local msgframe = CreateFrame("Frame", nil, UIParent)
|
yellowfive@57
|
208 msgframe:SetHeight(30)
|
yellowfive@57
|
209 msgframe:SetBackdrop(ControlBackdrop)
|
yellowfive@57
|
210 msgframe:SetBackdropColor(0,0,0)
|
yellowfive@57
|
211 msgframe:SetFrameStrata("FULLSCREEN_DIALOG")
|
yellowfive@57
|
212 msgframe:SetFrameLevel(1000)
|
yellowfive@57
|
213 msgframe:SetToplevel(true)
|
yellowfive@57
|
214
|
yellowfive@57
|
215 local msg = msgframe:CreateFontString(nil, "OVERLAY", "GameFontNormal")
|
yellowfive@57
|
216 msg:SetText("Press a key to bind, ESC to clear the binding or click the button again to cancel.")
|
yellowfive@57
|
217 msgframe.msg = msg
|
yellowfive@57
|
218 msg:SetPoint("TOPLEFT", 5, -5)
|
yellowfive@57
|
219 msgframe:SetScript("OnUpdate", keybindingMsgFixWidth)
|
yellowfive@57
|
220 msgframe:SetPoint("BOTTOM", button, "TOP")
|
yellowfive@57
|
221 msgframe:Hide()
|
yellowfive@57
|
222
|
yellowfive@57
|
223 local widget = {
|
yellowfive@57
|
224 button = button,
|
yellowfive@57
|
225 label = label,
|
yellowfive@57
|
226 msgframe = msgframe,
|
yellowfive@57
|
227 frame = frame,
|
yellowfive@57
|
228 alignoffset = 30,
|
yellowfive@57
|
229 type = Type
|
yellowfive@57
|
230 }
|
yellowfive@57
|
231 for method, func in pairs(methods) do
|
yellowfive@57
|
232 widget[method] = func
|
yellowfive@57
|
233 end
|
yellowfive@57
|
234 button.obj = widget
|
yellowfive@57
|
235
|
yellowfive@57
|
236 return AceGUI:RegisterAsWidget(widget)
|
yellowfive@57
|
237 end
|
yellowfive@57
|
238
|
yellowfive@57
|
239 AceGUI:RegisterWidgetType(Type, Constructor, Version)
|