Skip to content

Commit

Permalink
creation/destruction of frames
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuller committed Nov 21, 2010
1 parent ca7c019 commit 43b11e0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 57 deletions.
61 changes: 36 additions & 25 deletions ThunderCougarFalconBars.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
local name, addonTable = ...
local name, addonTable = ...

TCFB = setmetatable({}, {__index = addonTable})

TCFB.ToggleLock = function(self)
self.MajorTom:setLock(not self.MajorTom:getLock())
end

TCFB.Create = function(self, id)
local id = tonumber(id) or id

local bar = TCFB.Bar:New(id, {
default = {
enable = true,
show = true,
alpha = 1,
scale = 1,
point = ('CENTER;0;%d'):format((id - 1) * -36),
anchor = false,
},
alt = {
enable = id < 4,
alpha = 0.5,
scale = 1.5,
point = ('TOPLEFT;0;%d'):format((id - 1) * -36),
anchor = false,
}
})
bar:SetSize(400, 36)
end

TCFB.Destroy = function(self, id)
TCFB.Bar:GetBar(id):Free()
end

--add slash commands
SLASH_ThunderCougarFalconBars1 = '/thundercougarfalconbars'
SLASH_ThunderCougarFalconBars2 = '/tcfb'
SLASH_ThunderCougarFalconBars3 = '/tc'
SlashCmdList['ThunderCougarFalconBars'] = function(msg)
local cmd, args = string.split(' ', msg:lower())
local info = {string.split(' ', msg:lower())}
local cmd = info[1]

if cmd == 'lock' then
TCFB:ToggleLock()
elseif cmd == 'create' then
TCFB:Create(select(2, unpack(info)))
elseif cmd == 'destroy' then
TCFB:Destroy(select(2, unpack(info)))
end
end

Expand All @@ -22,28 +55,6 @@ do
TCFB.MajorTom:setLock(false)

for id = 1, 4 do
local bar = TCFB.Bar:New(id, {
default = {
enable = true,
show = true,
alpha = 1,
scale = 1,
point = ('CENTER;0;%d'):format((id - 1) * -36),
anchor = false,
},
alt = {
enable = id < 4,
alpha = 0.5,
scale = 1.5,
point = ('TOPLEFT;0;%d'):format((id - 1) * -36),
anchor = false,
}
})
bar:SetSize(400, 36)

-- local bg = bar:CreateTexture(nil, 'BACKGROUND')
-- bg:SetAllPoints(bar)
-- bg:SetTexture(0, 0.5, 0, 0.5)
-- bar.bg = bg
TCFB:Create(id)
end
end
55 changes: 42 additions & 13 deletions core/bar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local TCFB = select(2, ...)
TCFB.Bar = Bar

local DELIMITER = ';' --compact settings delimiter
local frames = {}
local active, destroyed = {}, {}

local function frame_Create(id)
local frame = CreateFrame('Frame', nil, UIParent, 'SecureHandlerStateTemplate')
Expand All @@ -29,6 +29,14 @@ local function frame_Create(id)
self:GetFrameRef('dragFrame'):SetAttribute('state-lock', newstate)
]])

frame:SetAttribute('_onstate-destroy', [[
self:CallMethod('ForDocked', 'ClearAnchor')
self:CallMethod('SetUserPlaced', false)
self:ChildUpdate('state-destroy', newstate)
self:GetFrameRef('dragFrame'):SetAttribute('state-destroy', newstate)
]])

-- load many state attributes
frame:SetAttribute('lodas', [[
local state = self:GetAttribute('state-main') or 'default'
Expand Down Expand Up @@ -124,28 +132,47 @@ local function frame_Create(id)
end

function Bar:New(frameId, settings)
local f = self:Restore(frameId) or self:Create(frameId)
f:LoadSettings(settings)

TCFB.MajorTom:addFrame(f)
active[frameId] = f

return f
end

function Bar:Create(frameId)
local f = self:Bind(frame_Create(frameId))
f:SetAttribute('myAttributes', 'enable,show,scale,alpha,point,anchor')
f:SetFrameRef('dragFrame', TCFB.DragFrame:New(f))
f.sets = settings

f:LoadSettings()
TCFB.MajorTom:addFrame(f)
frames[frameId] = f

return f
end

function Bar:Restore(frameId)
local f = destroyed[frameId]
if f then
destroyed[frameId] = nil
return f
end
end

function Bar:Free()
active[self:GetAttribute('id')] = nil
TCFB.MajorTom:removeFrame(self)
destroyed[self:GetAttribute('id')] = self
end


--[[ frame access ]]--


function Bar:GetAll()
return pairs(frames)
return pairs(active)
end

function Bar:GetBar(frameId)
return frames[tonumber(frameId) or frameId]
return active[tonumber(frameId) or frameId]
end

function Bar:ForAll(method, ...)
Expand All @@ -158,7 +185,7 @@ end
function Bar:ForDocked(method, ...)
local action = type(method) == 'string' and self[method] or method
for _,f in self:GetAll() do
if select(2, f:GetAnchor()) == self then
if select(2, f:GetAnchor()) == tostring(self:GetAttribute('id')) then
action(f, ...)
end
end
Expand Down Expand Up @@ -199,11 +226,13 @@ function Bar:Save(attribute, value, state)
if not stateSets then
self.sets[state] = {[attribute] = value}
end

stateSets[attribute] = value
end

function Bar:LoadSettings()
for state, attributes in pairs(self.sets) do
function Bar:LoadSettings(settings)
self.sets = settings
for state, attributes in pairs(settings) do
for attribute, value in pairs(attributes) do
self:Set(attribute, value, state)
end
Expand Down Expand Up @@ -299,12 +328,12 @@ end
function Bar:GetAnchor()
local anchor = self:Get('anchor')
if anchor then
local point, frameId, relPoint, x, y = string.split(';', anchor)
return point, self:GetBar(frameId), relPoint, x, y
return string.split(';', anchor)
end
end

function Bar:ClearAnchor()
self:SaveRelPosition()
self:Set('anchor', false)
end

Expand Down
4 changes: 3 additions & 1 deletion core/dragFrame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ end
function DragFrame:OnAttributeChanged(...)
local enabled = self:GetAttribute('state-enable')
local locked = self:GetAttribute('state-lock')
if enabled and not locked then
local destroyed = self:GetAttribute('state-destroy')

if enabled and not(locked or destroyed) then
self:Show()
else
self:Hide()
Expand Down
36 changes: 18 additions & 18 deletions core/majorTom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,30 @@ controller:SetAttribute('_onstate-lock', [[

--adds the given frame to control by majorTom
controller:SetAttribute('addFrame', [[
local f = self:GetFrameRef('addFrame')
local frameId = ...
local f = self:GetFrameRef('frame-' .. frameId)
f:SetParent(self)
f:SetAttribute('state-main', self:GetAttribute('state-main'))
f:SetAttribute('state-lock', self:GetAttribute('state-lock'))
f:SetAttribute('state-destroy', nil)
]])

--removes the frame from control by majorTom
controller:SetAttribute('remFrame', [[
local f = ...
if myFrames then
myFrames[f:GetAttribute('id')] = nil
end
controller:SetAttribute('delFrame', [[
local f = self:GetFrameRef('delFrame')
f:SetAttribute('state-destroy', true)
f:SetParent(nil)
f:Hide()
]])

controller:SetAttribute('getFrame', [[
local frameId = ...
if frameId then
return self:GetFrameRef('frame-' .. frameId)
local f = self:GetFrameRef('frame-' .. frameId)
if not f:GetAttribute('destroy') then
return f
end
end
]])

Expand All @@ -76,20 +81,15 @@ local TCFB = select(2, ...)
TCFB.MajorTom = {
--add frame to state control
addFrame = function(self, frame)
controller:SetFrameRef('addFrame', frame)
controller:SetFrameRef('frame-' .. frame:GetAttribute('id'), frame)
controller:Execute([[
self:RunAttribute('addFrame', self:GetFrameRef('addFrame'))
]])
local frameId = frame:GetAttribute('id')
controller:SetFrameRef('frame-' .. frameId, frame)
controller:Execute(string.format([[ self:RunAttribute('addFrame', '%s') ]], frameId))
end,

--remove frame from state control
removeFrame = function(self, frame)
controller:SetFrameRef('remFrame', frame)
controller:SetFrameRef('frame-' .. frame:GetAttribute('id'), nil)
controller:Execute([[
self:RunAttribute('remFrame', self:GetFrameRef('remFrame'))
]])
controller:SetFrameRef('delFrame', frame)
controller:Execute([[ self:RunAttribute('delFrame') ]])
end,

--updates the state driver for groundControl
Expand Down

0 comments on commit 43b11e0

Please sign in to comment.