Skip to content

Commit

Permalink
Merge pull request #402 from spacechase0/custom-widget-mml
Browse files Browse the repository at this point in the history
Allow custom widgets when loading MML
  • Loading branch information
rds1983 authored Sep 19, 2024
2 parents 005c032 + 086eaab commit 3ed960e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/Myra/Graphics2D/UI/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ internal SaveContext CreateSaveContext()
return CreateSaveContext(Stylesheet);
}

internal static LoadContext CreateLoadContext(AssetManager assetManager)
public static Dictionary<Assembly, string[]> ExtraWidgetAssembliesAndNamespaces = new Dictionary<Assembly, string[]>();

internal static LoadContext CreateLoadContext(IAssetManager assetManager)

Check failure on line 190 in src/Myra/Graphics2D/UI/Project.cs

View workflow job for this annotation

GitHub Actions / BuildAndPublish

The type or namespace name 'IAssetManager' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 190 in src/Myra/Graphics2D/UI/Project.cs

View workflow job for this annotation

GitHub Actions / BuildAndPublish

The type or namespace name 'IAssetManager' could not be found (are you missing a using directive or an assembly reference?)
{
Func<Type, string, object> resourceGetter = (t, name) =>
{
Expand All @@ -205,13 +207,11 @@ internal static LoadContext CreateLoadContext(AssetManager assetManager)
throw new Exception(string.Format("Type {0} isn't supported", t.Name));
};

Dictionary<Assembly, string[]> assemblies = new Dictionary<Assembly, string[]>(ExtraWidgetAssembliesAndNamespaces);
assemblies.Add(typeof(Widget).Assembly, new string[] { typeof(Widget).Namespace, typeof(PropertyGrid).Namespace });
return new LoadContext
{
Namespaces = new[]
{
typeof(Widget).Namespace,
typeof(PropertyGrid).Namespace,
},
Assemblies = assemblies,
LegacyClassNames = LegacyClassNames,
ObjectCreator = (t, el) => CreateItem(t, el),
ResourceGetter = resourceGetter
Expand Down
5 changes: 3 additions & 2 deletions src/Myra/Graphics2D/UI/Styles/Stylesheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Xml.Linq;
using Myra.MML;
using System.Collections;
using System.Reflection;
using FontStashSharp;
using Myra.Graphics2D.TextureAtlases;
using FontStashSharp.RichText;
Expand Down Expand Up @@ -410,9 +411,9 @@ public static Stylesheet LoadFromSource(string stylesheetXml,

var loadContext = new LoadContext
{
Namespaces = new[]
Assemblies = new Dictionary<Assembly, string[]>()
{
typeof(WidgetStyle).Namespace
{ typeof( WidgetStyle ).Assembly, new string[] { typeof( WidgetStyle ).Namespace } }
},
ResourceGetter = resourceGetter,
NodesToIgnore = new HashSet<string>(new[] { "Designer", "Colors", "Fonts" }),
Expand Down
19 changes: 13 additions & 6 deletions src/Myra/MML/LoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public void SetValue(object obj, object value)
public Dictionary<string, Color> Colors;
public HashSet<string> NodesToIgnore = null;
public Func<Type, XElement, object> ObjectCreator = (type, el) => Activator.CreateInstance(type);
public string[] Namespaces;
public Assembly Assembly = typeof(Widget).Assembly;
public Dictionary<Assembly, string[]> Assemblies;
public Func<Type, string, object> ResourceGetter = null;
public readonly List<Tuple<object, XElement>> ObjectsNodes = new List<Tuple<object, XElement>>();

Expand Down Expand Up @@ -317,14 +316,22 @@ where p.FindAttribute<ContentAttribute>()
}

Type itemType = null;
foreach(var ns in Namespaces)
foreach (var pair in Assemblies)
{
itemType = Assembly.GetType(ns + "." + widgetName);
if (itemType != null)
foreach (var ns in pair.Value)
{
break;
var widgetType = pair.Key.GetType(ns + "." + widgetName);
if (widgetType != null)
{
itemType = widgetType;
break;
}
}

if (itemType != null)
break;
}

if (itemType != null)
{
var item = ObjectCreator(itemType, child);
Expand Down

0 comments on commit 3ed960e

Please sign in to comment.