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

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