Myra Example

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

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:

GraphicsDevice.Clear(Color.Black);
_desktop.Render();