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

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