diff main.lua @ 10:f3a7bfebc283

Version 0.33
author Flick <flickerstreak@gmail.com>
date Tue, 20 Mar 2007 21:37:38 +0000
parents c05fd3e18b4f
children 2735edcf9ab7
line wrap: on
line diff
--- a/main.lua	Tue Mar 20 21:35:57 2007 +0000
+++ b/main.lua	Tue Mar 20 21:37:38 2007 +0000
@@ -9,6 +9,7 @@
 local L       = AceLibrary("AceLocale-2.2"):new("ReAction")
 local dewdrop = AceLibrary("Dewdrop-2.0")
 local tablet  = AceLibrary("Tablet-2.0")
+local ReBound = AceLibrary("ReBound-1.0")
 
 -- private functions
 local function tcopy(t)
@@ -25,10 +26,19 @@
 -- key binding label constants
 BINDING_HEADER_REACTION                = L["ReAction"]
 BINDING_NAME_REACTION_TOGGLELOCK       = L["Toggle ReAction Bar Lock"]
-BINDING_NAME_REBOUND_TOGGLEBINDINGMODE = L["Toggle ReBound Keybinding Mode"]
+BINDING_NAME_REACTION_TOGGLEKEYBIND    = L["ReAction Keybinding Mode"]
 
+-- UI panel strings
+REACTION_KEYBIND_TITLE        = L["ReAction Keybinding"]
+REACTION_KEYBIND_SUBTITLE     = L["Click Buttons to Set Keybindings"]
+REACTION_KEYBIND_DONE         = L["Save"]
+REACTION_KEYBIND_REVERT       = L["Revert"]
 
--- main object
+
+
+------------------------------
+-- AceAddon setup
+------------------------------
 local main = AceLibrary("AceAddon-2.0"):new(
   "AceConsole-2.0",
   "AceEvent-2.0",
@@ -36,15 +46,65 @@
   "FuBarPlugin-2.0"
 )
 
--- initial non-persistent state
-main.locked = true
+function main:OnInitialize()
+  self:RegisterChatCommand( {L["/reaction"], L["/rxn"]}, ReActionConsoleOptions, "REACTION" )
+  self:RegisterDB("ReActionDB","ReActionDBPC")
+  self:RegisterDefaults("profile", ReAction_DefaultProfile)
+  self:RegisterEvent("PLAYER_REGEN_DISABLED","CombatLockdown")
+  self:RegisterEvent("PLAYER_ENTERING_WORLD","HideDefaultBars")
+  self:DisableDefaultKeybindings()
 
--- set a global variable for Bindings.xml
-ReActionAddOn = main
+  -- initial non-persistent state
+  self.locked = true
+  self.bars   = { }
+end
 
+-- OnEnable is called at startup (startup = true, oldConfig = nil),
+-- and when the profile is changed (startup = false, oldConfig ~= nil )
+function main:OnEnable( startup, oldConfig )
+  if self.db.profile.firstRunDone ~= true then
+    self.db.profile.bars = tcopy(ReAction_DefaultBlizzardBars)
+  end
+  self:DestroyAllBars()
+  self:SetupBars()
+  self:UpgradeProfile()
+  self:SetupKeybindings( oldConfig and oldConfig.bindings )
+  if self.db.profile.firstRunDone ~= true then
+    self:Unlock()
+  end
+  self.db.profile.firstRunDone = true
+end
 
+function main:OnDisable()
+  self:Lock()
+end
+
+-- OnProfileEnable() is only called when switching profiles, NOT for the initial profile at load time.
+function main:OnProfileEnable( oldName, oldData )
+  self:OnEnable(false, oldData)
+end
+
+function main:UpgradeProfile()
+  if self.db.profile.firstRunDone ~= true and #self.db.profile.bindings == 0 then
+    for _, bar in pairs(self.bars) do
+      for _, button in pairs(bar.buttons) do
+        local key = ReBound:GetBinding(button:GetActionFrame(),"LeftButton")
+        if key and #key > 0 and not self.db.profile.bindings[key] then
+          self:REBOUND_BIND(key,button:GetActionFrame():GetName(),"LeftButton")
+        end
+      end
+    end
+  end
+end
+
+
+
+
+--------------------------------------------
 -- FuBar plugin setup
--- Even if FuBar isn't installed, this gives us a nice minimap-button interface.
+-- Even if FuBar isn't installed, the plugin 
+-- provides a nice  minimap-button interface.
+---------------------------------------------
 main.hasIcon = "Interface\\Icons\\INV_Qiraj_JewelEncased"
 main.hasNoColor = true
 main.hideMenuTitle = true
@@ -57,65 +117,11 @@
 -- have to do this after tcopy() above, otherwise it will try to copy the handler object (bad idea)
 ReActionGlobalMenuOptions.handler = main
 
-
-
-
--- Event handling
-function main:OnInitialize()
-  self:RegisterChatCommand( {L["/reaction"], L["/rxn"]}, ReActionConsoleOptions, "REACTION" )
-  self:RegisterDB("ReActionDB","ReActionDBPC")
-  self:RegisterDefaults("profile", ReAction_DefaultProfile)
-  self:RegisterEvent("PLAYER_REGEN_DISABLED","CombatLockdown")
-  self:RegisterEvent("PLAYER_ENTERING_WORLD","HideDefaultBars")
-  self:RegisterEvent("EVENT_REBOUND_KEYBINDING_MODE")
-end
-
-function main:OnEnable()
-  -- this gets called at startup and when the profile is changed
-  if self.db.profile.firstRunDone ~= true then
-    -- Do some "first-run" setup
-    self:StealKeyBindings()
-    self.db.profile.firstRunDone = true
-    self.db.profile.bars = tcopy(ReAction_DefaultBlizzardBars)
-  end
-  self:DestroyAllBars()
-  self:SetupBars()
-end
-
-function main:OnDisable()
-  self:Lock()
-end
-
-function main:OnProfileEnable()
-  -- for profile switching
-  self:OnEnable()
-end
-
-function main:CombatLockdown()
-  if not self:IsLocked() then
-    self:Lock()
-    ReBound:Disable()
-    UIErrorsFrame:AddMessage(L["ReAction bars locked when in combat"])
-  end
-end
-
-function main:EVENT_REBOUND_KEYBINDING_MODE(enabled)
-  for _, bar in pairs(self.bars) do
-    for __, button in pairs(bar.buttons) do
-      if button and button ~= EMPTY_BAR_SLOT then
-        button:TempShow(enabled)
-      end
-    end
-  end
-end
-
-
--- FuBar plugin methods
 function main:OnTooltipUpdate()
 	local c = tablet:AddCategory("columns", 2)
 	c:AddLine("text", L["Bar lock"], "text2", self.locked and ("|cffff0000"..L["Locked"].."|r") or ("|cffffcc00"..L["Unlocked"].."|r"))
   c:AddLine("text", L["Button lock"], "text2", LOCK_ACTIONBAR == "1" and ("|cffcc0000"..L["Locked"].."|r") or ("|cff00cc00"..L["Unlocked"].."|r"))
-  c:AddLine("text", L["Kebinding mode"], "text2", ReBound:IsEnabled() and ("|cff33ff33"..L["On"].."|r") or ("|cffffcc00"..L["Off"].."|r"))
+  c:AddLine("text", L["Kebinding mode"], "text2", self:GetKeybindMode() and ("|cff33ff33"..L["On"].."|r") or ("|cffffcc00"..L["Off"].."|r"))
 	tablet:SetHint(L["|cffffcc00Shift-Click for bar lock|n|cff33ff33Alt-Click|r for keybindings|nRight-click for menu"])
 end
 
@@ -124,17 +130,134 @@
 	  self:ToggleLocked()
     self:UpdateDisplay()
 	elseif IsAltKeyDown() then
-    ReBound:ToggleEnabled()
+    self:ToggleKeybindMode()
     self:UpdateDisplay()
   end
 end
 
 
--- lock/unlock bars
+
+
+------------------------------
+-- Key binding functions
+------------------------------
+function main:DisableDefaultKeybindings()
+  -- change the labels on all actionbar keybindings in the default
+  -- interface.
+  local label = "|cff999999("..L["Use ReAction"]..")|r"
+  for i = 1, 12 do
+    setglobal("BINDING_NAME_ACTIONBUTTON"..i,label)
+    for j = 1, 4 do
+      setglobal("BINDING_NAME_MULTIACTIONBAR"..j.."BUTTON"..i,label)
+    end
+  end
+  for i = 1, 6 do
+    setglobal("BINDING_NAME_ACTIONPAGE"..i,label)
+  end
+  for i = 1, 10 do
+    setglobal("BINDING_NAME_BONUSACTIONBUTTON"..i,label)
+    setglobal("BINDING_NAME_SHAPESHIFTBUTTON"..i,label)
+  end
+  BINDING_HEADER_ACTIONBAR = "|cff999999"..L["Action Bar Functions Disabled"].."|r"
+  BINDING_HEADER_MULTIACTIONBAR = "|cff999999"..L["Multi-Action Bar Functions Disabled"].."|r"
+  BINDING_NAME_NEXTACTIONPAGE = label
+  BINDING_NAME_PREVIOUSACTIONPAGE = label
+end
+
+function main:SetupKeybindings( previous )
+  if previous then
+    self:UnregisterEvent("REBOUND_BIND")
+    self:UnregisterEvent("REBOUND_UNBIND")
+    for key, binding in pairs(previous) do
+      ReBound:ClearBinding(key, getglobal(binding.target), binding.button, true)
+    end
+  end
+  if self.db.profile.firstRunDone ~= true then
+    self:StealKeyBindings()
+  else
+    for key, binding in pairs(self.db.profile.bindings) do
+      ReBound:SetBinding(key, getglobal(binding.target), binding.button)
+    end
+  end
+  SaveBindings(GetCurrentBindingSet())
+  self:RegisterEvent("REBOUND_BIND")
+  self:RegisterEvent("REBOUND_UNBIND")
+end
+
+function main:StealKeyBindings()
+  -- steal the keybindings of the main action bar and assign them to rebar 1, buttons 1-12
+  for i = 1, 12 do
+    local key = GetBindingKey("ACTIONBUTTON"..i)
+    if key and #key > 0 then
+      ReBound:ClearBinding(key,nil,nil,true) -- suppress notification printouts
+      ReBound:SetBinding(key, self.bars[1].buttons[i]:GetActionFrame(), "LeftButton")
+    end
+  end
+  SaveBindings(GetCurrentBindingSet())
+end
+
+function main:REBOUND_BIND(key, target, button)
+  if key and target then
+    self.db.profile.bindings[key] = { target = target, button = button }
+  end
+end
+
+function main:REBOUND_UNBIND(key)
+  if key then
+    self.db.profile.bindings[key] = nil
+  end
+end
+
+function main:ToggleKeybindMode()
+  self:SetKeybindMode(not self:GetKeybindMode())
+end
+
+function main:GetKeybindMode()
+  return self.keybindMode
+end
+
+function main:SetKeybindMode(enabled)
+  if not InCombatLockdown() then
+    self.keybindMode = enabled
+    for _, bar in pairs(self.bars) do
+      for __, button in pairs(bar.buttons) do
+        if button and button ~= EMPTY_BAR_SLOT then
+          button:TempShow(enabled)
+        end
+      end
+    end
+    if enabled then
+      ReBound:ShowAll()
+      ReActionKeybindDialog:Show()
+    else
+      ReBound:HideAll()
+      if ReActionKeybindDialog:IsShown() then
+        ReActionKeybindDialog:Hide()
+      end
+    end
+  else
+    UIErrorsFrame:AddMessage(ERROR_NOT_IN_COMBAT)
+  end
+end
+
+
+
+
+----------------------------
+-- Bar lock/unlock functions
+----------------------------
+function main:CombatLockdown()
+  if not self:IsLocked() then
+    self:Lock()
+    UIErrorsFrame:AddMessage(L["ReAction bars locked when in combat"])
+  end
+  ReActionKeybindDialog:Hide()
+end
+
 function main:SetLocked( lock )
   if lock ~= self.locked then
     if not lock and InCombatLockdown() then
-      UIErrorsFrame:AddMessage(SPELL_FAILED_AFFECTING_COMBAT)
+      UIErrorsFrame:AddMessage(ERROR_NOT_IN_COMBAT)
     else
       self.locked = lock and true or false -- force data integrity
       for _, bar in pairs(self.bars) do
@@ -172,7 +295,9 @@
 
 
 
--- Hide the default Blizzard main bar artwork
+--------------------------------------------------------
+-- Functions to hide the default Blizzard main bar parts
+--------------------------------------------------------
 function main:HideArt()
   if self.db.profile.hideArt then
     -- the pet bar is a child of MainMenuBar, and can't be hidden. Need to reparent it
@@ -207,8 +332,6 @@
   self:SetHideArt( not self:IsArtHidden() )
 end
 
-
-
 -- Hide default Blizzard bars
 local blizzDefaultBars = {
   ActionButton1,
@@ -245,13 +368,6 @@
   MultiBarBottomRight,
 }
 
-function main:StealKeyBindings()
-  -- steal the keybindings of the main action bar and assign them to rebar 1, buttons 1-12
-  for i = 1, 12 do
-    -- TODO: when we convert to override bindings
-  end
-end
-
 local function disableUIOptions()
   -- disable the buttons to hide/show the blizzard multiaction bars
   -- see UIOptionsFrame.lua and .xml
@@ -277,6 +393,11 @@
 end
 
 
+
+
+---------------------------------------
+-- Bar setup and manipulation functions
+---------------------------------------
 -- Reset bars to blizzard defaults
 function main:ResetBars()
   if InCombatLockdown() then
@@ -288,17 +409,6 @@
   end
 end
 
-
--- re-sync action IDs
-function main:ResyncActionIDs()
-  -- TODO
-end
-
-
-
--- Bar manipulation
-main.bars    = { }
-
 function main:DestroyAllBars()
   -- destroy any existing bars
   for id = 1, table.maxn(self.bars) do
@@ -306,7 +416,6 @@
   end
 end
 
-
 function main:SetupBars()
   -- hide the default Blizzard art, if configued
   self:HideArt()
@@ -388,6 +497,9 @@
   end
 end
 
+-- 
+-- this function is a wrapper for CreateBar() which looks up the bar type
+-- and constructs a new configuration object of the right type.
 function main:NewBar( type )
   if InCombatLockdown() then
     UIErrorsFrame:AddMessage(SPELL_FAILED_AFFECTING_COMBAT)
@@ -409,7 +521,9 @@
   end
 end
 
-
+--
+-- This function is a wrapper for DestroyBar() which does in-combat 
+-- checking and updates the config.
 function main:DeleteBar(id)
   if InCombatLockdown() then
     UIErrorsFrame:AddMessage(SPELL_FAILED_AFFECTING_COMBAT)
@@ -421,6 +535,12 @@
   end
 end
 
+
+
+
+-----------------
+-- General utility
+-----------------
 function main:ToggleIds()
   if self.showIds then
     ReAction:HideAllIds()
@@ -434,5 +554,7 @@
   return self.showIds
 end
 
+-- set a global variable for Bindings.xml
+ReActionAddOn = main