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