Difference between revisions of "Myra Example"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(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...")
 
(Draw)
 
(One intermediate revision by the same user not shown)
Line 33: Line 33:
 
};
 
};
  
grid.ColumnsProportions.Add(new Grid.Proportion(Grid.ProportionType.Auto));
+
grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto));
grid.ColumnsProportions.Add(new Grid.Proportion(Grid.ProportionType.Auto));
+
grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto));
grid.RowsProportions.Add(new Grid.Proportion(Grid.ProportionType.Auto));
+
grid.RowsProportions.Add(new Proportion(ProportionType.Auto));
grid.RowsProportions.Add(new Grid.Proportion(Grid.ProportionType.Auto));
+
grid.RowsProportions.Add(new Proportion(ProportionType.Auto));
  
// TextBlock
+
var helloWorld = new Label
var helloWorld = new TextBlock
 
 
{
 
{
 +
  Id = "label",
 
   Text = "Hello, World!"
 
   Text = "Hello, World!"
 
};
 
};
Line 48: Line 48:
 
var combo = new ComboBox
 
var combo = new ComboBox
 
{
 
{
   GridPositionX = 1,
+
   GridColumn = 1,
   GridPositionY = 0
+
   GridRow = 0
 
};
 
};
  
Line 58: Line 58:
  
 
// Button
 
// Button
var button = new Button
+
var button = new TextButton
 
{
 
{
   GridPositionX = 0,
+
   GridColumn = 0,
   GridPositionY = 1,
+
   GridRow = 1,
 
   Text = "Show"
 
   Text = "Show"
 
};
 
};
  
button.Down += (s, a) =>
+
button.Click += (s, a) =>
 
{
 
{
 
   var messageBox = Dialog.CreateMessageBox("Message", "Some message!");
 
   var messageBox = Dialog.CreateMessageBox("Message", "Some message!");
   messageBox.ShowModal(_host);
+
   messageBox.ShowModal(_desktop);
 
};
 
};
  
Line 76: Line 76:
 
var spinButton = new SpinButton
 
var spinButton = new SpinButton
 
{
 
{
   GridPositionX = 1,
+
   GridColumn = 1,
   GridPositionY = 1,
+
   GridRow = 1,
   WidthHint = 100,
+
   Width = 100,
 
   Nullable = true
 
   Nullable = true
 
};
 
};
Line 84: Line 84:
  
 
// Add it to the desktop
 
// Add it to the desktop
_host = new Desktop();
+
_desktop = new Desktop();
_host.Widgets.Add(grid);
+
_desktop.Root = grid;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 92: Line 92:
  
 
<syntaxhighlight lang=c#>
 
<syntaxhighlight lang=c#>
_host.Bounds = new Rectangle(0, 0, GraphicsDevice.PresentationParameters.BackBufferWidth,
+
GraphicsDevice.Clear(Color.Black);
  GraphicsDevice.PresentationParameters.BackBufferHeight);
+
_desktop.Render();
_host.Render();
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 11:10, 14 October 2021

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();