annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-Keybinding.lua @ 0:fc346da3afd9

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