comparison SkeletonKey/SkeletonKey.lua @ 16:cdd387d39137

filename refactor
author Nenue
date Fri, 29 Jul 2016 21:18:15 -0400
parents 32d64e42ec9b
children 500f9b2bd9ac
comparison
equal deleted inserted replaced
15:32d64e42ec9b 16:cdd387d39137
34 34
35 35
36 local BINDING_TYPE_SPECIALIZATION = 3 36 local BINDING_TYPE_SPECIALIZATION = 3
37 local BINDING_TYPE_CHARACTER = 2 37 local BINDING_TYPE_CHARACTER = 2
38 local BINDING_TYPE_GLOBAL = 1 38 local BINDING_TYPE_GLOBAL = 1
39
40
41 --- Caps Lock derivatives
42 local ACTION_SCRIPT = {
43 ['mount'] = "/script C_MountJournal.SummonByID(%d)",
44 ['macro'] = "%s",
45 ['equipset'] = "/script UseEquipmentSet(%d)",
46 ['spell'] = "/cast %s",
47 ['petaction'] = "/cast %s",
48 ['battlepet'] = SLASH_SUMMON_BATTLE_PET1 .. " %s",
49 ['item'] = "/use %s"
50 }
51
52
53 local PETACTION_SCRIPT = {
54 [PET_ACTION_MOVE_TO] = SLASH_PET_MOVE_TO1,
55 [PET_ACTION_ATTACK] = SLASH_PET_ATTACK1,
56 [PET_ACTION_FOLLOW] = SLASH_PET_FOLLOW1,
57 [PET_ACTION_WAIT] = SLASH_PET_STAY1,
58 [PET_MODE_AGGRESSIVE] = SLASH_PET_AGGRESSIVE1,
59 [PET_MODE_DEFENSIVE] = SLASH_PET_DEFENSIVE1,
60 [PET_MODE_PASSIVE] = SLASH_PET_PASSIVE1,
61 [PET_MODE_ASSIST] = SLASH_PET_ASSIST1,
62 }
63
64 local professionMappings = {
65 [5] = 3,
66 [7] = 4,
67 [9] = 5,
68 [10] = 6
69 }
70 kb.configTitle = { 39 kb.configTitle = {
71 [BINDING_TYPE_GLOBAL] = 'Global Binds', 40 [BINDING_TYPE_GLOBAL] = 'Global Binds',
72 [BINDING_TYPE_CHARACTER] = 'Character: %s', 41 [BINDING_TYPE_CHARACTER] = 'Character: %s',
73 [BINDING_TYPE_SPECIALIZATION] = 'Specialization: %s' 42 [BINDING_TYPE_SPECIALIZATION] = 'Specialization: %s'
74 } 43 }
177 return nil 146 return nil
178 end 147 end
179 end 148 end
180 149
181 150
182 --- Resolve the appropriate command and macroText for the given action parameters 151
183 kb.RegisterAction = function(type, id, name) 152
184 local macroText, macroName, command = '', '', ''
185
186 if type == 'spell' then
187 if kb.ProfessionCache[id] then
188 command = CLICK_KEYBINDER_KEY .. "profession_".. kb.ProfessionCache[id].profOffset .. '_' .. kb.ProfessionCache[id].spellNum
189 else
190 command = CLICK_KEYBINDER_KEY ..name
191 end
192 else
193 macroName = type .. ' ' .. name
194 macroText = ACTION_SCRIPT[type]:format(name)
195 local baseName, iterative = macroName, 1
196 while (macros[macroName] and macros[macroName][1] ~= macroText) do
197 print(' * cannot use|cFF00FF00', macroName, '|r"'.. (macros[macroName][1] or '') .. '"')
198 macroName = baseName .. '_' .. iterative
199 iterative = iterative + 1
200 end
201 if macroName ~= baseName then
202 print(' * Creating|cFF00FF00', macroName)
203 else
204 print(' * Re-using|cFF00FF00', macroName)
205 end
206 command = 'CLICK KeyBinderMacro:'.. macroName
207 macros[macroName] = {macroText, command }
208 end
209
210 print('RegisterAction', type, id, '->', command , macroText)
211 return macroName, macroText, command
212 end
213
214 --- Updates the current KeyBinding for the button's command
215 kb.StoreBinding = function(self, key)
216
217 if not self.command then
218 return
219 end
220
221 if key:match('[RL]SHIFT') or key:match('[RL]ALT') or key:match('[RL]CTRL') then
222 return
223 end
224 print('|cFFFFFF00received|cFFFFFF00', self:GetID(), '|cFF00FFFF', key)
225
226 local modifier = ''
227 if IsAltKeyDown() then
228 modifier = 'ALT-'
229 end
230 if IsControlKeyDown() then
231 modifier = modifier.. 'CTRL-'
232 end
233 if IsShiftKeyDown() then
234 modifier = modifier..'SHIFT-'
235 end
236 local binding = modifier..key
237
238 if key == 'ESCAPE' then
239 local keys = {GetBindingKey(self.command) }
240 --print('detected', #keys, 'bindings')
241 for i, key in pairs(keys) do
242 --print('clearing', key)
243 SetBinding(key, nil)
244 SaveBindings(GetCurrentBindingSet())
245 if kb.currentProfile.bindings[key] then
246 kb:print(L('BINDING_REMOVED', self.actionName, kb.configHeaders[db.bindMode]))
247 kb.currentProfile.bindings[key] = nil
248 end
249 if kb.currentProfile.talents[self.actionName] then
250 kb.currentProfile.talents[self.actionName] = nil
251 end
252 bindings[self.actionType][self.actionID] = nil
253 end
254 if kb.currentProfile.bound[self.command] then
255 kb.currentProfile.bound[self.command] = nil
256 --kb:print(BINDING_REMOVED:format(self.actionName, configHeaders[db.bindMode]))
257 end
258
259 bindsCommitted = false
260 self.active = false
261 else
262 if kb.SystemBinds[binding] then
263 kb.statustext:SetText(L('BINDING_FAILED_PROTECTED', key, kb.SystemBinds[binding]))
264 return
265 end
266
267
268 if self.command then
269
270 local previousKeys
271 local previousAction = GetBindingAction(binding)
272 local binding1, binding2, new1, new2
273 print(type(previousAction), previousAction)
274 if previousAction ~= "" and previousAction ~= self.command then
275 if protected[previousAction] then
276 -- bounce out if trying to use a protected key
277 kb.statustext:SetText(L('BINDING_FAILED_PROTECTED', key, GetBindingAction(binding)))
278 kb.bindingstext:SetText(nil)
279 return
280 else
281 kb:print('Discarding keybind for', previousAction)
282 -- todo: sort out retcon'd talent spells
283 end
284 end
285
286 self.pending = true
287 self.binding = binding
288
289 bindsCommitted = false
290 SetBinding(self.binding, self.command)
291 SaveBindings(GetCurrentBindingSet())
292
293 local talentInfo
294 if self.actionType == 'spell' and kb.TalentCache[self.actionID] then
295 print('conditional binding (talent = "'..self.actionName..'")')
296 talentInfo = {self.macroName, self.actionName, self.actionType, self.actionID}
297 local bindings = {GetBindingKey(self.command) }
298 for i, key in ipairs(bindings) do
299 tinsert(talentInfo, key)
300 end
301 end
302
303 for level, profile in ipairs(kb.orderedProfiles) do
304 if (level == db.bindMode) then
305 profile.bound[self.command] = true
306 if talentInfo then
307 profile.bindings[self.binding] = nil
308 else
309 profile.bindings[self.binding] = self.command
310 end
311 profile.talents[self.actionName] = talentInfo
312 else
313 profile.bindings[self.binding] = nil
314 profile.bound[self.command] = nil
315 kb.currentProfile.talents[self.actionName] = nil
316 end
317 if kb.currentProfile.talents[self.actionID] then
318 kb.currentProfile.talents[self.actionID] = nil
319 end
320 end
321
322 kb:print(L('BINDING_ASSIGNED', self.binding, self.actionName, kb.configHeaders[db.bindMode]))
323 end
324 end
325 kb.UpdateSlot(self, true)
326 KeyBinderSaveButton:Enable()
327 end
328 153
329 154
330 kb.inactiveTalentBindings = {} 155 kb.inactiveTalentBindings = {}
331 kb.ApplyTalentBinding = function(talentInfo, cache) 156 kb.ApplyTalentBinding = function(talentInfo, cache)
332 for i = 5, #talentInfo do 157 for i = 5, #talentInfo do