TriggerHappy
...
- Reaction score
- 341
Dialog System
TriggerHappy187
Introduction :
This system allows you to make dialogs easier.
Requirements :
- ABC ( Included in the map )
Changlog:
Version 1.1
Version 1.0a
- Fixed TriggerAction leak.
- Removed bj_ prefixes.
- Struct variables are now private.
Version 1.0a
- Fixed the destroy method
- Tidied up the demo usage
Code :
JASS:
library Dialogs requires ABC
globals
private constant integer BUTTON_AMOUNT = 16
endglobals
struct Dialog
private button array Button[BUTTON_AMOUNT]
private integer ButtonCount
private dialog Dialog
private string name
private string array ButtonName[BUTTON_AMOUNT]
private trigger DIALOG
private string clicked
static method create takes string name returns Dialog
local Dialog d = Dialog.allocate()
set d.Dialog = DialogCreate()
set d.name = name
set d.ButtonCount = 0
set d.DIALOG = CreateTrigger()
call SetDialogStructA(d.Dialog, d)
call TriggerRegisterDialogEvent(d.DIALOG, d.Dialog)
return d
endmethod
method Conditions takes nothing returns boolean
return true
endmethod
method ShowAll takes nothing returns nothing
local integer i = 0
call DialogSetMessage(this.Dialog, this.name)
loop
exitwhen i > 11
call DialogDisplay(Player(i), this.Dialog, true)
set i = i + 1
endloop
endmethod
method ShowPlayer takes player p returns nothing
call DialogDisplay(p, this.Dialog, true)
endmethod
method createButton takes string ButtonName returns nothing
set this.ButtonCount = this.ButtonCount + 1
set this.Button[this.ButtonCount] = DialogAddButton(this.Dialog, ButtonName, 0)
set this.ButtonName[this.ButtonCount] = ButtonName
endmethod
method createAction takes code func returns nothing
call TriggerAddCondition(this.DIALOG, Condition(func))
endmethod
method result takes nothing returns string
local button b = GetClickedButton()
local integer i = 0
loop
exitwhen i > BUTTON_AMOUNT
if this.Button<i> == b then
return this.ButtonName<i>
endif
set i = i + 1
endloop
return null
endmethod
method onDestroy takes nothing returns nothing
call ClearDialogStructA(.Dialog)
call DestroyTrigger(.DIALOG)
call DialogDestroy(.Dialog)
endmethod
endstruct
function ShowDialog takes Dialog d returns nothing
call d.ShowAll()
endfunction
function ShowDialogPlayer takes Dialog d, player p returns nothing
call d.ShowPlayer(p)
endfunction
function AddButton takes Dialog d, string ButtonName returns nothing
call d.createButton(ButtonName)
endfunction
function AddAction takes Dialog d, code func returns nothing
call d.createAction(func)
endfunction
function ClickedButton takes Dialog d returns string
return d.result()
endfunction
function GetDialog takes nothing returns integer
return GetDialogStructA(GetClickedDialog())
endfunction
function ClearDialog takes Dialog d returns nothing
call d.destroy()
endfunction
endlibrary</i></i>
Demo Usage :
JASS:
scope test initializer InitTrig
globals
Dialog test
endglobals
private function Actions takes nothing returns boolean // MUST HAVE RETURNS BOOLEAN
local Dialog d = GetDialog() // This is needed!
if ClickedButton(d) == "Yes" then // Checks if the button that was clicked was "Yes"
call BJDebugMsg("Damn Straight!")
elseif ClickedButton(d) == "No" then // Checks if the button that was clicked was "No"
call BJDebugMsg(".....Shutup")
endif
call ClearDialog(d)
return false // THIS MUST BE AT THE END OF THE FUNCTION
endfunction
private function CreateDialog takes nothing returns nothing
set test = Dialog.create("Do you like Clan Mapz?") // Creates the Dialog + it's name
call AddButton(test, "Yes") // Creates a button call "Yes"
call AddButton(test, "No") // Creates a button call "No"
call AddAction(test, function Actions) // The actions for the dialog
call ShowDialog(test) // Shows the dialog to all players
endfunction
//===========================================================================
private function InitTrig takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterTimerEventSingle(t, 1.00)
call TriggerAddAction(t, function CreateDialog)
endfunction
endscope