Thread Tools Display Modes
04/03/23, 10:03 AM   #1
Kismalyn
Join Date: Mar 2023
Posts: 4
conditoinally show a scene fragment

hi hive-dev-mind.

I have an addon that I need to show, via an on/off toggle conditionally. I can't get the fragment to stay hidden, after first enabling it, and then disabling it.

I saw this post and tried the following:

Code:
        fragment:SetConditional(function() 
		return settings.activeWindow
	end)
but it doesn't work, I still get the fragment showing even with the activeWindow var is false.

Ive also tried conditional code in a conventional callback, like this:

Code:
function Ui:ShowHideTLC(makeVis)
	local sceneHUD = SCENE_MANAGER:GetScene("hud")
	local sceneUI = SCENE_MANAGER:GetScene("hudui")
	local TLC = AddOnWindow
	local tlcFragment = ZO_SimpleSceneFragment:New(TLC)
	
        TLC:SetHidden(not makeVis)

	if (makeVis == true and settings.activeWindow == true) then
		sceneHUD:AddFragment(tlcFragment)
		sceneUI:AddFragment(tlcFragment)
	else
		sceneHUD:RemoveFragment(tlcFragment)
		sceneUI:RemoveFragment(tlcFragment)
	end
end

local function tlcFragmentChange(oldState, newState)
	if (newState == SCENE_FRAGMENT_SHOWN) then
		self:ShowHideTLC(true)
	elseif (newState == SCENE_FRAGMENT_HIDDEN) then
		self:ShowHideTLC(false)
	end
end

-- this is bound to a key
function Ui:ToggleShow()
	settings.activeWindow = not settings.activeWindow
	self:ShowHideTLC(settings.activeWindow)
end

-- this is in the init function
local tlcFragment = ZO_SimpleSceneFragment:New(AddOnWindow)
tlcFragment:RegisterCallback("StateChange", tlcFragmentChange)
I almost get the behaviour I want with this, except that once Ive toggled the addon visible, it will always appear on the HUD, even if I retoggle it off again (if that makes sense)

I have a feeling Im overcomplicating it, as I tend to do, but I cant figure it out. I'm new to Lua, let alone ESOUI.

Thanks!

Last edited by Kismalyn : 04/03/23 at 10:09 AM.
  Reply With Quote
04/03/23, 10:24 AM   #2
FlatBadger
 
FlatBadger's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2021
Posts: 17
It definitely looks like you're overcomplicating it :-)

This is just off the top of my head, so hopefully I'm not talking rubbish, but:

Try creating your tlcFragment as you are, then add it to the hud/hudui (or whereever you want it to show)eg.
Code:
SCENE_MANAGER:GetScene("hud"):AddFragment(tlcFragment)
SCENE_MANAGER:GetScene("hudui"):AddFragment(tlcFragment)
Then to hide/show it use:
Code:
tlcFragment:SetHiddenForReason("disabled", true)
or
Code:
tlcFragment:SetHiddenForReason("disabled", false)
No need to remove it from the scene again in this scenario.

If you like, you can use ZO_HUDFadeSceneFragment to have your fragment fade in/out - you just add a couple of extra parameters to the SetHiddenForReason call, eg.

Code:
tlcFragment:SetHiddenForReason("disable", true, FADE_IN_TIMEIN_MS, FADE_OUT_TIME_IN_MS)

Last edited by FlatBadger : 04/03/23 at 10:30 AM.
  Reply With Quote
04/03/23, 11:15 AM   #3
Kismalyn
Join Date: Mar 2023
Posts: 4
thanks for looking at this for me!

The adding to the scene works fine which I do in the CreateUI function.

The SetHiddenForReason call feels like exactly what I need. Ive tried to implement it as follows:
Code:
function Ui:ToggleShow()
	settings.activeWindow = not settings.activeWindow

	local fragment = ZO_SimpleSceneFragment:New(TLC)
	fragment:SetHiddenForReason("disabled", settings.activeWindow)   -- <<<<< line 76
end
but Im getting the error:

user:/AddOns/myAddon/Ui.lua:76: function expected instead of nil
stack traceback:
Ive looked in the docs and I can see the syntax seems to be correct, but it seems to be wanting a function somewhere, but I cant figure out where.. any ideas?
  Reply With Quote
04/03/23, 11:38 AM   #4
FlatBadger
 
FlatBadger's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2021
Posts: 17
I'm not near my machine at the moment - have your tried just using

fragment:Show()

and

fragment:Hide()
  Reply With Quote
04/03/23, 12:00 PM   #5
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,912
Only fragments support that function where ZO_MixinHideableSceneFragment was used during their initialization,
like ZO_ActionLayerFragment or ZO_HUDFadeSceneFragment

You need to use on of the fragments or call ZO_MixinHideableSceneFragment (yourFragmentObject) to add the function to your fragment.

See ZOs code at:
esoui/libraries/zo_scene/zo_scenefragmenttemplates.lua
  Reply With Quote
04/03/23, 12:44 PM   #6
Kismalyn
Join Date: Mar 2023
Posts: 4
woo! it worked. I did a combo of things from what you've said and some others.

For anyone else, here's what worked for me.

Code:
local settings = {
       activeWindow = false
}

local tlcFragment = nil

function Ui:ToggleShow()
	settings.activeWindow = not settings.activeWindow
	if(tlcFragment ~= nil) then
		if(settings.activeWindow == true) then
			tlcFragment:Show()
		else
			tlcFragment:Hide()
		end
	end
end

function Ui:AddTLCFragment(fragment)
	SCENE_MANAGER:GetScene("hud"):AddFragment(fragment)
	SCENE_MANAGER:GetScene("hudui"):AddFragment(fragment)
end

function Ui:CreateUI()
       -- /// blah blah blah code
        
        -- making this var global first seemed to avoid problems in the toggleShow function
        tlcFragment = ZO_SimpleSceneFragment:New(MyAppTLC)
	tlcFragment:SetConditional(function() return settings.activeWindow end)
	
	SCENE_MANAGER:GetScene("hud"):AddFragment(tlcFragment)
	SCENE_MANAGER:GetScene("hudui"):AddFragment(tlcFragment)
end
now it toggles on/off, and doesn't reappear after switching out of HUD/HUDUI even though its off due to the setConditional.

It was necessary to wrap in a ~= as it seemed to be hitting some kind of non-initialised race condition. Theres probably a better way of doing that part.

Thanks again for your help!
  Reply With Quote
04/03/23, 12:46 PM   #7
Kismalyn
Join Date: Mar 2023
Posts: 4
Originally Posted by Baertram View Post
Only fragments support that function where ZO_MixinHideableSceneFragment was used during their initialization,
like ZO_ActionLayerFragment or ZO_HUDFadeSceneFragment

You need to use on of the fragments or call ZO_MixinHideableSceneFragment (yourFragmentObject) to add the function to your fragment.

See ZOs code at:
esoui/libraries/zo_scene/zo_scenefragmenttemplates.lua
ahhhh that makes sense. Thanks for this.

This is an addon I'm likely taking over, so if I do, I might refactor in that format.
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » conditoinally show a scene fragment

Thread Tools
Display Modes

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