Skip to content

Commit 43d18a4

Browse files
authored
Merge pull request #10 from Meragon/alpha
Make sure to backup your project and expect a lot of errors if you were using non-winforms methods and properties.
2 parents e3d2a3d + d0360e7 commit 43d18a4

File tree

98 files changed

+4004
-3934
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+4004
-3934
lines changed

Core/API/IApiGraphics.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace System.Drawing.API
7+
{
8+
public interface IApiGraphics
9+
{
10+
ITexture CreateTexture(int width, int height);
11+
ITexture CreateTexture(object original);
12+
13+
void BeginGroup(float x, float y, float width, float height);
14+
void Clear(Color color);
15+
void DrawImage(Image image, Color color, float x, float y, float width, float height, float angle);
16+
void DrawImage(Image image, float x, float y, float width, float height, object material = null);
17+
void DrawLine(Pen pen, float x1, float y1, float x2, float y2, object material = null);
18+
void DrawPolygon(Pen pen, Point[] points, object material = null);
19+
void DrawRectangle(Pen pen, float x, float y, float width, float height, object material = null);
20+
void DrawString(string text, Font font, Color color, float x, float y, float width, float height, ContentAlignment align, object material = null);
21+
void EndGroup();
22+
void FillRectangle(Brush brush, float x, float y, float width, float height, object material = null);
23+
void FillRectangle(Color color, float x, float y, float width, float height, object material = null);
24+
void Focus();
25+
void FocusNext();
26+
SizeF MeasureString(string text, Font font);
27+
}
28+
}

Core/API/IApiTiming.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace System.Drawing.API
7+
{
8+
public interface IApiTiming
9+
{
10+
float DeltaTime { get; }
11+
}
12+
}

Core/Design/ControlDesigner.cs

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Drawing;
5+
using System.Linq;
6+
using System.Reflection;
7+
using System.Text;
8+
9+
namespace System.Windows.Forms.Design
10+
{
11+
public class ControlDesigner : IControlDesigner
12+
{
13+
private objectEditor editor;
14+
private bool toggleEditor = true;
15+
16+
public Control Control { get; set; }
17+
18+
public ControlDesigner(Control c)
19+
{
20+
Control = c;
21+
22+
editor = new objectEditor(c, Control.GetType().Name);
23+
editor.toggleEditor = true;
24+
}
25+
26+
public virtual object Draw(int width, int height)
27+
{
28+
if (Control == null) return null;
29+
30+
Control controlToSet = null;
31+
32+
Editor.BeginGroup(width - 24, "");
33+
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)
58+
{
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+
}
91+
}
92+
93+
public Control Draw()
94+
{
95+
Control control = null;
96+
if (props.Count == 0 && methods.Count == 0)
97+
{
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)
108+
{
109+
Editor.BeginVertical("Box");
110+
for (int i = 0; i < fields.Count; i++)
111+
{
112+
var tc = Draw(fields[i]);
113+
if (tc != null)
114+
control = tc;
115+
}
116+
Editor.EndVertical();
117+
118+
Editor.NewLine(1);
119+
}
120+
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+
}
128+
129+
// Methods.
130+
if (methods.Count > 0)
131+
{
132+
Editor.NewLine(1);
133+
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;
144+
}
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();
152+
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());
175+
176+
p.editor.Draw();
177+
}
178+
arrayIndex++;
179+
}
180+
}
181+
Editor.EndVertical();
182+
return controlToSet;
183+
}
184+
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+
}
192+
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)
213+
{
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);
218+
}
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;
245+
}
246+
public Control Draw(FieldInfo f)
247+
{
248+
// TODO: editors for fields.
249+
250+
var val = f.GetValue(obj);
251+
Editor.Label(f.Name, val);
252+
253+
return null;
254+
}
255+
public Control Draw(MethodInfo m)
256+
{
257+
if (Editor.Button(m.Name))
258+
{
259+
m.Invoke(obj, null);
260+
}
261+
262+
return null;
263+
}
264+
}
265+
}
266+
}

Core/Design/DesignerHelper.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace System.Windows.Forms.Design
7+
{
8+
internal static class DesignerHelper
9+
{
10+
public static ControlDesigner GetDesigner(this Control c)
11+
{
12+
return new ControlDesigner(c);
13+
}
14+
}
15+
}

System/Drawing/Editor.cs renamed to Core/Design/Editor.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using System.Windows.Forms;
6+
using Unity.API;
67
using UnityEngine;
78

89
namespace System.Drawing
@@ -16,13 +17,13 @@ public class Editor
1617

1718
public static bool WinFormsCompatible { get; set; }
1819

19-
public static void BeginGroup(float width)
20+
public static void BeginGroup(float width, string style = "Box")
2021
{
2122
_width = width;
2223
_nameWidth = 160;
2324
_contentWidth = width - _nameWidth;
2425

25-
UnityEngine.GUILayout.BeginVertical("Box");
26+
UnityEngine.GUILayout.BeginVertical(style);
2627
}
2728
public static void BeginHorizontal()
2829
{
@@ -108,7 +109,7 @@ public static void ColorField(string name, Color value, Action<Color> setColor)
108109
#if UNITY_EDITOR
109110
else
110111
{
111-
colorBuffer = System.Drawing.Color.FromUColor(UnityEditor.EditorGUILayout.ColorField(value.ToUColor(), UnityEngine.GUILayout.Width(_contentWidth)));
112+
colorBuffer = UnityEditor.EditorGUILayout.ColorField(value.ToUnityColor(), UnityEngine.GUILayout.Width(_contentWidth)).ToColor();
112113
if (colorBuffer != value && setColor != null)
113114
setColor.Invoke(colorBuffer);
114115
}

Core/Design/IControlDesigner.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace System.Windows.Forms.Design
7+
{
8+
public interface IControlDesigner
9+
{
10+
Control Control { get; }
11+
12+
object Draw(int width, int height);
13+
}
14+
}

Images/close.png

-14 Bytes
Loading

Images/curved_arrow_down.png

1 Byte
Loading

Images/curved_arrow_up.png

2 Bytes
Loading

0 commit comments

Comments
 (0)