rds1983 / Myra

UI Library for MonoGame, FNA and Stride
MIT License
728 stars 94 forks source link

Extending xmmp features to ensure hardcoding is (less) necessary #295

Closed LuckyScamper closed 3 years ago

LuckyScamper commented 3 years ago
  1. Events can now be called in a xmmp file

Project.LoadFromXml now has extra functions which allow one to enter a class instance to serve as eventhandler.

Project.LoadFromXml(File.ReadAllText("UI/MainMenuScreen.xmmp"), this); public void ContinueButton_Click(object sender, EventArgs e) { HasSceneEnded = true; }

in the xmmp you can enter public non-static methods that serve as the receipients for the events.

<Button Click="ContinueButton_Click" />

  1. You can now localize texts using xmmp

First, you have to enter the Func<string, string> in Project (static variable) which determines how a string is localized. In my own personal project I localize using .resx files and the following line of code is made to reflect that. However, other people may want to localize differently (SQLite, json, etc) so they can enter the lambda as they see fit.

This line of code should be implemented in the implementation of the Game class. There will not be any errors in your runtime if this isn't implemented, unless you are going to use the localize texts property.

Project.Localize = (key) => L18nManager.GetText(key);

Then, in xmmp

<Button Click="ContinueButton_Click" L18nTextKey="continue" />

Do note that L18nTextKey will in its set property override the Text property, so using this one alongside Text will likely not result in a desired outcome.

  1. Result:

The following code: `
Constants.GUIRenderer.Root = Project.LoadFromXml(File.ReadAllText("UI/MainMenuScreen.xmmp"), this).Root;

        // populate main screen
        _title = Constants.GUIRenderer.Root.FindWidgetById("TitleText") as Label;
        _title.Text = L18nManager.GetText("game_title");

        _continueButton = Constants.GUIRenderer.Root.FindWidgetById("ContinueButton") as ImageTextButton;
        _continueButton.Text = L18nManager.GetText("continue");
        _continueButton.Click += ContinueButton_Click;

        _newGameButton = Constants.GUIRenderer.Root.FindWidgetById("NewGameButton") as ImageTextButton;
        _newGameButton.Text = L18nManager.GetText("new_game");
        _newGameButton.Click += NewGameButton_Click;

        _loadGameButton = Constants.GUIRenderer.Root.FindWidgetById("LoadGameButton") as ImageTextButton;
        _loadGameButton.Text = L18nManager.GetText("load_game");
        _loadGameButton.Click += LoadGameButton_Click;

        _optionsButton = Constants.GUIRenderer.Root.FindWidgetById("OptionsButton") as ImageTextButton;
        _optionsButton.Text = L18nManager.GetText("options");
        _optionsButton.Click += OptionsButton_Click;

        _quitButton = Constants.GUIRenderer.Root.FindWidgetById("QuitButton") as ImageTextButton;
        _quitButton.Text = L18nManager.GetText("quit");
        _quitButton.Click += QuitButton_Click;

        UIRoot = Constants.GUIRenderer.Root;`

to this:

Constants.GUIRenderer.Root = Project.LoadFromXml(File.ReadAllText("UI/MainMenuScreen.xmmp"), this).Root; UIRoot = Constants.GUIRenderer.Root;

rds1983 commented 3 years ago

Thanks for the PR. After some thoughts I've decided that I dont need such localization solution. Myra needs localization solution, however probably I'll simply add something like this to MML:

nightblade9 commented 3 years ago

Are there any plans to update and merge this PR? Adding method names to the MML file declaration is really useful for my current project.

rds1983 commented 3 years ago

I'll take auto-subscribing to events, but localization probably will work like this: https://github.com/rds1983/Myra/issues/308 Thanks, @LuckyScamper!