Wowpedia

We have moved to Warcraft Wiki. Click here for information and the new URL.

READ MORE

Wowpedia
Line 159: Line 159:
 
* The 'text' fields and button1/2 fields are usually localized. Blizzard tends to use generic texts like ACCEPT, CANCEL, and OKAY; you can probably do the same. They wrap all those in a call to TEXT() for translation, like <tt>button1 = TEXT(ACCEPT)</tt>
 
* The 'text' fields and button1/2 fields are usually localized. Blizzard tends to use generic texts like ACCEPT, CANCEL, and OKAY; you can probably do the same. They wrap all those in a call to TEXT() for translation, like <tt>button1 = TEXT(ACCEPT)</tt>
 
* 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).
 
 
   
 
-Documentation by [[user:Farmbuyer|Farmbuyer]]
 
-Documentation by [[user:Farmbuyer|Farmbuyer]]

Revision as of 00:32, 22 July 2007

Introduction

This HOWTO covers the creation of what Blizzard calls static popups. These are the simple one-button and two-button dialog boxes you see when confirming a warlock summon, sending money through the mail, renaming a pet, and so forth.

As you might expect, all of the basic UI elements (positioning, sound effects, layout) are handled for you.


Basic Setup

The example on this page will assume that you have a function, called GreetTheWorld, 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 StaticPopupDialogs table, and populating the entry with required and optional information. Here is a simple two-button entry:

StaticPopupDialogs["EXAMPLE_HELLOWORLD"] = {
  text = "Do you want to greet the world today?",
  button1 = "Yes",
  button2 = "No",
  OnAccept = function()
      GreetTheWorld();
  end,
  timeout = 0,
  whileDead = 1,
  hideOnEscape = 1
};

The index into the array is an arbitrary string. It must be unique in the context of the array. (If you are familiar with the 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:

  • 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 "yes" button. Clicking this button will call the OnAccept function in the entry. In a popup with only one button, it is this one. 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 "no" button. If the entry has an OnCancel function, clicking this button will call it with "clicked" as the reason (see below).
  • OnAccept - This is a local function; it can be as complicated as you like. You do not need to explicitly hide the popup frame, it will be done for you.
  • timeout - After this many seconds, the dialog will go away. If the entry has an OnCancel function, it will be called with "timeout" as the reason. Dialogs which do not expire should set this to zero.
  • whileDead - Set to 1 if this dialog can be shown while the player is a ghost. You probably want to do this.
  • hideOnEscape - Set to 1 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.


Displaying the Popup

To display the dialog, call StaticPopup_Show with the name of the entry:

StaticPopup_Show ("EXAMPLE_HELLOWORLD");

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

This function has two more optional arguments, see Dialog Text Parameters below.

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


Advanced Setup

  • OnAccept - This function can take up to two arguments, both optional. They are for passing arbitrary data around the callbacks. There are very few examples of the use of the arguments. In general this is used with dialogs that have an editable text field, to pass the entered text back to the function. Also for chaining dialog boxes together.
  • OnCancel - This function can take up to two arguments, both optional. The first is used the same way as the first parameter in OnAccept (whatever that may actually be). The second is a string describing the reason the popup was cancelled; the game will pass this as required:
    • "override" - Another popup cancelled this one, or is set to 'exclusive' (see below), or the player is dead or logging out.
    • "timeout" - The popup's entry specified a nonzero timeout field, and the time expired.
    • "clicked" - The player clicked button2, or hit the escape key and hideOnEscape is set.


Dialog Text Parameters

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 StaticPopup_Show. The text field and these two arguments will be passed through the format function before being added to the dialog.

For example, the default guild invitation popup sets text = "%s invites you to join %s", and the display call is to StaticPopup_Show ("GUILD_INVITE", name_of_officer, name_of_guild).


Editable Text Fields

To add a editbox in the popup set the hasEditBox option to 1. The EditBox get the name "$parentEditBox", relative to the Popup. To get and set the value of the EditBox use the following code:

OnShow = function()
    getglobal(this:GetName().."EditBox"):SetText("Some data")
    -- notice: "this" is the StaticPopup, normaly its "StaticPopup1"
end,
OnAccept = function()
    getglobal(this:GetParent():GetName().."EditBox"):GetText()
    -- do whatever you want with it
    -- notice: "this" is the left button, normaly its "StaticPopup1Button1",
    --         so you need the GetParent() call.
end,
hasEditBox = 1,

Optional Features

There are some more settings available for use in static popup entries. This list is almost certainly incomplete, I'm just describing the ones which catch my eye. For more information, extract the StaticPopup.xml and .lua files from the default UI and browse.

  • sound - Play this sound when the dialog is displayed. For example, "igPlayerInvite".
  • hasMoneyFrame - This is for things like the money-in-mail confirmation. See the LUA file for more.
  • showAlert - Set this to 1 if you also want the OH NO SOMETHING BAD icon to be displayed in the popup. For example, deleting a mail message with an item still attached to it.
  • notClosableByLogout - Normally if a player quits the game, your popup will go away and its OnCancel function will be called. Set this field to 1 to avoid that behavior.
  • cancels - If your popup should make another popup go away, set this field to that popup's name. For example, cancels = "EXAMPLE_HELLOWORLD". If that popup is displayed at the time yours is shown, that popup's OnCancel function will be called and its frame hidden.
  • StartDelay and delayText - These fields are responsible for things like the delay before you can resurrect, before you get kicked out of an ungrouped instance, etc. The first is a function (a pointer to the function itself, not its name) to be called which returns the number of seconds which must pass before button1 can be used. The second is text to be displayed during the delay; it is formatted like the normal text field, with the time as the parameters.
  • exclusive - Set this to 1 to make your popup go away if any other popup is displayed.
  • OnShow and OnHide - These functions will be called when the popup frame is initially shown and finally hidden, respectively. Most popups can ignore these. A very few need to do something special.

As an example, consider the Ready Check from CT_RaidAssist, which pops up a two-button dialog box with a 30-second timeout. Currently the CT authors implement everything from scratch: 107 lines of XML specifying window, field, and button sizes, plus a couple lines of Lua here and there. It could be replaced with something like the following:

StaticPopupDialogs["EXAMPLE_CTRA_READY"] = {
  text = "%s has performed a ready check.  Are you ready?",
  button1 = "Yes",
  button2 = "No",
  OnAccept = function()
      CT_RA_SendReady();
  end,
  OnCancel = function (_,reason)
      if reason == "timeout" or reason == "clicked" then
          CT_RA_SendNotReady();
      else
          -- "override" ...?
      end;
  end,
  sound = "levelup2",
  timeout = 30,
  whileDead = 1,
  hideOnEscape = 1
};

and called via

StaticPopup_Show ("EXAMPLE_CTRA_READY", CT_RA_CheckReady_Person);

(Note: the above was written before the 1.11 game patch added a builtin ready check. While ready checks no longer need to be implemented by addon authors, the example is simple enough to remain here.)

Passing Arguments to Local Functions

It is possible, though not immediately obvious how, to pass arbitrary user data to the local functions (e.g. OnAccept, OnCancel).

Write your local function like this:

   OnAccept = function(argName)
                  DoSomethingWith(argName)
              end

And make the call like this:

   varName = "Some value"    -- This is the data you want to use in OnAccept
   local dialog = StaticPopup_Show("YOUR_POPUP")    -- dialog contains the frame object
   if(dialog) then
       dialog.data = varName    -- set the frame's data to the value you want
   end

If you'll notice, you are actually setting the frame's data (which is passed as the first argument to the local function) after the user has clicked the button. I'm not exactly sure how this mechanism works, my initial guess is that the popup functions are run in a separate thread to allow for this kind of manipulation.

Added by user:egingell: The above works because script execution is not halted by the static popup.

Notes and Observations

  • The 'text' fields and button1/2 fields are usually localized. Blizzard tends to use generic texts like ACCEPT, CANCEL, and OKAY; you can probably do the same. They wrap all those in a call to TEXT() for translation, like button1 = TEXT(ACCEPT)
  • While creating your popup entries, you will probably be doing a lot of UI reloading. Extract the FrameXML\DebugUI.xml 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 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).

-Documentation by Farmbuyer