strip some code from the actionbar module and move it to a generic ButtonBar module to be used for the stance and pet bar

This commit is contained in:
Hendrik Leppkes
2008-02-16 16:11:49 +00:00
parent b95223a020
commit e7841ed4d5
5 changed files with 209 additions and 130 deletions
+4 -76
View File
@@ -1,11 +1,9 @@
--[[ $Id$ ]]
local Bar = Bartender4.Bar.prototype
local ActionBar = setmetatable({}, {__index = Bar})
local ButtonBar = Bartender4.ButtonBar.prototype
local ActionBar = setmetatable({}, {__index = ButtonBar})
Bartender4.ActionBar = ActionBar
local math_floor = math.floor
--[[===================================================================================
ActionBar Options
===================================================================================]]--
@@ -18,9 +16,7 @@ do
local optionMap, getBar, callFunc
-- maps option keys to function names
optionMap = {
padding = "Padding",
buttons = "Buttons",
rows = "Rows",
enabled = "Enabled",
grid = "Grid",
style = "Style",
@@ -64,7 +60,7 @@ end
function module:GetOptionsObject()
if not self.baroptions then
local obj = Bar.GetOptionObject(self)
local obj = ButtonBar.GetOptionObject(self)
local cat_general = {
enabled ={
@@ -75,15 +71,6 @@ function module:GetOptionsObject()
set = optSetter,
get = optGetter,
},
padding = {
order = 40,
type = "range",
name = "Padding",
desc = "Configure the padding of the buttons.",
min = -10, max = 20, step = 1,
set = optSetter,
get = optGetter,
},
desc = {
order = 50,
type = "header",
@@ -115,15 +102,6 @@ function module:GetOptionsObject()
set = optSetter,
get = optGetter,
},
rows = {
order = 70,
name = "Rows",
desc = "Number of rows.",
type = "range",
min = 1, max = 12, step = 1,
set = optSetter,
get = optGetter,
},
}
obj:AddElementGroup("general", cat_general)
@@ -157,7 +135,7 @@ end
-- Apply the specified config to the bar and refresh all settings
function ActionBar:ApplyConfig(config)
Bar.ApplyConfig(self, config)
ButtonBar.ApplyConfig(self, config)
config = self.config
if not config.position then initialPosition(self) end
@@ -199,48 +177,11 @@ function ActionBar:UpdateButtons(numbuttons)
self:SetGrid()
end
-- align the buttons and correct the size of the bar overlay frame
function ActionBar:UpdateButtonLayout()
local numbuttons = self:GetButtons()
local buttons = self.buttons
local pad = self:GetPadding()
local Rows = self:GetRows()
local ButtonPerRow = math_floor(numbuttons / Rows + 0.5) -- just a precaution
Rows = math_floor(numbuttons / ButtonPerRow + 0.5)
self:SetSize((36 + pad) * ButtonPerRow - pad + 8, (36 + pad) * Rows - pad + 8)
-- anchor button 1 to the topleft corner of the bar
buttons[1]:ClearSetPoint("TOPLEFT", self, "TOPLEFT", 6, -3)
-- and anchor all other buttons relative to our button 1
for i = 2, numbuttons do
-- jump into a new row
if ((i-1) % ButtonPerRow) == 0 then
buttons[i]:ClearSetPoint("TOPLEFT", buttons[i-ButtonPerRow], "BOTTOMLEFT", 0, -pad)
-- align to the previous button
else
buttons[i]:ClearSetPoint("TOPLEFT", buttons[i-1], "TOPRIGHT", pad, 0)
end
end
end
--[[===================================================================================
ActionBar Config Interface
===================================================================================]]--
-- get the current padding
function ActionBar:GetPadding()
return self.config.padding
end
-- set the padding and refresh layout
function ActionBar:SetPadding(pad)
if pad ~= nil then
self.config.padding = pad
end
self:UpdateButtonLayout()
end
-- get the current number of buttons
function ActionBar:GetButtons()
@@ -250,19 +191,6 @@ end
-- set the number of buttons and refresh layout
ActionBar.SetButtons = ActionBar.UpdateButtons
-- get the current number of rows
function ActionBar:GetRows()
return self.config.rows
end
-- set the number of rows and refresh layout
function ActionBar:SetRows(rows)
if rows ~= nil then
self.config.rows = rows
end
self:UpdateButtonLayout()
end
function ActionBar:GetStyle()
return self.config.style
end
+2 -4
View File
@@ -8,8 +8,6 @@ local abdefaults = Bartender4:Merge({
['**'] = {
enabled = true,
buttons = 12,
padding = 2,
rows = 1,
hidemacrotext = false,
showgrid = false,
style = "dream",
@@ -45,7 +43,7 @@ local abdefaults = Bartender4:Merge({
[10] = {
enabled = false,
},
}, Bartender4.Bar.defaults)
}, Bartender4.ButtonBar.defaults)
local defaults = {
profile = {
@@ -223,7 +221,7 @@ end
-- Creates a new bar object based on the id and the specified config
function BT4ActionBars:Create(id, config)
local id = tostring(id)
local bar = setmetatable(Bartender4.Bar:Create(id, "SecureStateHeaderTemplate", config), ActionBar_MT)
local bar = setmetatable(Bartender4.ButtonBar:Create(id, "SecureStateHeaderTemplate", config), ActionBar_MT)
bar.module = self
self:CreateBarOption(id, self:GetOptionsTable())
+46 -50
View File
@@ -117,59 +117,55 @@ end
local options
function Bar:GetOptionObject()
if not options then
local otbl = {
general = {
type = "group",
cmdInline = true,
name = "General Settings",
order = 1,
args = {
show = {
order = 5,
type = "toggle",
name = "Show",
desc = "Show/Hide the bar.",
get = optGetter,
set = optSetter,
},
styleheader = {
order = 10,
type = "header",
name = "Bar Style & Layout",
},
alpha = {
order = 20,
name = "Alpha",
desc = "Configure the alpha of the bar.",
type = "range",
min = .1, max = 1, bigStep = 0.1,
get = optGetter,
set = optSetter,
},
scale = {
order = 30,
name = "Scale",
desc = "Configure the scale of the bar.",
type = "range",
min = .1, max = 2, step = 0.05, bigStep = 0.1,
get = optGetter,
set = optSetter,
},
local otbl = {
general = {
type = "group",
cmdInline = true,
name = "General Settings",
order = 1,
args = {
show = {
order = 5,
type = "toggle",
name = "Show",
desc = "Show/Hide the bar.",
get = optGetter,
set = optSetter,
},
styleheader = {
order = 10,
type = "header",
name = "Bar Style & Layout",
},
alpha = {
order = 20,
name = "Alpha",
desc = "Configure the alpha of the bar.",
type = "range",
min = .1, max = 1, bigStep = 0.1,
get = optGetter,
set = optSetter,
},
scale = {
order = 30,
name = "Scale",
desc = "Configure the scale of the bar.",
type = "range",
min = .1, max = 2, step = 0.05, bigStep = 0.1,
get = optGetter,
set = optSetter,
},
},
align = {
type = "group",
cmdInline = true,
name = "Alignment",
order = 10,
args = {},
}
},
align = {
type = "group",
cmdInline = true,
name = "Alignment",
order = 10,
args = {},
}
options = Bartender4:NewOptionObject(otbl)
end
return options
}
return Bartender4:NewOptionObject(otbl)
end
--[[===================================================================================
+1
View File
@@ -19,6 +19,7 @@ Options.lua
## Prototypes ##
ButtonStyle.lua
Bar.lua
ButtonBar.lua
Button.lua
## Modules ##
+156
View File
@@ -0,0 +1,156 @@
--[[ $Id: ActionBars.lua 60964 2008-02-11 18:12:12Z nevcairiel $ ]]
--[[ Generic Template for a Bar which contains Buttons ]]
local Bar = Bartender4.Bar.prototype
local ButtonBar = setmetatable({}, {__index = Bar})
local ButtonBar_MT = {__index = ButtonBar}
local defaults = Bartender4:Merge({
['**'] = {
padding = 2,
rows = 1,
}
}, Bartender4.Bar.defaults)
Bartender4.ButtonBar = {}
Bartender4.ButtonBar.prototype = ButtonBar
Bartender4.ButtonBar.defaults = defaults
function Bartender4.ButtonBar:Create(id, template, config)
local bar = setmetatable(Bartender4.Bar:Create(id, template, config), ButtonBar_MT)
return bar
end
--[[===================================================================================
Bar Options
===================================================================================]]--
-- option utilty functions
local optGetter, optSetter
do
local getBar, optionMap, callFunc
local barregistry = Bartender4.Bar.barregistry
-- maps option keys to function names
optionMap = {
rows = "Rows",
padding = "Padding",
}
-- retrieves a valid bar object from the barregistry table
function getBar(id)
local bar = barregistry[tostring(id)]
assert(bar, "Invalid bar id in options table.")
return bar
end
-- calls a function on the bar
function callFunc(bar, type, option, ...)
local func = type .. (optionMap[option] or option)
assert(bar[func], "Invalid get/set function.")
return bar[func](bar, ...)
end
-- universal function to get a option
function optGetter(info)
local bar = getBar(info[2])
local option = info[#info]
return callFunc(bar, "Get", option)
end
-- universal function to set a option
function optSetter(info, ...)
local bar = getBar(info[2])
local option = info[#info]
return callFunc(bar, "Set", option, ...)
end
end
local options
function ButtonBar:GetOptionObject()
local obj = Bar.GetOptionObject()
local otbl_general = {
padding = {
order = 40,
type = "range",
name = "Padding",
desc = "Configure the padding of the buttons.",
min = -10, max = 20, step = 1,
set = optSetter,
get = optGetter,
},
rows = {
order = 70,
name = "Rows",
desc = "Number of rows.",
type = "range",
min = 1, max = 12, step = 1,
set = optSetter,
get = optGetter,
},
}
obj:AddElementGroup("general", otbl_general)
return obj
end
function ButtonBar:ApplyConfig(config)
Bar.ApplyConfig(self, config)
-- any module inherting this template should call UpdateButtonLayout after setting up its buttons, we cannot call it here
--self:UpdateButtonLayout()
end
-- get the current padding
function ButtonBar:GetPadding()
return self.config.padding
end
-- set the padding and refresh layout
function ButtonBar:SetPadding(pad)
if pad ~= nil then
self.config.padding = pad
end
self:UpdateButtonLayout()
end
-- get the current number of rows
function ButtonBar:GetRows()
return self.config.rows
end
-- set the number of rows and refresh layout
function ButtonBar:SetRows(rows)
if rows ~= nil then
self.config.rows = rows
end
self:UpdateButtonLayout()
end
local math_floor = math.floor
-- align the buttons and correct the size of the bar overlay frame
function ButtonBar:UpdateButtonLayout()
local buttons = self.buttons
local numbuttons = #buttons
local pad = self:GetPadding()
local Rows = self:GetRows()
local ButtonPerRow = math_floor(numbuttons / Rows + 0.5) -- just a precaution
Rows = math_floor(numbuttons / ButtonPerRow + 0.5)
self:SetSize((36 + pad) * ButtonPerRow - pad + 8, (36 + pad) * Rows - pad + 8)
-- anchor button 1 to the topleft corner of the bar
buttons[1]:ClearSetPoint("TOPLEFT", self, "TOPLEFT", 6, -3)
-- and anchor all other buttons relative to our button 1
for i = 2, numbuttons do
-- jump into a new row
if ((i-1) % ButtonPerRow) == 0 then
buttons[i]:ClearSetPoint("TOPLEFT", buttons[i-ButtonPerRow], "BOTTOMLEFT", 0, -pad)
-- align to the previous button
else
buttons[i]:ClearSetPoint("TOPLEFT", buttons[i-1], "TOPRIGHT", pad, 0)
end
end
end