Navigation menu

WoW:Creating simple pop-up dialog boxes: Difference between revisions

Jump to navigation Jump to search
m
Move page script moved page Creating simple pop-up dialog boxes to WoW:Creating simple pop-up dialog boxes without leaving a redirect
(→‎Basic setup: note on avoiding some taint)
m (Move page script moved page Creating simple pop-up dialog boxes to WoW:Creating simple pop-up dialog boxes without leaving a redirect)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{UIHowTo}}
{{wow/uihowto}}
'''Static popups''' are simple one-, two-, or three-button dialog boxes you see when confirming a warlock summon, sending money
'''Static popups''' are simple one-, two-, or three-button dialog boxes you see when confirming a warlock summon, sending money
through the mail, renaming a pet, and so forth. This tutorial describes how you can use the StaticPopup code to display simple dialogs in your own addon.
through the mail, renaming a pet, and so forth. This tutorial describes how you can use the StaticPopup code to display simple dialogs in your own addon.
Line 9: Line 9:
In the "Hello, World" programming tradition, the example on this page will assume that you have a function, called <tt>GreetTheWorld</tt>, and that you are creating a simple dialog to ask the player whether or not to be socially outgoing.
In the "Hello, World" programming tradition, the example on this page will assume that you have a function, called <tt>GreetTheWorld</tt>, and that you are creating a simple dialog to ask the player whether or not to be socially outgoing.


New dialogs are created by adding an entry to the global <tt>StaticPopupDialogs</tt> table, and populating the entry with required and optional information. Here is a simple two-button entry:
New dialogs are created by adding an entry to the global <tt>StaticPopupDialogs</tt> table, and populating the entry with required and optional information. Here is a simple two-button entry:


  StaticPopupDialogs["EXAMPLE_HELLOWORLD"] = {
  StaticPopupDialogs["EXAMPLE_HELLOWORLD"] = {
Line 24: Line 24:
  }
  }


The index into the array is an arbitrary string. It must be unique in the context of the array. (If you are
The index in the array is an arbitrary string. It must be unique in the context of the array. (If you are
familiar with the [[HOWTO:_Create_a_Slash_Command|SlashCmdList]] array, this works exactly the same.)
familiar with the [[HOWTO:_Create_a_Slash_Command|SlashCmdList]] array, this works exactly the same.)


Here you see the basic required information, along with some settings that are strictly optional but should be given by all well-behaved entries:
Here you see the basic required information, along with some settings that are strictly optional but should be given by all well-behaved entries:


* ''text'' - This is the text inside the dialog box. For example, "Looting this item will bind it to you."
* ''text'' - This is the text inside the dialog box. For example, "Looting this item will bind it to you."
* ''button1'' - This is the text on the left-hand "yes" button. Clicking this button will call the <tt>OnAccept</tt> function in the entry. In a popup with only one button, it is this one. There are global variables includiung ACCEPT, CANCEL, and OKAY, which contain localized strings and are perfectly suited for assigning to the button* fields. Some dialogs (like the "you are not part of this instance's group and are going to get teleported" warning) do not even have a single button.
* ''button1'' - This is the text on the left-hand "yes" button. Clicking this button will call the <tt>OnAccept</tt> function in the entry. In a popup with only one button, it is this one. There are global variables includiung ACCEPT, CANCEL, and OKAY, which contain localized strings and are perfectly suited for assigning to the button* fields. Some dialogs (like the "you are not part of this instance's group and are going to get teleported" warning) do not even have a single button.
* ''button2'' - This is the text on the right-hand "no" button. If the entry has an <tt>OnCancel</tt> function, clicking this button will call it with "clicked" as the reason (see below).
* ''button2'' - This is the text on the right-hand "no" button. If the entry has an <tt>OnCancel</tt> function, clicking this button will call it with "clicked" as the reason (see below).
* ''OnAccept'' - This points to a function; it can be as complicated as you like and is typically a local "anonymous"-style function defined on the spot as shown above. You do not need to explicitly hide the popup frame; it will be hidden for you.
* ''OnAccept'' - This points to a function; it can be as complicated as you like and is typically a local "anonymous"-style function defined on the spot as shown above. You do not need to explicitly hide the popup frame; it will be hidden for you.
* ''timeout'' - After this many seconds, the dialog will go away. If the entry has an <tt>OnCancel</tt> function, it will be called with "timeout" as the reason. Dialogs which do not expire should set this to zero.
* ''timeout'' - After this many seconds, the dialog will go away. If the entry has an <tt>OnCancel</tt> function, it will be called with "timeout" as the reason. Dialogs which do not expire should set this to zero.
* ''whileDead'' - Set to true if this dialog can be shown while the player is a ghost. You probably want to do this.
* ''whileDead'' - Set to true if this dialog can be shown while the player is a ghost. You probably want to do this.
* ''hideOnEscape'' - Set to true if hitting the Escape key should be treated like clicking button2. You probably want to do this.
* ''hideOnEscape'' - Set to true if hitting the Escape key should be treated like clicking button2. You probably want to do this.


If your addon has a general OnLoad event handler, that is an excellent place to perform this array insertion. If the contents of the entry do not require any runtime information, you can perform the insertion during loadup by putting it at file scope.
If your addon has a general OnLoad event handler, that is an excellent place to perform this array insertion. If the contents of the entry do not require any runtime information, you can perform the insertion during loadup by putting it at file scope.


Note that this guide refers to setting boolean options to 'true' or 'false'; earlier editions used '1' and 'nil' for the same purposes. The fact of the matter is that you can set those options to anything which evaluates to true-valued or false-valued results according to Lua: 'nil' and 'false' are false-valued, anything else (including 0 and "") is true-valued. A lot of original Blizzard code was written for an earlier version of Lua, which did not have formal boolean types. Use whatever makes the most sense for your addon.
Note that this guide refers to setting boolean options to 'true' or 'false'; earlier editions used '1' and 'nil' for the same purposes. The fact of the matter is that you can set those options to anything which evaluates to true-valued or false-valued results according to Lua: 'nil' and 'false' are false-valued, anything else (including 0 and "") is true-valued. A lot of original Blizzard code was written for an earlier version of Lua, which did not have formal boolean types. Use whatever makes the most sense for your addon.


== Displaying the popup ==
== Displaying the popup ==
Line 47: Line 47:


The dialog will be put together and displayed, and the function returns as soon as the dialog is shown.
The dialog will be put together and displayed, and the function returns as soon as the dialog is shown.
If all goes well, the table object for that dialog will be returned from the function. If there
If all goes well, the table object for that dialog will be returned from the function. If there
are any problems, it returns nil.
are any problems, it returns nil.


This function has two more optional arguments, see [[#Dialog_Text_Parameters|Dialog Text Parameters]] below.
This function has two more optional arguments, see [[#Dialog_Text_Parameters|Dialog Text Parameters]] below.


Up to 4 static popups may be displayed at once. The UI will handle finding an open popup slot for you.
Up to 4 static popups may be displayed at once. The UI will handle finding an open popup slot for you.


== Hiding the popup ==
== Hiding the popup ==
Line 70: Line 70:


=== Dialog text parameters ===
=== Dialog text parameters ===
The ''text'' field of an entry can contain formatting placeholders. When the popup is actually displayed,
The ''text'' field of an entry can contain formatting placeholders. When the popup is actually displayed,
the calling function can pass two additional arguments to <tt>StaticPopup_Show</tt>. The ''text'' field and
the calling function can pass two additional arguments to <tt>StaticPopup_Show</tt>. The ''text'' field and
these two arguments will be passed through the [[API_format|format]] function before being added to the dialog.
these two arguments will be passed through the [[API_format|format]] function before being added to the dialog.


Line 166: Line 166:
* While creating your popup entries, you will probably be doing a lot of UI reloading.  Extract the <tt>[[FrameXML/DebugUI.xml]]</tt> file from the default UI, copy it into your addon folder, and add it to your .toc file.  This has two effects:  it starts verbose logging into FrameXML.log, and it adds a "Reload UI" button to your screen.  Very handy timesaver.  :-)
* While creating your popup entries, you will probably be doing a lot of UI reloading.  Extract the <tt>[[FrameXML/DebugUI.xml]]</tt> file from the default UI, copy it into your addon folder, and add it to your .toc file.  This has two effects:  it starts verbose logging into FrameXML.log, and it adds a "Reload UI" button to your screen.  Very handy timesaver.  :-)
* Added by [[user:Layrajha|Layrajha]]: The OnHide function will always be called after OnAccept or OnCancel have finished their execution. Therefore, it is safe to assume that your changes done in OnAccept or OnCancel will be done when OnHide is called. Also note that you can prevent the popup from hiding after clicking the "Accept" button: just make the OnAccept function return "true" (or anything different from "nil" and "false", it will work just as well).
* Added by [[user:Layrajha|Layrajha]]: The OnHide function will always be called after OnAccept or OnCancel have finished their execution. Therefore, it is safe to assume that your changes done in OnAccept or OnCancel will be done when OnHide is called. Also note that you can prevent the popup from hiding after clicking the "Accept" button: just make the OnAccept function return "true" (or anything different from "nil" and "false", it will work just as well).
[[Category:HOWTOs|Popup Simple Dialog Boxes]]
[[Category:HOWTOs|Popup Simple Dialog Boxes]]
Anonymous user