ESOUI SVN TaosGroupUltimate

[/] [trunk/] [TaosGroupUltimate/] [ui/] [CompactSwimlaneList.lua] - Rev 27

Go to most recent revision | Compare with Previous | Blame | View Log

--[[
        Addon: Taos Group Ultimate
        Author: TProg Taonnor
        Created by @Taonnor
]]--

--[[
        Global variables
]]--
local LOG_ACTIVE = false
local SWIMLANES = 6
local ROWS = 6
local REFRESHRATE = 3000 -- ms; RegisterForUpdate is in miliseconds
local TIMEOUT = 4 -- s; GetTimeStamp() is in seconds

--[[
        Class CompactSwimlaneList
]]--
CompactSwimlaneList = {}
CompactSwimlaneList.__index = CompactSwimlaneList

--[[
        Class Members
]]--
CompactSwimlaneList.Name = "TGU-CompactSwimlaneList"
CompactSwimlaneList.IsMocked = false
CompactSwimlaneList.Swimlanes = {}
CompactSwimlaneList.ConnectedPlayers = {}

--[[
        Sets visibility of labels
]]--
function CompactSwimlaneList.RefreshList()
        if (LOG_ACTIVE) then logTrace("CompactSwimlaneList.RefreshList") end

    -- Check all swimlanes
    for i,swimlane in ipairs(CompactSwimlaneList.Swimlanes) do
        CompactSwimlaneList.ClearPlayersFromSwimlane(swimlane)
        end
end

--[[
        Updates list row
]]--
function CompactSwimlaneList.UpdateListRow(player)
        if (LOG_ACTIVE) then 
        logTrace("CompactSwimlaneList.UpdateListRow")
    end

        if (player) then
        local swimLane = CompactSwimlaneList.GetSwimLane(player.UltimateGroup.GroupAbilityId)
        local row = nil

        if (swimLane) then
            row = CompactSwimlaneList.GetSwimLaneRow(swimLane, player.PlayerName)

            -- Update timestamp
            if (row ~= nil) then
                for i,swimlanePlayer in ipairs(swimLane.Players) do
                            if (swimlanePlayer.PlayerName == player.PlayerName) then
                        swimlanePlayer.LastMapPingTimestamp = GetTimeStamp()
                        break
                    end
                    end
            else
                -- Add new player
                local nextFreeRow = 1

                for i,player in ipairs(swimLane.Players) do
                            nextFreeRow = nextFreeRow + 1
                    end

                if (nextFreeRow <= ROWS) then
                    if (LOG_ACTIVE) then 
                        logDebug("CompactSwimlaneList.UpdateListRow, add player " .. tostring(player.PlayerName) .. " to row " .. tostring(nextFreeRow)) 
                    end

                    player.LastMapPingTimestamp = GetTimeStamp()
                    swimLane.Players[nextFreeRow] = player
                    row = swimLane.SwimlaneControl:GetNamedChild("Row" .. nextFreeRow)
                else
                    if (LOG_ACTIVE) then logDebug("CompactSwimlaneList.UpdateListRow, too much players for one swimlane " .. tostring(nextFreeRow)) end
                end
            end
        else
            if (LOG_ACTIVE) then logDebug("CompactSwimlaneList.UpdateListRow, swimlane not found for ultimategroup " .. tostring(ultimateGroup.GroupName)) end
        end

                if (row ~= nil) then
            local playerName = player.PlayerName
            local nameLength = string.len(playerName)

            if (nameLength > 6) then
                playerName = string.sub(playerName, 0, 5) .. ".."
            end

            row:GetNamedChild("SenderNameValueLabel"):SetText(playerName)
            row:GetNamedChild("RelativeUltimateStatusBar"):SetValue(player.RelativeUltimate)

                        if (player.IsPlayerDead) then
                -- Dead Color
                row:GetNamedChild("SenderNameValueLabel"):SetColor(0.5, 0.5, 0.5, 0.8)
                row:GetNamedChild("RelativeUltimateStatusBar"):SetColor(0.8, 0.03, 0.03, 0.7)
            elseif (player.RelativeUltimate == 100) then
                                -- Ready Color
                row:GetNamedChild("SenderNameValueLabel"):SetColor(1, 1, 1, 1)
                row:GetNamedChild("RelativeUltimateStatusBar"):SetColor(0.03, 0.7, 0.03, 0.7)
                        else
                                -- Inprogress Color
                row:GetNamedChild("SenderNameValueLabel"):SetColor(1, 1, 1, 0.8)
                row:GetNamedChild("RelativeUltimateStatusBar"):SetColor(0.03, 0.03, 0.7, 0.7)
                        end

            row:SetHidden(false)
                else
                        if (LOG_ACTIVE) then logDebug("CompactSwimlaneList.UpdateListRow, row nil " .. tostring(player.GroupNumber)) end
                end
        end
end

--[[
        Get swimlane from current SwimLanes
]]--
function CompactSwimlaneList.GetSwimLane(ultimateGroupId)
    if (LOG_ACTIVE) then 
        logTrace("CompactSwimlaneList.GetSwimLane")
        logDebug("ultimateGroupId: " .. tostring(ultimateGroupId))
    end

    if (ultimateGroupId ~= 0) then
        for i,swimLane in ipairs(CompactSwimlaneList.Swimlanes) do
                    if (swimLane.UltimateGroupId == ultimateGroupId) then
                return swimLane
            end
            end

        if (LOG_ACTIVE) then logDebug("CompactSwimlaneList.GetSwimLane, swimLane not found " .. tostring(ultimateGroupId)) end
        return nil
    else
        logError("CompactSwimlaneList.GetSwimLane, ultimateGroupId is 0")
        return nil
    end
end

--[[
        Get Player Row from current players in swimlane
]]--
function CompactSwimlaneList.GetSwimLaneRow(swimLane, playerName)
    if (LOG_ACTIVE) then 
        logTrace("CompactSwimlaneList.GetSwimLaneRow")
        logDebug("swimLane ID: " .. tostring(swimLane.Id))
    end

    if (swimLane) then
        for i,player in ipairs(swimLane.Players) do
            if (LOG_ACTIVE) then logDebug(player.PlayerName .. " == " .. playerName) end
                    if (player.PlayerName == playerName) then
                return swimLane.SwimlaneControl:GetNamedChild("Row" .. i)
            end
            end

        if (LOG_ACTIVE) then logDebug("CompactSwimlaneList.GetSwimLane, player not found " .. tostring(playerName)) end
        return nil
    else
        logError("CompactSwimlaneList.GetSwimLane, swimLane is nil")
        return nil
    end
end

--[[
        Clears all players in swimlane
]]--
function CompactSwimlaneList.ClearPlayersFromSwimlane(swimlane)
    if (LOG_ACTIVE) then 
        logTrace("CompactSwimlaneList.ClearPlayersFromSwimlane")
        logDebug("swimlane ID: " .. tostring(swimlane.Id))
    end

    if (swimlane) then
        for i=1, ROWS, 1 do
            local row = swimlane.SwimlaneControl:GetNamedChild("Row" .. i)
            local swimlanePlayer = swimlane.Players[i]

            if (swimlanePlayer ~= nil) then
                local isPlayerNotGrouped = IsUnitGrouped(swimlanePlayer.PingTag) == false

                if (CompactSwimlaneList.IsMocked) then
                    isPlayerNotGrouped = false
                end

                local isPlayerTimedOut = (GetTimeStamp() - swimlanePlayer.LastMapPingTimestamp) > TIMEOUT
                local isPlayerUltimateNotCorrect = swimlane.UltimateGroupId ~= swimlanePlayer.UltimateGroup.GroupAbilityId

                if (isPlayerNotGrouped or isPlayerTimedOut or isPlayerUltimateNotCorrect) then
                    if (LOG_ACTIVE) then logDebug("Player invalid, hide row: " .. tostring(i)) end

                    row:SetHidden(true)
                    table.remove(swimlane.Players, i)
                end
            else
                if (LOG_ACTIVE) then logDebug("Row empty, hide: " .. tostring(i)) end

                row:SetHidden(true)
            end
        end
    end
end

--[[
        SetControlMovable sets the Movable and MouseEnabled flag in UI elements
]]--
function CompactSwimlaneList.SetControlMovable(isMovable)
    if (LOG_ACTIVE) then 
        logTrace("CompactSwimlaneList.SetControlMovable")
        logDebug("isMovable: " .. tostring(isMovable))
    end

    CompactSwimlaneListControl:SetMovable(isMovable)
        CompactSwimlaneListControl:SetMouseEnabled(isMovable)
end

--[[
        RestorePosition sets CompactSwimlaneList on settings position
]]--
function CompactSwimlaneList.RestorePosition(posX, posY)
    if (LOG_ACTIVE) then 
        logTrace("CompactSwimlaneList.RestorePosition")
        logDebug("posX: " .. tostring(posX), 
                 "posY: " .. tostring(posY))
    end

        CompactSwimlaneListControl:ClearAnchors()
        CompactSwimlaneListControl:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, posX, posY)
end

--[[
        OnCompactSwimlaneListMoveStop saves current CompactSwimlaneList position to settings
]]--
function CompactSwimlaneList.OnCompactSwimlaneListMoveStop()
    if (LOG_ACTIVE) then logTrace("CompactSwimlaneList.OnCompactSwimlaneListMoveStop") end

        local left = CompactSwimlaneListControl:GetLeft()
        local top = CompactSwimlaneListControl:GetTop()
        
    SettingsHandler.SavedVariables.PosX = left
    SettingsHandler.SavedVariables.PosY = top

    if (LOG_ACTIVE) then 
        logDebug("PosX set: " .. tostring(SettingsHandler.SavedVariables.PosX), 
                 "PosY set: " .. tostring(SettingsHandler.SavedVariables.PosY))
    end
end

--[[
        SetControlHidden sets hidden on control
]]--
function CompactSwimlaneList.SetControlHidden()
    if (LOG_ACTIVE) then 
        logTrace("CompactSwimlaneList.SetControlHidden")
    end

    local isHidden = SettingsHandler.IsCompactSwimlaneListVisible() == false
    if (LOG_ACTIVE) then logDebug("isHidden: " .. tostring(isHidden)) end

    if (isHidden) then
        -- Start timeout timer
            EVENT_MANAGER:UnregisterForUpdate(CompactSwimlaneList.Name)

        CALLBACK_MANAGER:UnregisterCallback("TGU-GroupChanged", CompactSwimlaneList.RefreshList)
        CALLBACK_MANAGER:UnregisterCallback("TGU-MapPingChanged", CompactSwimlaneList.UpdateListRow)
        CALLBACK_MANAGER:UnregisterCallback("TGU-MovableChanged", CompactSwimlaneList.SetControlMovable)
        CALLBACK_MANAGER:UnregisterCallback("TGU-SwimlaneUltimateGroupIdChanged", CompactSwimlaneList.SetSwimlaneUltimate)
    else
        CompactSwimlaneList.SetControlMovable(SettingsHandler.SavedVariables.Movable)
        CompactSwimlaneList.RestorePosition(SettingsHandler.SavedVariables.PosX, SettingsHandler.SavedVariables.PosY)

        -- Start timeout timer
            EVENT_MANAGER:RegisterForUpdate(CompactSwimlaneList.Name, REFRESHRATE, CompactSwimlaneList.RefreshList)

        CALLBACK_MANAGER:RegisterCallback("TGU-GroupChanged", CompactSwimlaneList.RefreshList)
        CALLBACK_MANAGER:RegisterCallback("TGU-MapPingChanged", CompactSwimlaneList.UpdateListRow)
        CALLBACK_MANAGER:RegisterCallback("TGU-MovableChanged", CompactSwimlaneList.SetControlMovable)
        CALLBACK_MANAGER:RegisterCallback("TGU-SwimlaneUltimateGroupIdChanged", CompactSwimlaneList.SetSwimlaneUltimate)
    end

    CompactSwimlaneListControl:SetHidden(isHidden)
end

--[[
        OnSwimlaneHeaderClicked called on header clicked
]]--
function CompactSwimlaneList.OnSwimlaneHeaderClicked(button, swimlaneId)
    if (LOG_ACTIVE) then 
        logTrace("CompactSwimlaneList.OnSwimlaneHeaderClicked")
        logDebug(button, tostring(swimlaneId))
    end

    if (button ~= nil) then
        CALLBACK_MANAGER:RegisterCallback("TGU-SetUltimateGroup", CompactSwimlaneList.OnSetUltimateGroup)
        CALLBACK_MANAGER:FireCallbacks("TGU-ShowUltimateGroupMenu", button, swimlaneId)
    else
        logError("CompactSwimlaneList.OnSwimlaneHeaderClicked, button nil")
    end
end

--[[
        OnSetUltimateGroup called on header clicked
]]--
function CompactSwimlaneList.OnSetUltimateGroup(group, swimlaneId)
    if (LOG_ACTIVE) then 
        logTrace("CompactSwimlaneList.OnSetUltimateGroup")
        logDebug("group.GroupName: " .. group.GroupName, swimlaneId)
    end

    CALLBACK_MANAGER:UnregisterCallback("TGU-SetUltimateGroup", CompactSwimlaneList.OnSetUltimateGroup)

    if (group ~= nil and swimlaneId ~= nil and swimlaneId >= 1 and swimlaneId <= 6) then
        SettingsHandler.SetSwimlaneUltimateGroupIdSettings(swimlaneId, group)
    else
        logError("UltimateGroupMenu.ShowUltimateGroupMenu, group nil or swimlaneId invalid")
    end
end

--[[
        SetSwimlaneUltimate sets the swimlane header icon in base of ultimateGroupId
]]--
function CompactSwimlaneList.SetSwimlaneUltimate(swimlaneId, ultimateGroup)
    if (LOG_ACTIVE) then 
        logTrace("CompactSwimlaneList.SetSwimlaneUltimate")
        logDebug("swimlaneId: " .. tostring(swimlaneId),
                 "ultimateGroup.GroupName: " .. tostring(ultimateGroup.GroupName))
    end

    local swimlaneObject = CompactSwimlaneList.Swimlanes[swimlaneId]
    local iconControl = swimlaneObject.SwimlaneControl:GetNamedChild("Header"):GetNamedChild("SelectorButtonControl"):GetNamedChild("Icon")

    if (ultimateGroup ~= nil and iconControl ~= nil) then
        iconControl:SetTexture(GetAbilityIcon(ultimateGroup.GroupAbilityId))

        swimlaneObject.UltimateGroupId = ultimateGroup.GroupAbilityId
        CompactSwimlaneList.ClearPlayersFromSwimlane(swimlaneObject)
    else
        logError("CompactSwimlaneList.SetSwimlaneUltimateIcon, icon is " .. tostring(icon) .. ";" .. tostring(iconControl) .. ";" .. tostring(ultimateGroup))
    end
end

--[[
        CreateCompactSwimlaneListHeaders creates swimlane list headers
]]--
function CompactSwimlaneList.CreateCompactSwimlaneListHeaders()
    if (LOG_ACTIVE) then logTrace("CompactSwimlaneList.CreateCompactSwimlaneListHeaders") end

        for i=1, SWIMLANES, 1 do
        local ultimateGroupId = SettingsHandler.SavedVariables.SwimlaneUltimateGroupIds[i]
        local ultimateGroup = UltimateGroupHandler.GetUltimateGroupByAbilityId(ultimateGroupId)
        local swimlaneControlName = "Swimlane" .. tostring(i)
        local swimlaneControl = CompactSwimlaneListControl:GetNamedChild(swimlaneControlName)
        local icon = swimlaneControl:GetNamedChild("Header"):GetNamedChild("SelectorButtonControl"):GetNamedChild("Icon")
        local button = swimlaneControl:GetNamedChild("Header"):GetNamedChild("SelectorButtonControl"):GetNamedChild("Button")

        if (ultimateGroup ~= nil and icon ~= nil and button ~= nil and swimlaneControl ~= nil) then
            if (LOG_ACTIVE) then 
                logDebug("Create Swimlane: " .. tostring(i),
                         "ultimateGroup.GroupName: " .. tostring(ultimateGroup.GroupName),
                         "swimlaneControlName: " .. tostring(swimlaneControlName))
            end

            icon:SetTexture(GetAbilityIcon(ultimateGroup.GroupAbilityId))
            button:SetHandler("OnClicked", function() CompactSwimlaneList.OnSwimlaneHeaderClicked(button, i) end)

            local swimLane = {}
            swimLane.Id = i
            swimLane.UltimateGroupId = ultimateGroup.GroupAbilityId
            swimLane.SwimlaneControl = swimlaneControl
            swimLane.Players = {}

            CompactSwimlaneList.CreateCompactSwimlaneListRows(swimlaneControl)

                CompactSwimlaneList.Swimlanes[i] = swimLane
        else
            logError("CompactSwimlaneList.CreateCompactSwimlaneListHeaders, controls nil " .. tostring(swimlaneControl) .. ";" .. tostring(icon) .. ";" .. tostring(ultimateGroup))
        end
        end
end

--[[
        CreateCompactSwimlaneListRows creates swimlane lsit rows
]]--
function CompactSwimlaneList.CreateCompactSwimlaneListRows(swimlaneControl)
    if (LOG_ACTIVE) then logTrace("CompactSwimlaneList.CreateCompactSwimlaneListRows") end

    if (swimlaneControl ~= nil) then
            for i=1, ROWS, 1 do
                    local row = CreateControlFromVirtual("$(parent)Row", swimlaneControl, "CompactGroupUltimateSwimlaneRow", i)
            if (LOG_ACTIVE) then logDebug("Row created " .. row:GetName()) end

                    row:SetHidden(true) -- initial not visible

                    if i == 1 then
                row:SetAnchor(TOPLEFT, swimlaneControl, TOPLEFT, 0, 50)
            else
                row:SetAnchor(TOP, lastRow, BOTTOM, 0, -1)
            end

                    lastRow = row
            end
    else
        logError("CompactSwimlaneList.CreateCompactSwimlaneListRows, swimlaneControl nil.")
    end
end

--[[
        Initialize initializes CompactSwimlaneList
]]--
function CompactSwimlaneList.Initialize(isMovable, posX, posY, isMocked)
    if (LOG_ACTIVE) then 
        logTrace("CompactSwimlaneList.Initialize")
        logDebug("isMovable: " .. tostring(isMovable), 
                 "posX: " .. tostring(posX), 
                 "posY: " .. tostring(posY))
    end

    CompactSwimlaneList.IsMocked = isMocked

    CompactSwimlaneList.SetControlMovable(isMovable)
    CompactSwimlaneList.RestorePosition(posX, posY)
    CompactSwimlaneList.CreateCompactSwimlaneListHeaders()
    CompactSwimlaneList.SetControlHidden()

    CALLBACK_MANAGER:RegisterCallback("TGU-StyleChanged", CompactSwimlaneList.SetControlHidden)
end

Go to most recent revision | Compare with Previous | Blame