diff SkeletonKey/KeySlot.lua @ 16:cdd387d39137

filename refactor
author Nenue
date Fri, 29 Jul 2016 21:18:15 -0400
parents 32d64e42ec9b
children 500f9b2bd9ac
line wrap: on
line diff
--- a/SkeletonKey/KeySlot.lua	Fri Jul 29 03:27:15 2016 -0400
+++ b/SkeletonKey/KeySlot.lua	Fri Jul 29 21:18:15 2016 -0400
@@ -2,7 +2,7 @@
 -- KeySlot.lua
 -- Created: 7/28/2016 11:26 PM
 -- %file-revision%
--- All the internal slot logic is kept here
+-- Code dealing with the slot button innards; they are invoked by frame script and should only chain to Set/Release
 
 local kb, print = LibStub('LibKraken').register(KeyBinder, 'Slot')
 local CURSOR_SPELLSLOT, CURSOR_BOOKTYPE, CURSOR_PETACTION
@@ -11,7 +11,6 @@
 local BORDER_ASSIGNED = {1,1,1,1}
 local BORDER_DYNAMIC = {1,1,0,1}
 local BORDER_PENDING = {1,0.5,0,1 }
-
 local BUTTON_HEADERS = {
   ['spell'] = SPELLS,
   ['macro'] = MACRO,
@@ -444,6 +443,119 @@
 end
 
 
+--- Updates the current KeyBinding for the button's command
+kb.SaveSlot = function(self, key)
+
+  if not self.command then
+    return
+  end
+
+  if key:match('[RL]SHIFT') or key:match('[RL]ALT') or key:match('[RL]CTRL') then
+    return
+  end
+  print('|cFFFFFF00received|cFFFFFF00', self:GetID(), '|cFF00FFFF', key)
+
+  local modifier = ''
+  if IsAltKeyDown() then
+    modifier = 'ALT-'
+  end
+  if IsControlKeyDown() then
+    modifier = modifier.. 'CTRL-'
+  end
+  if IsShiftKeyDown() then
+    modifier = modifier..'SHIFT-'
+  end
+  local binding = modifier..key
+
+  if key == 'ESCAPE' then
+    local keys = {GetBindingKey(self.command) }
+    --print('detected', #keys, 'bindings')
+    for i, key in pairs(keys) do
+      --print('clearing', key)
+      SetBinding(key, nil)
+      SaveBindings(GetCurrentBindingSet())
+      if kb.currentProfile.bindings[key] then
+        kb:print(L('BINDING_REMOVED', self.actionName, kb.configHeaders[db.bindMode]))
+        kb.currentProfile.bindings[key] = nil
+      end
+      if kb.currentProfile.talents[self.actionName] then
+        kb.currentProfile.talents[self.actionName] = nil
+      end
+      bindings[self.actionType][self.actionID] = nil
+    end
+    if kb.currentProfile.bound[self.command] then
+      kb.currentProfile.bound[self.command] = nil
+      --kb:print(BINDING_REMOVED:format(self.actionName, configHeaders[db.bindMode]))
+    end
+
+    self.active = false
+  else
+    if kb.SystemBinds[binding] then
+      kb.statustext:SetText(L('BINDING_FAILED_PROTECTED', key, kb.SystemBinds[binding]))
+      return
+    end
+
+
+    if self.command then
+
+      local previousKeys
+      local previousAction = GetBindingAction(binding)
+      local binding1, binding2, new1, new2
+      print(type(previousAction), previousAction)
+      if previousAction ~= "" and previousAction ~= self.command then
+        if protected[previousAction] then
+          -- bounce out if trying to use a protected key
+          kb.statustext:SetText(L('BINDING_FAILED_PROTECTED', key, GetBindingAction(binding)))
+          kb.bindingstext:SetText(nil)
+          return
+        else
+          kb:print('Discarding keybind for', previousAction)
+          -- todo: sort out retcon'd talent spells
+        end
+      end
+
+      self.binding = binding
+
+      SetBinding(self.binding, self.command)
+      SaveBindings(GetCurrentBindingSet())
+
+      local talentInfo
+      if self.actionType == 'spell' and kb.TalentCache[self.actionID] then
+        print('conditional binding (talent = "'..self.actionName..'")')
+        talentInfo = {self.macroName, self.actionName, self.actionType, self.actionID}
+        local bindings = {GetBindingKey(self.command) }
+        for i, key in ipairs(bindings) do
+          tinsert(talentInfo, key)
+        end
+      end
+
+      for level, profile in ipairs(kb.orderedProfiles) do
+        if (level == db.bindMode) then
+          profile.bound[self.command] = true
+          if talentInfo then
+            profile.bindings[self.binding] = nil
+          else
+            profile.bindings[self.binding] = self.command
+          end
+          profile.talents[self.actionName] = talentInfo
+        else
+          profile.bindings[self.binding] = nil
+          profile.bound[self.command] = nil
+          kb.currentProfile.talents[self.actionName] = nil
+        end
+        if kb.currentProfile.talents[self.actionID] then
+          kb.currentProfile.talents[self.actionID] = nil
+        end
+      end
+
+      kb:print(L('BINDING_ASSIGNED', self.binding, self.actionName, kb.configHeaders[db.bindMode]))
+    end
+  end
+  kb.UpdateSlot(self, true)
+  KeyBinderSaveButton:Enable()
+end
+
+
 
 --- Add to blizzard interfaces
 StaticPopupDialogs["SKELETONKEY_CONFIRM_ASSIGN_SLOT"] = {