Thread Tools Display Modes
03/22/15, 08:41 AM   #1
lightz39
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 21
Window Manager Help

Hey everyone, Im currently trying to learn how window manager works. For my example all I want to do is display the curent FPS on screen. (I know this is something the default game can do) I have a couple questions on how it works.

First of all I see a lot of addons set wm = WINDOW_MANAGER. Is this required? Does it just save you from having to type WINDOW_MANAGER everytime. You could put wm.whatever instead?

Secondly how does it actually work. So I want to create my label that says "FPS: XX" xx being the actual framerate taken from GetFramerate()(?) Would my code be something like this

Code:
function AddonName:CreateControl()
fps_label = WINDOW_MANAGER:CreateControl(name, nil(Can I do this here? Do I need a parent?), CT_LABEL)
fps_label:SetText(fps)
Any help is appreciated. I'm sure I'm way off.
  Reply With Quote
03/22/15, 09:00 AM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,000
As fas as I know wm = WINDOW_MANAGER is not necessary. It only creates a pointer/reference to WINDOW_MANAGER so you can use the variable wm (which could be "w", or "win" or "foo" as well) as it is shorter to type.

Basically your code is correct. The Window manager actually "manages" all the controls the game uses. So you can create it by help of the manager, give it a name (like MyFPSLabel) so you can find the control again by using

local myFpsLabelControl = WINDOW_MANAGER:GetControlByName("MyFPSLabel", "")

You should pay attention to define your controls and functions local, if you don#t want to use them outside the functions. Otherwisw, if only your addon needs the variables define them local in your addon. Otherwise they glitch to global and can be used all over the whole game, even by other addons sometimes.

If the framerate returned by GetFrameRate() is not a string you can force a type by using

fps = GetFrameRate()
fps_label:SetText(tostring(fps))

Or use something like
fps_label:SetText(string.format(fps))

Oh and to your parent question:
It can be nil, yes. If you set the parent the control will be "below" the parent in the hierarchy.
Hint: Install the addon ZGOO, move your mose over the control you create and type /zgoo mouse in the chat
You'll see the parent and child controls in the ZGOO list then, by opening the functions GetChildren() or GetParent() and are able to toggle the visibility so you can see on your srceen which control it is, and what child controls will be hidden/shown as well if you hide/show the parent.

Last edited by Baertram : 03/22/15 at 09:02 AM.
  Reply With Quote
03/22/15, 10:35 AM   #3
lightz39
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 21
Thank you for your help, this makes things more clear.
  Reply With Quote
03/22/15, 12:15 PM   #4
lightz39
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 21
Ok, so I was able to get it to display my FPS but my next question is how do I get it to constantly update instead of just displaying the number i grabbed at the time it was loaded?

I imagine it has to be on some sort of update function. I haven't really worked with that yet though. Would I register it as an event?

So I seemed to have figured it out but It was from copy and pasting the buffer tutorial. Maybe some help on why this works?

Code:
local BufferTable = {}
    function BufferReached(key, buffer)
        if key == nil then return end
        if BufferTable[key] == nil then BufferTable[key] = {} end
        BufferTable[key].buffer = buffer or 1
        BufferTable[key].now = GetFrameTimeSeconds()
        if BufferTable[key].last == nil then BufferTable[key].last = BufferTable[key].now end
        BufferTable[key].diff = BufferTable[key].now - BufferTable[key].last
        BufferTable[key].eval = BufferTable[key].diff >= BufferTable[key].buffer
        if BufferTable[key].eval then BufferTable[key].last = BufferTable[key].now end
        return BufferTable[key].eval
    end

function NmSystemBar:Initialize()
	local fps = GetFramerate()
	if not BufferReached("fpsbuffer", 1) then return; end
	NmSystemBarWindowLabel:SetText("FPS: "..zo_round(fps))
EVENT_MANAGER:UnregisterForEvent(NmSystemBar.name, EVENT_ADD_ON_LOADED)
end

Last edited by lightz39 : 03/22/15 at 12:46 PM.
  Reply With Quote
03/22/15, 01:43 PM   #5
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Lua Code:
  1. local function UpdateFunction()
  2.     local fps = ("%d"):format(GetFramerate())
  3.     NmSystemBarWindowLabel:SetText(fps)
  4. end
  5.  
  6. local interval = 200 --milliseconds
  7. EVENT_MANAGER:RegisterForUpdate("uniqueName", interval, UpdateFunction)

Last edited by Garkin : 03/22/15 at 01:47 PM.
  Reply With Quote
03/22/15, 03:59 PM   #6
lightz39
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 21
thank you that worked great. My FPS counter works as intended however I am getting a function expected instead of nil stack traceback in the main chunk. Any ideas?
  Reply With Quote
03/22/15, 05:51 PM   #7
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by lightz39 View Post
thank you that worked great. My FPS counter works as intended however I am getting a function expected instead of nil stack traceback in the main chunk. Any ideas?
It's probably error message that function which you call from XML doesn't exist. Can you post your code here?
  Reply With Quote
03/22/15, 06:33 PM   #8
lightz39
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 21
Sure, the best way to learn is to have someone tell you what you're doing wrong.

Lua Code:
  1. NmSystemBar = {}
  2.  
  3. NmSystemBar.name = "NmSystemBar"
  4.  
  5. function NmSystemBar:Initialize()
  6. EVENT_MANAGER:UnregisterForEvent(NmSystemBar.name, EVENT_ADD_ON_LOADED)
  7. end
  8.  
  9. function NmSystemBar.OnAddonLoaded(event, addonName)
  10.     if addonName == NmSystemBar.name then
  11.         NmSystemBar:Initialize()
  12.     end
  13. end
  14.  
  15. local function OnUpdate()
  16.     local fps = GetFramerate()
  17.     NmSystemBarWindowLabel:SetText("FPS: "..zo_round(fps))
  18. end
  19.  
  20. EVENT_MANAGER:RegisterForUpdate("FPS_Update", 1000, OnUpdate)
  21.  
  22. EVENT_MANAGER:RegisterForEvent(NmSystemBar.name, EVENT_ADD_ON_LOADED, NmSystemBar.OnAddonLoaded)

Xml Code:
  1. <GuiXml>
  2.    <Controls>
  3.    <TopLevelControl name="NmSystemBarWindow" clampedToScreen="true" mouseEnabled="true" movable="true" hidden="false"> 
  4.       <Dimensions x="173" y="70" />
  5.       <ClampedToScreenInsets top="20" bottom="-20" left="20" right="-20"/>
  6.       <Anchor point="CENTER" />
  7.  
  8.      <Controls>
  9.         <Texture name="$(parent)Bg" textureFile="EsoUI/Art/Performance/StatusMeterMunge.dds">
  10.         <Dimensions x="256" y="256"/>
  11.         <Anchor point="CENTER" relativeTo="$(parent)" relativePoint="CENTER"/>
  12.         </Texture>
  13.     </Controls>
  14.  
  15.     <Controls>
  16.         <Label name="$(parent)Label" font="ZoFontWinT2" color="c5c29e" verticalAlignment="CENTER" horizontalAlignment="CENTER"  >  
  17.             <AnchorFill/>
  18.          </Label>
  19.     </Controls>
  20.      
  21.    </TopLevelControl>
  22.    </Controls>
  23. </GuiXml>

Thanks for the help.

EDIT: I know there is nothing to do with window manager in that code but I figured first I'd get the thing to work before I dive into something I'm not comfortable with yet.
  Reply With Quote
03/22/15, 06:47 PM   #9
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
From the code I believe that UI error is because you register OnUpdate before your window is created. Move this line:
Lua Code:
  1. EVENT_MANAGER:RegisterForUpdate("FPS_Update", 1000, OnUpdate)
to NmSystemBar:Initialize().

So your code will be something like:
Lua Code:
  1. NmSystemBar = {}
  2.  
  3. NmSystemBar.name = "NmSystemBar"
  4.  
  5. function NmSystemBar:Initialize()
  6.     EVENT_MANAGER:UnregisterForEvent(NmSystemBar.name, EVENT_ADD_ON_LOADED)
  7.     EVENT_MANAGER:RegisterForUpdate("FPS_Update", 1000, OnUpdate)
  8. end
  9.  
  10. function NmSystemBar.OnAddonLoaded(event, addonName)
  11.     if addonName == NmSystemBar.name then
  12.         NmSystemBar:Initialize()
  13.     end
  14. end
  15.  
  16. local function OnUpdate()
  17.     local fps = GetFramerate()
  18.     NmSystemBarWindowLabel:SetText("FPS: "..zo_round(fps))
  19. end
  20.  
  21. EVENT_MANAGER:RegisterForEvent(NmSystemBar.name, EVENT_ADD_ON_LOADED, NmSystemBar.OnAddonLoaded)
  Reply With Quote
03/22/15, 06:59 PM   #10
lightz39
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 21
Hum, I put the line up like you suggested and got an error and the fps stopped drawing on the background. So I put it back to where it was to see if it still worked and the original error no longer happens. Weird.
  Reply With Quote
03/22/15, 07:40 PM   #11
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by lightz39 View Post
Hum, I put the line up like you suggested and got an error and the fps stopped drawing on the background. So I put it back to where it was to see if it still worked and the original error no longer happens. Weird.
Hm, my bad - function OnUpdate is local, so it has to be defined before you call it. It should be:

Lua Code:
  1. NmSystemBar = {}
  2.  
  3. NmSystemBar.name = "NmSystemBar"
  4.  
  5. local function OnUpdate()
  6.     local fps = GetFramerate()
  7.     NmSystemBarWindowLabel:SetText("FPS: "..zo_round(fps))
  8. end
  9.  
  10. function NmSystemBar:Initialize()
  11.     EVENT_MANAGER:UnregisterForEvent(NmSystemBar.name, EVENT_ADD_ON_LOADED)
  12.     EVENT_MANAGER:RegisterForUpdate("FPS_Update", 1000, OnUpdate)
  13. end
  14.  
  15. function NmSystemBar.OnAddonLoaded(event, addonName)
  16.     if addonName == NmSystemBar.name then
  17.         NmSystemBar:Initialize()
  18.     end
  19. end
  20.  
  21. EVENT_MANAGER:RegisterForEvent(NmSystemBar.name, EVENT_ADD_ON_LOADED, NmSystemBar.OnAddonLoaded)
  Reply With Quote
03/22/15, 07:50 PM   #12
lightz39
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 21
Ah, excellent all works as it should now. Thank you very much.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Window Manager Help


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off