comparison Libs/AceGUI-3.0/widgets/AceGUIWidget-Keybinding.lua @ 0:c6ff7ba0e8f6

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