//==============================================================================
// DIALOG SYSTEM BY COHADAR -- v3.1
//==============================================================================
//
// PURPOUSE:
// * Displaying dialogs the easy way
// * Retrieving dialog results the easy way
//
//
// HOW TO USE:
// * Dialog is basically a struct wrapper around native dialog
//
// * First create a dialog,
// # local Dialog d = Dialog.create()
//
// than add a title,
// # call d.SetMessage("Some Text")
//
// than add couple of buttons
// # call d.AddButton("First Button", HK_A)
// # call d.AddButton("Second Button", HK_B)
// # call d.AddButton("Third", HK_C)
//
// HK_X is a hotkey constant for a button,
// there are constants for all letters + numbers 0..9
// hotkeys are not case-sensitive
//
// now add a callback method for your dialog
// # call d.AddAction( function SomeFunction )
// this is the function that will be called when a player presses a button
//
// And finally show the dialog in one of two ways:
// # call d.ShowAll() // Displays dialog to all human players
// # call d.Show( player ) // Displays dialog to specified player
//
// Inside your callback function you can get the clicked Dialog struct like this
// local Dialog d = Dialog.Get()
// and then use d.GetResult() to get the hotkey of a clicked button
//
// You can also use GetTriggerPlayer() to find out witch player pressed the button
//
// When you are done with Dialog you can:
// # call d.destroy() // destroy it completely
// # call d.clear() // or recycle it
//
// PROS:
// * Extremelly easy to use compared to native dialogs
// * It is fool-proof and will warn you if you try to do something stupid
//
// DETAILS:
// * Don't release Dialogs before you are sure user has selected something
// I recommend to never destroy Dialogs,
// just create them when they are first time called and then show/hide them as needed
//
// THANKS:
// * Magentix for finding the triggeraction leak and suggesting clear method
// * Vexorian for creating Table so I don't have to mess with handles myself
//
// REQUIREMENTS:
// * Table v3.0 or higher
//
// HOW TO IMPORT:
// * Just create a trigger named Dialog
// * convert it to text and replace the whole trigger text with this one
//
//==============================================================================
library Dialog uses Table
globals
// Dialog button hotkey constants
endglobals
//===========================================================================
struct Dialog
private string messageText =
""
private static HandleTable dialogTable
private static HandleTable buttonTable
static method onInit takes nothing returns nothing
set Dialog.dialogTable = HandleTable.create() // Table
set Dialog.buttonTable = HandleTable.create() // Table
endmethod
static method create takes nothing returns Dialog
local Dialog ret = Dialog.allocate()
return ret
endmethod
method clear takes nothing returns nothing
if .a != null then
set .a = null
endif
set .messageText = ""
set .button_count = 0
endmethod
method onDestroy takes nothing returns nothing
if .a != null then
endif
call .dialogTable.flush(.d) // Table
endmethod
static method Get takes nothing returns Dialog
endmethod
method GetResult
takes nothing returns integer endmethod
method SetMessage
takes string messageText
returns nothing set .messageText = messageText
endmethod
method AddButton
takes string buttonText,
integer hotkey
returns nothing set .button_count = .button_count + 1
endmethod
method AddAction
takes code actionFunc
returns nothing if .a != null then
call BJDebugMsg(
"|c00FF0000Dialog.AddAction: you cannot set more than one dialog action")
else
endif
endmethod
method Show
takes player whichPlayer
returns nothing if .a == null then
call BJDebugMsg(
"|c00FF0000Dialog.Show: You forgot to set a dialog action")
endif
if .button_count == 0 then
call BJDebugMsg(
"|c00FF0000Dialog.Show: You cannot show dialog with no buttons")
else
// message must be set before every display because of some bug.
endif
endmethod
method ShowAll takes nothing returns nothing
loop
exitwhen i>=12 // maximum of human players is 12
endif
endif
set i = i + 1
endloop
endmethod
method Hide
takes player whichPlayer
returns nothing endmethod
method HideAll takes nothing returns nothing
loop
exitwhen i>=12 // maximum of human players is 12
set i = i + 1
endloop
endmethod
endstruct
endlibrary