Skip to content

Commit

Permalink
inspector update, removing more stuff, added FormBorderStyle property,
Browse files Browse the repository at this point in the history
  • Loading branch information
Meragon committed Jun 21, 2017
1 parent cf5c439 commit fce5ab1
Show file tree
Hide file tree
Showing 29 changed files with 454 additions and 222 deletions.
116 changes: 99 additions & 17 deletions Core/Design/ControlDesigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ namespace System.Windows.Forms.Design
{
public class ControlDesigner : IControlDesigner
{
private objectEditor editor;
private bool toggleEditor = true;
private readonly objectEditor editor;

public Control Control { get; set; }

Expand All @@ -29,6 +28,7 @@ public virtual object Draw(int width, int height)

Control controlToSet = null;

Editor.SetBackColor(Color.White);
Editor.BeginGroup(width - 24, "");

controlToSet = editor.Draw();
Expand All @@ -40,6 +40,7 @@ public virtual object Draw(int width, int height)

private class controlProperty
{
public readonly List<objectEditor> arrayEditors = new List<objectEditor>();
public objectEditor editor;
public bool expanded;
public PropertyInfo info;
Expand All @@ -51,6 +52,7 @@ private class objectEditor
private readonly object obj;
private readonly List<controlProperty> props;
private readonly string name;
private int rgb = 255;

public bool toggleEditor;

Expand All @@ -67,6 +69,9 @@ public objectEditor(object o, string objName)
for (int i = 0; i < pList.Count; i++)
{
var p = pList[i];
if (p.DeclaringType == typeof(Delegate)) continue;
if (p.Name == "Item") continue; // this[] will throw an exception.

var cp = new controlProperty()
{
info = p,
Expand Down Expand Up @@ -99,13 +104,15 @@ public Control Draw()
return null;
}

Editor.BeginVertical("Box");
toggleEditor = Editor.Foldout(name, toggleEditor);
var style = toggleEditor ? "Box" : null;
Editor.BeginVertical(style);
if (toggleEditor)
{
// Fields.
if (fields.Count > 0)
{
Editor.SetBackColor(Color.AliceBlue);
Editor.BeginVertical("Box");
for (int i = 0; i < fields.Count; i++)
{
Expand All @@ -119,6 +126,9 @@ public Control Draw()
}

// Properties.
Editor.SetBackColor(Color.White);
if (toggleEditor)
Editor.SetBackColor(Color.FromArgb(rgb, rgb, rgb));
for (int i = 0; i < props.Count; i++)
{
var tc = Draw(props[i]);
Expand All @@ -127,6 +137,7 @@ public Control Draw()
}

// Methods.
Editor.SetBackColor(Color.White);
if (methods.Count > 0)
{
Editor.NewLine(1);
Expand All @@ -140,21 +151,33 @@ public Control Draw()
}
}
Editor.EndVertical();

if (toggleEditor)
Editor.NewLine(1);

return control;
}
public Control Draw(controlProperty p)
{
Control controlToSet = null;

if (p.info.CanRead == false) return null;

var val = p.info.GetValue(obj, null);
Type type = null;
if (val != null)
type = val.GetType();
else
{
Editor.Label(p.info.Name, "null");
return null;
}

// Array & List.
if (val is string == false)
if ((type != null && type.IsArray) || val is IEnumerable)
if (type.IsArray || val is IEnumerable)
{
Editor.BeginVertical("Box");
Editor.BeginVertical();
p.expanded = Editor.Foldout(p.info.Name, p.expanded);
if (p.expanded)
{
Expand All @@ -170,10 +193,16 @@ public Control Draw(controlProperty p)
}
else
{
if (p.editor == null)
p.editor = new objectEditor(e, arrayIndex.ToString());

p.editor.Draw();
if (arrayIndex >= p.arrayEditors.Count)
{
var aEditor = new objectEditor(e, arrayIndex.ToString());
aEditor.rgb = rgb - 25;
if (aEditor.rgb < 128)
aEditor.rgb = 128;
p.arrayEditors.Add(aEditor);
}

p.arrayEditors[arrayIndex].Draw();
}
arrayIndex++;
}
Expand All @@ -183,16 +212,20 @@ public Control Draw(controlProperty p)
}

// If there is no Set() method then skip.
var canSet = true;
var pSetMethod = p.info.GetSetMethod(true);
if (pSetMethod == null || pSetMethod.IsPrivate)
{
Editor.Label(p.info.Name, val);
return null;
}
canSet = false;

// Other editors.
if (val is bool)
{
if (canSet == false)
{
Editor.Label(p.info.Name, val);
return null;
}

var bVal = (bool)val;
var ebVal = Editor.BooleanField(p.info.Name, bVal);
if (ebVal.Changed)
Expand All @@ -206,37 +239,86 @@ public Control Draw(controlProperty p)
}
else if (val is Color)
{
if (canSet == false)
{
Editor.Label(p.info.Name, val);
return null;
}

var colorVal = (Color)val;
Editor.ColorField(p.info.Name, colorVal, c => p.info.SetValue(obj, c, null));
}
else if (val is string)
{
if (canSet == false)
{
Editor.Label(p.info.Name, val);
return null;
}

var stringtVal = (string)val;
var esVal = Editor.TextField(p.info.Name, stringtVal);
if (esVal.Changed)
p.info.SetValue(obj, esVal.Value, null);
}
else if (val is int)
{
var eiVal = Editor.IntField(p.info.Name, (int) val);
if (canSet == false)
{
Editor.Label(p.info.Name, val);
return null;
}

var eiVal = Editor.IntField(p.info.Name, (int)val);
if (eiVal.Changed)
p.info.SetValue(obj, eiVal.Value[0], null);
}
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)
{
if (canSet == false)
{
Editor.Label(p.info.Name, val);
return null;
}

// TODO: editors for common types (like for int ^up there).
Editor.Label(p.info.Name, val);
}
else if (val is Enum)
{
var eeVal = Editor.EnumField(p.info.Name, (Enum)val);
if (eeVal.Changed)
p.info.SetValue(obj, eeVal.Value, null);
if (canSet == false)
{
Editor.Label(p.info.Name, val);
return null;
}

var enumHasFlagAttribute = val.GetType().GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0;
var enumOptions = Enum.GetNames(val.GetType());

if (enumHasFlagAttribute)
{
// TODO: not gonna work with 'None' flag.
// https://forum.unity3d.com/threads/editorguilayout-enummaskfield-doesnt-use-enums-values.233332/
var eeVal = Editor.MaskField(p.info.Name, Convert.ToInt32(val), enumOptions);
if (eeVal.Changed)
p.info.SetValue(obj, eeVal.Value, null);
}
else
{
var eeVal = Editor.EnumField(p.info.Name, (Enum)val);
if (eeVal.Changed)
p.info.SetValue(obj, eeVal.Value, null);
}
}
else if (val != null)
{
if (p.editor == null)
{
p.editor = new objectEditor(val, p.info.Name);
p.editor.rgb = rgb - 25;
if (p.editor.rgb < 128)
p.editor.rgb = 128;
}

p.editor.Draw();
}
Expand Down
27 changes: 26 additions & 1 deletion Core/Design/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public static void BeginVertical()
}
public static void BeginVertical(string style)
{
UnityEngine.GUILayout.BeginVertical(style);
if (string.IsNullOrEmpty(style))
UnityEngine.GUILayout.BeginVertical();
else
UnityEngine.GUILayout.BeginVertical(style);
}
public static void BeginVertical(GUIStyle style)
{
Expand Down Expand Up @@ -196,6 +199,23 @@ public static EditorValue<int[]> IntField(string name, params int[] value)

return new EditorValue<int[]>(intBuffer, changed);
}
public static EditorValue<int> MaskField(string name, int value, string[] options)
{
UnityEngine.GUILayout.BeginHorizontal();
UnityEngine.GUILayout.Label(name + ":", UnityEngine.GUILayout.Width(_nameWidth));
var buffer = value;
if (WinFormsCompatible)
{

}
#if UNITY_EDITOR
else
buffer = UnityEditor.EditorGUILayout.MaskField(value, options);
#endif
UnityEngine.GUILayout.EndHorizontal();

return new EditorValue<int>(buffer, buffer != value);
}
public static void NewLine(int cnt)
{
for (int i = 0; i < cnt; i++)
Expand Down Expand Up @@ -266,6 +286,11 @@ public static bool Toggle(string name, bool value)
{
return UnityEngine.GUILayout.Toggle(value, name, UnityEngine.GUILayout.Width(_width));
}

public static void SetBackColor(Color color)
{
GUI.backgroundColor = color.ToUnityColor();
}
}

public struct EditorValue<T>
Expand Down
8 changes: 2 additions & 6 deletions System/Drawing/Bitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@ public Bitmap(int width, int height)
{
uTexture = Graphics.ApiGraphics.CreateTexture(width, height);
}

public override void Apply()
{
uTexture.Apply();
}

public void ClearColor(Color c, bool apply = true)
{
var colors = new Color[Width * Height];
for (int i = 0; i < colors.Length; i++)
colors[i] = c;
uTexture.SetPixels(colors);
if (apply) Apply();
if (apply) this.Apply();
}
public Color GetPixel(int x, int y)
{
Expand Down
Loading

0 comments on commit fce5ab1

Please sign in to comment.