Dialog System

Scripts

14 years
edited 8 years
Compatibility Notice:

  • library name has been changed from Dialogs to Dialog
  • function GetTriggerDialog() has been replaced with Dialog.Get()

Code (JASS) Select
1
2
//==============================================================================
3
//                    DIALOG SYSTEM BY COHADAR -- v3.1
4
//==============================================================================
5
//
6
//  PURPOUSE:
7
//       * Displaying dialogs the easy way
8
//       * Retrieving dialog results the easy way
9
//
10
//
11
//  HOW TO USE:
12
//       * Dialog is basically a struct wrapper around native dialog 
13
//
14
//       * First create a dialog,
15
//            # local Dialog d = Dialog.create()
16
//
17
//         than add a title,
18
//            # call d.SetMessage("Some Text")
19
//
20
//         than add couple of buttons
21
//            #	call d.AddButton("First Button",  HK_A)
22
//            #	call d.AddButton("Second Button", HK_B)
23
//            #	call d.AddButton("Third",         HK_C)
24
//
25
//         HK_X is a hotkey constant for a button,
26
//         there are constants for all letters + numbers 0..9
27
//         hotkeys are not case-sensitive
28
//
29
//         now add a callback method for your dialog
30
//            # call d.AddAction( function SomeFunction )
31
//         this is the function that will be called when a player presses a button
32
//
33
//         And finally show the dialog in one of two ways:
34
//            # call d.ShowAll()      // Displays dialog to all human players
35
//            # call d.Show( player ) // Displays dialog to specified player
36
//
37
//         Inside your callback function you can get the clicked Dialog struct like this
38
//         local Dialog d = Dialog.Get() 
39
//         and then use d.GetResult() to get the hotkey of a clicked button
40
//
41
//         You can also use GetTriggerPlayer() to find out witch player pressed the button
42
//
43
//         When you are done with Dialog you can:
44
//            # call d.destroy()  // destroy it completely
45
//            # call d.clear()    // or recycle it
46
//
47
//  PROS: 
48
//       * Extremelly easy to use compared to native dialogs
49
//       * It is fool-proof and will warn you if you try to do something stupid
50
//
51
//  DETAILS:
52
//       * Don't release Dialogs before you are sure user has selected something
53
//         I recommend to never destroy Dialogs, 
54
//         just create them when they are first time called and then show/hide them as needed
55
// 
56
//  THANKS:
57
//       * Magentix for finding the triggeraction leak and suggesting clear method
58
//       * Vexorian for creating Table so I don't have to mess with handles myself
59
//         
60
//  REQUIREMENTS:
61
//       * Table v3.0 or higher
62
//
63
//  HOW TO IMPORT:
64
//       * Just create a trigger named Dialog
65
//       * convert it to text and replace the whole trigger text with this one
66
//
67
//==============================================================================
68
library Dialog uses Table
69
70
globals
71
	// Dialog button hotkey constants
72
    constant integer HK_ESC = 512
73
    
74
	constant integer HK_0 = 48
75
	constant integer HK_1 = 49
76
	constant integer HK_2 = 50
77
	constant integer HK_3 = 51
78
	constant integer HK_4 = 52
79
	constant integer HK_5 = 53
80
	constant integer HK_6 = 54
81
	constant integer HK_7 = 55
82
	constant integer HK_8 = 56
83
	constant integer HK_9 = 57
84
	
85
	constant integer HK_A = 65
86
	constant integer HK_B = 66
87
	constant integer HK_C = 67
88
	constant integer HK_D = 68
89
	constant integer HK_E = 69
90
	constant integer HK_F = 70
91
	constant integer HK_G = 71
92
	constant integer HK_H = 72
93
	constant integer HK_I = 73
94
	constant integer HK_J = 74
95
	constant integer HK_K = 75
96
	constant integer HK_L = 76
97
	constant integer HK_M = 77
98
	constant integer HK_N = 78
99
	constant integer HK_O = 79
100
	constant integer HK_P = 80
101
	constant integer HK_Q = 81
102
	constant integer HK_R = 82
103
	constant integer HK_S = 83
104
	constant integer HK_T = 84
105
	constant integer HK_U = 85
106
	constant integer HK_V = 86
107
	constant integer HK_W = 87
108
	constant integer HK_X = 88
109
	constant integer HK_Y = 89
110
	constant integer HK_Z = 90
111
endglobals
112
113
//===========================================================================
114
struct Dialog
115
    private trigger t = CreateTrigger()
116
    private triggeraction a = null
117
	private dialog  d = DialogCreate()
118
    private string messageText = ""
119
    private integer button_count = 0
120
    
121
    private static HandleTable dialogTable
122
    private static HandleTable buttonTable
123
    
124
    static method onInit takes nothing returns nothing
125
        set Dialog.dialogTable = HandleTable.create() // Table
126
        set Dialog.buttonTable = HandleTable.create() // Table
127
    endmethod
128
    
129
	static method create takes nothing returns Dialog
130
		local Dialog ret = Dialog.allocate()
131
	    call TriggerRegisterDialogEvent( ret.t, ret.d )
132
        set .dialogTable[GetClickedDialog()] = ret // Table
133
		return ret
134
	endmethod
135
    
136
	method clear takes nothing returns nothing
137
        if .a != null then
138
            call TriggerRemoveAction(.t, .a)
139
            set .a = null
140
        endif
141
        set .messageText = ""
142
        set .button_count = 0
143
        call DialogClear(.d)
144
	endmethod       
145
146
	method onDestroy takes nothing returns nothing
147
        if .a != null then
148
            call TriggerRemoveAction(.t, .a)
149
        endif
150
		call DestroyTrigger(.t)
151
        call .dialogTable.flush(.d) // Table
152
        call DialogDestroy(.d)
153
	endmethod    
154
    
155
156
    static method Get takes nothing returns Dialog 
157
        return .dialogTable[GetClickedDialog()] // Table   
158
    endmethod
159
    
160
	method GetResult takes nothing returns integer
161
		return .buttonTable[GetClickedButton()] // Table
162
	endmethod
163
	
164
	method SetMessage takes string messageText returns nothing
165
        set .messageText = messageText
166
	endmethod
167
	
168
    method AddButton takes string buttonText, integer hotkey returns nothing
169
        set .buttonTable[DialogAddButton(.d,  buttonText, hotkey)] = hotkey
170
        set .button_count = .button_count + 1
171
	endmethod	
172
	
173
	method AddAction takes code actionFunc returns nothing
174
	    if .a != null then
175
		    call BJDebugMsg("|c00FF0000Dialog.AddAction: you cannot set more than one dialog action")
176
		else
177
			set .a = TriggerAddAction( .t, actionFunc )
178
		endif
179
	endmethod
180
    
181
	method Show takes player whichPlayer returns nothing
182
	    if .a == null then
183
		    call BJDebugMsg("|c00FF0000Dialog.Show: You forgot to set a dialog action")
184
		endif
185
		if .button_count == 0 then
186
		    call BJDebugMsg("|c00FF0000Dialog.Show: You cannot show dialog with no buttons")
187
		else
188
            // message must be set before every display because of some bug.
189
            call DialogSetMessage( .d, .messageText )
190
			call DialogDisplay(whichPlayer, .d, true) 
191
		endif
192
	endmethod
193
	
194
	method ShowAll takes nothing returns nothing
195
		local integer i = 0
196
		loop
197
		    exitwhen i>=12 // maximum of human players is 12
198
			if GetPlayerController(Player(i)) == MAP_CONTROL_USER then
199
            if GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING then
200
                call .Show(Player(i))			
201
            endif
202
			endif
203
			set i = i + 1
204
		endloop
205
	endmethod
206
	
207
	method Hide takes player whichPlayer returns nothing
208
	    call DialogDisplay(whichPlayer, .d, false) 
209
	endmethod
210
211
	method HideAll takes nothing returns nothing
212
		local integer i = 0
213
		loop
214
		    exitwhen i>=12 // maximum of human players is 12
215
                call .Hide(Player(i))			
216
			set i = i + 1
217
		endloop
218
	endmethod
219
	
220
endstruct
221
222
endlibrary
223


Map contains dialog examples and can be used as nice weather testing utility.
  • Dialogs_v3.1.w3x (25.8 KB)