ESOUI SVN TaosGroupUltimate

[/] [trunk/] [TaosGroupUltimate/] [ui/] [SwimlaneList.lua] - Rev 14

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 SwimlaneList
]]--
SwimlaneList = {}
SwimlaneList.__index = SwimlaneList

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

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

    local currentTimestamp = GetTimeStamp()
    
    -- Get players to remove that not anymore in group or timed out and hide their row
    for i,swimlane in ipairs(SwimlaneList.Swimlanes) do
        for j=#swimlane.Players, 1, -1 do
            local isUnitGrouped = IsUnitGrouped(swimlane.Players[j].PingTag)
            local timeoutTime = (currentTimestamp - swimlane.Players[j].LastMapPingTimestamp)
        
            if (SwimlaneList.IsMocked) then
                isUnitGrouped = true
            end

            if (LOG_ACTIVE) then 
                logDebug("swimlane: " .. tostring(swimlane.Id),
                         "player.PlayerName: " .. tostring(swimlane.Players[j].PlayerName),
                         "isUnitGrouped: " .. tostring(isUnitGrouped),
                         "timeoutTime: " .. tostring(timeoutTime))
            end

            if (isUnitGrouped == false or timeoutTime > TIMEOUT) then
                local row = SwimlaneList.GetSwimLaneRow(swimlane, swimlane.Players[j].PlayerName)

                if (row ~= nil) then
                    if (LOG_ACTIVE) then logDebug("SwimlaneList.RefreshList, hide row " .. row:GetName() .. 
                                                  " of swimlane " .. swimlane.Id .. 
                                                  " and remove player " .. swimlane.Players[j].PlayerName) end
                    row:SetHidden(true)
                    table.remove(swimlane.Players, j)
                end
            end
        end
        end
end

--[[
        Updates list row
]]--
function SwimlaneList.UpdateListRow(player)
        if (LOG_ACTIVE) then 
        logTrace("SwimlaneList.UpdateListRow")
--        logDebug("player.PingTag: " .. tostring(player.PingTag),
--                 "player.GroupNumber: " .. tostring(player.GroupNumber), 
--                 "player.PlayerName: " .. tostring(player.PlayerName), 
--                 "player.IsPlayerDead: " .. tostring(player.IsPlayerDead), 
--                 "player.AbilityID: " .. tostring(player.AbilityID), 
--                 "player.UltimateName: " .. tostring(player.UltimateName), 
--                 "player.UltimateIcon: " .. tostring(player.UltimateIcon), 
--                 "player.RelativeUltimate: " .. tostring(player.RelativeUltimate))
    end

        if (player) then
        local ultimateGroup = UltimateGroupHandler.GetUltimateGroupByAbilityId(player.AbilityID)
        local swimLane = SwimlaneList.GetSwimLane(ultimateGroup.GroupAbitiyId)
        local row = nil

        if (swimLane) then
            row = SwimlaneList.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()
                    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("SwimlaneList.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 logError("SwimlaneList.UpdateListRow, too much players for one swimlane " .. tostring(nextFreeRow)) end
                end
            end
        else
            if (LOG_ACTIVE) then logError("SwimlaneList.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 > 10) then
                playerName = string.sub(playerName, 0, 10) .. "..."
            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 logError("SwimlaneList.UpdateListRow, row nil " .. tostring(player.GroupNumber)) end
                end
        end
end

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

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

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

--[[
        Get Player Row from current players in swimlane
]]--
function SwimlaneList.GetSwimLaneRow(swimLane, playerName)
    if (LOG_ACTIVE) then 
        logTrace("SwimlaneList.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("SwimlaneList.GetSwimLane, player not found " .. tostring(playerName)) end
        return nil
    else
        if (LOG_ACTIVE) then logError("SwimlaneList.GetSwimLane, swimLane is nil") end
        return nil
    end
end

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

    if (swimlane) then
        for i=1, ROWS, 1 do
            local row = swimlane.SwimlaneControl:GetNamedChild("Row" .. i)
            row:SetHidden(true)
        end
    end
end

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

    SwimlaneListControl:SetMovable(isMovable)
        SwimlaneListControl:SetMouseEnabled(isMovable)
end

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

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

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

        local left = SwimlaneListControl:GetLeft()
        local top = SwimlaneListControl: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 SwimlaneList.SetControlHidden(isHidden)
    if (LOG_ACTIVE) then 
        logTrace("SwimlaneList.SetControlHidden")
        logDebug("isHidden: " .. tostring(isHidden))
    end

    SwimlaneListControl:SetHidden(isHidden)
end

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

    local swimlaneObject = SwimlaneList.Swimlanes[swimlaneId]
    local iconControl = swimlaneObject.SwimlaneControl:GetNamedChild("Header"):GetNamedChild("Border"):GetNamedChild("Icon")
    local labelControl = swimlaneObject.SwimlaneControl:GetNamedChild("Header"):GetNamedChild("UltimateLabel")

    if (ultimateGroup ~= nil and iconControl ~= nil and labelControl ~= nil) then
        iconControl:SetTexture(GetAbilityIcon(ultimateGroup.GroupAbitiyId))
        labelControl:SetText(ultimateGroup.GroupName)

        swimlaneObject.UltimateGroupId = ultimateGroup.GroupAbitiyId
        SwimlaneList.ClearPlayersFromSwimlane(swimlaneObject)
    else
        if (LOG_ACTIVE) then 
            logError("SwimlaneList.SetSwimlaneUltimateIcon, icon is " .. 
                     tostring(icon) .. ";" .. 
                     tostring(iconControl) .. ";" .. 
                     tostring(ultimateGroup))
        end
    end
end

--[[
        CreateSwimLaneListHeaders creates swimlane list headers
]]--
function SwimlaneList.CreateSwimLaneListHeaders()
    if (LOG_ACTIVE) then logTrace("SwimlaneList.CreateSwimLaneListHeaders") 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 = SwimlaneListControl:GetNamedChild(swimlaneControlName)
        local icon = swimlaneControl:GetNamedChild("Header"):GetNamedChild("Border"):GetNamedChild("Icon")
        local label = swimlaneControl:GetNamedChild("Header"):GetNamedChild("UltimateLabel")

        if (ultimateGroup ~= nil and icon ~= nil and label ~= 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.GroupAbitiyId))
            label:SetText(ultimateGroup.GroupName)

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

            SwimlaneList.CreateSwimlaneListRows(swimlaneControl)

                SwimlaneList.Swimlanes[i] = swimLane
        else
            if (LOG_ACTIVE) then 
                logError("SwimlaneList.CreateSwimLaneListHeaders, controls nil " .. 
                         tostring(swimlaneControl) .. ";" .. 
                         tostring(icon) .. ";" .. 
                         tostring(label) .. ";" .. 
                         tostring(ultimateGroup))
            end
        end
        end
end

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

    if (swimlaneControl ~= nil) then
            for i=1, ROWS, 1 do
                    local row = CreateControlFromVirtual("$(parent)Row", swimlaneControl, "GroupUltimateSwimlaneRow", 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, 25)
            else
                row:SetAnchor(TOP, lastRow, BOTTOM, 0, -1)
            end

                    lastRow = row
            end
    else
        if (LOG_ACTIVE) then 
            logError("SwimlaneList.CreateSwimlaneListRows, swimlaneControl nil.")
        end
    end
end

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

    SwimlaneList.IsMocked = isMocked

    SwimlaneList.SetControlHidden(isHidden)
    SwimlaneList.SetControlMovable(isMovable)
    SwimlaneList.RestorePosition(posX, posY)
    SwimlaneList.CreateSwimLaneListHeaders()

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

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

Go to most recent revision | Compare with Previous | Blame