System Dialogs

Cohadar

master of fugue
Reaction score
209
JASS:
//==============================================================================
//                    DIALOG SYSTEM BY COHADAR -- v2.0
//==============================================================================
//
//  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 = NewDialog()
//
//         than we add a title,
//            # call d.SetMessage("Some Text")
//
//         and than we 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 we add a callback method for our dialog
//            # call d.AddAction( function SomeFunction )
//         this is the function witch will be called when a player presses a button
//
//         And finally we 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 = GetTriggerDialog() 
//         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
//
//  PROS: 
//       * Extremelly easy to use compared to native dialogs
//       * It is fool-proof and will warn you if you try to do something stupid
//
//  CONS:
//       * You can have a maximum of 16 dialog buttons, lol 
//
//  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
//         
//  REQUIREMENTS:
//       * ABC v5.0
//
//  HOW TO IMPORT:
//       * Just create a trigger named Dialogs
//       * convert it to text and replace the whole trigger text with this one
//
//==============================================================================
library Dialogs

globals
	// Dialog button hotkey constants
    constant integer HK_ESC = 512
    
	constant integer HK_0 = 48
	constant integer HK_1 = 49
	constant integer HK_2 = 50
	constant integer HK_3 = 51
	constant integer HK_4 = 52
	constant integer HK_5 = 53
	constant integer HK_6 = 54
	constant integer HK_7 = 55
	constant integer HK_8 = 56
	constant integer HK_9 = 57
	
	constant integer HK_A = 65
	constant integer HK_B = 66
	constant integer HK_C = 67
	constant integer HK_D = 68
	constant integer HK_E = 69
	constant integer HK_F = 70
	constant integer HK_G = 71
	constant integer HK_H = 72
	constant integer HK_I = 73
	constant integer HK_J = 74
	constant integer HK_K = 75
	constant integer HK_L = 76
	constant integer HK_M = 77
	constant integer HK_N = 78
	constant integer HK_O = 79
	constant integer HK_P = 80
	constant integer HK_Q = 81
	constant integer HK_R = 82
	constant integer HK_S = 83
	constant integer HK_T = 84
	constant integer HK_U = 85
	constant integer HK_V = 86
	constant integer HK_W = 87
	constant integer HK_X = 88
	constant integer HK_Y = 89
	constant integer HK_Z = 90
endglobals

globals
	private constant integer MAX_BUTTONS = 16 // maximum of buttons on dialog
endglobals


//===========================================================================
//  Use this inside dialog action function
//===========================================================================
function GetTriggerDialog takes nothing returns Dialog
    return GetDialogStructA(GetClickedDialog())
endfunction


//===========================================================================
struct Dialog
    private trigger t = CreateTrigger()
	private dialog  d = DialogCreate()
	private boolean isActionSet = false

    private string messageText = ""
    
	private integer button_count = 0
	private button  array buttons[MAX_BUTTONS]
	private integer array hotkeys[MAX_BUTTONS]
    
	static method create takes nothing returns Dialog
		local Dialog ret = Dialog.allocate()
	    call TriggerRegisterDialogEvent( ret.t, ret.d )
        call SetDialogStructA(ret.d, ret)
		return ret
	endmethod
    
	method onDestroy takes nothing returns nothing
		call DestroyTrigger(.t)
        call ClearDialogStructA(.d)
        call DialogDestroy(.d)
	endmethod    
    
	method GetResult takes nothing returns integer
        local button b = GetClickedButton()
		local integer i = 0
		loop
			exitwhen i >= MAX_BUTTONS
			if b == this.buttons<i> then
			    return this.hotkeys<i>
			endif
			set i = i + 1
		endloop
        call BJDebugMsg(&quot;|c00FF0000ERROR: Unknown dialog hotkey&quot;)
		return 0
	endmethod
	
	method SetMessage takes string messageText returns nothing
        set .messageText = messageText
	endmethod
	
    method AddButton takes string buttonText, integer hotkey returns nothing
		local button b 
		if this.button_count &gt;= MAX_BUTTONS then
		    call BJDebugMsg(&quot;|c00FF0000ERROR: Maximum number of dialog buttons is &quot; + I2S(MAX_BUTTONS))
		else
			set b = DialogAddButton( this.d,  buttonText , hotkey )
			set this.buttons[this.button_count] = b
			set this.hotkeys[this.button_count] = hotkey
			set  this.button_count = this.button_count + 1
		endif
	endmethod	
	
	method AddAction takes code actionFunc returns nothing
	    if this.isActionSet == true then
		    call BJDebugMsg(&quot;|c00FF0000ERROR: Dialog.AddAction - you cannot set more than one dialog action&quot;)
		else
		    set this.isActionSet = true
			call TriggerAddAction( this.t, actionFunc )
		endif
	endmethod
    
	method Show takes player whichPlayer returns nothing
	    if this.isActionSet == false then
		    call BJDebugMsg(&quot;|c00FF0000WARNING: You forgot to set a dialog action&quot;)
		endif
		if this.button_count == 0 then
		    call BJDebugMsg(&quot;|c00FF0000ERROR: You cannot show dialog with no buttons&quot;)
		else
            // message must be set before every display because of some bug.
            call DialogSetMessage( this.d, this.messageText )
			call DialogDisplay(whichPlayer, this.d, true) 
		endif
	endmethod
	
	method ShowAll takes nothing returns nothing
		local integer i = 0
		loop
		    exitwhen i&gt;=12 // maximum of human players is 12
			if GetPlayerController(Player(i)) == MAP_CONTROL_USER and GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING  then
                call this.Show(Player(i))			
			endif
			set i = i + 1
		endloop
	endmethod
	
	method Hide takes player whichPlayer returns nothing
	    call DialogDisplay(whichPlayer, this.d, false) 
	endmethod

	method HideAll takes nothing returns nothing
		local integer i = 0
		loop
		    exitwhen i&gt;=12 // maximum of human players is 12
                call this.Hide(Player(i))			
			set i = i + 1
		endloop
	endmethod
	
endstruct

endlibrary
</i></i>


JASS:
//===========================================================================
//
//   Here is a small example how to use Dialog system.
//   You need 3 things for every Dialog.
//
//   1. global Dialog variable set to zero
//   2. trigger action that will create Dialog on first use and display it
//   3. dialog Callback that will handle Dialog results
//
///  Dialog system is based on hotkeys, you must assign a different hotkey 
//   to each button you add to a Dialog.
//   This hotkeys are at the same time a keybord shortcuts for your dialog buttons
//   and a numeric result of your dialog.
// 
//===========================================================================

scope yn

globals
    // always initialize global Dialogs with zero
    // so you can check that on first use
    Dialog ynDialog = 0
endglobals

//===========================================================================
private function Callback takes nothing returns nothing
    local integer result = GetTriggerDialog().GetResult()
	
    if result == HK_Y then
            call BJDebugMsg(&quot;Yes please.&quot;)
	elseif result == HK_N then
            call BJDebugMsg(&quot;No tnx.&quot;)
    elseif result == HK_ESC then        
            return
	else
        // This should never happen, but it is a good idea to check it
        // Just in case we add/remove some buttons and then forget to update Callback
	    call BJDebugMsg(&quot;ERROR: Callback UNKNOWN RESULT #&quot; + I2S(result)) 
	endif
    
endfunction

//===========================================================================
private function Actions takes nothing returns nothing
	// create Dialog on first use
    if ynDialog == 0 then
	    set  ynDialog = Dialog.create()
		
        call ynDialog.SetMessage(&quot;You Need More Explaining?&quot;)
		
        call ynDialog.AddButton(&quot;Yes&quot;, HK_Y)
		call ynDialog.AddButton(&quot;No&quot;,  HK_N)
		call ynDialog.AddButton(&quot;Cancel&quot;,  HK_ESC)

		call ynDialog.AddAction( function Callback )
	endif

	call ynDialog.Show(GetTriggerPlayer()) 
endfunction

//===========================================================================
public function InitTrig takes nothing returns nothing
    local trigger trig = CreateTrigger()
    call TriggerRegisterPlayerChatEvent(trig, Player(0),&quot;-yn&quot;, true)
    call TriggerAddAction( trig, function Actions )
endfunction

endscope


Download the test map, there is something interesting there.
if you don't you will be sorry for the rest of your lives.
 

Attachments

  • Dialogs_v2.0.w3x
    39.2 KB · Views: 251

Sim

Forum Administrator
Staff member
Reaction score
534
Explaining more in detail would be required.

> *Displaying dialogs the easy way
> *Retrieving dialog results the easy way

Is not descriptive enough.

> *Dialog is basically a struct wrapper around native dialog

Is basically not very user-friendly.
 

Cohadar

master of fugue
Reaction score
209
It is detailed exactly in the amount it should be.
All mayor function calls are listed and explained how and where to be used.

You will get the user friendly idea if you look at the test trigger that uses dialogs
(it is in the demo map ofc.)
 

Sim

Forum Administrator
Staff member
Reaction score
534
Then explain it in the post.

How it works, explained using basic ideas.
 

emjlr3

Change can be a good thing
Reaction score
395
very neat, takes away all the difficulties of storing and retrieving the dialog properties for you, gj!

could have used something similar a few weeks ago heh

but then I would have needed ABC, and quite frankly, 15 lines of my CSData variant works for that
 

Cohadar

master of fugue
Reaction score
209
There are only 3 ABC calls there,
it is really easy to replace them with CSData if you really need to.

Besides ABC is just one more trigger, and it is pure jass,
it does not depend on any events or special variable setup or anything
it absolutely has no side effects and "installation" comes down to pasting a text
inside a trigger.
 

emjlr3

Change can be a good thing
Reaction score
395
I know, I am just pulling your chain

sometimes old habits are hard to break
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • WildTurkey WildTurkey:
    is there a stephen green in the house?
    +1
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top