view SkeletonKey/UI.lua @ 12:8f8917d111c9

- spec tab texture
author Nenue
date Thu, 28 Jul 2016 17:45:17 -0400
parents f9c18c0a6377
children eeec4a600064
line wrap: on
line source
-- KrakTool
-- UI.lua
-- Created: 7/28/2016 3:39 PM
-- %file-revision%
--

local kb, print = LibStub("LibKraken").register(KeyBinder, 'KeySlot')
local BINDS_PER_ROW = 2
local BUTTON_HSPACING = 128
local BUTTON_SPACING = 4
local BUTTON_PADDING = 12
local BINDING_TYPE_SPECIALIZATION = 3
local BINDING_TYPE_CHARACTER = 2
local BINDING_TYPE_GLOBAL = 1
local KEY_BUTTON_SIZE = 48
local MIN_BIND_SLOTS = 32
local TAB_OFFSET = 12
local TAB_HEIGHT = 40
local TAB_SPACING = 2
local BORDER_UNASSIGNED = {0.2,0.2,0.2,1 }
local BORDER_ASSIGNED = {0.5,0.5,0.5,1 }
local BORDER_DYNAMIC = {1,1,0,1}
local BORDER_PENDING = {1,0.5,0,1 }

local BUTTON_HEADERS = {
  ['spell'] = SPELLS,
  ['macro'] = MACRO,
  ['petaction'] = PET,
  ['mount'] = MOUNT,
  ['battlepet'] = BATTLEPET,


  [5] = PROFESSIONS_FIRST_AID,
  [7] = PROFESSIONS_COOKING,
  [9] = PROFESSIONS_FISHING,
  [10] = PROFESSIONS_ARCHAEOLOGY,

}

local BINDING_SCHEME_COLOR = {
  [BINDING_TYPE_GLOBAL] = {0,.125,.5,.5},
  [BINDING_TYPE_CHARACTER] = {0,0.25,0,0.5},
  [BINDING_TYPE_SPECIALIZATION] = {.25,0,0,0.5},
}
local BINDING_SCHEME_VERTEX = {
  [BINDING_TYPE_GLOBAL] = {0,.5,1,1},
  [BINDING_TYPE_CHARACTER] = {0,1,0,1},
  [BINDING_TYPE_SPECIALIZATION] = {1,1,1,1},
}
local BINDING_SCHEME_TEXT = {
  [BINDING_TYPE_SPECIALIZATION] = {0, 1, 1},
  [BINDING_TYPE_CHARACTER] = {0, 1, 0},
  [BINDING_TYPE_GLOBAL] = {0, 1, 1}
}

local restingAlpha = 0.7
local fadeTime, fadeDelay = .30, 0.15
local numButtons = BINDS_PER_ROW * 8
local saveButton
local KeyButton_OnKeyDown = function(self, key)
  kb.StoreBinding(self, key)
end
local KeyButton_OnClick = function(self, click)
  print(self:GetName(), 'OnMouseDown', click)
  if click == 'LeftButton' then
    kb.DropToSlot(self)
  elseif click == 'RightButton' then
    kb.ReleaseSlot(self)
  else
    kb.StoreBinding(self, click:upper())
  end
end

local KeyButton_OnDragStart = function(self)
  kb.PickupSlot(self)
end

local KeyButton_OnReceiveDrag = function(self, ...)
  kb.DropToSlot(self)
end


local KeyBinder_OnUpdate = function(self, elapsed)
  self.elapsed = self.elapsed + elapsed
  self.throttle = self.throttle + elapsed

  if (self.throttle >= 0.032) then
    self.throttle = 0
  else
    return
  end

  local progress = 1
  if self.elapsed > fadeTime then
    self.elapsed = 0
    self.fadeStep = 0
    --self.statustext:SetText(nil)
    --self.bindingstext:SetText(nil)
    self:SetScript('OnUpdate', nil)
  else
    if self.elapsed < fadeDelay then
      progress = 0
    else
      self.fadeStep = self.fadeStep + 1
      progress = (self.elapsed - fadeDelay) /(fadeTime - fadeDelay)
    end
    --print(self.fadeStep, format('%.02f/%.02f', (self.elapsed - fadeDelay) ,(fadeTime - fadeDelay)) , progress)
  end

  local alpha = 1 - progress * (1- restingAlpha)
  self.statustext:SetAlpha(alpha)
  self.bindingstext:SetAlpha(alpha)
end

local KeyButton_OnUpdate = function(self)
  if not self.command then
    return
  end

  if self:IsMouseOver() then
    kb.elapsed = 0
    if not self.active then
      -- only set this handler when the button is activated/mouseOver
      self.active = true
      self:SetScript('OnKeyDown', KeyButton_OnKeyDown)

      kb.statustext:SetText(self.statusText .. ': '..self.actionName)
      kb.bindingstext:SetText(self.bindingText)
      kb.fadeStep = 0
      kb.throttle = 0
      kb:SetScript('OnUpdate', KeyBinder_OnUpdate)

    end
  else
    if self.active then
      self.active = nil
      self:SetScript('OnKeyDown', nil)
    end
  end
end

local KeyBinder_OnMouseWheel = function(self, delta)
  print(self, delta, self.scrollOffset, (self.scrollOffset <= 0))


  if IsControlKeyDown() then
    KEY_BUTTON_SIZE = KEY_BUTTON_SIZE - delta
  else


    if (delta > 0) and (self.scrollOffset <= 0) then
      return
    elseif delta < 0 and kb.scrollOffset >= 42 then
      return
    end
    kb.scrollOffset = ceil(kb.scrollOffset - (delta * BINDS_PER_ROW))
  end

  kb.ui(true)
end

local KeyBinder_OnHide = function()
  KeyBinderImportLog:Hide()
end

local CloseButton_OnClick = function()
  kb.db.showUI = false
  kb:Hide()
end
local CancelButton_OnClick = function()
  kb.RevertBindings()
end
local SaveButton_OnClick = function()
  kb.ConfirmBindings()
end

local KeyBinder_Initialize = function()


  kb.scrollOffset = 0
  kb.tabAnchor = {'TOPLEFT', kb.profilebg, 'TOPLEFT', BUTTON_PADDING, -BUTTON_SPACING}
  kb.tabGrowth = {'TOPLEFT', nil,'TOPRIGHT', BUTTON_SPACING, 0}
  kb.tabSize = {TAB_HEIGHT, TAB_HEIGHT }
  kb.UIPanelAnchor = {'TOPLEFT', kb.sourcesbg, 'TOPLEFT', BUTTON_PADDING, -BUTTON_SPACING}
  kb.UIPanelGrowth = {'TOPLEFT', nil, 'BOTTOMLEFT', 0, -2 }
  kb.UIPanelSize = {84, 32 }
  kb.UIPanelIcon = {24, 32, 'LEFT', -12, 0}
  kb.controlsAnchor = {'BOTTOMLEFT', kb.footer, BUTTON_PADDING, BUTTON_PADDING }
  kb.controlsGrowth = {'BOTTOMLEFT', nil, 'BOTTOMRIGHT', BUTTON_SPACING, 0}

  -- order of these is important
  kb:tab('KeyBinderGlobalTab',
    kb.configTitle[BINDING_TYPE_GLOBAL] .. '\n' .. kb.configDescription[BINDING_TYPE_GLOBAL], "Interface\\ICONS\\item_azereansphere", {0.15,.85,.15,.85})
  kb:tab('KeyBinderCharacterTab',
    kb.configHeaders[BINDING_TYPE_CHARACTER] .. '\n' .. kb.configDescription[BINDING_TYPE_CHARACTER], nil)
  kb:tab('KeyBinderSpecTab',
    kb.configHeaders[BINDING_TYPE_SPECIALIZATION] .. '\n' .. kb.configDescription[BINDING_TYPE_SPECIALIZATION], kb.specInfo.texture)
  KeyBinderCharacterTab.icon:SetTexCoord(0.15,.85,.15,.85)



  --portraitLayers[1] = KeyBinderCharacterTab.icon

  saveButton = kb:button('KeyBinderSaveButton', 'Refresh', 'Reload current bindings and refresh panel.', SaveButton_OnClick)
  --restoreButton = kb:button('KeyBinderRestoreButton', 'Discard', 'Revert all changes.', CancelButton_OnClick)
  --clearButton = kb:button('KeyBinderClearButton', 'Clear Page', 'Release all buttons.', ResetButton_OnClick)

  kb:uibutton(
    'KeyBinderSpellBookButton', 'SpellBook', nil,
    function() ToggleSpellBook(BOOKTYPE_SPELL) end,
    "Interface\\BUTTONS\\UI-MicroButton-Spellbook-Up", {0, 1, .4, 1})
  kb:uibutton(
    'KeyBinderTalentFrameButton', TALENTS, SPECIALIZATION,
    function() ToggleTalentFrame() end,
    "Interface\\BUTTONS\\UI-MicroButton-Talents-Up", {0, 1, .4, 1})

  kb:uibutton(
    'KeyBinderMacroFrameButton', 'Macros', nil,
    function() if MacroFrame and MacroFrame:IsVisible() then
      HideUIPanel(MacroFrame)
    else
      ShowMacroFrame() end
    end,
    "Interface\\BUTTONS\\UI-MicroButton-Help-Up", {0, 1, .4, 1})

  kb:uibutton(
    'KeyBinderInventoryButton', 'Bags', nil,
    function() OpenAllBags() end,
    "Interface\\BUTTONS\\UI-MicroButtonCharacter-Up", {0, 1, .4, 1})



  kb.info:SetPoint('TOPLEFT', kb.UIPanels[1], 'BOTTOMLEFT', 0, -BUTTON_SPACING)
  HEADER_OFFSET = kb.UIPanels[1]:GetHeight() + BUTTON_PADDING
      + kb.info:GetHeight()
  FOOTER_OFFSET = saveButton:GetHeight() + BUTTON_PADDING

  kb:SetScript('OnHide', KeyBinder_OnHide)
  kb:SetScript('OnMouseWheel', KeyBinder_OnMouseWheel)
  kb.CloseButton:SetScript('OnClick', CloseButton_OnClick)

end

--- Resets button command
kb.ReleaseSlot = function(self)
  local slot = self:GetID()


  if kb.currentProfile.buttons[slot] then
    kb.currentProfile.buttons[slot] = nil
  end
  if self.command then
    kb.currentProfile.commands[self.command] = nil
  end
  if self.actionType == 'spell' and IsTalentSpell(self.actionName) then
    if kb.currentProfile.talents[self.actionID] then
      kb.currentProfile.talents[self.actionID] = nil
    end
  end
  local droppedKeys = {}

  -- doing removal in second loop to avoid possible iterator shenanigans
  for k,v in pairs(kb.currentProfile.bindings) do
    if v == self.command then
      tinsert(droppedKeys, k)
    end
  end
  if #droppedKeys >=1 then
    for i, k in ipairs(droppedKeys) do
      kb.currentProfile.bindings[k] = nil
    end
  end

  self.isAvailable = nil
  self.isDynamic = nil
  self.bindingText = nil
  self.statusText = nil
  self.command = nil
  self.actionType = nil
  self.actionID = nil
  self.actionName = nil
  self.pickupSlot = nil
  self.pickupBook = nil
  self.macroName = nil
  self.profile = nil
  self.icon:SetTexture(nil)
  self.border:SetColorTexture(unpack(BORDER_UNASSIGNED))
  self:EnableKeyboard(false)
  self:SetScript('OnKeyDown', nil)
end

kb.SetSlot = function(self, command, name, icon, actionType, actionID, macroName, macroText, pickupSlot, pickupBook)
  local slot = self:GetID()
  local isDynamic, isAvailable

  print('|cFFFFFF00SetSlot|r:', self:GetID())
  if command then

    if actionType == 'spell' then
      local professionNum, spellNum = command:match("profession_(%d)_(%d)")

      if (professionNum and spellNum) then
        isDynamic = 'profession'
        local cacheInfo = kb.ProfessionCache[professionNum..'_'..spellNum]
        if cacheInfo then
          isAvailable = true
          name = cacheInfo.spellName
          icon = cacheInfo.icon
          actionID = cacheInfo.spellID
          self.profIndex = cacheInfo.profIndex
          self.spellOffset = cacheInfo.spellOffset
        end
        print(' Special slot: |cFF00FFFFProfession|r', professionNum, spellNum, isDynamic, isAvailable)

        self.professionNum = tonumber(professionNum)
        self.spellNum = tonumber(spellNum)

      else
        if kb.TalentCache[actionID] then
          isDynamic = 'talent'
          print(' Special slot: |cFFBBFF00talent|r', name, isAvailable)
        end

        isAvailable = GetSpellInfo(name)
      end
      actionID = name
    elseif actionType == 'macro' then
      if not actionID then
        actionID = GetMacroIndexByName(name)
      end
      isAvailable = true
    else
      --- Journal selections
      -- todo: consider using the deep end of blizzard action bar instead
      if not actionID then
        actionID = command:match("^KeyBinderMacro:(.+)")
      end
      isAvailable = true
    end

    if isAvailable then
      local oldCommand = command
      macroName, macroText, command = kb.RegisterAction(actionType, actionID)
      if oldCommand ~= command then
        print('|cFFFF4400fixing command string', actionType, actionID, name)
        kb.currentProfile.bound[oldCommand] = nil
        kb.currentProfile.bound[command] = slot
        for k,v in pairs(kb.currentProfile.bindings) do
          if v == oldCommand then
            kb.currentProfile.bindings[k] = command
          end
        end
      end
      kb.LoadBinding(command, name, icon, actionType, actionID, macroName, macroText)
    end

    if actionType == 'petaction' then
      self.pickupSlot = pickupSlot
      self.pickupBook = pickupBook
    else
      self.pickupSlot = nil
      self.pickupBook = nil
    end

    actionID = actionID or 0
    self:EnableKeyboard(true)
    print(' |cFF00FF00kb.currentProfile.buttons['..slot..'] |cFF00FFFF=|r |cFF00FFFF"'.. command.. '"|r |cFF00FF00"'.. name, '"|r |cFFFFFF00icon:'.. icon .. '|r |cFFFF8800"'.. actionType, '"|r |cFFFF0088id:'.. actionID ..'|r |cFF00FF00"'.. macroName .. '"|r')
    kb.currentProfile.buttons[slot] = {command, name, icon, actionType, actionID, macroName, macroText, pickupSlot, pickupBook}

    -- Clean up conflicting entries for loaded button
    local previous = kb.currentProfile.commands[command]
    if previous ~= slot and kb.buttons[previous] then
      kb.ReleaseSlot(kb.buttons[previous])
    end
    kb.currentProfile.commands[command] = slot
  end

  self.isAvailable = isAvailable
  self.isDynamic = isDynamic

  self.macroText = macroText
  self.macroName = macroName
  self.actionType = actionType
  self.actionID = actionID
  self.actionName = name
  self.command = command
  self.icon:SetTexture(icon)
  self.profile = kb.db.bindMode
  self:RegisterForDrag('LeftButton')
end

--- Retrieves button at index; creates said button and instates any stored parameters
do
  local leftSlot, upSlot
  local buttonsDepth = 0
  kb.GetSlot = function(index)

    local slot  = index + kb.scrollOffset

    if not kb.buttons[index] then
      local button = CreateFrame('CheckButton', 'KeyBinderSlot'..index, kb, 'KeyButton')
      button:SetScript('OnClick', KeyButton_OnClick)
      button:SetScript('OnUpdate', KeyButton_OnUpdate)
      button:SetScript('OnDragStart', KeyButton_OnDragStart)
      button:SetScript('OnReceiveDrag', KeyButton_OnReceiveDrag)
      button:RegisterForClicks('AnyUp')


      local newRow = (mod(index, BINDS_PER_ROW) == 1)

      if index == 1 then
        button:SetPoint('TOPLEFT', kb.bg, 'TOPLEFT', BUTTON_PADDING, - BUTTON_PADDING)
        upSlot = button
        buttonsDepth = KEY_BUTTON_SIZE + BUTTON_PADDING * 2
      elseif newRow then
        button:SetPoint('TOPLEFT', upSlot, 'BOTTOMLEFT', 0, -BUTTON_SPACING)
        upSlot = button
        buttonsDepth = buttonsDepth + KEY_BUTTON_SIZE + BUTTON_SPACING
      else
        button:SetPoint('TOPLEFT', leftSlot, 'TOPRIGHT', BUTTON_HSPACING, 0)
      end

      button:SetSize(KEY_BUTTON_SIZE, KEY_BUTTON_SIZE)
      button:Show()
      kb.buttons[index] = button
      leftSlot = button
    end
    return kb.buttons[index]
  end
end

--- Updates profile assignment and button contents
kb.UpdateSlot = function(self, force)
  local slot = self:GetID()

  if force then
    if kb.currentProfile.buttons[slot] then
      kb.SetSlot(self, unpack(kb.currentProfile.buttons[slot]))
    else
      kb.ReleaseSlot(self)
    end
  end

  if self.command then
    print('['..slot..'] =', self.command, GetBindingKey(self.command))

    if self.pending then
      self.border:SetColorTexture(unpack(BORDER_PENDING))
    elseif self.isDynamic then
      self.border:SetColorTexture(unpack(BORDER_DYNAMIC))
    else
      self.border:SetColorTexture(unpack(BORDER_ASSIGNED))
    end

    if self.actionType == 'macro' then
      self.macro:Show()
    else
      self.macro:Hide()
      if self.actionType == 'spell' then
        local dummy = GetSpellInfo(self.actionName)
        if not dummy then
          self.icon:SetDesaturated(true)
        else
          self.icon:SetDesaturated(false)
        end

      end
    end

    if self.isDynamic then
      print('|cFFFFBB00UpdateSlot|r: ', self.isDynamic, self.isAvailable, self.actionID)
    end

    if self.isDynamic == 'profession'  then
      local profText = (self.spellNum == 1) and TRADE_SKILLS or (BUTTON_HEADERS[self.profIndex] or GetProfessionInfo(self.profIndex))
      if self.isAvailable then
        print(self.profIndex, 'spnum', type(self.spellNum), (self.spellNum == 1))

        self.statusText = '|cFFFFFF00'..profText..'|r'
        self.bindingText = kb.BindingString(GetBindingKey(self.command))
      else
        self.statusText = '|cFFFF4400'..profText..'|r'
        self.actionName = '(need to train profession #'..self.profNum..')'
        self.bindingText ='?'
      end
    elseif self.isDynamic == 'talent' then

      self.statusText = '|cFF00FFFF'.. TALENT .. '|r'
      if self.isAvailable then
        self.bindingText = kb.BindingString(GetBindingKey(self.command))
      else
        if kb.inactiveTalentBindings[self.actionID] then
          print(self.actionID, #kb.inactiveTalentBindings[self.actionID])
          self.bindingText= kb.BindingString(unpack(kb.inactiveTalentBindings[self.actionID]))
        end

      end
    else
      self.statusText = '|cFF00FF00'.. (BUTTON_HEADERS[self.actionType] and BUTTON_HEADERS[self.actionType] or self.actionType) .. '|r'
      self.bindingText = kb.BindingString(GetBindingKey(self.command))
    end

    local locked, layer = kb.IsCommandBound(self)
    if locked then
      self.icon:SetAlpha(0.5)
    else
      self.icon:SetAlpha(1)
    end

    if self.actionType == 'spell' then
      self.icon:SetTexture(GetSpellTexture(self.actionID))
    end
  end

  if not self.isAvailable then
    self.bind:SetTextColor(0.7,0.7,0.7,1)
  else
    self.bind:SetTextColor(1,1,1,1)
  end

  self.header:SetText(self.statusText)
  self.bind:SetText(self.bindingText)
  self.details:SetText(self.actionName)
end

--- push current information into living UI
kb.ui = function(force)
  for i, module in ipairs(kb.modules) do
    if module.ui then
      module.ui(force)
    end
  end

  if not kb.db.showUI then
    print('---end of refresh')
    return
  end
  if not kb.loaded then
    KeyBinder_Initialize()
    kb.loaded = true
  end
  for i = 1, numButtons do
    local button = kb.GetSlot(i)
    button:SetID(i+kb.scrollOffset)
    kb.UpdateSlot(button, force)
  end


  --- Frame Sizing
  kb.profilebg:SetHeight(kb.tabSize[2] + BUTTON_PADDING * 2 + kb.profiletext:GetStringHeight())

  kb.bg:SetWidth((KEY_BUTTON_SIZE + BUTTON_HSPACING + BUTTON_SPACING) * BINDS_PER_ROW + BUTTON_PADDING*2 - BUTTON_SPACING)
  local numRows = numButtons/BINDS_PER_ROW

  kb.bg:SetHeight((KEY_BUTTON_SIZE + BUTTON_SPACING) * numRows + BUTTON_PADDING*2 - BUTTON_SPACING)

  kb:SetHeight(kb.headerbg:GetHeight() + kb.profilebg:GetHeight() + kb.bg:GetHeight() + kb.footer:GetHeight())
  kb:SetWidth((kb.sourcesbg:GetWidth() +(BINDS_PER_ROW * (KEY_BUTTON_SIZE + BUTTON_HSPACING) + (BINDS_PER_ROW - 1) * BUTTON_SPACING + BUTTON_PADDING * 2) ))

  kb.bg:SetColorTexture(unpack(BINDING_SCHEME_COLOR[kb.db.bindMode]))
  for i, tab in ipairs(kb.tabButtons) do
    local border = tab:GetNormalTexture()
    local tabTexture = "Interface\\Buttons\\UI-Quickslot2"
    local left, top, right, bottom = -12, 12, 13, -13
    if i == kb.db.bindMode then
      tabTexture = "Interface\\Buttons\\CheckButtonGlow"
      left, top, right, bottom = -14, 14, 15, -15
      tab.icon:SetDesaturated(false)
      if tab.icon2 then tab.icon2:SetDesaturated(false) end
      border:SetDesaturated(true)
      border:SetVertexColor(1,1,1, 1)
    else
      tab.icon:SetDesaturated(true)
      if tab.icon2 then tab.icon2:SetDesaturated(true) end
      border:SetDesaturated(false)
      border:SetVertexColor(1,1,1)
    end
    border:SetTexture(tabTexture)
    border:SetPoint('TOPLEFT', tab, 'TOPLEFT', left, top)
    border:SetPoint('BOTTOMRIGHT', tab, 'BOTTOMRIGHT', right, bottom)
  end

  KeyBinderSpecTab.icon:SetTexture(kb.specInfo.texture)
  SetPortraitTexture(KeyBinderCharacterTab.icon, 'player')

  kb.profiletext:SetText(kb.configHeaders[kb.db.bindMode])
  print(kb.db.bindMode, kb.configHeaders[kb.db.bindMode], kb:GetSize())
  print(kb:GetPoint(1))

  kb:Show()

  -- Reset this so talent cache can be rebuilt
  kb.talentsPushed = nil
end

kb.AcceptAssignment = function(self, ...)
  local popup = StaticPopupDialogs["SKELETONKEY_CONFIRM_ASSIGN_SLOT"]
  local source = loadedProfiles[popup.oldProfile]
  kb.SetSlot(popup.slot, unpack(popup.args))
  kb.UpdateSlot(popup.slot)
  kb:SetScript('OnMouseWheel', KeyBinder_OnMouseWheel) -- re-enable scrolling
  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
}