Warcraft 3 documentation
vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

Dialog System No New Posts Codes & Snippets

Started by
moyack

0 Members and 1 Guest are viewing this topic.

Dialog System
on: December 01, 2011, 02:31:54 PM
Category: Interface, System
Language: vJASS
Download Demo Map

Compatibility Notice:
  • library name has been changed from Dialogs to Dialog
  • function GetTriggerDialog() has been replaced with Dialog.Get()

Code: jass
  1. //==============================================================================
  2. //                    DIALOG SYSTEM BY COHADAR -- v3.1
  3. //==============================================================================
  4. //
  5. //  PURPOUSE:
  6. //       * Displaying dialogs the easy way
  7. //       * Retrieving dialog results the easy way
  8. //
  9. //
  10. //  HOW TO USE:
  11. //       * Dialog is basically a struct wrapper around native dialog
  12. //
  13. //       * First create a dialog,
  14. //            # local Dialog d = Dialog.create()
  15. //
  16. //         than add a title,
  17. //            # call d.SetMessage("Some Text")
  18. //
  19. //         than add couple of buttons
  20. //            #   call d.AddButton("First Button",  HK_A)
  21. //            #   call d.AddButton("Second Button", HK_B)
  22. //            #   call d.AddButton("Third",         HK_C)
  23. //
  24. //         HK_X is a hotkey constant for a button,
  25. //         there are constants for all letters + numbers 0..9
  26. //         hotkeys are not case-sensitive
  27. //
  28. //         now add a callback method for your dialog
  29. //            # call d.AddAction( function SomeFunction )
  30. //         this is the function that will be called when a player presses a button
  31. //
  32. //         And finally show the dialog in one of two ways:
  33. //            # call d.ShowAll()      // Displays dialog to all human players
  34. //            # call d.Show( player ) // Displays dialog to specified player
  35. //
  36. //         Inside your callback function you can get the clicked Dialog struct like this
  37. //         local Dialog d = Dialog.Get()
  38. //         and then use d.GetResult() to get the hotkey of a clicked button
  39. //
  40. //         You can also use GetTriggerPlayer() to find out witch player pressed the button
  41. //
  42. //         When you are done with Dialog you can:
  43. //            # call d.destroy()  // destroy it completely
  44. //            # call d.clear()    // or recycle it
  45. //
  46. //  PROS:
  47. //       * Extremelly easy to use compared to native dialogs
  48. //       * It is fool-proof and will warn you if you try to do something stupid
  49. //
  50. //  DETAILS:
  51. //       * Don't release Dialogs before you are sure user has selected something
  52. //         I recommend to never destroy Dialogs,
  53. //         just create them when they are first time called and then show/hide them as needed
  54. //
  55. //  THANKS:
  56. //       * Magentix for finding the triggeraction leak and suggesting clear method
  57. //       * Vexorian for creating Table so I don't have to mess with handles myself
  58. //         
  59. //  REQUIREMENTS:
  60. //       * Table v3.0 or higher
  61. //
  62. //  HOW TO IMPORT:
  63. //       * Just create a trigger named Dialog
  64. //       * convert it to text and replace the whole trigger text with this one
  65. //
  66. //==============================================================================
  67. library Dialog uses Table
  68.  
  69. globals
  70.         // Dialog button hotkey constants
  71.     constant integer HK_ESC = 512
  72.    
  73.         constant integer HK_0 = 48
  74.         constant integer HK_1 = 49
  75.         constant integer HK_2 = 50
  76.         constant integer HK_3 = 51
  77.         constant integer HK_4 = 52
  78.         constant integer HK_5 = 53
  79.         constant integer HK_6 = 54
  80.         constant integer HK_7 = 55
  81.         constant integer HK_8 = 56
  82.         constant integer HK_9 = 57
  83.        
  84.         constant integer HK_A = 65
  85.         constant integer HK_B = 66
  86.         constant integer HK_C = 67
  87.         constant integer HK_D = 68
  88.         constant integer HK_E = 69
  89.         constant integer HK_F = 70
  90.         constant integer HK_G = 71
  91.         constant integer HK_H = 72
  92.         constant integer HK_I = 73
  93.         constant integer HK_J = 74
  94.         constant integer HK_K = 75
  95.         constant integer HK_L = 76
  96.         constant integer HK_M = 77
  97.         constant integer HK_N = 78
  98.         constant integer HK_O = 79
  99.         constant integer HK_P = 80
  100.         constant integer HK_Q = 81
  101.         constant integer HK_R = 82
  102.         constant integer HK_S = 83
  103.         constant integer HK_T = 84
  104.         constant integer HK_U = 85
  105.         constant integer HK_V = 86
  106.         constant integer HK_W = 87
  107.         constant integer HK_X = 88
  108.         constant integer HK_Y = 89
  109.         constant integer HK_Z = 90
  110. endglobals
  111.  
  112. //===========================================================================
  113. struct Dialog
  114.     private trigger t = CreateTrigger()
  115.     private triggeraction a = null
  116.         private dialog  d = DialogCreate()
  117.     private string messageText = ""
  118.     private integer button_count = 0
  119.    
  120.     private static HandleTable dialogTable
  121.     private static HandleTable buttonTable
  122.    
  123.     static method onInit takes nothing returns nothing
  124.         set Dialog.dialogTable = HandleTable.create() // Table
  125.         set Dialog.buttonTable = HandleTable.create() // Table
  126.     endmethod
  127.    
  128.         static method create takes nothing returns Dialog
  129.                 local Dialog ret = Dialog.allocate()
  130.             call TriggerRegisterDialogEvent( ret.t, ret.d )
  131.         set .dialogTable[GetClickedDialog()] = ret // Table
  132.                 return ret
  133.         endmethod
  134.    
  135.         method clear takes nothing returns nothing
  136.         if .a != null then
  137.             call TriggerRemoveAction(.t, .a)
  138.             set .a = null
  139.         endif
  140.         set .messageText = ""
  141.         set .button_count = 0
  142.         call DialogClear(.d)
  143.         endmethod       
  144.  
  145.         method onDestroy takes nothing returns nothing
  146.         if .a != null then
  147.             call TriggerRemoveAction(.t, .a)
  148.         endif
  149.                 call DestroyTrigger(.t)
  150.         call .dialogTable.flush(.d) // Table
  151.         call DialogDestroy(.d)
  152.         endmethod   
  153.    
  154.  
  155.     static method Get takes nothing returns Dialog
  156.         return .dialogTable[GetClickedDialog()] // Table   
  157.     endmethod
  158.    
  159.         method GetResult takes nothing returns integer
  160.                 return .buttonTable[GetClickedButton()] // Table
  161.         endmethod
  162.        
  163.         method SetMessage takes string messageText returns nothing
  164.         set .messageText = messageText
  165.         endmethod
  166.        
  167.     method AddButton takes string buttonText, integer hotkey returns nothing
  168.         set .buttonTable[DialogAddButton(.d,  buttonText, hotkey)] = hotkey
  169.         set .button_count = .button_count + 1
  170.         endmethod      
  171.        
  172.         method AddAction takes code actionFunc returns nothing
  173.             if .a != null then
  174.                     call BJDebugMsg("|c00FF0000Dialog.AddAction: you cannot set more than one dialog action")
  175.                 else
  176.                         set .a = TriggerAddAction( .t, actionFunc )
  177.                 endif
  178.         endmethod
  179.    
  180.         method Show takes player whichPlayer returns nothing
  181.             if .a == null then
  182.                     call BJDebugMsg("|c00FF0000Dialog.Show: You forgot to set a dialog action")
  183.                 endif
  184.                 if .button_count == 0 then
  185.                     call BJDebugMsg("|c00FF0000Dialog.Show: You cannot show dialog with no buttons")
  186.                 else
  187.             // message must be set before every display because of some bug.
  188.             call DialogSetMessage( .d, .messageText )
  189.                         call DialogDisplay(whichPlayer, .d, true)
  190.                 endif
  191.         endmethod
  192.        
  193.         method ShowAll takes nothing returns nothing
  194.                 local integer i = 0
  195.                 loop
  196.                     exitwhen i>=12 // maximum of human players is 12
  197.                         if GetPlayerController(Player(i)) == MAP_CONTROL_USER then
  198.             if GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING then
  199.                 call .Show(Player(i))                  
  200.             endif
  201.                         endif
  202.                         set i = i + 1
  203.                 endloop
  204.         endmethod
  205.        
  206.         method Hide takes player whichPlayer returns nothing
  207.             call DialogDisplay(whichPlayer, .d, false)
  208.         endmethod
  209.  
  210.         method HideAll takes nothing returns nothing
  211.                 local integer i = 0
  212.                 loop
  213.                     exitwhen i>=12 // maximum of human players is 12
  214.                 call .Hide(Player(i))                  
  215.                         set i = i + 1
  216.                 endloop
  217.         endmethod
  218.        
  219. endstruct
  220.  
  221. endlibrary
  222.  

Map contains dialog examples and can be used as nice weather testing utility.
« Last Edit: December 18, 2017, 01:47:02 AM by moyack »



 

Started by ashujon

Replies: 0
Views: 2971
Warcraft III Spells and Systems

Started by azlier

Replies: 0
Views: 1788
Codes & Snippets

Started by Magtheridon96

Replies: 0
Views: 2022
Codes & Snippets

Started by moyack

Replies: 6
Views: 18996
Codes & Snippets
Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...