annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-Keybinding.lua @ 25:ac501d71c890 tip

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