Does anyone else find Verse UI really slow?

This is my player UI class. It keeps all the UI in a collection and then can add it to a active stack. If you push more UI to the screen while you already have something there, it will cover it.

This works really good when you have multiple UI instances opening and you want to disable the one below without actually removing it.

AS A VERY IMPORTANT NOTE! At the time I did not implement ShouldConsumeInput when adding to the player screen. This will have to be implemented.

kingdom_ui_type := enum
{
    UNDEFINED_UI,
    INPUT_CONSUMER_UI,
    MAIN_MENU_UI,
    LOADOUT_PLAYER_WEAPONS_MENU_UI,
    LOADOUT_WEAPON_SELECTION_MENU_UI,
    BLACKSMITH_SHOP_UI,
    AMMO_SHOP_UI,
    CONSUMABLES_SHOP_UI,
    RECRUITMENT_SHOP_UI,
    LOADOUT_PERK_SELECTION_MENU_UI,
    EQUIPMENT_SHOP_UI
}

kingdom_player_ui := class()
{
    OwningPlayer:player;

    var MaybeCurrentActiveUI<private>:?kingdom_player_active_ui_handle = false;
    var ActiveUIStack<private>:[]kingdom_player_active_ui_handle = array{};

    var PlayerUICollection<public>:[kingdom_ui_type]kingdom_base_player_ui = map{};

    AddUIToPlayerScreen(UIType:kingdom_ui_type, ShouldConsumeInput:logic):void=
    {
        if (UIEntryToActivate := PlayerUICollection[UIType])
        {
            var IsUIAlreadyActive:logic = false;
            
            if (CurrentActiveUI := MaybeCurrentActiveUI?,
            CurrentActiveUI.UIType = UIType)
            {
                set IsUIAlreadyActive = true;
            }

            if (IsUIAlreadyActive = false)
            {
                var MaybeActiveUIHandle:?kingdom_player_active_ui_handle = false;
                if (ActiveUIStack.Length > 0)
                {
                    for (ActiveUI : ActiveUIStack,
                    ActiveUI.UIType = UIType)
                    {
                        set MaybeActiveUIHandle = option{ActiveUI};
                    }
                }

                if (MaybeActiveUIHandle?)
                {
                    # Remove the entry from the array
                    # we add this back in after
                    RemoveEntryFromActiveUI(UIType);
                }
                else
                {
                    NewUIHandle := kingdom_player_active_ui_handle
                    {
                        UIType := UIType,
                        UIHandle := UIEntryToActivate
                    };

                    set MaybeActiveUIHandle = option{NewUIHandle};
                }

                if (NewActiveUIHandle := MaybeActiveUIHandle?)
                {
                    if (CurrentActiveUI := MaybeCurrentActiveUI?)
                    {
                        CurrentActiveUI.UIHandle.RemoveCanvasFromPlayer(OwningPlayer);
                    }
                    
                    set ActiveUIStack += array{NewActiveUIHandle};
                    set MaybeCurrentActiveUI = option{NewActiveUIHandle};

                    UIEntryToActivate.AddCanvasToPlayer(OwningPlayer, player_ui_slot{InputMode:= ui_input_mode.All});
                    UIEntryToActivate.SetVisibility(widget_visibility.Visible);
                }
            }
            else
            {
                Print("Attempted to push UI to screen but it is already on the screen.");
            }
        }
        else
        {
            Print("No UI found in the player collection.");
        }
    }

    RemoveUIFromPlayerScreen(UITypeToRemove:kingdom_ui_type):void=
    {
        # We we have no current active UI, something has gone wrong
        if (ActiveUI := MaybeCurrentActiveUI?)
        {
            MaybeRemovedUI := RemoveEntryFromActiveUI(UITypeToRemove);
            if (RemovedUI := MaybeRemovedUI?)
            {
                RemovedUI.RemoveCanvasFromPlayer(OwningPlayer);
            }

            if (ActiveUIStack.Length > 0,
            NextActiveUI := ActiveUIStack[ActiveUIStack.Length - 1])
            {
                if (ActiveUI.UIType = UITypeToRemove)
                {   
                    NextActiveUI.UIHandle.AddCanvasToPlayer(OwningPlayer, player_ui_slot{InputMode:= ui_input_mode.All});
                    NextActiveUI.UIHandle.SetVisibility(widget_visibility.Visible);
                    set MaybeCurrentActiveUI = option{NextActiveUI};
                }
            }
            else
            {
                set MaybeCurrentActiveUI = false;
            }
        }
        else
        {
            Print("Tried to remove UI from player but player has no active UI!");
        }
    }

    RemoveAllUIFromPlayerScreen():void=
    {
        for (ActiveUI : ActiveUIStack)
        {
            ActiveUI.UIHandle.RemoveCanvasFromPlayer(OwningPlayer);
        }

        set ActiveUIStack = array{};
        set MaybeCurrentActiveUI = false;
    }

    # This should keep the order of the array
    RemoveEntryFromActiveUI<private>(UITypeToRemove:kingdom_ui_type):?kingdom_base_player_ui=
    {
        var NewUIStack:[]kingdom_player_active_ui_handle = array{};
        var RemovedUI:?kingdom_base_player_ui = false;
        for (UIEntry : ActiveUIStack)
        {
            if (UIEntry.UIType <> UITypeToRemove)
            {
                set NewUIStack += array{UIEntry};
            }
            else
            {
                set RemovedUI = option{UIEntry.UIHandle};
            }
        }

        set ActiveUIStack = NewUIStack;
        return RemovedUI;
    }
}
1 Like