diff SkeletonKey/BindingsFrame.lua @ 52:81a7c71c4483

- fixed safety override - fixed pet journal drag/drop - fixed confirmation cancel button
author Nenue
date Fri, 19 Aug 2016 10:46:36 -0400
parents 1aba8a6fd4a9
children 9eebce04e69b
line wrap: on
line diff
--- a/SkeletonKey/BindingsFrame.lua	Fri Aug 19 09:29:23 2016 -0400
+++ b/SkeletonKey/BindingsFrame.lua	Fri Aug 19 10:46:36 2016 -0400
@@ -2,9 +2,21 @@
 -- BindingsFrame.lua
 -- Created: 7/28/2016 3:39 PM
 -- %file-revision%
--- Code piecing the interface together
+-- Handles the arrangement of and interaction with the SkeletonKey frame
+--[=[
+  -- some useful texture paths
+  [[Interface\PaperDollInfoFrame\UI-GearManager-Undo]]
+  [[Interface\PetPaperDollFrame\UI-PetHappiness]]
+  [[Interface\RAIDFRAME\ReadyCheck-Waiting]]
+  [[Interface\RAIDFRAME\ReadyCheck-Read]]
+  [[Interface\RAIDFRAME\ReadyCheck-NotReady]]
+  [[Interface\TradeSkillFrame\UI-TradeSkill-LinkButton]]
+  [[Interface\TUTORIALFRAME\UI-TUTORIAL-FRAME]]
+  [[Interface\UI-TutorialFrame-QuestGiver\UI-TutorialFrame-QuestGray]]
+--]=]
 
-local kb, print = LibStub("LibKraken").register(KeyBinder, 'KeySlot')
+local kb, print = LibStub("LibKraken").register(KeyBinder, 'BindingsUI')
+local L = kb.L
 local BINDS_PER_ROW = 2
 local BINDING_TYPE_SPECIALIZATION = 3
 local BINDING_TYPE_CHARACTER = 2
@@ -32,50 +44,46 @@
   [BINDING_TYPE_GLOBAL] = {0, 1, 1}
 }
 
---[=[
-  -- some useful texture paths
-  [[Interface\PaperDollInfoFrame\UI-GearManager-Undo]]
-  [[Interface\PetPaperDollFrame\UI-PetHappiness]]
-  [[Interface\RAIDFRAME\ReadyCheck-Waiting]]
-  [[Interface\RAIDFRAME\ReadyCheck-Read]]
-  [[Interface\RAIDFRAME\ReadyCheck-NotReady]]
-  [[Interface\TradeSkillFrame\UI-TradeSkill-LinkButton]]
-  [[Interface\TUTORIALFRAME\UI-TUTORIAL-FRAME]]
-  [[Interface\UI-TutorialFrame-QuestGiver\UI-TutorialFrame-QuestGray]]
---]=]
-local lastFolder
-local addT = function(frame, key, folder, name)
-  folder = folder or lastFolder
-  frame[key] = frame:CreateTexture()
-  frame[key]:SetTexture('Interface\\' .. folder .. '\\' .. name)
-  frame[key]:SetPoint('TOPLEFT')
-  lastFolder = folder
+local match, strupper = string.match, string.upper
+local tremove, tinsert, ipairs, pairs, unpack = table.remove, table.insert, ipairs, pairs, unpack
+local tonumber, tostring = tonumber, tostring
+local GetCursorInfo, ClearCursor, ResetCursor = GetCursorInfo, ClearCursor, ResetCursor
+local IsShiftKeyDown, IsControlKeyDown, IsAltKeyDown = IsShiftKeyDown, IsControlKeyDown, IsAltKeyDown
+local GetBindingAction, GetBindingKey, GetCurrentBindingSet = GetBindingAction, GetBindingKey, GetCurrentBindingSet
+local SetBinding, SaveBindings = SetBinding, SaveBindings
+
+kb.ProcessInput = function(key)
+  if key == 'ESCAPE' then
+    kb.DeactivateSlot(kb.saveTarget)
+    kb.ui()
+    return
+  end
+
+  if (match(key, '[RL]SHIFT') or match(key, '[RL]ALT') or match(key, '[RL]CTRL')) then
+    return
+  end
+
+  if kb.saveTarget then
+    if kb.SaveSlot(kb.saveTarget, key) then
+      if not (kb.db.stickyMode or kb.db.hoverInput) then
+        kb.DeactivateSlot(kb.saveTarget)
+      end
+    end
+    kb.ui()
+  end
 end
 
+local lastFolder
 local restingAlpha = 0.7
 local fadeTime, fadeDelay = .30, 0.15
 local saveButton
 
 local KeyButton_OnKeyDown = function(self, key)
-  kb.saveTarget.border:SetColorTexture(1,1,1,1)
+  local st = kb.saveTarget
+  kb.ProcessInput(key)
 end
 local KeyButton_OnKeyUp = function(self, key)
-  kb.UpdateSlot(kb.saveTarget)
-  if key == 'ESCAPE' then
-    kb.DeactivateSlot(kb.saveTarget)
-    kb.ui()
-    return
-  end
-  if key:match('[RL]SHIFT') or key:match('[RL]ALT') or key:match('[RL]CTRL') then
-    return
-  elseif kb.saveTarget then
-    if kb.SaveSlot(kb.saveTarget, key) then
-      if not (kb.db.stickyMode or kb.db.hoverInput) then
-        kb.DeactivateSlot(kb.saveTarget)
-      end
-      kb.ui()
-    end
-  end
+  local st = kb.saveTarget
 end
 
 local KeyButton_OnClick = function(self, click)
@@ -97,13 +105,8 @@
   elseif click == 'RightButton' then
     kb.ReleaseSlot(self)
     kb.ui()
-  elseif kb.saveTarget then
-    if kb.SaveSlot(kb.saveTarget, string.upper(click)) then
-      if not (kb.db.stickyMode or kb.db.hoverInput) then
-        kb.DeactivateSlot(kb.saveTarget)
-      end
-      kb.ui()
-    end
+  else
+    kb.ProcessInput(strupper(click))
   end
 end
 
@@ -213,15 +216,10 @@
   frame.header:SetText(header)
 
   frame:SetScript('OnClick', function(self)
+    kb.db[dbKey] = self:GetChecked()
     if callback then
       callback(self)
     end
-    kb.db[dbKey] = self:GetChecked()
-    if not kb.db[dbKey] then
-      if kb.saveTarget then
-        kb.DeactivateSlot(kb.saveTarget)
-      end
-    end
     kb.ui()
   end)
 
@@ -360,7 +358,11 @@
     function() OpenAllBags() end,
     "Interface\\BUTTONS\\UI-MicroButtonCharacter-Up", {0, 1, .4, 1})
 
-  KeyBinder_CheckButton(KeyBinderStickyMode, 'Enabled', 'Disabled', 'stickyMode', 'Keep input active after receiving a key.', nil, 'Sticky:')
+  KeyBinder_CheckButton(KeyBinderStickyMode, 'Enabled', 'Disabled', 'stickyMode', 'Keep input active after receiving a key.', function()
+    if kb.saveTarget and (not kb.db.stickyMode) then
+      kb.DeactivateSlot(kb.saveTarget)
+    end
+  end, 'Sticky:')
   KeyBinder_CheckButton(KeyBinderHoverInput, 'MouseOver', 'Click', 'hoverInput', 'Enable key input when the cursor is over a binding slot.', nil, 'Bind by:')
   KeyBinder_CheckButton(KeyBinderProtectBindings, 'Block', 'Allow', 'protectBlizKeys', 'Allow overwriting Blizzard UI bindings.', nil, 'Safety:')
 
@@ -491,6 +493,140 @@
   kb.talentsPushed = nil
 end
 
+--- Associate processed input with the given slot's metadata
+kb.SaveSlot = function(self, key)
+
+  if not self.command 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
+
+  -- check for system bindings
+  --bprint('|cFFFFFF00SaveBind|r', 'protectKeys', kb.db.protectBlizKeys)
+  if kb.db.protectBlizKeys and kb.SystemBindings[binding] then
+    kb.statustext:SetText(L('BINDING_FAILED_PROTECTED', binding, kb.SystemBindings[binding]))
+    kb.bindingstext:SetText(nil)
+    return false
+  end
+
+  -- check for other keys
+  local previousCommand = GetBindingAction(binding)
+  if previousCommand ~= "" and previousCommand ~= self.command then
+    local actionType, actionID, name = kb.GetCommandAction(previousCommand)
+    if actionType then
+      local keys = {GetBindingKey(previousCommand) }
+      local  i = 1
+      while keys[i] do
+        if keys[i] == binding then
+          tremove(keys, i)
+          kb.UpdateBindingsCache(actionType, actionID, keys)
+          break
+        end
+        i = i + 1
+      end
+    end
+  end
+
+  local currentHotKeys = {GetBindingKey(self.command)}
+  local found
+  for i, key in ipairs(currentHotKeys) do
+    if key == binding then
+      found = true
+      kb:print('hotkey already assigned')
+    end
+  end
+  if not found then
+    tinsert(currentHotKeys, 1, binding)
+    kb.UpdateBindingsCache(self.actionType, self.actionID, currentHotKeys)
+  end
+
+
+
+  print('SetBinding', binding, self.command)
+  SetBinding(binding, self.command)
+  SaveBindings(GetCurrentBindingSet())
+  self.binding = binding
+
+  local talentInfo
+  if self.actionType == 'spell' and kb.TalentCache[self.actionID] then
+    print('store dynamicType talent')
+    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 == kb.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.currentHeader))
+  kb.UpdateSlot(self, true)
+  return true
+end
+
+
+kb.UnbindSlot = function(self)
+
+  local keys = {GetBindingKey(self.command) }
+  if #keys >= 1 then
+    kb.UpdateBindingsCache(self.actionType, self.actionID, {})
+  end
+
+
+  --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.currentHeader))
+      kb.currentProfile.bindings[key] = nil
+    end
+    if kb.currentProfile.talents[self.actionName] then
+      kb.currentProfile.talents[self.actionName] = nil
+    end
+
+    kb.bindings[tostring(self.actionType)..'_'..tostring(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
+  kb.UpdateSlot(self, true)
+end
+
 kb.AcceptAssignment = function(self, ...)
   local popup = StaticPopupDialogs["SKELETONKEY_CONFIRM_ASSIGN_SLOT"]
   local source = kb.  loadedProfiles[popup.oldProfile]
@@ -500,3 +636,15 @@
   ClearCursor()
   ResetCursor()
 end
+
+--- Add to blizzard interfaces
+StaticPopupDialogs["SKELETONKEY_CONFIRM_ASSIGN_SLOT"] = {
+  text = "Confirm moving an assigned command.",
+  button1 = OKAY,
+  button2 = CANCEL,
+  timeout = 0,
+  whileDead = 1,
+  showAlert = 1,
+  OnAccept = kb.AcceptAssignment,
+  OnCancel = function() kb:SetScript('OnMouseWheel', KeyBinder_OnMouseWheel) end
+}
\ No newline at end of file