[Snippet] MessageQueue

Scripts

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

Code (jass) Select
1
/**********************************
2
*
3
*   MessageQueue
4
*   v1.0.0.0
5
*   By Magtheridon96
6
*
7
*   - Enqueues messages to display with
8
*     one call rather than having multiple
9
*     calls.
10
*
11
*   - All messages will be displayed based 
12
*     on the longest duration given for any
13
*     message in one trigger function call
14
*     stack.
15
*
16
*       call MessageQueue.push("Hello", 2)
17
*       call MessageQueue.push("Okay", 4)
18
*
19
*   - Both of the above messages will be 
20
*     displayed for 4 seconds.
21
*
22
*   API:
23
*   ----
24
*
25
*       struct MessageQueue extends array
26
*
27
*           static method enqueue takes string message, real time returns nothing
28
*           static method insert takes string message, real time returns nothing
29
*           static method push takes string message, real time returns nothing
30
*           static method add takes string message, real time returns nothing
31
*               - Enqueues a message to be displayed.
32
*
33
*           static method clear takes nothing returns nothing
34
*               - Clears the message queue.
35
*
36
*           static method enable takes nothing returns nothing
37
*           static method disable takes nothing returns nothing
38
*               - When the system is disabled, incoming messages will be ignored.
39
*
40
**********************************/
41
library MessageQueue
42
43
    globals
44
        private constant real DEFAULT_X = 0
45
        private constant real DEFAULT_Y = 0
46
    endglobals
47
    
48
    struct MessageQueue extends array
49
        private static player localPlayer
50
        private static timer t = CreateTimer()
51
        private static boolean enabled = true
52
        private static integer count = 0
53
        private static real duration = 0
54
        private static string text = ""
55
        
56
        private static method display takes nothing returns nothing
57
            call DisplayTimedTextToPlayer(localPlayer, DEFAULT_X, DEFAULT_Y, duration, text)
58
        endmethod
59
        
60
        static method enable takes nothing returns nothing
61
            set enabled = true
62
        endmethod
63
        static method disable takes nothing returns nothing
64
            set enabled = false
65
        endmethod
66
        
67
        static method enqueue takes string message, real time returns nothing
68
            if enabled then
69
                if count == 0 then
70
                    /*
71
                    *   We have to start the timer since this is the first message
72
                    *   to be enqueued in the queue.
73
                    */
74
                    call TimerStart(t, 0, false, function thistype.display)
75
                    
76
                    /*
77
                    *   We can safely just set the values
78
                    */
79
                    set duration = time
80
                    set text = message
81
                    set count = 1
82
                else
83
                    /*
84
                    *   If the current message has been given a greater duration
85
                    *   than the one currently in use, then we set the current 
86
                    *   duration to this new one.
87
                    */
88
                    if time > duration then
89
                        set duration = time
90
                    endif
91
                    
92
                    /*
93
                    *   We add the text to the queue with a line-break
94
                    *   to simulate multiple calls of DisplayTimedTextToPlayer.
95
                    */
96
                    set text = text + "\n" + message
97
                    set count = count + 1
98
                endif
99
            endif
100
        endmethod
101
        
102
        static method push takes string message, real time returns nothing
103
            call enqueue(message, time)
104
        endmethod
105
        
106
        static method add takes string message, real time returns nothing
107
            call enqueue(message, time)
108
        endmethod
109
        
110
        static method insert takes string message, real time returns nothing
111
            call enqueue(message, time)
112
        endmethod
113
        
114
        static method clear takes nothing returns nothing
115
            /*
116
            *   This will reset the queue.
117
            */
118
            set text = ""
119
            set duration = 0
120
            set count = 0
121
            call PauseTimer(t)
122
        endmethod
123
        
124
        private static method onInit takes nothing returns nothing
125
            /*
126
            *   GetLocalPlayer() calls may cause crashes in global blocks.
127
            */
128
            set localPlayer = GetLocalPlayer()
129
        endmethod
130
    endstruct
131
    
132
endlibrary


Feel free to comment.