annotate SkeletonKey/SkeletonKey.lua @ 17:500f9b2bd9ac

- for RegisterAction, use a function table instead of that if/then rats nest - consequently pet bar actions work now - unlocalize bindings data - activate keyslot input manually instead of on mouse over - activate checkbox to keep input mode active - dynamic buttons update in real time for petaction/talent/profession spells
author Nenue
date Sat, 30 Jul 2016 00:08:00 -0400
parents cdd387d39137
children 67db6b712bf3
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@0 46
Nenue@14 47 kb.inactiveTalentBindings = {}
Nenue@6 48 kb.configHeaders = {}
Nenue@6 49 kb.loadedProfiles = {}
Nenue@6 50 kb.orderedProfiles = {}
Nenue@6 51 kb.buttons = {}
Nenue@6 52 kb.macros = {}
Nenue@17 53 kb.bindings = {}
Nenue@15 54 kb.petFrames = {} -- pet data is slightly delayed, their buttons are indexed here so they can be refreshed
Nenue@15 55 kb.talentFrames = {}
Nenue@15 56 kb.professionFrames = {}
Nenue@0 57
Nenue@14 58 -- these are sent to plugin
Nenue@14 59
Nenue@14 60
Nenue@14 61 local db
Nenue@5 62 local bindHeader, currentHeader = '', ''
Nenue@5 63 local specID, specGlobalID, specName, specDesc, specTexture, characterHeader = 0, 0, 'SPEC_NAME', 'SPEC_DESCRIPTION', 'Interface\\ICONS\\INV_Misc_QuestionMark', 'PLAYER_NAME'
Nenue@5 64 local classHeader, className, classID = '', '', 0
Nenue@5 65 local bindsCommitted = true
Nenue@5 66 local forceButtonUpdate = false
Nenue@5 67
Nenue@5 68 --- Control handles
Nenue@0 69 local saveButton, restoreButton, clearButton
Nenue@0 70
Nenue@5 71 --- Returns conflicting assignment and binding profiles for use in displaying confirmations
Nenue@6 72 kb.IsCommandBound = function(self, command)
Nenue@6 73 local isAssigned, assignedBy = false, db.bindMode
Nenue@6 74 local isBound, boundBy = false, db.bindMode
Nenue@5 75
Nenue@5 76
Nenue@5 77 command = command or self.command
Nenue@6 78 for i = 1, #kb.orderedProfiles do
Nenue@6 79 local tier = kb.orderedProfiles[i]
Nenue@6 80 if i ~= db.bindMode then
Nenue@5 81
Nenue@5 82 if tier.commands[command] then
Nenue@5 83 isAssigned = true
Nenue@5 84 assignedBy = i
Nenue@5 85 end
Nenue@5 86 if tier.bound[command] then
Nenue@5 87 isBound = true
Nenue@5 88 boundBy = i
Nenue@5 89 end
Nenue@5 90
Nenue@5 91
Nenue@5 92 --print(' *', configHeaders[i], tier.commands[command], tier.bound[command])
Nenue@5 93
Nenue@5 94 if isAssigned and isBound then
Nenue@0 95 break
Nenue@0 96 end
Nenue@0 97 end
Nenue@5 98
Nenue@0 99 end
Nenue@5 100
Nenue@17 101 print('|cFFFFFF00IsCommandBound:|r', command,'|r [profile:', db.bindMode .. ']', isAssigned, isBound, assignedBy, boundBy)
Nenue@5 102 return isAssigned, isBound, assignedBy, boundBy
Nenue@0 103 end
Nenue@0 104
Nenue@5 105 local talentSpellHardCodes = {
Nenue@5 106 [109248] = 'Binding Shot',
Nenue@5 107 }
Nenue@5 108
Nenue@0 109 --- Returns a value for use with Texture:SetDesaturated()
Nenue@6 110 kb.BindingIsLocked = function(key)
Nenue@0 111 local success = false
Nenue@6 112 for i = 1, db.bindMode-1 do
Nenue@6 113 local tier = kb.orderedProfiles[i]
Nenue@0 114 if tier.bindings[key] then
Nenue@0 115 success = true
Nenue@0 116 break
Nenue@0 117 end
Nenue@0 118 end
Nenue@0 119 return success
Nenue@0 120 end
Nenue@0 121
Nenue@0 122 --- Translates GetBindingKey() results into a printable string.
Nenue@6 123 kb.BindingString = function(...)
Nenue@0 124 local stack = {}
Nenue@0 125 for i = 1, select('#', ...) do
Nenue@0 126 local key = select(i, ...)
Nenue@5 127 stack[i] = key:gsub('SHIFT', 's'):gsub('ALT', 'a'):gsub('CTRL', 'c'):gsub('SPACE', 'Sp'):gsub('BUTTON', 'M '):gsub('NUMPAD', '# ')
Nenue@0 128 end
Nenue@0 129
Nenue@0 130 if #stack >= 1 then
Nenue@0 131 return table.concat(stack, ',')
Nenue@0 132 else
Nenue@0 133 return nil
Nenue@0 134 end
Nenue@0 135 end
Nenue@0 136
Nenue@5 137
Nenue@5 138
Nenue@5 139
Nenue@0 140
Nenue@5 141 kb.Command = function(args, editor)
Nenue@5 142 if args:match("import") then
Nenue@5 143 kb.ImportCommmit(args)
Nenue@5 144 return
Nenue@5 145 elseif args:match("scan") then
Nenue@5 146 kb.ImportScan(args)
Nenue@5 147 kb.ui()
Nenue@5 148 return
Nenue@5 149 elseif args:match("load") then
Nenue@5 150 kb:ApplyAllBindings()
Nenue@0 151 return
Nenue@0 152 end
Nenue@0 153
Nenue@5 154 if db.showUI then
Nenue@5 155 db.showUI = false
Nenue@5 156 kb:print('|cFFFFFF00KeyBinds|r trace, |cFFFF0000OFF|r.')
Nenue@5 157 kb:Hide()
Nenue@5 158 else
Nenue@1 159 db.showUI = true
Nenue@5 160 kb:print('|cFFFFFF00KeyBinds|r trace, |cFF00FF00ON|r.')
Nenue@5 161 end
Nenue@5 162 kb.ui(true)
Nenue@5 163 end
Nenue@5 164
Nenue@5 165 kb.InitProfile = function(profile, prototype)
Nenue@5 166 if not profile then
Nenue@5 167 profile = {}
Nenue@5 168 end
Nenue@5 169 if prototype then
Nenue@5 170 print('appplying prototype', prototype)
Nenue@5 171 for k,v in pairs(prototype) do
Nenue@5 172 if not profile[k] then
Nenue@5 173 profile[k] = v
Nenue@5 174 end
Nenue@5 175 end
Nenue@0 176 end
Nenue@0 177
Nenue@5 178 profile.bound = profile.bound or {}
Nenue@5 179 profile.buttons = profile.buttons or {}
Nenue@5 180 profile.commands = profile.commands or {}
Nenue@5 181 profile.bindings = profile.bindings or {}
Nenue@5 182 profile.macros = profile.macros or {}
Nenue@5 183 profile.talents = profile.talents or {}
Nenue@5 184 return profile
Nenue@5 185 end
Nenue@5 186
Nenue@5 187 kb.ResetProfile = function(profile, prototype)
Nenue@6 188 if profile == kb.currentProfile then
Nenue@5 189 for i, button in pairs(buttons) do
Nenue@5 190 kb.ReleaseSlot(button)
Nenue@5 191 end
Nenue@5 192 end
Nenue@5 193 table.wipe(profile)
Nenue@5 194 kb.InitProfile(profile, prototype)
Nenue@5 195 end
Nenue@5 196
Nenue@5 197
Nenue@5 198
Nenue@5 199 --- Handles constructing spec profiles as they are selected
Nenue@5 200
Nenue@5 201
Nenue@5 202 --- Obtains profile data or creates the necessary tables
Nenue@5 203 kb.SelectProfileSet = function(name)
Nenue@5 204
Nenue@6 205 local defaultMode
Nenue@5 206 --- General info
Nenue@5 207 classHeader, className, classID = UnitClass('player')
Nenue@5 208 print('|cFF00FF00profile:|r', name)
Nenue@5 209 print('|cFF00FF00class:|r', UnitClass('player'))
Nenue@5 210
Nenue@6 211 defaultMode = BINDING_TYPE_GLOBAL
Nenue@17 212 if db[name] then
Nenue@6 213 defaultMode = BINDING_TYPE_CHARACTER
Nenue@17 214 if db[name][kb.specInfo.id] then
Nenue@17 215 defaultMode = BINDING_TYPE_SPECIALIZATION
Nenue@17 216 end
Nenue@5 217 end
Nenue@5 218
Nenue@17 219 db[name] = kb.InitProfile(db[name],
Nenue@17 220 {
Nenue@17 221 classHeader = classHeader,
Nenue@17 222 className = className,
Nenue@17 223 classID = classID
Nenue@17 224 })
Nenue@17 225 db[name][kb.specInfo.id] = kb.InitProfile(db[name][kb.specInfo.id],
Nenue@17 226 {
Nenue@17 227 specID = kb.specInfo.id,
Nenue@17 228 specName = kb.specInfo.name
Nenue@17 229 })
Nenue@5 230
Nenue@17 231 kb.loadedProfiles[BINDING_TYPE_GLOBAL] = db
Nenue@17 232 kb.loadedProfiles[BINDING_TYPE_CHARACTER] = db[name]
Nenue@17 233 kb.loadedProfiles[BINDING_TYPE_SPECIALIZATION] = db[name][kb.specInfo.id]
Nenue@17 234 kb.orderedProfiles = {db, db[name], db[name][kb.specInfo.id]}
Nenue@17 235
Nenue@15 236 if (not db.bindMode) or (not kb.configTitle[db.bindMode]) then
Nenue@6 237 print('fixing bad bindMode value, was', db.bindMode)
Nenue@6 238 db.bindMode = defaultMode
Nenue@5 239 end
Nenue@5 240
Nenue@5 241
Nenue@5 242 print(BINDING_TYPE_GLOBAL)
Nenue@6 243 kb.configHeaders[BINDING_TYPE_GLOBAL] = kb.configTitle[BINDING_TYPE_GLOBAL]
Nenue@6 244 kb.configHeaders[BINDING_TYPE_CHARACTER] = kb.configTitle[BINDING_TYPE_CHARACTER]:format(UnitName('player', true))
Nenue@10 245 kb.configHeaders[BINDING_TYPE_SPECIALIZATION] = kb.configTitle[BINDING_TYPE_SPECIALIZATION]:format(kb.specInfo.name)
Nenue@5 246
Nenue@5 247
Nenue@6 248 setmetatable(kb.loadedProfiles[BINDING_TYPE_GLOBAL], {__tostring =function() return kb.configHeaders[BINDING_TYPE_GLOBAL] end})
Nenue@6 249 setmetatable(kb.loadedProfiles[BINDING_TYPE_CHARACTER], {__tostring =function() return kb.configHeaders[BINDING_TYPE_CHARACTER] end})
Nenue@6 250 setmetatable(kb.loadedProfiles[BINDING_TYPE_SPECIALIZATION], {__tostring =function() return kb.configHeaders[BINDING_TYPE_SPECIALIZATION] end})
Nenue@5 251
Nenue@6 252 print('|cFF00FF00bindMode:|r', db.bindMode)
Nenue@6 253 kb.currentProfile = kb.loadedProfiles[db.bindMode]
Nenue@17 254 kb.currentHeader = kb.configHeaders[db.bindMode]
Nenue@5 255 end
Nenue@5 256
Nenue@5 257 local scrollCache = {}
Nenue@5 258 kb.SelectTab = function(self)
Nenue@6 259 scrollCache[db.bindMode] = kb.scrollOffset
Nenue@5 260 db.bindMode = self:GetID()
Nenue@6 261 kb.currentProfile = kb.loadedProfiles[self:GetID()]
Nenue@17 262 kb.currentHeader = kb.configHeaders[db.bindMode]
Nenue@6 263 kb.scrollOffset = scrollCache[db.bindMode] or 0
Nenue@5 264 kb.ui(true)
Nenue@5 265 end
Nenue@5 266
Nenue@5 267 kb.RevertBindings = function()
Nenue@5 268 -- todo: reversion code
Nenue@5 269 end
Nenue@5 270
Nenue@5 271 kb.ConfirmBindings = function()
Nenue@5 272 kb.ApplyAllBindings()
Nenue@5 273 kb.ui()
Nenue@5 274 end
Nenue@5 275
Nenue@5 276
Nenue@5 277
Nenue@5 278
Nenue@5 279 --- post ADDON_LOADED
Nenue@5 280 kb.variables = function()
Nenue@17 281 SkeletonKeyDB = kb.InitProfile(SkeletonKeyDB, {})
Nenue@5 282 kb.db = SkeletonKeyDB
Nenue@5 283 kb.playerName = UnitName('player')
Nenue@5 284 kb.playerRealm = SelectedRealmName()
Nenue@5 285 kb.profileName = kb.playerRealm .. '_' .. kb.playerName
Nenue@5 286 db = kb.db
Nenue@5 287
Nenue@17 288 kb.UpdateSpecInfo()
Nenue@17 289 kb.UpdateTalentInfo()
Nenue@5 290 kb.SelectProfileSet(kb.profileName)
Nenue@6 291 -- todo: redo import checking
Nenue@6 292
Nenue@15 293 kb.UpdateSystemBinds()
Nenue@5 294 kb.ApplyAllBindings()
Nenue@5 295
Nenue@5 296 kb.ui(true)
Nenue@0 297 end
Nenue@0 298
Nenue@1 299
Nenue@5 300 kb.wrap = function(module)
Nenue@5 301 kb.modules = kb.modules or {}
Nenue@5 302 tinsert(kb.modules, module)
Nenue@0 303 end
Nenue@0 304
Nenue@5 305 -- Volatiles Access
Nenue@17 306 kb.GetBindings = function() return kb.bindings end
Nenue@17 307 kb.GetButtons = function() return kb.buttons end
Nenue@6 308 kb.GetCharacterProfile = function () return kb.loadedProfiles[BINDING_TYPE_CHARACTER] end
Nenue@6 309 kb.GetGlobalProfile = function () return kb.loadedProfiles[BINDING_TYPE_GLOBAL] end
Nenue@6 310 kb.GetSpecProfile = function () return kb.loadedProfiles[BINDING_TYPE_SPECIALIZATION] end
Nenue@0 311
Nenue@0 312
Nenue@5 313 SLASH_SKB1 = "/skb"
Nenue@5 314 SLASH_SKB2 = "/skeletonkey"
Nenue@5 315 SlashCmdList.SKB = kb.Command