Skip to content

Quick Start Tutorial

skzk edited this page Aug 23, 2019 · 38 revisions
  1. Create MonoGame/FNA project.
  2. Reference Myra.
  3. Add following field in the Game class:
private Desktop _desktop;
  1. Add following code to the constructor to make the mouse cursor visible:
IsMouseVisible = true;
  1. Add following code in the LoadContent method, which will create 2x2 grid and populate it with some widgets:
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
{
  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.MouseDown += (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.Widgets.Add(grid);
  1. Add following code to the Draw method:
GraphicsDevice.Clear(Color.Black);

_desktop.Bounds = new Rectangle(0, 0, GraphicsDevice.PresentationParameters.BackBufferWidth, 
  GraphicsDevice.PresentationParameters.BackBufferHeight);
_desktop.Render();

It would result in following: