-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
PropertyEditor.cs
157 lines (136 loc) · 5.13 KB
/
PropertyEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Gtk;
namespace GeneticSharp.Runner.GtkApp
{
/// <summary>
/// A property editor to genetic algorithm operators.
/// </summary>
public partial class PropertyEditor : Gtk.Dialog
{
#region Fields
private Type m_objectType;
private Dictionary<PropertyInfo, Widget> m_widgetMap;
#endregion
#region Constructors
public PropertyEditor(Type objectType, object objectInstance)
{
this.Build();
buttonOk.Clicked += delegate
{
BindToObject();
Destroy();
};
buttonCancel.Clicked += delegate
{
Destroy();
};
m_objectType = objectType;
if (objectInstance == null || objectInstance.GetType() != objectType)
{
ObjectInstance = Activator.CreateInstance(objectType);
}
else
{
ObjectInstance = objectInstance;
}
m_widgetMap = new Dictionary<PropertyInfo, Widget>();
BindToWidget();
VBox.ResizeMode = ResizeMode.Immediate;
VBox.CheckResize();
ShowAll();
}
#endregion
#region Properties
public object ObjectInstance { get; private set; }
#endregion
#region Methods
public static bool HasEditableProperties(Type objectType)
{
return GetObjectProperties(objectType).Length > 0;
}
private static PropertyInfo[] GetObjectProperties(Type objectType)
{
return objectType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly).Where(p => p.CanWrite).ToArray();
}
private void BindToWidget()
{
foreach (var p in GetObjectProperties(m_objectType))
{
var hbox = new HBox();
var label = new Label(p.Name);
hbox.Add(label);
var value = p.GetValue(ObjectInstance, null);
Widget input = null;
if (p.PropertyType == typeof(int))
{
var spinButton = new SpinButton(0, int.MaxValue, 1);
spinButton.Value = Convert.ToDouble(value);
input = spinButton;
}
if (p.PropertyType == typeof(bool))
{
var toggle = new ToggleButton();
toggle.Active = Convert.ToBoolean(value);
input = toggle;
}
else if (p.PropertyType == typeof(float) || p.PropertyType == typeof(double))
{
var horizontalScale = new HScale(0, 1, 0.05);
if (ObjectInstance != null)
{
horizontalScale.Value = Convert.ToDouble(value);
}
input = horizontalScale;
}
else if (p.PropertyType == typeof(TimeSpan))
{
var secondsHBox = new HBox();
var seconds = new SpinButton(0, int.MaxValue, 1);
seconds.Value = ((TimeSpan)value).TotalSeconds;
secondsHBox.Add(seconds);
var secondsLabel = new Label("seconds");
secondsHBox.Add(secondsLabel);
input = secondsHBox;
}
if (input != null)
{
m_widgetMap.Add(p, input);
hbox.Add(input);
}
VBox.Add(hbox);
}
}
private void BindToObject()
{
foreach (var m in m_widgetMap)
{
if (m.Key.PropertyType == typeof(int))
{
m.Key.SetValue(ObjectInstance, ((SpinButton)m.Value).ValueAsInt, new object[0]);
}
else if (m.Key.PropertyType == typeof(bool))
{
m.Key.SetValue(ObjectInstance, ((ToggleButton)m.Value).Active, new object[0]);
}
else if (m.Key.PropertyType == typeof(float))
{
m.Key.SetValue(ObjectInstance, Convert.ToSingle(((HScale)m.Value).Value), new object[0]);
}
else if (m.Key.PropertyType == typeof(double))
{
m.Key.SetValue(ObjectInstance, ((HScale)m.Value).Value, new object[0]);
}
else if (m.Key.PropertyType == typeof(TimeSpan))
{
var hbox = (HBox)m.Value;
var seconds = ((SpinButton)hbox.Children[0]).Value;
m.Key.SetValue(ObjectInstance, TimeSpan.FromSeconds(seconds), new object[0]);
}
}
}
#endregion
}
}