Mercurial > wow > icu
comparison Libs/AceGUI-3.0/widgets/AceGUIWidget-Keybinding.lua @ 0:98c6f55e6619
First commit
| author | Xiiph |
|---|---|
| date | Sat, 05 Feb 2011 16:45:02 +0100 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:98c6f55e6619 |
|---|---|
| 1 --[[----------------------------------------------------------------------------- | |
| 2 Keybinding Widget | |
| 3 Set Keybindings in the Config UI. | |
| 4 -------------------------------------------------------------------------------]] | |
| 5 local Type, Version = "Keybinding", 22 | |
| 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 self.button:EnableKeyboard(false) | |
| 112 end, | |
| 113 | |
| 114 -- ["OnRelease"] = nil, | |
| 115 | |
| 116 ["SetDisabled"] = function(self, disabled) | |
| 117 self.disabled = disabled | |
| 118 if disabled then | |
| 119 self.button:Disable() | |
| 120 self.label:SetTextColor(0.5,0.5,0.5) | |
| 121 else | |
| 122 self.button:Enable() | |
| 123 self.label:SetTextColor(1,1,1) | |
| 124 end | |
| 125 end, | |
| 126 | |
| 127 ["SetKey"] = function(self, key) | |
| 128 if (key or "") == "" then | |
| 129 self.button:SetText(NOT_BOUND) | |
| 130 self.button:SetNormalFontObject("GameFontNormal") | |
| 131 else | |
| 132 self.button:SetText(key) | |
| 133 self.button:SetNormalFontObject("GameFontHighlight") | |
| 134 end | |
| 135 end, | |
| 136 | |
| 137 ["GetKey"] = function(self) | |
| 138 local key = self.button:GetText() | |
| 139 if key == NOT_BOUND then | |
| 140 key = nil | |
| 141 end | |
| 142 return key | |
| 143 end, | |
| 144 | |
| 145 ["SetLabel"] = function(self, label) | |
| 146 self.label:SetText(label or "") | |
| 147 if (label or "") == "" then | |
| 148 self.alignoffset = nil | |
| 149 self:SetHeight(24) | |
| 150 else | |
| 151 self.alignoffset = 30 | |
| 152 self:SetHeight(44) | |
| 153 end | |
| 154 end, | |
| 155 } | |
| 156 | |
| 157 --[[----------------------------------------------------------------------------- | |
| 158 Constructor | |
| 159 -------------------------------------------------------------------------------]] | |
| 160 | |
| 161 local ControlBackdrop = { | |
| 162 bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", | |
| 163 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", | |
| 164 tile = true, tileSize = 16, edgeSize = 16, | |
| 165 insets = { left = 3, right = 3, top = 3, bottom = 3 } | |
| 166 } | |
| 167 | |
| 168 local function keybindingMsgFixWidth(frame) | |
| 169 frame:SetWidth(frame.msg:GetWidth() + 10) | |
| 170 frame:SetScript("OnUpdate", nil) | |
| 171 end | |
| 172 | |
| 173 local function Constructor() | |
| 174 local name = "AceGUI30KeybindingButton" .. AceGUI:GetNextWidgetNum(Type) | |
| 175 | |
| 176 local frame = CreateFrame("Frame", nil, UIParent) | |
| 177 local button = CreateFrame("Button", name, frame, "UIPanelButtonTemplate2") | |
| 178 | |
| 179 button:EnableMouse(true) | |
| 180 button:RegisterForClicks("AnyDown") | |
| 181 button:SetScript("OnEnter", Control_OnEnter) | |
| 182 button:SetScript("OnLeave", Control_OnLeave) | |
| 183 button:SetScript("OnClick", Keybinding_OnClick) | |
| 184 button:SetScript("OnKeyDown", Keybinding_OnKeyDown) | |
| 185 button:SetScript("OnMouseDown", Keybinding_OnMouseDown) | |
| 186 button:SetPoint("BOTTOMLEFT") | |
| 187 button:SetPoint("BOTTOMRIGHT") | |
| 188 button:SetHeight(24) | |
| 189 button:EnableKeyboard(false) | |
| 190 | |
| 191 local text = button:GetFontString() | |
| 192 text:SetPoint("LEFT", 7, 0) | |
| 193 text:SetPoint("RIGHT", -7, 0) | |
| 194 | |
| 195 local label = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight") | |
| 196 label:SetPoint("TOPLEFT") | |
| 197 label:SetPoint("TOPRIGHT") | |
| 198 label:SetJustifyH("CENTER") | |
| 199 label:SetHeight(18) | |
| 200 | |
| 201 local msgframe = CreateFrame("Frame", nil, UIParent) | |
| 202 msgframe:SetHeight(30) | |
| 203 msgframe:SetBackdrop(ControlBackdrop) | |
| 204 msgframe:SetBackdropColor(0,0,0) | |
| 205 msgframe:SetFrameStrata("FULLSCREEN_DIALOG") | |
| 206 msgframe:SetFrameLevel(1000) | |
| 207 | |
| 208 local msg = msgframe:CreateFontString(nil, "OVERLAY", "GameFontNormal") | |
| 209 msg:SetText("Press a key to bind, ESC to clear the binding or click the button again to cancel.") | |
| 210 msgframe.msg = msg | |
| 211 msg:SetPoint("TOPLEFT", 5, -5) | |
| 212 msgframe:SetScript("OnUpdate", keybindingMsgFixWidth) | |
| 213 msgframe:SetPoint("BOTTOM", button, "TOP") | |
| 214 msgframe:Hide() | |
| 215 | |
| 216 local widget = { | |
| 217 button = button, | |
| 218 label = label, | |
| 219 msgframe = msgframe, | |
| 220 frame = frame, | |
| 221 alignoffset = 30, | |
| 222 type = Type | |
| 223 } | |
| 224 for method, func in pairs(methods) do | |
| 225 widget[method] = func | |
| 226 end | |
| 227 button.obj = widget | |
| 228 | |
| 229 return AceGUI:RegisterAsWidget(widget) | |
| 230 end | |
| 231 | |
| 232 AceGUI:RegisterWidgetType(Type, Constructor, Version) |
