- Joined
- Dec 29, 2008
- Messages
- 271
JASS:
library Dialog requires Table
// Dialog System by The_Witcher
//
// Some simple wrappers for easier dialog handling
//
// Feel free to use!
//
// requires Table by Bribe: [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url]
//
globals
private constant integer MAX_BUTTONS = 12 //Max amount of buttons per dialog
endglobals
// New Things:
//
// function GetTriggerButton takes nothing returns DialogButton
//
//
// struct DialogButton
// operator [] takes button b returns dialogButton (useful to get the data of the clicked button)
// string text [read-only] (the text on the button)
// integer hotkey [read-only] (the hotkey for the button)
// integer index [read-only] (the button's position in the dialog, first is 0)
// integer data (user data for you)
//
// method create takes Dialog dlg, string title, integer hotkey returns DialogButton
//
// method registerClick takes trigger t returns nothing
//
//
// struct Dialog
// operator [] takes integer i returns dialogButton
// string text (the dialog's title, set this to another value to change the dialogs title aswell)
// integer data (user data for you)
//
// method create takes string title returns Dialog (creates a new dialog with the given title)
// method addButton takes string text, integer hotkey returns dialogButton (adds a new button and returns it)
// method clear takes nothing returns nothing (clears the dialog of all buttons)
//
// method display takes player toPlayer, boolean flag returns nothing
// method displayToAll takes boolean flag returns nothing
//
// method register takes code func returns nothing (fires the given function whenever a button for this dialog is clicked)
//
//
//==============================================================================
//========================= System Code ========================================
//==============================================================================
private module InitM
private static method onInit takes nothing returns nothing
set .buttons = Table.create()
endmethod
endmodule
struct DialogButton
private static Table buttons
private button b
readonly string text
readonly integer hotkey
readonly integer index
integer data
static method operator [] takes button b returns DialogButton
return .buttons[GetHandleId(b)]
endmethod
method registerClick takes trigger t returns nothing
call TriggerRegisterDialogButtonEvent(t, .b)
endmethod
static method create takes Dialog dlg, string title, integer hotkey returns DialogButton
local DialogButton this = DialogButton.allocate()
set .b = DialogAddButton(dlg.d, title, hotkey)
set .hotkey = hotkey
set .text = title
set .index = dlg.count
set .buttons[GetHandleId(.b)] = this
call dlg.add(this)
return this
endmethod
method destroy takes nothing returns nothing
call .buttons.remove(GetHandleId(.b))
endmethod
implement InitM
endstruct
struct Dialog extends array
private static integer instanceCount = 0
private static Dialog recycle = 0
private Dialog recycleNext
private static DialogButton array buttons
readonly dialog d
readonly integer count
private string s
private trigger t
integer data
method operator [] takes integer i returns DialogButton
return .buttons[this * MAX_BUTTONS + i]
endmethod
method operator text takes nothing returns string
return .s
endmethod
method operator text= takes string s returns nothing
set .s = s
call DialogSetMessage(.d, s)
endmethod
method clear takes nothing returns nothing
loop
exitwhen .count == 0
set .count = .count - 1
call .buttons[this * MAX_BUTTONS + .count].destroy()
endloop
call DialogClear(.d)
endmethod
method display takes player toPlayer, boolean flag returns nothing
call DialogDisplay(toPlayer, .d, flag)
endmethod
method displayToAll takes boolean flag returns nothing
call DialogDisplay(GetLocalPlayer(), .d, flag)
endmethod
method register takes code func returns nothing
call TriggerAddAction(.t, func)
endmethod
method add takes DialogButton b returns nothing
if .count < MAX_BUTTONS then
set .buttons[this * MAX_BUTTONS + .count] = b
set .count = .count + 1
else
debug call BJDebugMsg("DIALOG ERROR: More than 12 buttons were added to a dialog.")
endif
endmethod
method addButton takes string text returns DialogButton
return .addHotkeyButton( text, 0)
endmethod
method addHotkeyButton takes string text, integer hotkey returns DialogButton
return DialogButton.create(this, text, hotkey)
endmethod
static method create takes string title returns Dialog
local Dialog this
if (.recycle == 0) then
set .instanceCount = .instanceCount + 1
set this = .instanceCount
else
set this = .recycle
set .recycle = .recycle.recycleNext
endif
set .d = DialogCreate()
set .count = 0
set .s = title
call DialogSetMessage(.d, title)
set .t = CreateTrigger()
call TriggerRegisterDialogEvent(.t, .d)
return this
endmethod
method destroy takes nothing returns nothing
call .clear()
call DestroyTrigger(.t)
call DialogDestroy(.d)
set .recycleNext = .recycle
set .recycle = this
endmethod
endstruct
function GetTriggerButton takes nothing returns DialogButton
return DialogButton[GetClickedButton()]
endfunction
endlibrary
//Code indented using The_Witcher's Script Language Aligner
//Download the newest version and report bugs at [url]www.hiveworkshop.com[/url]
Last edited: