Skip to content

Commit d0360e7

Browse files
committed
better inspector, ButtonBase, button styles & image index with list, completed Padding struct, fixed TreeView.PrevNode, more 'public->internal' stuff
1 parent 078d3ad commit d0360e7

28 files changed

+674
-365
lines changed

Core/Design/ControlDesigner.cs

Lines changed: 215 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
using System.Linq;
66
using System.Reflection;
77
using System.Text;
8-
using UnityEngine.UI;
98

109
namespace System.Windows.Forms.Design
1110
{
1211
public class ControlDesigner : IControlDesigner
1312
{
14-
private List<controlProperty> props;
13+
private objectEditor editor;
1514
private bool toggleEditor = true;
1615

1716
public Control Control { get; set; }
@@ -20,122 +19,248 @@ public ControlDesigner(Control c)
2019
{
2120
Control = c;
2221

23-
if (Control != null)
24-
Init();
22+
editor = new objectEditor(c, Control.GetType().Name);
23+
editor.toggleEditor = true;
2524
}
2625

27-
private Control DrawProperty(controlProperty p)
26+
public virtual object Draw(int width, int height)
2827
{
28+
if (Control == null) return null;
29+
2930
Control controlToSet = null;
30-
var val = p.info.GetValue(Control, null);
31-
Type type = null;
32-
if (val != null)
33-
type = val.GetType();
3431

35-
var pSetMethod = p.info.GetSetMethod(true);
36-
if (pSetMethod == null || pSetMethod.IsPrivate)
37-
{
38-
Editor.Label(p.info.Name, val);
39-
return null;
40-
}
32+
Editor.BeginGroup(width - 24, "");
4133

42-
if (val is bool)
43-
{
44-
var bVal = (bool)val;
45-
var ebVal = Editor.BooleanField(p.info.Name, bVal);
46-
if (ebVal.Changed)
47-
p.info.SetValue(Control, ebVal.Value, null);
48-
}
49-
else if (val is Control)
50-
{
51-
var cVal = val as Control;
52-
if (Editor.Button(p.info.Name, cVal.GetType().Name))
53-
controlToSet = cVal;
54-
}
55-
else if (val is Color)
56-
{
57-
var colorVal = (Color)val;
58-
Editor.ColorField(p.info.Name, colorVal, c => p.info.SetValue(Control, c, null));
59-
}
60-
else if (val is string)
34+
controlToSet = editor.Draw();
35+
36+
Editor.EndGroup();
37+
38+
return controlToSet;
39+
}
40+
41+
private class controlProperty
42+
{
43+
public objectEditor editor;
44+
public bool expanded;
45+
public PropertyInfo info;
46+
}
47+
private class objectEditor
48+
{
49+
private readonly List<FieldInfo> fields;
50+
private readonly List<MethodInfo> methods;
51+
private readonly object obj;
52+
private readonly List<controlProperty> props;
53+
private readonly string name;
54+
55+
public bool toggleEditor;
56+
57+
public objectEditor(object o, string objName)
6158
{
62-
var stringtVal = (string)val;
63-
var esVal = Editor.TextField(p.info.Name, stringtVal);
64-
if (esVal.Changed)
65-
p.info.SetValue(Control, esVal.Value, null);
59+
obj = o;
60+
name = objName;
61+
62+
var objType = obj.GetType();
63+
var pList = objType.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();
64+
pList.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.Ordinal));
65+
66+
props = new List<controlProperty>();
67+
for (int i = 0; i < pList.Count; i++)
68+
{
69+
var p = pList[i];
70+
var cp = new controlProperty()
71+
{
72+
info = p,
73+
};
74+
75+
props.Add(cp);
76+
}
77+
78+
fields = objType.GetFields(BindingFlags.Public | BindingFlags.Instance).ToList();
79+
fields.Sort((x, y) => x.Name.CompareTo(y.Name));
80+
81+
methods = objType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public).ToList();
82+
methods.Sort((x, y) => String.Compare(x.Name, y.Name, StringComparison.Ordinal));
83+
for (int i = 0; i < methods.Count; i++)
84+
{
85+
var m = methods[i];
86+
if (m.GetParameters().Length == 0 && m.ReturnType == typeof(void)) continue;
87+
88+
methods.RemoveAt(i);
89+
i--;
90+
}
6691
}
67-
else if ((type != null && type.IsArray) || val is IEnumerable)
92+
93+
public Control Draw()
6894
{
69-
p.expanded = Editor.Foldout(p.info.Name, p.expanded);
70-
if (p.expanded)
95+
Control control = null;
96+
if (props.Count == 0 && methods.Count == 0)
7197
{
72-
var vEnum = val as IEnumerable;
73-
foreach (var e in vEnum)
98+
Editor.Label(name);
99+
return null;
100+
}
101+
102+
Editor.BeginVertical("Box");
103+
toggleEditor = Editor.Foldout(name, toggleEditor);
104+
if (toggleEditor)
105+
{
106+
// Fields.
107+
if (fields.Count > 0)
74108
{
75-
var ec = e as Control;
76-
if (ec != null)
109+
Editor.BeginVertical("Box");
110+
for (int i = 0; i < fields.Count; i++)
77111
{
78-
if (Editor.Button(ec.ToString()))
79-
controlToSet = ec;
112+
var tc = Draw(fields[i]);
113+
if (tc != null)
114+
control = tc;
80115
}
81-
else
82-
Editor.Label(e);
116+
Editor.EndVertical();
117+
118+
Editor.NewLine(1);
83119
}
84-
}
85-
}
86-
else
87-
Editor.Label(p.info.Name, val);
88120

89-
return controlToSet;
90-
}
91-
private void Init()
92-
{
93-
var pList = Control.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();
94-
pList.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.Ordinal));
121+
// Properties.
122+
for (int i = 0; i < props.Count; i++)
123+
{
124+
var tc = Draw(props[i]);
125+
if (tc != null)
126+
control = tc;
127+
}
95128

96-
props = new List<controlProperty>();
97-
for (int i = 0; i < pList.Count; i++)
98-
{
99-
var p = pList[i];
100-
var cp = new controlProperty()
101-
{
102-
info = p,
103-
};
129+
// Methods.
130+
if (methods.Count > 0)
131+
{
132+
Editor.NewLine(1);
104133

105-
props.Add(cp);
134+
for (int i = 0; i < methods.Count; i++)
135+
{
136+
var tc = Draw(methods[i]);
137+
if (tc != null)
138+
control = tc;
139+
}
140+
}
141+
}
142+
Editor.EndVertical();
143+
return control;
106144
}
107-
}
145+
public Control Draw(controlProperty p)
146+
{
147+
Control controlToSet = null;
148+
var val = p.info.GetValue(obj, null);
149+
Type type = null;
150+
if (val != null)
151+
type = val.GetType();
108152

109-
public virtual object Draw(int width, int height)
110-
{
111-
if (Control == null) return null;
112-
if (props == null) return null;
153+
// Array & List.
154+
if (val is string == false)
155+
if ((type != null && type.IsArray) || val is IEnumerable)
156+
{
157+
Editor.BeginVertical("Box");
158+
p.expanded = Editor.Foldout(p.info.Name, p.expanded);
159+
if (p.expanded)
160+
{
161+
var vEnum = val as IEnumerable;
162+
var arrayIndex = 0;
163+
foreach (var e in vEnum)
164+
{
165+
var ec = e as Control;
166+
if (ec != null)
167+
{
168+
if (Editor.Button(ec.ToString()))
169+
controlToSet = ec;
170+
}
171+
else
172+
{
173+
if (p.editor == null)
174+
p.editor = new objectEditor(e, arrayIndex.ToString());
113175

114-
Control controlToSet = null;
176+
p.editor.Draw();
177+
}
178+
arrayIndex++;
179+
}
180+
}
181+
Editor.EndVertical();
182+
return controlToSet;
183+
}
115184

116-
Editor.BeginGroup(width - 24);
185+
// If there is no Set() method then skip.
186+
var pSetMethod = p.info.GetSetMethod(true);
187+
if (pSetMethod == null || pSetMethod.IsPrivate)
188+
{
189+
Editor.Label(p.info.Name, val);
190+
return null;
191+
}
117192

118-
toggleEditor = Editor.Foldout("Control (" + Control.GetType().Name + ")", toggleEditor);
119-
if (toggleEditor)
120-
{
121-
for (int i = 0; i < props.Count; i++)
193+
// Other editors.
194+
if (val is bool)
195+
{
196+
var bVal = (bool)val;
197+
var ebVal = Editor.BooleanField(p.info.Name, bVal);
198+
if (ebVal.Changed)
199+
p.info.SetValue(obj, ebVal.Value, null);
200+
}
201+
else if (val is Control)
202+
{
203+
var cVal = val as Control;
204+
if (Editor.Button(p.info.Name, cVal.GetType().Name))
205+
controlToSet = cVal;
206+
}
207+
else if (val is Color)
208+
{
209+
var colorVal = (Color)val;
210+
Editor.ColorField(p.info.Name, colorVal, c => p.info.SetValue(obj, c, null));
211+
}
212+
else if (val is string)
122213
{
123-
var p = props[i];
124-
var c = DrawProperty(p);
125-
if (c != null)
126-
controlToSet = c;
214+
var stringtVal = (string)val;
215+
var esVal = Editor.TextField(p.info.Name, stringtVal);
216+
if (esVal.Changed)
217+
p.info.SetValue(obj, esVal.Value, null);
127218
}
219+
else if (val is int)
220+
{
221+
var eiVal = Editor.IntField(p.info.Name, (int) val);
222+
if (eiVal.Changed)
223+
p.info.SetValue(obj, eiVal.Value[0], null);
224+
}
225+
else if (val is byte || val is sbyte || val is short || val is ushort || val is uint || val is long || val is ulong || val is float || val is double)
226+
{
227+
// TODO: editors for common types (like for int ^up there).
228+
Editor.Label(p.info.Name, val);
229+
}
230+
else if (val is Enum)
231+
{
232+
var eeVal = Editor.EnumField(p.info.Name, (Enum)val);
233+
if (eeVal.Changed)
234+
p.info.SetValue(obj, eeVal.Value, null);
235+
}
236+
else if (val != null)
237+
{
238+
if (p.editor == null)
239+
p.editor = new objectEditor(val, p.info.Name);
240+
241+
p.editor.Draw();
242+
}
243+
244+
return controlToSet;
128245
}
246+
public Control Draw(FieldInfo f)
247+
{
248+
// TODO: editors for fields.
129249

130-
Editor.EndGroup();
250+
var val = f.GetValue(obj);
251+
Editor.Label(f.Name, val);
131252

132-
return controlToSet;
133-
}
253+
return null;
254+
}
255+
public Control Draw(MethodInfo m)
256+
{
257+
if (Editor.Button(m.Name))
258+
{
259+
m.Invoke(obj, null);
260+
}
134261

135-
private class controlProperty
136-
{
137-
public bool expanded;
138-
public PropertyInfo info;
262+
return null;
263+
}
139264
}
140265
}
141266
}

Core/Design/Editor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public class Editor
1717

1818
public static bool WinFormsCompatible { get; set; }
1919

20-
public static void BeginGroup(float width)
20+
public static void BeginGroup(float width, string style = "Box")
2121
{
2222
_width = width;
2323
_nameWidth = 160;
2424
_contentWidth = width - _nameWidth;
2525

26-
UnityEngine.GUILayout.BeginVertical("Box");
26+
UnityEngine.GUILayout.BeginVertical(style);
2727
}
2828
public static void BeginHorizontal()
2929
{

Images/close.png

-14 Bytes
Loading

Images/curved_arrow_down.png

1 Byte
Loading

Images/curved_arrow_up.png

2 Bytes
Loading

System/Windows/Forms/Application.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -731,19 +731,5 @@ public enum KeyEvents
731731
Down,
732732
Up
733733
}
734-
735-
private class _HotKey
736-
{
737-
public Control hWnd { get; private set; }
738-
public int Id { get; private set; }
739-
public Keys Key { get; private set; }
740-
741-
public _HotKey(Control hwnd, int id, Keys key)
742-
{
743-
this.hWnd = hwnd;
744-
this.Id = id;
745-
this.Key = key;
746-
}
747-
}
748734
}
749735
}

0 commit comments

Comments
 (0)