ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   General Authoring Discussion (https://www.esoui.com/forums/forumdisplay.php?f=174)
-   -   conditoinally show a scene fragment (https://www.esoui.com/forums/showthread.php?t=10507)

Kismalyn 04/03/23 10:03 AM

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!

FlatBadger 04/03/23 10:24 AM

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)

Kismalyn 04/03/23 11:15 AM

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:

Quote:

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?

FlatBadger 04/03/23 11:38 AM

I'm not near my machine at the moment - have your tried just using

fragment:Show()

and

fragment:Hide()

Baertram 04/03/23 12:00 PM

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

Kismalyn 04/03/23 12:44 PM

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!

Kismalyn 04/03/23 12:46 PM

Quote:

Originally Posted by Baertram (Post 47506)
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.


All times are GMT -6. The time now is 01:40 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI