rds1983 / Myra

UI Library for MonoGame, FNA and Stride
MIT License
704 stars 94 forks source link

Allow custom widgets when loading MML #402

Open spacechase0 opened 9 months ago

spacechase0 commented 9 months ago

Example:

namespace MyGameProject;

public class ItemSlot : ButtonBase<Panel>
{
    // ...
}
Project.ExtraWidgetAssembliesAndNamespaces.Add( typeof( ItemSlot ).Assembly, new string[] { typeof( ItemSlot ).Namespace } );
Grid g = Project.LoadFromXml(File.ReadAllText(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "assets/inventory.xmmp")), MyraUtil.AssetManager).Root as Grid;
var slots = g.GetChildren().Where(w => w is ItemSlot).Cast<ItemSlot>().ToArray();
slots[0].Stack = new Items.ItemStack("dagger") { Quantity = 1 };
<?xml version="1.0" encoding="UTF-8" ?>
<Project>
  <Project.ExportOptions />
  <Grid RowSpacing="4" ColumnSpacing="4">
    <Grid.DefaultColumnProportion Type="Pixels" Value="40"/>      
    <Grid.DefaultRowProportion Type="Pixels" Value="40"/>
    <ItemSlot GridRow="0" GridColumn="0" />
    <!-- a lot more of ItemSlot with different GridRow / GridColumn here -->
  </Grid>
</Project>