diff Button.lua @ 2:8e0ff8ae4c08

Version 0.2
author Flick <flickerstreak@gmail.com>
date Tue, 20 Mar 2007 21:08:31 +0000
parents c11ca1d8ed91
children
line wrap: on
line diff
--- a/Button.lua	Tue Mar 20 21:03:57 2007 +0000
+++ b/Button.lua	Tue Mar 20 21:08:31 2007 +0000
@@ -1,5 +1,5 @@
 -- private constants
-local namePrefix = "ReActionButton_"
+local namePrefix = "ReActionButton"
 local _G = getfenv(0)
 local ACTION_FREE = { }
 local MAX_ACTIONS = 120
@@ -14,6 +14,16 @@
   A = { r=0.1, g=1.0, b=0.1, a=1.0 },  -- alt
 }
 
+-- TODO: localize these key names with GetBindingText(KEY_)
+local keybindAbbreviations = {
+  ["Mouse Button "] = "M-",
+  ["Spacebar"] = "Sp",
+  ["Num Pad "] = "Num-",
+  ["Page Up"]  = "PgUp",
+  ["Page Down"] = "PgDn",
+  [" Arrow"] = "",
+}
+
 local equippedActionBorderColor = { r=0, g=1.0, b=0, a=0.35 }
 
 local actionUsableColor        = { r=1.0, g=1.0, b=1.0, a=1.0 }
@@ -60,14 +70,13 @@
 
   t.inUse = true
   if t.button then
-    t.button:Configure(parent,config,barIdx,id)
+    t.button:Configure(parent,config,barIdx)
   else
     t.button = self:new(parent,config,barIdx,id)
   end
 
-  if actionButtonTbl[t.button:GetActionID()].inUse ~= true then
-  end
-
+  -- fix screwy config with overlapping IDs
+  config.actionIDs[barIdx] = id
   return t.button
 end
 
@@ -100,14 +109,9 @@
   ReActionButton.super.prototype.init(self)
 
   -- create the button widget
-  self.name = namePrefix.."_"..id
+  self.name = namePrefix..id
   self.button = CreateFrame("CheckButton", self.name, parentFrame, "ReActionButtonTemplate")
   
-  -- create the actionID label on the control widget
-  local actionIDLabel = self.button:CreateFontString(nil,"ARTWORK", "NumberFontNormalSmall")
-  actionIDLabel:SetPoint("BOTTOMLEFT",0,0)
-  actionIDLabel:Hide()
-
   -- store references to the various sub-frames so we don't have to look it up all the time
   self.frames      = {
     hotkey         = _G[self.name.."HotKey"],
@@ -119,30 +123,50 @@
     border         = _G[self.name.."Border"],
     normalTexture  = _G[self.name.."NormalTexture"],
     flash          = _G[self.name.."Flash"],
-    actionID       = actionIDLabel
+    actionID       = _G[self.name.."ActionID"],
   }
 
   -- provide a reference back to this object for the frame to use in event handlers
   self.button.rxnBtn = self
 
+  -- set the action ID
+  self:SetActionID(id)
+
+  -- register button with ReBinder for keybinding
+  ReBinder:AddKeybindTarget(self.button)
+
   -- initialize
-  self:Configure(parentFrame, config, barIdx, id)
+  self:Configure(parentFrame, config, barIdx)
+end
+
+local function tcopy(t)
+  local r = { }
+  for k, v in pairs(t) do
+    r[k] = (type(v) == "table" and tcopy(v) or v)
+  end
+  return r
 end
 
 function ReActionButton.prototype:Recycle()
   local b = self.button
-  local action = self:GetActionID()
-
-  self.config.actionIDs[self.barIdx] = nil
 
   self:SetKeyBinding(nil)
   self:UpdateDisplay()
   b:UnregisterAllEvents()
+  b:SetParent(ReActionButtonRecycleFrame)
+  b:ClearAllPoints()
+  b:SetPoint("TOPLEFT",0,0)
   b:Hide()
-  b:ClearAllPoints()
-  b:SetParent(ReActionButtonRecycleFrame)
+  self.config = tcopy(self.config) -- ew, but necessary
 end
 
+function ReActionButton.prototype:BarUnlocked()
+  self:ShowGridTmp()
+end
+
+function ReActionButton.prototype:BarLocked()
+  self:HideGridTmp()
+end
 
 
 -- set the button location
@@ -166,10 +190,10 @@
 
 
 -- configuration and setup
-function ReActionButton.prototype:Configure( parentFrame, config, barIdx, id )
+function ReActionButton.prototype:Configure( parentFrame, config, barIdx )
   self.config   = config
   self.barIdx   = barIdx
-  self.showGrid = config.showGrid and 1 or 0
+  self.showGridTmp_ = 0
 
   self.button:ClearAllPoints()
   self.button:SetParent(parentFrame)
@@ -177,12 +201,6 @@
   self:SetupAttributes()
   self:RegisterStaticEvents()
 
-  if id then
-    -- set action ID
-    self:SetActionID(id)
-  else
-    self:ApplyActionID()
-  end
   self:ApplyLayout()
   self:ApplyStyle()
 
@@ -193,7 +211,6 @@
   local b = self.button
   b:SetAttribute("type", "action")
 	b:SetAttribute("shift-type*", ATTRIBUTE_NOOP)
-  b:SetAttribute("alt-type*", ATTRIBUTE_NOOP)
 	b:SetAttribute("checkselfcast", true)
 	b:SetAttribute("useparent-unit", true)
 end
@@ -384,80 +401,19 @@
   return GetBindingKey("CLICK "..self.name..":LeftButton")
 end
 
-function ReActionButton.prototype:ShowAssignKeybinding()
-  local f = ReActionKeybindFrame
-  f:ClearAllPoints()
-  f:SetPoint("BOTTOM", self.button, "TOP", 0, 10)
-  ReActionKeybindFrameButton.keybindTarget = self
-  local k = self:GetKeyBinding()
-  if k then
-    local txt = GetBindingText(k, "KEY_")
-    ReActionKeybindFrameButton:SetText(txt or "")
-  end
-  f:Show()
-end
-
-
-local mouseButtonConvert = {
-  LeftButton = "BUTTON1",
-  RightButton = "BUTTON2",
-  MiddleButton = "BUTTON3",
-  Button4 = "BUTTON4",
-  Button5 = "BUTTON5"
-}
-
-function ReActionButton.prototype:HandleKeybindAssign(button, key, mouseButton)
-  mouseButton = mouseButton and mouseButtonConvert[mouseButton]
-  if mouseButton ~= "BUTTON1" and mouseButton ~= "BUTTON2" then
-    key = key or mouseButton
-    if key == nil or key == "UNKNOWN" or key == "SHIFT" or key == "CTRL" or key == "ALT" then 
-      return
-    end
-    if key == "ESCAPE" then
-      ReActionKeybindFrame:Hide()
-      return
-    end
-		if IsShiftKeyDown() then 
-      key = "SHIFT-"..key
-    end
-		if IsControlKeyDown() then
-			key = "CTRL-"..key
-		end
-		if IsAltKeyDown() then
-			keyPressed = "ALT-"..key
-		end
-    local oldAction = GetBindingAction(key)
-    local oldKey = self:GetKeyBinding()
-    if oldAction then
-      -- can't pop a modal dialog box, will need to think something up
-    end
-    if oldKey == key then
-      SetBinding(key,nil)
-      key = nil
-    end
-    self:SetKeyBinding(key)
-    button:SetText(key and GetBindingText(key, "KEY_") or "")
-    self:UpdateDisplay()
-    SaveBindings(2) -- 2 = character specific bindings... hmm...
-  end
-  button.selected = false
-  this:SetButtonState("NORMAL")
-end
-
-
 -- action ID functions
 function ReActionButton.prototype:SetActionID( id )
-  self.config.actionIDs[self.barIdx] = tonumber(id) -- force data integrity
+  self.actionID = tonumber(id) -- force data integrity
   self:ApplyActionID()
 end
 
 function ReActionButton.prototype:GetActionID()
-  return self.config.actionIDs[self.barIdx]
+  return self.actionID
 end
 
 function ReActionButton.prototype:ApplyActionID()
   local action = tonumber(self:GetActionID())
-  self.button:SetAttribute("action",action or nil)
+  self.button:SetAttribute("action",action)
   self.frames.actionID:SetText(action or "")
 end
 
@@ -488,23 +444,23 @@
 end
 
 function ReActionButton.prototype:ShowGridTmp()
-  self.showGrid = self.showGrid + 1
+  self.showGridTmp_ = self.showGridTmp_ + 1
   self:UpdateVisibility()
 end
 
 function ReActionButton.prototype:HideGridTmp()
-  self.showGrid = self.showGrid - 1
+  self.showGridTmp_ = self.showGridTmp_ - 1
   self:UpdateVisibility()
 end
 
 function ReActionButton.prototype:ShowGrid()
   self.config.showGrid = true
-  self:ShowGridTmp()
+  self:UpdateVisibility()
 end
 
 function ReActionButton.prototype:HideGrid()
   self.config.showGrid = false
-  self:HideGridTmp()
+  self:UpdateVisibility()
 end
 
 
@@ -516,8 +472,11 @@
   if self.config.keyBindLoc then
     local h = f.hotkey
     local loc = self.config.keyBindLoc
+    local top = string.match(loc,"TOP")
+    local bottom = string.match(loc, "BOTTOM")
     h:ClearAllPoints()
-    h:SetPoint(loc,0,0)
+    h:SetWidth(40)
+    h:SetPoint(top or bottom,0,top and 2 or -2)
     local j
     if string.match(loc,"LEFT") then
       j = "LEFT"
@@ -532,8 +491,11 @@
   if self.config.stackCountLoc then
     local c = f.count
     local loc = self.config.stackCountLoc
+    local top = string.match(loc,"TOP")
+    local bottom = string.match(loc, "BOTTOM")
     c:ClearAllPoints()
-    c:SetPoint(loc,0,0)
+    c:SetWidth(40)
+    c:SetPoint(top or bottom,0,top and 2 or -2)
     local j
     if string.match(loc,"LEFT") then
       j = "LEFT"
@@ -699,6 +661,11 @@
   local key = self:GetKeyBinding()
   local txt = GetBindingText(key, "KEY_",1)
 
+  -- abbreviate long key names
+  for pat, rep in pairs(keybindAbbreviations) do
+    txt = string.gsub(txt,pat,rep)
+  end
+   
   if txt then
     f.hotkey:SetText(string.upper(txt))
     self:ColorHotKey()
@@ -775,7 +742,7 @@
   elseif action and HasAction(action) then
     b:GetNormalTexture():SetAlpha(1.0)
     b:Show()
-  elseif self.showGrid > 0 then
+  elseif self.showGridTmp_ > 0 or self.config.showGrid then
     b:GetNormalTexture():SetAlpha(0.5)
     self.frames.cooldown:Hide()
     b:Show()
@@ -795,7 +762,7 @@
 
 function ReActionButton.prototype:UpdateTooltip()
   local action = self:GetActionID()
-	if action and GameTooltip:SetAction(action) then
+	if GameTooltip:IsOwned(self.button) and action and GameTooltip:SetAction(action) then
 		self.tooltipTime = TOOLTIP_UPDATE_TIME
 	else
 		self.tooltipTime = nil