- Joined
- Nov 11, 2006
- Messages
- 7,591
This is a wrapper to ease the creation of dialogs. It does not offer bonus functionality, but it will ease the process of creating dialogs by giving you a clear API with nice documentation. That is the only goal of this snippet.
I realized shortly after I made this that The_Witcher already submitted a similar system that even has a similar API. His also does more than mine. But mine is cool--it does just the right amount of things. It is kind of like the little bear's soup in goldilocks.
Anyway, there are multiple ways to approach handling a dialog. Personally, I like consolidating the functions since they usually have similar behavior. Other people may prefer to have a separate callback per dialog button (in which case, use The_Witcher's system).
Check out the sample code for an example of its usage. Enjoy.
Sample:
I realized shortly after I made this that The_Witcher already submitted a similar system that even has a similar API. His also does more than mine. But mine is cool--it does just the right amount of things. It is kind of like the little bear's soup in goldilocks.
Anyway, there are multiple ways to approach handling a dialog. Personally, I like consolidating the functions since they usually have similar behavior. Other people may prefer to have a separate callback per dialog button (in which case, use The_Witcher's system).
Check out the sample code for an example of its usage. Enjoy.
JASS:
library Dialog /* v.1.1.0.0
**************************************************
*
* A struct wrapper for easy dialog creation.
*
**************************************************
*
*/ requires Table /* [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url]
*
**************************************************
*
* struct Dialog
*
* static method create() returns thistype
*
* Creates a new dialog and returns its instance.
*
* method operator title= takes string s
*
* Sets the title message for the dialog.
*
* method addButton( string text, integer hotkey ) returns button
*
* Adds a button to the dialog that reads <text>.
* The hotkey serves as a shortcut to press a dialog
* button. Example input: 'a', 'b', 512 (esc).
*
* method display( player p, boolean flag )
*
* Shows/hides a dialog for a particular player.
*
* method displayAll( boolean flag )
*
* Shows/hides a dialog for all players.
*
* method clear()
*
* Clears a dialog of its message and buttons.
*
* method destroy()
*
* Destroys a dialog and its instance.
*
* -- Event API --
*
* method registerClickEvent( boolexpr b )
*
* Registers when a dialog button is clicked.
*
* static method getClickedDialog() returns Dialog
* static method getClickedButton() returns button
* static method getPlayer() returns player
*
* Event responses.
*
**************************************************
*
* Constants - Credits to DysfunctionaI for the list!
*/
globals
constant integer KEY_SPACEBAR = 32
/* Number Pad */
constant integer KEY_NUMPAD_0 = 257
constant integer KEY_NUMPAD_1 = 258
constant integer KEY_NUMPAD_2 = 259
constant integer KEY_NUMPAD_3 = 260
constant integer KEY_NUMPAD_4 = 261
constant integer KEY_NUMPAD_5 = 262
constant integer KEY_NUMPAD_6 = 263
constant integer KEY_NUMPAD_7 = 264
constant integer KEY_NUMPAD_8 = 265
constant integer KEY_NUMPAD_9 = 266
constant integer KEY_NUMPAD_PLUS = 267
constant integer KEY_NUMPAD_MINUS = 268
constant integer KEY_NUMPAD_ASTERISK = 269
constant integer KEY_NUMPAD_SLASH = 270
constant integer KEY_NUMPAD_PERIOD = 271
constant integer KEY_EQUALS = 272
constant integer KEY_MINUS = 273
constant integer KEY_LEFT_BRACKET = 274
constant integer KEY_RIGHT_BRACKET = 275
constant integer KEY_BACKSLASH = 276
constant integer KEY_SEMICOLON = 277
constant integer KEY_APOSTROPHE = 278
constant integer KEY_COMMA = 279
constant integer KEY_PERIOD = 280
constant integer KEY_SLASH = 281
constant integer KEY_ESCAPE = 512
constant integer KEY_BACKSPACE = 514
/* Arrows */
constant integer KEY_LEFT_ARROW = 516
constant integer KEY_UP_ARROW = 517
constant integer KEY_RIGHT_ARROW = 518
constant integer KEY_DOWN_ARROW = 519
constant integer KEY_INSERT = 520
constant integer KEY_DELETE = 521
constant integer KEY_HOME = 522
constant integer KEY_END = 523
constant integer KEY_PAGE_UP = 524
constant integer KEY_PAGE_DOWN = 525
constant integer KEY_CAPS_LOCK = 526
constant integer KEY_NUM_LOCK = 527
constant integer KEY_SCROLL_LOCK = 528
constant integer KEY_PAUSE = 529
/* Function Buttons */
constant integer KEY_F1 = 768
constant integer KEY_F2 = 769
constant integer KEY_F3 = 770
constant integer KEY_F4 = 771
constant integer KEY_F5 = 772
constant integer KEY_F6 = 773
constant integer KEY_F7 = 774
constant integer KEY_F8 = 775
constant integer KEY_F9 = 776
constant integer KEY_F10 = 777
constant integer KEY_F11 = 778
constant integer KEY_F12 = 779
endglobals
/*
**************************************************/
globals
private Table instance = 0
endglobals
private module DialogInit
private static method onInit takes nothing returns nothing
set instance = Table.create()
endmethod
endmodule
struct Dialog
private dialog dg
private trigger click
private string msg
static method getClickedDialog takes nothing returns thistype
return instance[GetHandleId(GetClickedDialog())]
endmethod
static method getClickedButton takes nothing returns button
return GetClickedButton()
endmethod
static method getPlayer takes nothing returns player
return GetTriggerPlayer()
endmethod
method operator title= takes string text returns nothing
call DialogSetMessage(this.dg, text)
set this.msg = text
endmethod
method addButton takes string text, integer hotkey returns button
return DialogAddButton(this.dg, text, hotkey)
endmethod
method display takes player p, boolean flag returns nothing
call DialogDisplay(p, this.dg, flag)
call DialogSetMessage(this.dg, this.msg)
endmethod
method displayAll takes boolean flag returns nothing
call DialogDisplay(GetLocalPlayer(), this.dg, flag)
call DialogSetMessage(this.dg, this.msg)
endmethod
method clear takes nothing returns nothing
call DialogClear(this.dg)
set this.msg = ""
endmethod
method registerClickEvent takes boolexpr b returns nothing
if this.click == null then
set instance[GetHandleId(this.dg)] = this
set this.click = CreateTrigger()
call TriggerRegisterDialogEvent(this.click, this.dg)
endif
call TriggerAddCondition(this.click, b)
endmethod
method destroy takes nothing returns nothing
debug if this.dg == null then
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "[Dialog] Attempted to destroy null dialog.")
debug endif
if this.click != null then
call DestroyTrigger(this.click)
call instance.remove(GetHandleId(this.dg))
set this.click = null
endif
call DialogClear(this.dg)
call DialogDestroy(this.dg)
set this.dg = null
call this.deallocate()
endmethod
static method create takes nothing returns thistype
local thistype this = thistype.allocate()
set this.dg = DialogCreate()
return this
endmethod
implement DialogInit
endstruct
endlibrary
Sample:
JASS:
scope Testing initializer Init
// Creates a dialog with buttons for different fruit
// When you click one, it will make an item that has absolutely
// nothing to do with the fruit you chose
globals
private button array buttons
endglobals
private function DialogEvent takes nothing returns boolean
local Dialog log = Dialog.getClickedDialog()
local button b = Dialog.getClickedButton()
call BJDebugMsg("Clicked: " + I2S(log))
call BJDebugMsg("Player Name: " + GetPlayerName(GetTriggerPlayer()))
if b == buttons[0] then
call CreateItem('afac', 0, 0)
elseif b == buttons[1] then
call CreateItem('spsh', 0, 0)
elseif b == buttons[2] then
call CreateItem('ajen', 0, 0)
elseif b == buttons[3] then
call CreateItem('bgst', 0, 0)
endif
call log.destroy()
set b = null
return false
endfunction
private function AppendHotkey takes string source, string hotkey returns string
return "|cffffcc00[" + hotkey + "]|r " + source
endfunction
private function DialogTest takes nothing returns nothing
local Dialog log = Dialog.create()
set log.title = "Fruit"
set buttons[0] = log.addButton(AppendHotkey("Apple", "A"), 'A')
set buttons[1] = log.addButton(AppendHotkey("Banana", "B"), 'B')
set buttons[2] = log.addButton(AppendHotkey("Orange", "C"), 'C')
set buttons[3] = log.addButton(AppendHotkey("Hyoi Pear", "D"), 'D')
call log.displayAll(true)
call log.registerClickEvent(Condition(function DialogEvent))
endfunction
private function Init takes nothing returns nothing
call TimerStart(CreateTimer(), 0, false, function DialogTest)
endfunction
endscope
Last edited: