Skip to content

Commit 3626b8d

Browse files
committed
Fixes #497
1 parent bbeba33 commit 3626b8d

File tree

12 files changed

+106
-56
lines changed

12 files changed

+106
-56
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
3+
namespace Myra.Attributes
4+
{
5+
[AttributeUsage(AttributeTargets.Property)]
6+
public class RangeAttribute : Attribute
7+
{
8+
public float? Minimum { get; }
9+
public float? Maximum { get; }
10+
11+
private RangeAttribute(float? min, float? max)
12+
{
13+
if (min != null && max != null && min > max)
14+
{
15+
throw new ArgumentException("min > max");
16+
}
17+
18+
Minimum = min;
19+
Maximum = max;
20+
}
21+
22+
public RangeAttribute(float min): this(min, null)
23+
{
24+
}
25+
26+
public RangeAttribute(float min, float max) : this((float?)min, (float?)max)
27+
{
28+
}
29+
}
30+
}

src/Myra/Graphics2D/Thickness.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#if MONOGAME || FNA
77
using Microsoft.Xna.Framework;
8+
using Myra.Attributes;
89
#elif STRIDE
910
using Stride.Core.Mathematics;
1011
#else
@@ -17,22 +18,17 @@ public struct Thickness
1718
{
1819
public static readonly Thickness Zero = new Thickness();
1920

20-
public int Left
21-
{
22-
get; set;
23-
}
24-
public int Right
25-
{
26-
get; set;
27-
}
28-
public int Top
29-
{
30-
get; set;
31-
}
32-
public int Bottom
33-
{
34-
get; set;
35-
}
21+
[Range(0)]
22+
public int Left { get; set; }
23+
24+
[Range(0)]
25+
public int Right { get; set; }
26+
27+
[Range(0)]
28+
public int Top { get; set; }
29+
30+
[Range(0)]
31+
public int Bottom { get; set; }
3632

3733
[Browsable(false)]
3834
[XmlIgnore]

src/Myra/Graphics2D/UI/Containers/Grid.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.ComponentModel;
66
using System.Xml.Serialization;
77
using Myra.MML;
8+
using Myra.Attributes;
9+
810

911
#if MONOGAME || FNA
1012
using Microsoft.Xna.Framework;
@@ -28,13 +30,21 @@ public enum GridSelectionMode
2830
public class Grid : Container
2931
{
3032
public static readonly AttachedPropertyInfo<int> ColumnProperty =
31-
AttachedPropertiesRegistry.Create(typeof(Grid), "Column", 0, AttachedPropertyOption.AffectsArrange);
33+
AttachedPropertiesRegistry.Create(typeof(Grid), "Column", 0,
34+
AttachedPropertyOption.AffectsArrange,
35+
new Attribute[] { new RangeAttribute(0) });
3236
public static readonly AttachedPropertyInfo<int> RowProperty =
33-
AttachedPropertiesRegistry.Create(typeof(Grid), "Row", 0, AttachedPropertyOption.AffectsArrange);
37+
AttachedPropertiesRegistry.Create(typeof(Grid), "Row", 0,
38+
AttachedPropertyOption.AffectsArrange,
39+
new Attribute[] { new RangeAttribute(0) });
3440
public static readonly AttachedPropertyInfo<int> ColumnSpanProperty =
35-
AttachedPropertiesRegistry.Create(typeof(Grid), "ColumnSpan", 1, AttachedPropertyOption.AffectsArrange);
41+
AttachedPropertiesRegistry.Create(typeof(Grid), "ColumnSpan", 1,
42+
AttachedPropertyOption.AffectsArrange,
43+
new Attribute[] { new RangeAttribute(1) });
3644
public static readonly AttachedPropertyInfo<int> RowSpanProperty =
37-
AttachedPropertiesRegistry.Create(typeof(Grid), "RowSpan", 1, AttachedPropertyOption.AffectsArrange);
45+
AttachedPropertiesRegistry.Create(typeof(Grid), "RowSpan", 1,
46+
AttachedPropertyOption.AffectsArrange,
47+
new Attribute[] { new RangeAttribute(1) });
3848

3949
private readonly GridLayout _layout = new GridLayout();
4050

src/Myra/Graphics2D/UI/Containers/StackPanel.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ namespace Myra.Graphics2D.UI
1919
public abstract class StackPanel : Container
2020
{
2121
public static readonly AttachedPropertyInfo<ProportionType> ProportionTypeProperty =
22-
AttachedPropertiesRegistry.Create(typeof(StackPanel), "ProportionType", ProportionType.Auto, AttachedPropertyOption.AffectsMeasure);
22+
AttachedPropertiesRegistry.Create(typeof(StackPanel), "ProportionType",
23+
ProportionType.Auto, AttachedPropertyOption.AffectsMeasure);
2324
public static readonly AttachedPropertyInfo<float> ProportionValueProperty =
24-
AttachedPropertiesRegistry.Create(typeof(StackPanel), "ProportionValue", 1.0f, AttachedPropertyOption.AffectsMeasure);
25+
AttachedPropertiesRegistry.Create(typeof(StackPanel), "ProportionValue",
26+
1.0f, AttachedPropertyOption.AffectsMeasure,
27+
new Attribute[] { new RangeAttribute(0.0f) });
2528

2629
private readonly StackPanelLayout _layout;
2730
private readonly ObservableCollection<Proportion> _proportions = new ObservableCollection<Proportion>();

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
using Myra.MML;
22
using System;
3-
using System.Reflection;
3+
using System.Linq;
44

55
namespace Myra.Graphics2D.UI.Properties
66
{
77
internal class AttachedPropertyRecord : Record
88
{
99
private readonly BaseAttachedPropertyInfo _property;
1010

11-
public override MemberInfo MemberInfo => null;
12-
1311
public AttachedPropertyRecord(BaseAttachedPropertyInfo property)
1412
{
1513
_property = property ?? throw new ArgumentNullException(nameof(property));
@@ -22,5 +20,15 @@ public AttachedPropertyRecord(BaseAttachedPropertyInfo property)
2220
public override object GetValue(object obj) => _property.GetValueObject((Widget)obj);
2321

2422
public override void SetValue(object obj, object value) => _property.SetValueObject((Widget)obj, value);
23+
24+
public override T FindAttribute<T>()
25+
{
26+
if (_property.Attributes == null)
27+
{
28+
return null;
29+
}
30+
31+
return (T)(from a in _property.Attributes where a.GetType() == typeof(T) select a).FirstOrDefault();
32+
}
2533
}
2634
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Myra.Graphics2D.UI.Properties
55
{
6-
internal class FieldRecord : Record
6+
internal class FieldRecord : ReflectionRecord
77
{
88
private readonly FieldInfo _fieldInfo;
99

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,13 @@ private SpinButton CreateNumericEditor(Record record, bool hasSetter)
717717
Value = value != null ? (float)Convert.ChangeType(value, typeof(float)) : default(float?)
718718
};
719719

720+
var rangeAttribute = record.FindAttribute<RangeAttribute>();
721+
if (rangeAttribute != null)
722+
{
723+
spinButton.Minimum = rangeAttribute.Minimum;
724+
spinButton.Maximum = rangeAttribute.Maximum;
725+
}
726+
720727
if (hasSetter)
721728
{
722729
spinButton.ValueChanged += (sender, args) =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Myra.Graphics2D.UI.Properties
55
{
6-
internal class PropertyRecord : Record
6+
internal class PropertyRecord : ReflectionRecord
77
{
88
private readonly PropertyInfo _propertyInfo;
99

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Myra.Utility;
2-
using System;
3-
using System.Reflection;
1+
using System;
42

53
namespace Myra.Graphics2D.UI.Properties
64
{
@@ -11,29 +9,10 @@ public abstract class Record
119
public abstract string Name { get; }
1210
public abstract Type Type { get; }
1311
public string Category { get; set; }
14-
public abstract MemberInfo MemberInfo { get; }
1512

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

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-
}
16+
public abstract T FindAttribute<T>() where T : Attribute;
3817
}
3918
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Myra.Utility;
2+
using System.Reflection;
3+
4+
namespace Myra.Graphics2D.UI.Properties
5+
{
6+
internal abstract class ReflectionRecord : Record
7+
{
8+
public abstract MemberInfo MemberInfo { get; }
9+
10+
public override T FindAttribute<T>()
11+
{
12+
return MemberInfo.FindAttribute<T>();
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)