using Gum.Forms;
using Gum.Forms.Controls;
using Gum.Forms.DefaultVisuals.V3;
using Gum.Mvvm;
using Gum.Wireframe;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Gum;
using Gum.GueDeriving;
using System.Collections.ObjectModel;
namespace MonoGameAndGum;
// This is the top-level ViewModel for the example.
public class TopLevelViewModel : ViewModel
{
public ObservableCollection<WeaponViewModel> Weapons
{
get;
private set;
} = new ObservableCollection<WeaponViewModel>();
}
// This is the view model representing a single weapon.
// Each instance of WeaponViewModel results in an item
// added to the ItemsControl.
public class WeaponViewModel : ViewModel
{
public string WeaponName
{
get => Get<string>();
set => Set(value);
}
public string WeaponDetails
{
get => Get<string>();
set => Set(value);
}
}
// A special button which includes text and subtext
public class WeaponDisplay : ListBoxItem
{
TextRuntime subText;
public WeaponDisplay() : base()
{
Visual.Height = 10;
var text = ((ListBoxItemVisual)this.Visual).TextInstance;
text.Anchor(Gum.Wireframe.Anchor.TopLeft);
// add a 2nd Text instance under the main text:
subText = new TextRuntime();
subText.Y = 22;
subText.Color = new Color(220, 220, 200);
subText.WidthUnits = Gum.DataTypes.DimensionUnitType.RelativeToParent;
this.AddChild(subText);
}
public override void UpdateToObject(object o)
{
var asWeaponViewModel = o as WeaponViewModel;
if(asWeaponViewModel != null)
{
coreText.RawText = asWeaponViewModel.WeaponName;
subText.Text = asWeaponViewModel.WeaponDetails;
}
}
}
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
GumService GumUI => GumService.Default;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
GumUI.Initialize(this);
var viewModel = new TopLevelViewModel();
var comboBox = new ComboBox();
comboBox.AddToRoot();
comboBox.Anchor(Anchor.Center);
comboBox.Width = 300;
comboBox.BindingContext = viewModel;
comboBox.SetBinding(
nameof(comboBox.Items),
nameof(viewModel.Weapons));
comboBox.DisplayMemberPath = nameof(WeaponViewModel.WeaponName);
comboBox.FrameworkElementTemplate =
new Gum.Forms.FrameworkElementTemplate(typeof(WeaponDisplay));
viewModel.Weapons.Add(new WeaponViewModel
{
WeaponName = "Sword",
WeaponDetails = "A sharp blade used for cutting."
});
viewModel.Weapons.Add(new WeaponViewModel
{
WeaponName = "Bow",
WeaponDetails = "A ranged weapon that shoots arrows."
});
viewModel.Weapons.Add(new WeaponViewModel
{
WeaponName = "Staff",
WeaponDetails = "A magical staff used for casting spells."
});
viewModel.Weapons.Add(new WeaponViewModel
{
WeaponName = "Dagger",
WeaponDetails = "A small, sharp blade used for stabbing."
});
base.Initialize();
}
protected override void Update(GameTime gameTime)
{
GumUI.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GumUI.Draw();
base.Draw(gameTime);
}
}