Myra Example

From TRCCompSci - AQA Computer Science
Revision as of 16:55, 30 January 2019 by Admin (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 Grid.Proportion(Grid.ProportionType.Auto));
grid.ColumnsProportions.Add(new Grid.Proportion(Grid.ProportionType.Auto));
grid.RowsProportions.Add(new Grid.Proportion(Grid.ProportionType.Auto));
grid.RowsProportions.Add(new Grid.Proportion(Grid.ProportionType.Auto));

// TextBlock
var helloWorld = new TextBlock
{
  Text = "Hello, World!"
};
grid.Widgets.Add(helloWorld);

// ComboBox
var combo = new ComboBox
{
  GridPositionX = 1,
  GridPositionY = 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 Button
{
  GridPositionX = 0,
  GridPositionY = 1,
  Text = "Show"
};

button.Down += (s, a) =>
{
  var messageBox = Dialog.CreateMessageBox("Message", "Some message!");
  messageBox.ShowModal(_host);
};

grid.Widgets.Add(button);

// Spin button
var spinButton = new SpinButton
{
  GridPositionX = 1,
  GridPositionY = 1,
  WidthHint = 100,
  Nullable = true
};
grid.Widgets.Add(spinButton);

// Add it to the desktop
_host = new Desktop();
_host.Widgets.Add(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();