Skip to content

Commit 5c7bc37

Browse files
committed
Version 1.5.9
1 parent b3be414 commit 5c7bc37

File tree

10 files changed

+165
-80
lines changed

10 files changed

+165
-80
lines changed

Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<PackageProjectUrl>https://github.com/rds1983/Myra</PackageProjectUrl>
77
<RootNamespace>Myra</RootNamespace>
88
<Description>UI Library for MonoGame, FNA and Stride</Description>
9-
<VersionPrefix>1.5.8</VersionPrefix>
9+
<VersionPrefix>1.5.9</VersionPrefix>
1010
<XNAssetsVersion>0.7.5</XNAssetsVersion>
1111
<FontStashSharpVersion>1.3.9</FontStashSharpVersion>
1212
<LangVersion>8.0</LangVersion>

src/Myra/Graphics2D/UI/Properties/AttachedPropertyRecord.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
using Myra.MML;
22
using System;
3+
using System.Reflection;
34

45
namespace Myra.Graphics2D.UI.Properties
56
{
67
internal class AttachedPropertyRecord : Record
78
{
89
private readonly BaseAttachedPropertyInfo _property;
910

11+
public override MemberInfo MemberInfo => null;
12+
1013
public AttachedPropertyRecord(BaseAttachedPropertyInfo property)
1114
{
1215
_property = property ?? throw new ArgumentNullException(nameof(property));
@@ -16,9 +19,6 @@ public AttachedPropertyRecord(BaseAttachedPropertyInfo property)
1619
public override string Name => _property.Name;
1720

1821
public override Type Type => _property.PropertyType;
19-
20-
public override T FindAttribute<T>() => null;
21-
2222
public override object GetValue(object obj) => _property.GetValueObject((Widget)obj);
2323

2424
public override void SetValue(object obj, object value) => _property.SetValueObject((Widget)obj, value);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Myra.Graphics2D.UI.Properties
6+
{
7+
public class CustomValue
8+
{
9+
public string Name { get; }
10+
public object Value { get; }
11+
12+
public CustomValue(string name, object value)
13+
{
14+
Name = name;
15+
Value = value;
16+
}
17+
}
18+
19+
public class CustomValues
20+
{
21+
public CustomValue[] Values { get; }
22+
public int? SelectedIndex { get; set; }
23+
24+
public CustomValues(IEnumerable<CustomValue> values)
25+
{
26+
if (values == null)
27+
{
28+
throw new ArgumentNullException(nameof(values));
29+
}
30+
31+
Values = values.ToArray();
32+
}
33+
}
34+
}

src/Myra/Graphics2D/UI/Properties/FieldRecord.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Myra.Utility;
2-
using System;
1+
using System;
32
using System.Reflection;
43

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

20+
public override MemberInfo MemberInfo => _fieldInfo;
21+
2122
public FieldRecord(FieldInfo fieldInfo)
2223
{
2324
_fieldInfo = fieldInfo;
@@ -32,10 +33,5 @@ public override void SetValue(object obj, object value)
3233
{
3334
_fieldInfo.SetValue(obj, value);
3435
}
35-
36-
public override T FindAttribute<T>()
37-
{
38-
return _fieldInfo.FindAttribute<T>();
39-
}
4036
}
4137
}

src/Myra/Graphics2D/UI/Properties/PropertyGrid.cs

+70-57
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public string Filter
331331

332332
[Browsable(false)]
333333
[XmlIgnore]
334-
public Func<Record, object[]> CustomValuesProvider;
334+
public Func<object, Record, CustomValues> CustomValuesProvider;
335335

336336
[Browsable(false)]
337337
[XmlIgnore]
@@ -416,31 +416,31 @@ private void SetValue(Record record, object obj, object value)
416416
record.SetValue(obj, value);
417417
}
418418

419-
private ComboView CreateCustomValuesEditor(Record record, object[] customValues, bool hasSetter)
419+
private ComboView CreateCustomValuesEditor(Record record, CustomValues customValues, bool hasSetter)
420420
{
421421
var propertyType = record.Type;
422422
var value = record.GetValue(_object);
423423

424424
var cv = new ComboView();
425-
foreach (var v in customValues)
425+
foreach (var v in customValues.Values)
426426
{
427427
var label = new Label
428428
{
429-
Text = v.ToString(),
430-
Tag = v
429+
Text = v.Name,
430+
Tag = v.Value
431431
};
432432

433433
cv.Widgets.Add(label);
434434
}
435435

436-
cv.SelectedIndex = Array.IndexOf(customValues, value);
436+
cv.SelectedIndex = customValues.SelectedIndex;
437437
if (hasSetter)
438438
{
439439
cv.SelectedIndexChanged += (sender, args) =>
440440
{
441-
var item = cv.SelectedIndex != null ? customValues[cv.SelectedIndex.Value] : null;
441+
var item = cv.SelectedIndex != null ? customValues.Values[cv.SelectedIndex.Value].Value : null;
442442
SetValue(record, _object, item);
443-
FireChanged(propertyType.Name);
443+
FireChanged(record.Name);
444444
};
445445
}
446446
else
@@ -1147,19 +1147,25 @@ private void FillSubGrid(ref int y, IReadOnlyList<Record> records)
11471147
var propertyType = record.Type;
11481148

11491149
Proportion rowProportion;
1150-
object[] customValues = null;
1150+
CustomValues customValues = null;
1151+
1152+
var needsSubGrid = false;
11511153
if ((valueWidget = CustomWidgetProvider?.Invoke(record, _object)) != null)
11521154
{
11531155

11541156
}
1155-
else if (CustomValuesProvider != null && (customValues = CustomValuesProvider(record)) != null)
1157+
else if (CustomValuesProvider != null && (customValues = CustomValuesProvider(_object, record)) != null)
11561158
{
1157-
if (customValues.Length == 0)
1159+
if (customValues.Values.Length == 0)
11581160
{
11591161
continue;
11601162
}
11611163

11621164
valueWidget = CreateCustomValuesEditor(record, customValues, hasSetter);
1165+
if (value != null && !value.GetType().IsPrimitive && value.GetType() != typeof(string))
1166+
{
1167+
needsSubGrid = true;
1168+
}
11631169
}
11641170
else if (propertyType == typeof(bool))
11651171
{
@@ -1176,7 +1182,6 @@ private void FillSubGrid(ref int y, IReadOnlyList<Record> records)
11761182
else if (propertyType.IsNumericType() ||
11771183
(propertyType.IsNullablePrimitive() && propertyType.GetNullableType().IsNumericType()))
11781184
{
1179-
11801185
valueWidget = CreateNumericEditor(record, hasSetter);
11811186
}
11821187
else if (propertyType == typeof(string) && record.FindAttribute<FilePathAttribute>() != null)
@@ -1228,6 +1233,59 @@ private void FillSubGrid(ref int y, IReadOnlyList<Record> records)
12281233
#endif
12291234
#endif
12301235
else
1236+
{
1237+
if (value == null)
1238+
{
1239+
var tb = new Label();
1240+
tb.ApplyLabelStyle(PropertyGridStyle.LabelStyle);
1241+
tb.Text = "null";
1242+
1243+
valueWidget = tb;
1244+
} else
1245+
{
1246+
needsSubGrid = true;
1247+
}
1248+
}
1249+
1250+
if (valueWidget != null)
1251+
{
1252+
var name = record.Name;
1253+
var dn = record.FindAttribute<DisplayNameAttribute>();
1254+
1255+
if (dn != null)
1256+
{
1257+
name = dn.DisplayName;
1258+
}
1259+
1260+
if (!PassesFilter(name))
1261+
{
1262+
continue;
1263+
}
1264+
1265+
var nameLabel = new Label
1266+
{
1267+
Text = name,
1268+
VerticalAlignment = VerticalAlignment.Center,
1269+
};
1270+
1271+
Grid.SetColumn(nameLabel, 0);
1272+
Grid.SetRow(nameLabel, oldY);
1273+
1274+
Children.Add(nameLabel);
1275+
1276+
Grid.SetColumn(valueWidget, 1);
1277+
Grid.SetRow(valueWidget, oldY);
1278+
valueWidget.HorizontalAlignment = HorizontalAlignment.Stretch;
1279+
valueWidget.VerticalAlignment = VerticalAlignment.Top;
1280+
1281+
Children.Add(valueWidget);
1282+
1283+
rowProportion = new Proportion(ProportionType.Auto);
1284+
_layout.RowsProportions.Add(rowProportion);
1285+
++y;
1286+
}
1287+
1288+
if (needsSubGrid)
12311289
{
12321290
// Subgrid
12331291
if (value != null)
@@ -1247,52 +1305,7 @@ private void FillSubGrid(ref int y, IReadOnlyList<Record> records)
12471305

12481306
continue;
12491307
}
1250-
1251-
var tb = new Label();
1252-
tb.ApplyLabelStyle(PropertyGridStyle.LabelStyle);
1253-
tb.Text = "null";
1254-
1255-
valueWidget = tb;
12561308
}
1257-
1258-
if (valueWidget == null)
1259-
{
1260-
continue;
1261-
}
1262-
1263-
var name = record.Name;
1264-
var dn = record.FindAttribute<DisplayNameAttribute>();
1265-
1266-
if (dn != null)
1267-
{
1268-
name = dn.DisplayName;
1269-
}
1270-
1271-
if (!PassesFilter(name))
1272-
{
1273-
continue;
1274-
}
1275-
1276-
var nameLabel = new Label
1277-
{
1278-
Text = name,
1279-
VerticalAlignment = VerticalAlignment.Center,
1280-
};
1281-
Grid.SetColumn(nameLabel, 0);
1282-
Grid.SetRow(nameLabel, oldY);
1283-
1284-
Children.Add(nameLabel);
1285-
1286-
Grid.SetColumn(valueWidget, 1);
1287-
Grid.SetRow(valueWidget, oldY);
1288-
valueWidget.HorizontalAlignment = HorizontalAlignment.Stretch;
1289-
valueWidget.VerticalAlignment = VerticalAlignment.Top;
1290-
1291-
Children.Add(valueWidget);
1292-
1293-
rowProportion = new Proportion(ProportionType.Auto);
1294-
_layout.RowsProportions.Add(rowProportion);
1295-
++y;
12961309
}
12971310
}
12981311

src/Myra/Graphics2D/UI/Properties/PropertyRecord.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Myra.Utility;
2-
using System;
1+
using System;
32
using System.Reflection;
43

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

20+
public override MemberInfo MemberInfo => _propertyInfo;
21+
2122
public PropertyRecord(PropertyInfo propertyInfo)
2223
{
2324
_propertyInfo = propertyInfo;
@@ -32,10 +33,5 @@ public override void SetValue(object obj, object value)
3233
{
3334
_propertyInfo.SetValue(obj, value);
3435
}
35-
36-
public override T FindAttribute<T>()
37-
{
38-
return _propertyInfo.FindAttribute<T>();
39-
}
4036
}
4137
}

src/Myra/Graphics2D/UI/Properties/Record.cs

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
using Myra.Utility;
2+
using System;
3+
using System.Reflection;
24

35
namespace Myra.Graphics2D.UI.Properties
46
{
@@ -9,10 +11,29 @@ public abstract class Record
911
public abstract string Name { get; }
1012
public abstract Type Type { get; }
1113
public string Category { get; set; }
14+
public abstract MemberInfo MemberInfo { get; }
1215

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

16-
public abstract T FindAttribute<T>() where T : Attribute;
19+
public T FindAttribute<T>() where T : Attribute
20+
{
21+
if (MemberInfo == null)
22+
{
23+
return null;
24+
}
25+
26+
return MemberInfo.FindAttribute<T>();
27+
}
28+
29+
public T[] FindAttributes<T>() where T: Attribute
30+
{
31+
if (MemberInfo == null)
32+
{
33+
return null;
34+
}
35+
36+
return MemberInfo.FindAttributes<T>();
37+
}
1738
}
1839
}

src/Myra/Utility/Reflection.cs

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public static T FindAttribute<T>(this MemberInfo property) where T : Attribute
1313
return result;
1414
}
1515

16+
public static T[] FindAttributes<T>(this MemberInfo property) where T : Attribute
17+
{
18+
return (from T a in property.GetCustomAttributes<T>(true) select a).ToArray();
19+
}
20+
1621
public static T FindAttribute<T>(this Type type) where T : Attribute
1722
{
1823
var result = (from T a in type.GetCustomAttributes<T>(true) select a).FirstOrDefault();

src/MyraPad/Program.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using AssetManagementBase;
2+
using System;
23

34
namespace MyraPad
45
{
@@ -12,6 +13,7 @@ static void Main(string[] args)
1213
{
1314
try
1415
{
16+
AMBConfiguration.Logger = Console.WriteLine;
1517
using (var studio = new Studio(args))
1618
{
1719
studio.Run();

0 commit comments

Comments
 (0)