annotate SkeletonKey/SkeletonKey.lua @ 19:67db6b712bf3

- option checkbutton literals are stored by enclosure - detect and save any old keybindings when a slot is assigned (anything that begins with 'CLICK KeyBinder*' is ours) - mouseover mode key input will stay active after leaving a button - button border flashes when a non-modifier key is pressed
author Nenue
date Sat, 30 Jul 2016 19:08:11 -0400
parents 500f9b2bd9ac
children 73df13211b22
rev   line source
Nenue@0 1 --------------------------------------------
Nenue@5 2 -- SkeletonKey
Nenue@5 3 -- Krakyn-Mal'Ganis
Nenue@0 4 -- @project-revision@ @project-hash@
Nenue@0 5 -- @file-revision@ @file-hash@
Nenue@0 6 -- Created: 6/16/2016 3:47 AM
Nenue@0 7 --------------------------------------------
Nenue@0 8 -- kb
Nenue@5 9 -- .StoreBinding(button, key) bind current keystroke to command
Nenue@5 10 -- .GetSlot(index) return display slot
Nenue@5 11 -- .SetSlot(button, command, name, icon) assign display slot
Nenue@5 12 -- .ReleaseSlot(button) clear button command
Nenue@5 13 -- .UpdateSlot(button) update button contents
Nenue@5 14 -- .SelectProfile(name) set profile character
Nenue@5 15 -- .ApplyBindings(bindings) walk table with SetBinding()
Nenue@0 16
Nenue@5 17 local _
Nenue@5 18 local kb, print = LibStub("LibKraken").register(KeyBinder)
Nenue@14 19 kb.L = setmetatable({}, {
Nenue@17 20 __call = function(t, k, ...) return format(t[k] or k, ...) end
Nenue@14 21 })
Nenue@14 22 local L = kb.L
Nenue@7 23
Nenue@5 24 --- Caps Lock literals
Nenue@5 25 local CLASS_ICON_TEXTURE = "Interface\\GLUES\\CHARACTERCREATE\\UI-CHARACTERCREATE-CLASSES"
Nenue@14 26 L.BINDING_ASSIGNED = '|cFF00FF00%s|r assigned to |cFFFFFF00%s|r (%s).'
Nenue@14 27 L.BINDING_REMOVED = '|cFFFFFF00%s|r (|cFF00FFFF%s|r) unbound.'
Nenue@14 28 L.BINDING_FAILED_PROTECTED = '|cFFFF4400Unable to use |r|cFF00FF00%s|r|cFFFF4400 (currently |cFFFFFF00%s|r|cFFFF4400)|r'
Nenue@14 29
Nenue@14 30
Nenue@5 31 local BINDING_TYPE_SPECIALIZATION = 3
Nenue@5 32 local BINDING_TYPE_CHARACTER = 2
Nenue@5 33 local BINDING_TYPE_GLOBAL = 1
Nenue@6 34 kb.configTitle = {
Nenue@17 35 [BINDING_TYPE_GLOBAL] = L('Global Binds'),
Nenue@17 36 [BINDING_TYPE_CHARACTER] = L('Character: %%s'),
Nenue@17 37 [BINDING_TYPE_SPECIALIZATION] = L('Specialization: %%s')
Nenue@0 38 }
Nenue@6 39 kb.configDescription = {
Nenue@17 40 [BINDING_TYPE_GLOBAL] = L('The bindings are applied globally.'),
Nenue@17 41 [BINDING_TYPE_CHARACTER] = L('Applied when you log onto this character.'),
Nenue@17 42 [BINDING_TYPE_SPECIALIZATION] = L('Applied when you select this specialization.'),
Nenue@5 43 }
Nenue@5 44
Nenue@0 45
Nenue@19 46 kb.SystemBindings = {}
Nenue@19 47 kb.ActionTypes = {}
Nenue@19 48 kb.TalentBindings = {}
Nenue@0 49
Nenue@6 50 kb.configHeaders = {}
Nenue@6 51 kb.loadedProfiles = {}
Nenue@6 52 kb.orderedProfiles = {}
Nenue@6 53 kb.buttons = {}
Nenue@6 54 kb.macros = {}
Nenue@17 55 kb.bindings = {}
Nenue@15 56 kb.petFrames = {} -- pet data is slightly delayed, their buttons are indexed here so they can be refreshed
Nenue@15 57 kb.talentFrames = {}
Nenue@15 58 kb.professionFrames = {}
Nenue@0 59
Nenue@14 60 -- these are sent to plugin
Nenue@14 61
Nenue@14 62
Nenue@14 63 local db
Nenue@5 64 local bindHeader, currentHeader = '', ''
Nenue@5 65 local specID, specGlobalID, specName, specDesc, specTexture, characterHeader = 0, 0, 'SPEC_NAME', 'SPEC_DESCRIPTION', 'Interface\\ICONS\\INV_Misc_QuestionMark', 'PLAYER_NAME'
Nenue@5 66 local classHeader, className, classID = '', '', 0
Nenue@5 67 local bindsCommitted = true
Nenue@5 68 local forceButtonUpdate = false
Nenue@5 69
Nenue@5 70 --- Control handles
Nenue@0 71 local saveButton, restoreButton, clearButton
Nenue@0 72
Nenue@5 73 --- Returns conflicting assignment and binding profiles for use in displaying confirmations
Nenue@6 74 kb.IsCommandBound = function(self, command)
Nenue@6 75 local isAssigned, assignedBy = false, db.bindMode
Nenue@6 76 local isBound, boundBy = false, db.bindMode
Nenue@5 77
Nenue@5 78
Nenue@5 79 command = command or self.command
Nenue@6 80 for i = 1, #kb.orderedProfiles do
Nenue@6 81 local tier = kb.orderedProfiles[i]
Nenue@6 82 if i ~= db.bindMode then
Nenue@5 83
Nenue@5 84 if tier.commands[command] then
Nenue@5 85 isAssigned = true
Nenue@5 86 assignedBy = i
Nenue@5 87 end
Nenue@5 88 if tier.bound[command] then
Nenue@5 89 isBound = true
Nenue@5 90 boundBy = i
Nenue@5 91 end
Nenue@5 92
Nenue@5 93
Nenue@5 94 --print(' *', configHeaders[i], tier.commands[command], tier.bound[command])
Nenue@5 95
Nenue@5 96 if isAssigned and isBound then
Nenue@0 97 break
Nenue@0 98 end
Nenue@0 99 end
Nenue@5 100
Nenue@0 101 end
Nenue@5 102
Nenue@17 103 print('|cFFFFFF00IsCommandBound:|r', command,'|r [profile:', db.bindMode .. ']', isAssigned, isBound, assignedBy, boundBy)
Nenue@5 104 return isAssigned, isBound, assignedBy, boundBy
Nenue@0 105 end
Nenue@0 106
Nenue@5 107 local talentSpellHardCodes = {
Nenue@5 108 [109248] = 'Binding Shot',
Nenue@5 109 }
Nenue@5 110
Nenue@0 111 --- Returns a value for use with Texture:SetDesaturated()
Nenue@6 112 kb.BindingIsLocked = function(key)
Nenue@0 113 local success = false
Nenue@6 114 for i = 1, db.bindMode-1 do
Nenue@6 115 local tier = kb.orderedProfiles[i]
Nenue@0 116 if tier.bindings[key] then
Nenue@0 117 success = true
Nenue@0 118 break
Nenue@0 119 end
Nenue@0 120 end
Nenue@0 121 return success
Nenue@0 122 end
Nenue@0 123
Nenue@0 124 --- Translates GetBindingKey() results into a printable string.
Nenue@6 125 kb.BindingString = function(...)
Nenue@0 126 local stack = {}
Nenue@0 127 for i = 1, select('#', ...) do
Nenue@0 128 local key = select(i, ...)
Nenue@5 129 stack[i] = key:gsub('SHIFT', 's'):gsub('ALT', 'a'):gsub('CTRL', 'c'):gsub('SPACE', 'Sp'):gsub('BUTTON', 'M '):gsub('NUMPAD', '# ')
Nenue@0 130 end
Nenue@0 131
Nenue@0 132 if #stack >= 1 then
Nenue@0 133 return table.concat(stack, ',')
Nenue@0 134 else
Nenue@0 135 return nil
Nenue@0 136 end
Nenue@0 137 end
Nenue@0 138
Nenue@5 139
Nenue@5 140
Nenue@5 141
Nenue@0 142
Nenue@5 143 kb.Command = function(args, editor)
Nenue@5 144 if args:match("import") then
Nenue@5 145 kb.ImportCommmit(args)
Nenue@5 146 return
Nenue@5 147 elseif args:match("scan") then
Nenue@5 148 kb.ImportScan(args)
Nenue@5 149 kb.ui()
Nenue@5 150 return
Nenue@5 151 elseif args:match("load") then
Nenue@5 152 kb:ApplyAllBindings()
Nenue@0 153 return
Nenue@0 154 end
Nenue@0 155
Nenue@5 156 if db.showUI then
Nenue@5 157 db.showUI = false
Nenue@5 158 kb:print('|cFFFFFF00KeyBinds|r trace, |cFFFF0000OFF|r.')
Nenue@5 159 kb:Hide()
Nenue@5 160 else
Nenue@1 161 db.showUI = true
Nenue@5 162 kb:print('|cFFFFFF00KeyBinds|r trace, |cFF00FF00ON|r.')
Nenue@5 163 end
Nenue@5 164 kb.ui(true)
Nenue@5 165 end
Nenue@5 166
Nenue@5 167 kb.InitProfile = function(profile, prototype)
Nenue@5 168 if not profile then
Nenue@5 169 profile = {}
Nenue@5 170 end
Nenue@5 171 if prototype then
Nenue@5 172 print('appplying prototype', prototype)
Nenue@5 173 for k,v in pairs(prototype) do
Nenue@5 174 if not profile[k] then
Nenue@5 175 profile[k] = v
Nenue@5 176 end
Nenue@5 177 end
Nenue@0 178 end
Nenue@0 179
Nenue@5 180 profile.bound = profile.bound or {}
Nenue@5 181 profile.buttons = profile.buttons or {}
Nenue@5 182 profile.commands = profile.commands or {}
Nenue@5 183 profile.bindings = profile.bindings or {}
Nenue@5 184 profile.macros = profile.macros or {}
Nenue@5 185 profile.talents = profile.talents or {}
Nenue@5 186 return profile
Nenue@5 187 end
Nenue@5 188
Nenue@5 189 kb.ResetProfile = function(profile, prototype)
Nenue@6 190 if profile == kb.currentProfile then
Nenue@5 191 for i, button in pairs(buttons) do
Nenue@5 192 kb.ReleaseSlot(button)
Nenue@5 193 end
Nenue@5 194 end
Nenue@5 195 table.wipe(profile)
Nenue@5 196 kb.InitProfile(profile, prototype)
Nenue@5 197 end
Nenue@5 198
Nenue@5 199
Nenue@5 200
Nenue@5 201 --- Handles constructing spec profiles as they are selected
Nenue@5 202
Nenue@5 203
Nenue@5 204 --- Obtains profile data or creates the necessary tables
Nenue@5 205 kb.SelectProfileSet = function(name)
Nenue@5 206
Nenue@6 207 local defaultMode
Nenue@5 208 --- General info
Nenue@5 209 classHeader, className, classID = UnitClass('player')
Nenue@5 210 print('|cFF00FF00profile:|r', name)
Nenue@5 211 print('|cFF00FF00class:|r', UnitClass('player'))
Nenue@5 212
Nenue@6 213 defaultMode = BINDING_TYPE_GLOBAL
Nenue@17 214 if db[name] then
Nenue@6 215 defaultMode = BINDING_TYPE_CHARACTER
Nenue@17 216 if db[name][kb.specInfo.id] then
Nenue@17 217 defaultMode = BINDING_TYPE_SPECIALIZATION
Nenue@17 218 end
Nenue@5 219 end
Nenue@5 220
Nenue@17 221 db[name] = kb.InitProfile(db[name],
Nenue@17 222 {
Nenue@17 223 classHeader = classHeader,
Nenue@17 224 className = className,
Nenue@17 225 classID = classID
Nenue@17 226 })
Nenue@17 227 db[name][kb.specInfo.id] = kb.InitProfile(db[name][kb.specInfo.id],
Nenue@17 228 {
Nenue@17 229 specID = kb.specInfo.id,
Nenue@17 230 specName = kb.specInfo.name
Nenue@17 231 })
Nenue@5 232
Nenue@17 233 kb.loadedProfiles[BINDING_TYPE_GLOBAL] = db
Nenue@17 234 kb.loadedProfiles[BINDING_TYPE_CHARACTER] = db[name]
Nenue@17 235 kb.loadedProfiles[BINDING_TYPE_SPECIALIZATION] = db[name][kb.specInfo.id]
Nenue@17 236 kb.orderedProfiles = {db, db[name], db[name][kb.specInfo.id]}
Nenue@17 237
Nenue@15 238 if (not db.bindMode) or (not kb.configTitle[db.bindMode]) then
Nenue@6 239 print('fixing bad bindMode value, was', db.bindMode)
Nenue@6 240 db.bindMode = defaultMode
Nenue@5 241 end
Nenue@5 242
Nenue@5 243
Nenue@5 244 print(BINDING_TYPE_GLOBAL)
Nenue@6 245 kb.configHeaders[BINDING_TYPE_GLOBAL] = kb.configTitle[BINDING_TYPE_GLOBAL]
Nenue@6 246 kb.configHeaders[BINDING_TYPE_CHARACTER] = kb.configTitle[BINDING_TYPE_CHARACTER]:format(UnitName('player', true))
Nenue@10 247 kb.configHeaders[BINDING_TYPE_SPECIALIZATION] = kb.configTitle[BINDING_TYPE_SPECIALIZATION]:format(kb.specInfo.name)
Nenue@5 248
Nenue@5 249
Nenue@6 250 setmetatable(kb.loadedProfiles[BINDING_TYPE_GLOBAL], {__tostring =function() return kb.configHeaders[BINDING_TYPE_GLOBAL] end})
Nenue@6 251 setmetatable(kb.loadedProfiles[BINDING_TYPE_CHARACTER], {__tostring =function() return kb.configHeaders[BINDING_TYPE_CHARACTER] end})
Nenue@6 252 setmetatable(kb.loadedProfiles[BINDING_TYPE_SPECIALIZATION], {__tostring =function() return kb.configHeaders[BINDING_TYPE_SPECIALIZATION] end})
Nenue@5 253
Nenue@6 254 print('|cFF00FF00bindMode:|r', db.bindMode)
Nenue@6 255 kb.currentProfile = kb.loadedProfiles[db.bindMode]
Nenue@17 256 kb.currentHeader = kb.configHeaders[db.bindMode]
Nenue@5 257 end
Nenue@5 258
Nenue@5 259 local scrollCache = {}
Nenue@5 260 kb.SelectTab = function(self)
Nenue@6 261 scrollCache[db.bindMode] = kb.scrollOffset
Nenue@5 262 db.bindMode = self:GetID()
Nenue@6 263 kb.currentProfile = kb.loadedProfiles[self:GetID()]
Nenue@17 264 kb.currentHeader = kb.configHeaders[db.bindMode]
Nenue@6 265 kb.scrollOffset = scrollCache[db.bindMode] or 0
Nenue@5 266 kb.ui(true)
Nenue@5 267 end
Nenue@5 268
Nenue@5 269 kb.RevertBindings = function()
Nenue@5 270 -- todo: reversion code
Nenue@5 271 end
Nenue@5 272
Nenue@5 273 kb.ConfirmBindings = function()
Nenue@5 274 kb.ApplyAllBindings()
Nenue@5 275 kb.ui()
Nenue@5 276 end
Nenue@5 277
Nenue@5 278
Nenue@5 279
Nenue@5 280
Nenue@5 281 --- post ADDON_LOADED
Nenue@5 282 kb.variables = function()
Nenue@17 283 SkeletonKeyDB = kb.InitProfile(SkeletonKeyDB, {})
Nenue@5 284 kb.db = SkeletonKeyDB
Nenue@5 285 kb.playerName = UnitName('player')
Nenue@5 286 kb.playerRealm = SelectedRealmName()
Nenue@5 287 kb.profileName = kb.playerRealm .. '_' .. kb.playerName
Nenue@5 288 db = kb.db
Nenue@5 289
Nenue@17 290 kb.UpdateSpecInfo()
Nenue@17 291 kb.UpdateTalentInfo()
Nenue@5 292 kb.SelectProfileSet(kb.profileName)
Nenue@6 293 -- todo: redo import checking
Nenue@6 294
Nenue@15 295 kb.UpdateSystemBinds()
Nenue@5 296 kb.ApplyAllBindings()
Nenue@5 297
Nenue@5 298 kb.ui(true)
Nenue@0 299 end
Nenue@0 300
Nenue@1 301
Nenue@5 302 kb.wrap = function(module)
Nenue@5 303 kb.modules = kb.modules or {}
Nenue@5 304 tinsert(kb.modules, module)
Nenue@0 305 end
Nenue@0 306
Nenue@5 307 -- Volatiles Access
Nenue@17 308 kb.GetBindings = function() return kb.bindings end
Nenue@17 309 kb.GetButtons = function() return kb.buttons end
Nenue@6 310 kb.GetCharacterProfile = function () return kb.loadedProfiles[BINDING_TYPE_CHARACTER] end
Nenue@6 311 kb.GetGlobalProfile = function () return kb.loadedProfiles[BINDING_TYPE_GLOBAL] end
Nenue@6 312 kb.GetSpecProfile = function () return kb.loadedProfiles[BINDING_TYPE_SPECIALIZATION] end
Nenue@0 313
Nenue@0 314
Nenue@5 315 SLASH_SKB1 = "/skb"
Nenue@5 316 SLASH_SKB2 = "/skeletonkey"
Nenue@5 317 SlashCmdList.SKB = kb.Command