view classes/ReBound.lua @ 8:c05fd3e18b4f

Version 0.31
author Flick <flickerstreak@gmail.com>
date Tue, 20 Mar 2007 21:33:59 +0000
parents f920db5fc6b1
children
line wrap: on
line source
-- ReBound.lua
-- 


local AceEvent = AceLibrary("AceEvent-2.0")

ReBound = { }

-- initial values
ReBound.active = false

ReBound.targets = { }

function ReBound:AddKeybindTarget( t )
  if t then
    self.targets[t] = CreateFrame("Button", nil, t, "ReBoundClickBindingTemplate")
    self.targets[t].keybindTarget = t:GetName()
  end
end

function ReBound:RemoveKeybindTarget( t )
  if t then
    self.targets[t] = nil
  end
end

function ReBound:ShowClickBindingButtons()
  AceEvent:TriggerEvent("EVENT_REBOUND_KEYBINDING_MODE", true)
  for tgt, clickFrame in pairs(self.targets) do
    clickFrame:Show()
  end    
end

function ReBound:HideClickBindingButtons()
  AceEvent:TriggerEvent("EVENT_REBOUND_KEYBINDING_MODE", false)
  for tgt, clickFrame in pairs(self.targets) do
    clickFrame:Hide()
  end    
end

function ReBound:ClearSelectedKey()
  self.selectedKey = nil
  SetCursor(nil) -- reset cursor to default state
end

function ReBound:ToggleEnabled()
  if self:IsEnabled() then
    self:Disable()
  else
    self:Enable()
  end
end

function ReBound:IsEnabled()
  return ReBoundFrame:IsVisible()
end

function ReBound:Enable()
  if InCombatLockdown() then
    UIErrorsFrame:AddMessage("Can't set keybindings in combat")
  else
    ReBoundFrame:Show()
  end
end

function ReBound:Disable()
  ReBoundFrame:Hide()
end


local mouseButtonConvert = {
  LeftButton = "BUTTON1",
  RightButton = "BUTTON2",
  MiddleButton = "BUTTON3",
  Button4 = "BUTTON4",
  Button5 = "BUTTON5"
}

function ReBound:HandleKeyPressed( key )
  if key == nil or key == "UNKNOWN" or key == "SHIFT" or key == "CTRL" or key == "ALT" then 
    return
  end
  if IsShiftKeyDown() then 
    key = "SHIFT-"..key
  end
  if IsControlKeyDown() then
    key = "CTRL-"..key
  end
  if IsAltKeyDown() then
    key = "ALT-"..key
  end
  if key == "ESCAPE" or GetBindingAction(key) == "ReBound_TOGGLEBINDINGMODE" then
    ReBoundFrame:Hide()
    return nil, nil
  end
  key = mouseButtonConvert[key] or key
  
  self.selectedKey = key
  
  -- change cursor to glowing hand
  SetCursor("CAST_CURSOR")

  local keyTxt = GetBindingText(key, "KEY_")
  local cmd    = GetBindingAction(key)
  local cmdTxt
  
  if cmd then
    cmdTxt = GetBindingText(cmd, "BINDING_NAME_")
  end
  
  -- make click-bindings look prettier
  local btnName
  if cmdTxt then
    btnName = string.match(cmdTxt,"CLICK (.+)\:LeftButton")
    btnName = btnName or string.match(cmdTxt,"CLICK (.+)\:RightButton")
  end
  
  return keyTxt, btnName or cmdTxt
end

-- TODO: move to override-binding model and store data in profile
function ReBound:BindSelectedKeyTo( btnName )
  if self.selectedKey and btnName then
    self:ClearBinding(btnName)
    SetBindingClick(self.selectedKey, btnName, "LeftButton")
    SaveBindings(2) -- 2 = character-specific
    ReBoundFrame.statusMsg:SetText(GetBindingText(self.selectedKey, "KEY_") .. " is now bound to " .. btnName)
    ReBoundFrame.selectedKey:SetText("(none)")
    ReBoundFrame.currentAction:SetText("(none)")
    self:ClearSelectedKey()
  end
end


function ReBound:ClearBinding( btnName )
  if btnName then
    local current = GetBindingKey("CLICK "..btnName..":LeftButton")
    if current then
      SetBinding(current, nil)
      ReBoundFrame.statusMsg:SetText("|cFFFF3333"..btnName .. " is now unbound|r")
    end
  end
end


function ReBound:UpdateCurrentTarget( btnName )
  local msg = ""
  if btnName then
    msg = btnName.." is currently "
    local current = GetBindingKey("CLICK "..btnName..":LeftButton")
    if current then
      msg = msg .. "bound to " .. GetBindingText(current, "KEY_")
    else
      msg = msg .. " not bound"
    end
  end
  ReBoundFrame.statusMsg:SetText(msg)
end