annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-Keybinding.lua @ 59:8a4c1438dbdf

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