API:Hello World Gui

From TheWarWiki

Jump to: navigation, search

WAR API Help

This is a simple example of how to create a window with a label and a button.

Note: You will need to have the libslash addon installed as you will need to enter a slash command to open the window.

Create a folder called HelloWorldGui in your Warhammer Online - Age of Reckoning\Interface\AddOns\ directory

Make a new file, HelloWorldGui.mod in the HelloWorldGui folder. This file contains the setup information about the mod which is utilised by the warhammer interpreter.

Populate the file with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<ModuleFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <UiMod name="HelloWorldGui" version="1.0" date="01/10/2009" >
        <Author name="medikage" email="medikage@gmail.com" />
        <Description text="Demonstrates how to create a window that will display some text and a button turn itself off" />
        <VersionSettings gameVersion="1.3.3" windowsVersion="1.0"/>
        <Dependencies>
      <Dependency name="EASystem_WindowUtils" />
      <Dependency name="LibSlash" />
        </Dependencies>
        <Files>
            <File name="HelloWorldGui.lua" />
      <File name="HelloWorldGui.xml" />
        </Files>     
        <OnInitialize>
            <CallFunction name="HelloWorldGui.Initialise" />
        </OnInitialize>
        <OnUpdate/>
        <OnShutdown/>
    </UiMod>
</ModuleFile>

Now make a new file, HelloWorldGui.xml in the HelloWorldGui folder. This file will contain all of the visual component characteristics including name, relationships, inheritance, positioning, size and hooks between UI events and functions.

Populate the file with the following contents:

<Interface xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Interface.xsd">
  <!-- Name the script files that need to be looked in when applying the event handler hooks -->
  <Scripts>
    <Script file="HelloWorldGui.lua" />
  </Scripts>
  <Windows>
    <!-- This is the overall window that will act as a container for everything else. 
         Note: It is not visible by default
         name="HelloWorldGui": This is the handle for the window. When ever we wish to reference
                               this window via lua code, we use this handle.
         inherits="EA_Window_Default": This option lets warhammer know that this window inherits
                                       all of the attributes and characteristics associated with 
                                       "EA_Window_Default"
         layer="secondary": Haven't worked with this a lot yet but it's my understanding that the
                            layer refers to the z positioning of the window (i.e. Which window 
                            will be visible if they occupy the same screen space)
         movable="true": This option denotes that the window can be moved by the user 
                         (Note: this is separate from the layout editor moving which is covered
                         in the .lua file)
         savesettings="false": This option denotes that if the screen is altered at game time in
                               any way, warhammer should not remember it for next time.
    -->
    <Window name="HelloWorldGui" inherits="EA_Window_Default" layer="secondary" movable="true" savesettings="false">
      <!-- This tag sets the size of the container. In this case 280 pixels wide by 150 long -->
      <Size>
        <AbsPoint x="280" y="150"/>
      </Size>
      <!-- Anchors tell warhammer where to place the window in relation to something else. 
           In this case we are setting the screens center (point="center") to align with our 
           containers center ( relativePoint="center") 
      -->
      <Anchors>
        <Anchor point = "center" relativePoint = "center"/>
      </Anchors>
      <!-- Now that we have created the overarching window/container, we can add any other 
           object we like within it, including other windows 
      -->
      <Windows>
        <!-- This will be the visible background for our container. 
           name="$parentBackground": Note this name is different to the name above as it 
                                     contains $parent. This means use the name of the parent
                                     object, in this case "HelloWorldGui", as part of the name
                                     Therefore the name for our background "$parentBackground"
                                     will translate to "HelloWorldGuiBackground" at runtime.
           inherits="EA_Window_DefaultBackgroundFrame": This option means that the settings 
                                                        for our background will inherit the 
                                                        standard mythic windows look and feel.
                                                        (i.e. It will be coloured black and will
                                                        contain a nice border around the edge) 
        -->
        <Window name="$parentBackground" inherits="EA_Window_DefaultBackgroundFrame" handleinput="true" >
          <!-- By setting two anchor points, one at the top left and one at bottom right, of 
               this window to the parent window "HelloWorldGui" we ensure that if the size of 
               "HelloWorldGui" is altered, the background it resized automattically 
          -->
          <Anchors>
            <Anchor point="topleft" relativePoint="topleft" relativeTo="$parent" />
            <Anchor point="bottomright" relativePoint="bottomright" relativeTo="$parent"/>
          </Anchors>
        </Window>    
        <!-- This is the label that will hold the text that we assign via lua coding
             font="font_chat_text": Denotes which font should be used by this label.
             textalign="center": Denotes the alignment of the text
        -->
        <Label name="$parentLabel" font="font_chat_text" textalign="center">          
          <Size><AbsPoint x="200" y="40" /></Size>
          <Anchors>
            <Anchor point="topleft" relativePoint="topleft" relativeTo="$parent">
              <!-- This AbsPoint is an offset.  When combined with the anchor information above 
                   it means, align the topleft of the label 40 pixels to the right and 40 pixels
                   down from the topleft of parent window.
                   Note: A negative x value will off set to the left whilst a negative y value 
                   will off set up
                -->
              <AbsPoint x = "40" y = "40"/>
            </Anchor>
          </Anchors>
          <!-- This sets the colour of the text displayed in the label.  Default colour is White
               if no colour setting is provided -->
          <Color r="255" g="204" b="102" />
        </Label>
        <!-- The following declaration creates a button to add some interaction functionality to 
             our window.
        -->
        <Button name="$parentButton" inherits="EA_Button_DefaultResizeable" textalign="center">
          <Anchors>
            <Anchor point="bottom" relativePoint="bottom" relativeTo="$parent">
              <AbsPoint x = "" y = "-20"/>
            </Anchor>
          </Anchors>
          <!-- The event handler adds a hook on an event to a lua function. In this case, 
               whenever the Left mouse button is release on this button, the function 
               HelloWorldGui.CloseWindow will be called 
          -->
          <EventHandlers>
            <EventHandler event="OnLButtonUp" function="HelloWorldGui.CloseWindow"/>
          </EventHandlers>
        </Button>
      </Windows>
    </Window>
  </Windows>
</Interface>

Now make a new file, HelloWorldGui.lua in the HelloWorldGui folder. This file contains all of the LUA code.

Populate the file with the following contents:

HelloWorldGui = {}
 
function HelloWorldGui.Initialise()
   -- Register the slash command.  Now when the user types "/helloworld" the HelloWorld.Slash 
   -- function will be actioned. 
   LibSlash.RegisterSlashCmd("helloworld", HelloWorldGui.Slash)

   -- Create the window and then hide it.  
   CreateWindow("HelloWorldGui", true)
   WindowSetShowing("HelloWorldGui",false)

   -- Apply text to the label and button so they are not blank.
   LabelSetText("HelloWorldGuiLabel", L"Hello, World!")
   ButtonSetText("HelloWorldGuiButton", L"Close")

   -- Register the window with the LayoutEditor so that it can be moved and resized via the 
   -- Settings/Layout Editor screen. Note: You will need to have the SaveSettings="true" option 
   -- set in the <Window> XML tag if you want these changes to persist beyond game closure.
   LayoutEditor.RegisterWindow ( "HelloWorldGui"             -- Window Name
                               , L"HelloWorldGui"            -- Window Display Name
                               , L"GUI Demonstration Window" -- Window Description
                               , false                       -- Allow Size Width
                               , false                       -- Allow Size Height
                               , true                        -- Allow Hiding
                               , nil                         -- Allowable Anchor List
                               )
end

function HelloWorldGui.CloseWindow()
   --When the button is clicked, hide the HelloWorldGui window.
   WindowSetShowing("HelloWorldGui",false)
end

function HelloWorldGui.Slash()
   --When the user types "/helloworld", show the HelloWorldGui window
   WindowSetShowing("HelloWorldGui",true)
end

That's it. Start up warhammer and then type /helloworld to see your new window and click the button labeled "Close" to hide it again.

Personal tools