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

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