Skip to content

Commit

Permalink
Version 1.5.9
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Dec 26, 2024
1 parent b3be414 commit 5c7bc37
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageProjectUrl>https://github.com/rds1983/Myra</PackageProjectUrl>
<RootNamespace>Myra</RootNamespace>
<Description>UI Library for MonoGame, FNA and Stride</Description>
<VersionPrefix>1.5.8</VersionPrefix>
<VersionPrefix>1.5.9</VersionPrefix>
<XNAssetsVersion>0.7.5</XNAssetsVersion>
<FontStashSharpVersion>1.3.9</FontStashSharpVersion>
<LangVersion>8.0</LangVersion>
Expand Down
6 changes: 3 additions & 3 deletions src/Myra/Graphics2D/UI/Properties/AttachedPropertyRecord.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using Myra.MML;
using System;
using System.Reflection;

namespace Myra.Graphics2D.UI.Properties
{
internal class AttachedPropertyRecord : Record
{
private readonly BaseAttachedPropertyInfo _property;

public override MemberInfo MemberInfo => null;

public AttachedPropertyRecord(BaseAttachedPropertyInfo property)
{
_property = property ?? throw new ArgumentNullException(nameof(property));
Expand All @@ -16,9 +19,6 @@ public AttachedPropertyRecord(BaseAttachedPropertyInfo property)
public override string Name => _property.Name;

public override Type Type => _property.PropertyType;

public override T FindAttribute<T>() => null;

public override object GetValue(object obj) => _property.GetValueObject((Widget)obj);

public override void SetValue(object obj, object value) => _property.SetValueObject((Widget)obj, value);
Expand Down
34 changes: 34 additions & 0 deletions src/Myra/Graphics2D/UI/Properties/CustomValues.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Myra.Graphics2D.UI.Properties
{
public class CustomValue
{
public string Name { get; }
public object Value { get; }

public CustomValue(string name, object value)
{
Name = name;
Value = value;
}
}

public class CustomValues
{
public CustomValue[] Values { get; }
public int? SelectedIndex { get; set; }

public CustomValues(IEnumerable<CustomValue> values)
{
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}

Values = values.ToArray();
}
}
}
10 changes: 3 additions & 7 deletions src/Myra/Graphics2D/UI/Properties/FieldRecord.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Myra.Utility;
using System;
using System;
using System.Reflection;

namespace Myra.Graphics2D.UI.Properties
Expand All @@ -18,6 +17,8 @@ public override Type Type
get { return _fieldInfo.FieldType; }
}

public override MemberInfo MemberInfo => _fieldInfo;

public FieldRecord(FieldInfo fieldInfo)
{
_fieldInfo = fieldInfo;
Expand All @@ -32,10 +33,5 @@ public override void SetValue(object obj, object value)
{
_fieldInfo.SetValue(obj, value);
}

public override T FindAttribute<T>()
{
return _fieldInfo.FindAttribute<T>();
}
}
}
127 changes: 70 additions & 57 deletions src/Myra/Graphics2D/UI/Properties/PropertyGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public string Filter

[Browsable(false)]
[XmlIgnore]
public Func<Record, object[]> CustomValuesProvider;
public Func<object, Record, CustomValues> CustomValuesProvider;

[Browsable(false)]
[XmlIgnore]
Expand Down Expand Up @@ -416,31 +416,31 @@ private void SetValue(Record record, object obj, object value)
record.SetValue(obj, value);
}

private ComboView CreateCustomValuesEditor(Record record, object[] customValues, bool hasSetter)
private ComboView CreateCustomValuesEditor(Record record, CustomValues customValues, bool hasSetter)
{
var propertyType = record.Type;
var value = record.GetValue(_object);

var cv = new ComboView();
foreach (var v in customValues)
foreach (var v in customValues.Values)
{
var label = new Label
{
Text = v.ToString(),
Tag = v
Text = v.Name,
Tag = v.Value
};

cv.Widgets.Add(label);
}

cv.SelectedIndex = Array.IndexOf(customValues, value);
cv.SelectedIndex = customValues.SelectedIndex;
if (hasSetter)
{
cv.SelectedIndexChanged += (sender, args) =>
{
var item = cv.SelectedIndex != null ? customValues[cv.SelectedIndex.Value] : null;
var item = cv.SelectedIndex != null ? customValues.Values[cv.SelectedIndex.Value].Value : null;
SetValue(record, _object, item);
FireChanged(propertyType.Name);
FireChanged(record.Name);
};
}
else
Expand Down Expand Up @@ -1147,19 +1147,25 @@ private void FillSubGrid(ref int y, IReadOnlyList<Record> records)
var propertyType = record.Type;

Proportion rowProportion;
object[] customValues = null;
CustomValues customValues = null;

var needsSubGrid = false;
if ((valueWidget = CustomWidgetProvider?.Invoke(record, _object)) != null)
{

}
else if (CustomValuesProvider != null && (customValues = CustomValuesProvider(record)) != null)
else if (CustomValuesProvider != null && (customValues = CustomValuesProvider(_object, record)) != null)
{
if (customValues.Length == 0)
if (customValues.Values.Length == 0)
{
continue;
}

valueWidget = CreateCustomValuesEditor(record, customValues, hasSetter);
if (value != null && !value.GetType().IsPrimitive && value.GetType() != typeof(string))
{
needsSubGrid = true;
}
}
else if (propertyType == typeof(bool))
{
Expand All @@ -1176,7 +1182,6 @@ private void FillSubGrid(ref int y, IReadOnlyList<Record> records)
else if (propertyType.IsNumericType() ||
(propertyType.IsNullablePrimitive() && propertyType.GetNullableType().IsNumericType()))
{

valueWidget = CreateNumericEditor(record, hasSetter);
}
else if (propertyType == typeof(string) && record.FindAttribute<FilePathAttribute>() != null)
Expand Down Expand Up @@ -1228,6 +1233,59 @@ private void FillSubGrid(ref int y, IReadOnlyList<Record> records)
#endif
#endif
else
{
if (value == null)
{
var tb = new Label();
tb.ApplyLabelStyle(PropertyGridStyle.LabelStyle);
tb.Text = "null";

valueWidget = tb;
} else
{
needsSubGrid = true;
}
}

if (valueWidget != null)
{
var name = record.Name;
var dn = record.FindAttribute<DisplayNameAttribute>();

if (dn != null)
{
name = dn.DisplayName;
}

if (!PassesFilter(name))
{
continue;
}

var nameLabel = new Label
{
Text = name,
VerticalAlignment = VerticalAlignment.Center,
};

Grid.SetColumn(nameLabel, 0);
Grid.SetRow(nameLabel, oldY);

Children.Add(nameLabel);

Grid.SetColumn(valueWidget, 1);
Grid.SetRow(valueWidget, oldY);
valueWidget.HorizontalAlignment = HorizontalAlignment.Stretch;
valueWidget.VerticalAlignment = VerticalAlignment.Top;

Children.Add(valueWidget);

rowProportion = new Proportion(ProportionType.Auto);
_layout.RowsProportions.Add(rowProportion);
++y;
}

if (needsSubGrid)
{
// Subgrid
if (value != null)
Expand All @@ -1247,52 +1305,7 @@ private void FillSubGrid(ref int y, IReadOnlyList<Record> records)

continue;
}

var tb = new Label();
tb.ApplyLabelStyle(PropertyGridStyle.LabelStyle);
tb.Text = "null";

valueWidget = tb;
}

if (valueWidget == null)
{
continue;
}

var name = record.Name;
var dn = record.FindAttribute<DisplayNameAttribute>();

if (dn != null)
{
name = dn.DisplayName;
}

if (!PassesFilter(name))
{
continue;
}

var nameLabel = new Label
{
Text = name,
VerticalAlignment = VerticalAlignment.Center,
};
Grid.SetColumn(nameLabel, 0);
Grid.SetRow(nameLabel, oldY);

Children.Add(nameLabel);

Grid.SetColumn(valueWidget, 1);
Grid.SetRow(valueWidget, oldY);
valueWidget.HorizontalAlignment = HorizontalAlignment.Stretch;
valueWidget.VerticalAlignment = VerticalAlignment.Top;

Children.Add(valueWidget);

rowProportion = new Proportion(ProportionType.Auto);
_layout.RowsProportions.Add(rowProportion);
++y;
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/Myra/Graphics2D/UI/Properties/PropertyRecord.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Myra.Utility;
using System;
using System;
using System.Reflection;

namespace Myra.Graphics2D.UI.Properties
Expand All @@ -18,6 +17,8 @@ public override Type Type
get { return _propertyInfo.PropertyType; }
}

public override MemberInfo MemberInfo => _propertyInfo;

public PropertyRecord(PropertyInfo propertyInfo)
{
_propertyInfo = propertyInfo;
Expand All @@ -32,10 +33,5 @@ public override void SetValue(object obj, object value)
{
_propertyInfo.SetValue(obj, value);
}

public override T FindAttribute<T>()
{
return _propertyInfo.FindAttribute<T>();
}
}
}
25 changes: 23 additions & 2 deletions src/Myra/Graphics2D/UI/Properties/Record.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Myra.Utility;
using System;
using System.Reflection;

namespace Myra.Graphics2D.UI.Properties
{
Expand All @@ -9,10 +11,29 @@ public abstract class Record
public abstract string Name { get; }
public abstract Type Type { get; }
public string Category { get; set; }
public abstract MemberInfo MemberInfo { get; }

public abstract object GetValue(object obj);
public abstract void SetValue(object obj, object value);

public abstract T FindAttribute<T>() where T : Attribute;
public T FindAttribute<T>() where T : Attribute
{
if (MemberInfo == null)
{
return null;
}

return MemberInfo.FindAttribute<T>();
}

public T[] FindAttributes<T>() where T: Attribute
{
if (MemberInfo == null)
{
return null;
}

return MemberInfo.FindAttributes<T>();
}
}
}
5 changes: 5 additions & 0 deletions src/Myra/Utility/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public static T FindAttribute<T>(this MemberInfo property) where T : Attribute
return result;
}

public static T[] FindAttributes<T>(this MemberInfo property) where T : Attribute
{
return (from T a in property.GetCustomAttributes<T>(true) select a).ToArray();
}

public static T FindAttribute<T>(this Type type) where T : Attribute
{
var result = (from T a in type.GetCustomAttributes<T>(true) select a).FirstOrDefault();
Expand Down
4 changes: 3 additions & 1 deletion src/MyraPad/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using AssetManagementBase;
using System;

namespace MyraPad
{
Expand All @@ -12,6 +13,7 @@ static void Main(string[] args)
{
try
{
AMBConfiguration.Logger = Console.WriteLine;
using (var studio = new Studio(args))
{
studio.Run();
Expand Down
Loading

0 comments on commit 5c7bc37

Please sign in to comment.