Difference between revisions of "Myra Example"
(Created page with "==Create MonoGame Project== You need to start with a MonoGame project. ==Install Myra== In the nuget console install Myra by typing: install-package Myra ==Variables Requi...") |
(→LoadContent) |
||
Line 33: | Line 33: | ||
}; | }; | ||
− | grid.ColumnsProportions.Add(new | + | grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto)); |
− | grid.ColumnsProportions.Add(new | + | grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto)); |
− | grid.RowsProportions.Add(new | + | grid.RowsProportions.Add(new Proportion(ProportionType.Auto)); |
− | grid.RowsProportions.Add(new | + | grid.RowsProportions.Add(new Proportion(ProportionType.Auto)); |
− | + | var helloWorld = new Label | |
− | var helloWorld = new | ||
{ | { | ||
+ | Id = "label", | ||
Text = "Hello, World!" | Text = "Hello, World!" | ||
}; | }; | ||
Line 48: | Line 48: | ||
var combo = new ComboBox | var combo = new ComboBox | ||
{ | { | ||
− | + | GridColumn = 1, | |
− | + | GridRow = 0 | |
}; | }; | ||
Line 58: | Line 58: | ||
// Button | // Button | ||
− | var button = new | + | var button = new TextButton |
{ | { | ||
− | + | GridColumn = 0, | |
− | + | GridRow = 1, | |
Text = "Show" | Text = "Show" | ||
}; | }; | ||
− | button. | + | button.Click += (s, a) => |
{ | { | ||
var messageBox = Dialog.CreateMessageBox("Message", "Some message!"); | var messageBox = Dialog.CreateMessageBox("Message", "Some message!"); | ||
− | messageBox.ShowModal( | + | messageBox.ShowModal(_desktop); |
}; | }; | ||
Line 76: | Line 76: | ||
var spinButton = new SpinButton | var spinButton = new SpinButton | ||
{ | { | ||
− | + | GridColumn = 1, | |
− | + | GridRow = 1, | |
− | + | Width = 100, | |
Nullable = true | Nullable = true | ||
}; | }; | ||
Line 84: | Line 84: | ||
// Add it to the desktop | // Add it to the desktop | ||
− | + | _desktop = new Desktop(); | |
− | + | _desktop.Root = grid; | |
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 10:09, 14 October 2021
Contents
Create MonoGame Project
You need to start with a MonoGame project.
Install Myra
In the nuget console install Myra by typing:
install-package Myra
Variables Required
You will need to add the following as a variable at the top of the Game1 class:
private Desktop _host;
Game1 Constructor
You need to add the following line to the Game1 constructor in order to see the mouse:
IsMouseVisible = true;
LoadContent
The following code will create a panel, for now add it to LoadContent because it only runs once. Ideally you would create a screen class and individual sub classes for each screen:
MyraEnvironment.Game = this;
var grid = new Grid
{
RowSpacing = 8,
ColumnSpacing = 8
};
grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto));
grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto));
grid.RowsProportions.Add(new Proportion(ProportionType.Auto));
grid.RowsProportions.Add(new Proportion(ProportionType.Auto));
var helloWorld = new Label
{
Id = "label",
Text = "Hello, World!"
};
grid.Widgets.Add(helloWorld);
// ComboBox
var combo = new ComboBox
{
GridColumn = 1,
GridRow = 0
};
combo.Items.Add(new ListItem("Red", Color.Red));
combo.Items.Add(new ListItem("Green", Color.Green));
combo.Items.Add(new ListItem("Blue", Color.Blue));
grid.Widgets.Add(combo);
// Button
var button = new TextButton
{
GridColumn = 0,
GridRow = 1,
Text = "Show"
};
button.Click += (s, a) =>
{
var messageBox = Dialog.CreateMessageBox("Message", "Some message!");
messageBox.ShowModal(_desktop);
};
grid.Widgets.Add(button);
// Spin button
var spinButton = new SpinButton
{
GridColumn = 1,
GridRow = 1,
Width = 100,
Nullable = true
};
grid.Widgets.Add(spinButton);
// Add it to the desktop
_desktop = new Desktop();
_desktop.Root = grid;
Draw
In the draw method, add the following after the GraphicsDevice.Clear() line:
_host.Bounds = new Rectangle(0, 0, GraphicsDevice.PresentationParameters.BackBufferWidth,
GraphicsDevice.PresentationParameters.BackBufferHeight);
_host.Render();