annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-Keybinding.lua @ 23:52973d00a183

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