[Snippet] MessageQueue

Scripts

14 years
edited 8 years
This will 'merge' DisplayTimedTextToPlayer calls in the same trigger-function-call-stack.

Code (jass) Select
/**********************************
*
*   MessageQueue
*   v1.0.0.0
*   By Magtheridon96
*
*   - Enqueues messages to display with
*     one call rather than having multiple
*     calls.
*
*   - All messages will be displayed based
*     on the longest duration given for any
*     message in one trigger function call
*     stack.
*
*       call MessageQueue.push("Hello", 2)
*       call MessageQueue.push("Okay", 4)
*
*   - Both of the above messages will be
*     displayed for 4 seconds.
*
*   API:
*   ----
*
*       struct MessageQueue extends array
*
*           static method enqueue takes string message, real time returns nothing
*           static method insert takes string message, real time returns nothing
*           static method push takes string message, real time returns nothing
*           static method add takes string message, real time returns nothing
*               - Enqueues a message to be displayed.
*
*           static method clear takes nothing returns nothing
*               - Clears the message queue.
*
*           static method enable takes nothing returns nothing
*           static method disable takes nothing returns nothing
*               - When the system is disabled, incoming messages will be ignored.
*
**********************************/
library MessageQueue

    globals
        private constant real DEFAULT_X = 0
        private constant real DEFAULT_Y = 0
    endglobals
   
    struct MessageQueue extends array
        private static player localPlayer
        private static timer t = CreateTimer()
        private static boolean enabled = true
        private static integer count = 0
        private static real duration = 0
        private static string text = ""
       
        private static method display takes nothing returns nothing
            call DisplayTimedTextToPlayer(localPlayer, DEFAULT_X, DEFAULT_Y, duration, text)
        endmethod
       
        static method enable takes nothing returns nothing
            set enabled = true
        endmethod
        static method disable takes nothing returns nothing
            set enabled = false
        endmethod
       
        static method enqueue takes string message, real time returns nothing
            if enabled then
                if count == 0 then
                    /*
                    *   We have to start the timer since this is the first message
                    *   to be enqueued in the queue.
                    */
                    call TimerStart(t, 0, false, function thistype.display)
                   
                    /*
                    *   We can safely just set the values
                    */
                    set duration = time
                    set text = message
                    set count = 1
                else
                    /*
                    *   If the current message has been given a greater duration
                    *   than the one currently in use, then we set the current
                    *   duration to this new one.
                    */
                    if time > duration then
                        set duration = time
                    endif
                   
                    /*
                    *   We add the text to the queue with a line-break
                    *   to simulate multiple calls of DisplayTimedTextToPlayer.
                    */
                    set text = text + "\n" + message
                    set count = count + 1
                endif
            endif
        endmethod
       
        static method push takes string message, real time returns nothing
            call enqueue(message, time)
        endmethod
       
        static method add takes string message, real time returns nothing
            call enqueue(message, time)
        endmethod
       
        static method insert takes string message, real time returns nothing
            call enqueue(message, time)
        endmethod
       
        static method clear takes nothing returns nothing
            /*
            *   This will reset the queue.
            */
            set text = ""
            set duration = 0
            set count = 0
            call PauseTimer(t)
        endmethod
       
        private static method onInit takes nothing returns nothing
            /*
            *   GetLocalPlayer() calls may cause crashes in global blocks.
            */
            set localPlayer = GetLocalPlayer()
        endmethod
    endstruct
   
endlibrary


Feel free to comment.