Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

declarative XAML - dynamic window size to screen (width and height) #21

Closed
rufreakde opened this issue Oct 6, 2024 · 2 comments
Closed

Comments

@rufreakde
Copy link

Hi,
is it possible to have the window object to dynamically fill the full size of the "real window"?

I tried to use the defaults with fill but then it doesnt use the full size of the screen.
I was not able to find something as "ScreenSize" "Fill" or "100%".

https://github.com/Videogamers0/MGUI/wiki/Basic-Layout#size-and-alignment

@Videogamers0
Copy link
Owner

If you want a piece of content to use a percentage of its parent's allocated bounds, you would typically use a Grid with * grid lengths on the row or column definitions. But if you want the root-level Window to always be sized to fill the entire screen bounds, then you would have to manually modify the window's Width and Height values when the MonoGame's game window is resized. There's a sample of this in the SampleHUD which does something like this in its constructor:

UpdateWindowBounds();
if (Desktop.Renderer.Host is GameRenderHost<Game1> Host)
    Host.Game.Window.ClientSizeChanged += (sender, e) => UpdateWindowBounds();
private void UpdateWindowBounds()
{
    Window.Left = Desktop.ValidScreenBounds.Left;
    Window.Top = Desktop.ValidScreenBounds.Top;
    Window.WindowWidth = Desktop.ValidScreenBounds.Width;
    Window.WindowHeight = Desktop.ValidScreenBounds.Height;
}

@rufreakde
Copy link
Author

Thanks again this worked like a charm!

    protected void LoadUIFromFile(string path, Visibility isVisible, Dictionary<string, Func<MGButton, bool>> btnToFuncMap)
    {
        TextSerializer serializer = new();
        serializer.Deserialize(path, out string XAMLString);
        MGWindow UiWindow = MGUI.Core.UI.XAML.XAMLParser.LoadRootWindow(UIMainScreen, XAMLString, false);

        // Clamp window to Screen
        if (UIMainScreen.Renderer.Host is GameRenderHost<Game1> Host)
            Host.Game.Window.ClientSizeChanged += (sender, e) => UpdateWindowBounds(UiWindow, Host.GetBounds());

        this.UIMainScreen.Windows.Add(UiWindow);
        UiWindow.Visibility = isVisible;
        SetupButtonAnimations(UiWindow);
        // generic type string
        SetFunctionToButtonBinding<string>(UiWindow, btnToFuncMap);
    }

    private void UpdateWindowBounds(MGWindow window, Microsoft.Xna.Framework.Rectangle bounds)
    {
        window.Left = bounds.Left;
        window.Top = bounds.Top;
        window.WindowWidth = bounds.Width;
        window.WindowHeight = bounds.Height;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants