⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 countdoomtimer.lua

📁 时间太紧了
💻 LUA
📖 第 1 页 / 共 2 页
字号:
-- CountDoom 0.46
-- CountDoomTimer stuff
-- Author: Scrum


CDTimer_maxButtons = 5;
CDTimer_numTimers = 0;


CDTIMERPRIORITY_LOW     = 2;
CDTIMERPRIORITY_MEDIUM  = 1;
CDTIMERPRIORITY_HIGH    = 0;

CDTIMEREVENT_ONUPDATE       = 1;
CDTIMEREVENT_ONTIMERWARNING = 2;
CDTIMEREVENT_ONTIMEREND     = 3;
CDTIMEREVENT_ONMOVETIMER    = 4;
CDTIMEREVENT_ONENTER        = 5;
CDTIMEREVENT_ONLEAVE        = 6;
CDTIMEREVENT_ONMOUSEDOWN    = 7;
CDTIMEREVENT_ONMOUSEUP      = 8;
CDTIMEREVENT_ONCLICK        = 9;
CDTIMEREVENT_MAXEVENTS      = 10;



CDTimers = {};

--Internal members
--userHandle        - custom object to store anything in a timer
--startTime         - time timer was created
--warningTime       - number of seconds before warning is signaled
--duration          - lifetime of timer.  -1 is infinite
--icon              - cache of texture to be displayed
--text              - cache of text to be displayed    
--flashInterval     - flash period in seconds. 0 = disabled
--flashTime         - time within flash period [0 .. flashInterval]
--funcHandlers      - table of callbacks based on events
--priority          - determines button priority {HIGH, NORMAL, LOW}
--countDown         - true if display a countdown versus a countup
--prefix            - text added to beginning of timer countdown text
--suffix            - text added to end of timer countdown text

--Public methods
--CDTimer_Create( priority, duration, warningTime, handle )
--CDTimer_Destroy( timerIndex ) 
--CDTimer_SetFunctionHandler( timerIndex, handlerName, funcHandler )
--CDTimer_GetRemainingTime( timerIndex )
--CDTimer_SetText( timerIndex, timerText )
--CDTimer_GetText( timerIndex )
--CDTimer_SetIcon( timerIndex, timerIcon )
--CDTimer_GetIcon( timerIndex )
--CDTimer_SetWarningTime( timerIndex, warningTime )
--CDTimer_GetWarningTime( timerIndex )
--CDTimer_SetUserHandle( timerIndex, userHandle )
--CDTimer_GetUserHandle( timerIndex )
--CDTimer_SetCountDown( timerIndex, countDown );
--CDTimer_GetCountDown( timerIndex );
--CDTimer_EnableFlash( timerIndex, interval )
--CDTimer_DisableFlash( timerIndex )
--CDTimer_SetPriority( timerIndex, priority );
--CDTimer_Copy( destIndex, srcIndex );
--CDTimer_SetTimerPrefix( timerPrefix, prefixStr )
--CDTimer_SetTimerSuffix( timerPrefix, suffixStr )

--Private methods
--CDTimer_GenericHandler( timerIndex, eventName, arg1 )
--CDTimer_OnUpdate(arg1)
--CDTimer_OnEnter(arg1)
--CDTimer_OnLeave(arg1)
--CDTimer_OnMouseDown(arg1)
--CDTimer_OnMouseUp(arg1)
--CDTimer_OnClick(arg1)
--CDTimer_ShowButton( timerIndex, showButton )
--CDTimer_SetAlpha( timerIndex, alpha ) - NOTE: This is used by the flashing logic and shouldn't be called directly
--CDTimer_UpdateFlash( timerIndex, timeDelta )


local function CDTimer_Dump( timerIndex )
    CountDoom.DebugPrint( "Timer: " .. timerIndex );
    CountDoom.DebugPrint( "userHandle: " .. CDTimers[ timerIndex ].userHandle );
    CountDoom.DebugPrint( "startTime: " .. CDTimers[ timerIndex ].startTime );
    CountDoom.DebugPrint( "warningTime: " .. CDTimers[ timerIndex ].warningTime );
    CountDoom.DebugPrint( "duration: " .. CDTimers[ timerIndex ].duration );
    CountDoom.DebugPrint( "icon: " .. CountDoom.ToString( CDTimers[ timerIndex ].icon ) );
    CountDoom.DebugPrint( "text: " .. CountDoom.ToString( CDTimers[ timerIndex ].text ) );
    CountDoom.DebugPrint( "flashInterval: " .. CDTimers[ timerIndex ].flashInterval );
    CountDoom.DebugPrint( "flashTime: " .. CDTimers[ timerIndex ].flashTime );
    CountDoom.DebugPrint( "priority: " .. CDTimers[ timerIndex ].priority );
end


local function CDTimer_DumpAll()
    local timerIndex;
    for timerIndex = 0, CDTimer_numTimers - 1, 1 do
        CDTimer_Dump( timerIndex );
    end
end


local function CDTimer_SetAlpha( timerIndex, alpha )
    if CDTimers[ timerIndex ] == nil then
        CountDoom.DebugPrint( "Invalid timerIndex in CDTimer_SetAlpha" );
        return;
    end

    local buttonName = "CDTimerButton" .. timerIndex .. "_DurationText";
    local buttonItem = getglobal( buttonName );
    if( buttonItem ~= nil ) then
        buttonItem:SetAlpha( alpha );
    elseif timerIndex < CDTimer_maxButtons then
        CountDoom.DebugPrint( "Unable to call " .. buttonName .. ":SetAlpha()" );
    end
    
    --TODO set the icon alpha also
end


local function CDTimer_ShowButton( timerIndex, showButton )
    CountDoom.DebugPrint( "CDTimer_ShowButton(" .. CountDoom.ToString( timerIndex ) .. ")" );

    if timerIndex >= CDTimer_numTimers then
        CountDoom.DebugPrint( "CDTimer_ShowButton: Invalid index " .. timerIndex );
        return
    end

    local buttonName = "CDTimerButton" .. timerIndex;
    local buttonItem = getglobal( buttonName );
    if( buttonItem ~= nil ) then
        if( showButton ) then
            CountDoom.DebugPrint( "CDTimer_ShowButton: calling Show() " );
            buttonItem:Show();
        else
            CountDoom.DebugPrint( "CDTimer_ShowButton: calling Hide() " );
            buttonItem:Hide();
        end
    elseif timerIndex < CDTimer_maxButtons then
        CountDoom.DebugPrint( "Unable to call " .. buttonName .. ":Show()" );
    end
end


local function CDTimer_UpdateFlash( timerIndex, timeDelta )
    if CDTimers[ timerIndex ] == nil then
        CountDoom.DebugPrint( "Invalid timerIndex in CDTimer_UpdateFlash" );
        return -1;
    end
    
    if CDTimers[ timerIndex ].flashInterval <= 0.0 then
        return
    end
    
    CDTimers[ timerIndex ].flashTime = CDTimers[ timerIndex ].flashTime + timeDelta;
    while CDTimers[ timerIndex ].flashTime > CDTimers[ timerIndex ].flashInterval do
        CDTimers[ timerIndex ].flashTime = CDTimers[ timerIndex ].flashTime - CDTimers[ timerIndex ].flashInterval;
    end
    
    local alpha = 2.0 * CDTimers[ timerIndex ].flashTime / CDTimers[ timerIndex ].flashInterval;
    if( alpha > 1.0 ) then
        alpha = 2.0 - alpha;
    end
    
    CDTimer_SetAlpha( timerIndex, alpha );
end


local function CDTimer_GenericHandler( timerIndex, eventName, arg1 )
    
    if timerIndex == nil then
        --CountDoom.DebugPrint( "ButtonID doesn't map to a valid timerIndex in CDTimer_" .. eventName );
        return nil;
    end
    
    if CDTimers[ timerIndex ] == nil then
        CountDoom.DebugPrint( "Invalid timerIndex in CDTimer_" .. eventName );
        return nil;
    end
    
    -- Call the callback function
    if CDTimers[ timerIndex ].funcHandlers ~= nil then
        local funcHandler = CDTimers[ timerIndex ].funcHandlers[ eventName ];
        if funcHandler ~= nil then
            funcHandler( timerIndex, arg1 );
        end
    end
        
    return timerIndex;
end


function CDTimer_UpdateTimer( timerIndex )
    
    if( CDTimers[ timerIndex ] == nil ) then
        CountDoom.DebugPrint( "Invalid timerIndex in CDTimer_OnUpdate: " .. timerIndex );
        return;
    end
    
    local currentTime = GetTime();
    local rawDelta = currentTime - CDTimers[ timerIndex ].startTime;
    local duration = CDTimers[ timerIndex ].duration;
    local warningTime = CDTimers[ timerIndex ].warningTime;
    local finished = false;

    local delta = floor(rawDelta);
    if( delta > duration ) then
        delta = duration;
        finished = true;
    end
    
    -- Update the timer
    local flashAlpha = 1.0;
    
    local textColor = {};
    local alpha = 1.0;
    
    if (rawDelta > duration) then
        textColor["r"] = 1.0;
        textColor["g"] = 0.0;
        textColor["b"] = 0.0;
    elseif (rawDelta > warningTime) then
        alpha = (rawDelta - warningTime) / (duration - warningTime);
        textColor["r"] = 1.0;
        textColor["g"] = 1.0 - alpha;
        textColor["b"] = 0.0;
    else
        alpha = rawDelta / warningTime;
        textColor["r"] = 1.0;
        textColor["g"] = 1.0;
        textColor["b"] = 1.0 - alpha;
    end
    
    -- Invert for count down situations
    local timeDelta = rawDelta;
    if( CDTimers[ timerIndex ].countDown ) then
        timeDelta = duration - rawDelta;
    end

    local signStr = "";
    if timeDelta < 0 then
        signStr = "-";
        timeDelta = -timeDelta;
    end

    local minutes = floor(timeDelta/60);
    local seconds = floor(math.mod(timeDelta, 60));
    local hseconds = floor(math.mod(floor(rawDelta*100), 100));

    local htimeText = "";

    if (CountDoom.config.hseconds == true) then
        if (hseconds >= 10) then
            htimeText = "." .. hseconds;
        else
            htimeText = ".0" .. hseconds;
        end
    end

    local timeText = "";
    if (seconds >= 10) then
        timeText = signStr .. minutes .. ":" .. seconds .. htimeText;
    else
        timeText = signStr .. minutes .. ":0" .. seconds .. htimeText;
    end

    -- Add any prefix or suffix
    if CDTimers[ timerIndex ].prefix ~= nil then
        timeText = CDTimers[ timerIndex ].prefix .. timeText;
    end

    if CDTimers[ timerIndex ].suffix ~= nil then
        timeText = timeText .. CDTimers[ timerIndex ].suffix;
    end
    
    -- Set the timer text
    CDTimer_SetText( timerIndex, timeText, textColor );
    
    -- Update flash time
    local flashRate = 0.1;
    if CDTimers[ timerIndex ].flashInterval ~= 0.0 then
        CDTimer_UpdateFlash( timerIndex, flashRate );
    else
        CDTimer_SetAlpha( timerIndex, 1.0 );
    end
    
    -- Callback for custom OnUpdate calls
    CDTimer_GenericHandler( timerIndex, CDTIMEREVENT_ONUPDATE, arg1 );
    
    if( finished ) then
        --CountDoom.DebugPrint( "Calling OnTimerEnd for timer #" .. timerIndex );
        CDTimer_GenericHandler( timerIndex, CDTIMEREVENT_ONTIMEREND, delta );
    elseif( delta >= warningTime ) then
        --CountDoom.DebugPrint( "Calling OnTimerWarning for timer #" .. timerIndex );
        CDTimer_GenericHandler( timerIndex, CDTIMEREVENT_ONTIMERWARNING, delta );
    end
end


function CDTimer_OnUpdate( arg1 )
    local timerIndex = this:GetID();
    
    CDTimer_UpdateTimer( timerIndex );
end


function CDTimer_OnEnter( arg1 )
    local timerIndex = this:GetID();
    
    -- put the tool tip in the default position
    GameTooltip_SetDefaultAnchor(GameTooltip, this);
    
    -- set the tool tip text
    GameTooltip:SetText(COUNTDOOM_TITLE, 255/255, 209/255, 0/255);
    
    CDTimer_GenericHandler( timerIndex, CDTIMEREVENT_ONENTER, arg1 );
    
    GameTooltip:AddLine(COUNTDOOM_DESCRIPTION, 80/255, 143/255, 148/255);
    GameTooltip:Show();
end


function CDTimer_OnLeave( arg1 )
    local timerIndex = this:GetID();
    CDTimer_GenericHandler( timerIndex, CDTIMEREVENT_ONLEAVE, arg1 );
    
    GameTooltip:Hide(arg1);
end


function CDTimer_OnMouseDown( arg1 )
    -- if not loaded yet then get out
    if (CountDoom.initialized == false) then
        return;
    end
    
    local timerIndex = this:GetID();
    CDTimer_GenericHandler( timerIndex, CDTIMEREVENT_ONMOUSEDOWN, arg1 );
end


function CDTimer_OnMouseUp( arg1 )
    -- if not loaded yet then get out
    if CountDoom.initialized == false then
        return;
    end
    
    local timerIndex = this:GetID();
    CDTimer_GenericHandler( timerIndex, CDTIMEREVENT_ONMOUSEUP, arg1 );
end


function CDTimer_OnClick( arg1 )
    -- if not loaded yet then get out
    if CountDoom.initialized == false then
        return;
    end
    
    local timerIndex = this:GetID();
    CDTimer_GenericHandler( timerIndex, CDTIMEREVENT_ONCLICK, arg1 );
end


local function CDTimer_Constructor( timerIndex )
    if CDTimers[ timerIndex ] == nil then
        CDTimers[ timerIndex ] = {};
    end
    
    CDTimers[ timerIndex ].userHandle = nil;
    CDTimers[ timerIndex ].priority = nil;
    CDTimers[ timerIndex ].startTime = GetTime();
    CDTimers[ timerIndex ].warningTime = nil;
    CDTimers[ timerIndex ].duration = nil;
    CDTimers[ timerIndex ].icon = nil;
    CDTimers[ timerIndex ].text = nil;
    CDTimers[ timerIndex ].flashInterval = 0.0;
    CDTimers[ timerIndex ].flashTime = 0.0;
    CDTimers[ timerIndex ].countDown = true;
    CDTimers[ timerIndex ].prefix = nil;
    CDTimers[ timerIndex ].suffix = nil;

    CDTimers[ timerIndex ].funcHandlers = nil;
    CDTimers[ timerIndex ].funcHandlers = {};
end


local function CDTimer_GetInsertionIndex( priority, duration )
    local foundIndex = CDTimer_numTimers;
    
    if( CDTimer_numTimers > 0 ) then
   
        local currentTime = GetTime();

        -- Loop through all items stopping at first button less than 
        local timerIndex;
        for timerIndex = 0, CDTimer_numTimers - 1, 1 do
            
            if( CDTimers[ timerIndex ] ~= nil ) then
            
                -- Check for priority is a lower number (higher priority)
                if( priority < CDTimers[ timerIndex ].priority ) then
                    foundIndex = timerIndex;
                    break;
                end
                
                -- If equivalent, check remaining time
                local elapsed = currentTime - CDTimers[ timerIndex ].startTime;
                local remaining = CDTimers[ timerIndex ].duration - elapsed;
                
                if( duration <= remaining ) then
                    foundIndex = timerIndex;
                    break;
                end
            else
                foundIndex = timerIndex;
                break;
            end
        end
    
        -- if it's the last index, we're done
        if( foundIndex <= CDTimer_numTimers ) then
        
            -- Slide all the items to the next slots
            CountDoom.DebugPrint( "CDTimer_numTimers " .. CDTimer_numTimers );
            CountDoom.DebugPrint( "foundIndex " .. foundIndex );
            
            for newID = CDTimer_numTimers, foundIndex + 1, -1 do
            
                local oldID = newID - 1;
                
                CountDoom.DebugPrint( "oldID: " .. oldID .. "  newID: " .. newID );
                
                CDTimer_Copy( newID, oldID );
            end
        end
    end
    
    local buttonToEnable = CDTimer_numTimers;
        
    CDTimer_numTimers = CDTimer_numTimers + 1;
    
    -- Enable the last button 
    CDTimer_ShowButton( buttonToEnable, true );
    

    return foundIndex;
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -