Skip to content

Commit 849957e

Browse files
committed
fixed an issue when the app losing focus with Alt+Tab keys and focused control is not losing their focus;
fixed #33; added methods support for IEnumerable/Array types in ObjectDesigner;
1 parent 022bd38 commit 849957e

File tree

5 files changed

+85
-78
lines changed

5 files changed

+85
-78
lines changed

System/Windows/Forms/Application.cs

+3
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ internal void OnLostFocus()
358358
{
359359
hoveredControl.selected = false;
360360
hoveredControl.RaiseOnLostFocus(EventArgs.Empty);
361+
hoveredControl = null;
362+
363+
Control.lastSelected = null;
361364
}
362365

363366
if (LostFocus != null)

Unity/API/UnityGdi.cs

+16-14
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
using System.Windows.Forms;
88

99
using UE = UnityEngine;
10-
10+
1111
public class UnityGdi : IApiGraphics
1212
{
1313
public static UE.Color cursorSelectionColor = Color.FromArgb(128, SystemColors.Highlight).ToUnityColor();
14-
public static UE.Texture2D defaultTexture = UnityWinForms.DefaultSprite;
14+
public static UE.Texture2D defaultTexture = UnityWinForms.DefaultTexture;
1515

1616
private static readonly UE.GUIContent textContent = new UE.GUIContent(""); // GUIContent.Temp(text) replacement.
1717
private static readonly UE.Rect sourceRect = new UE.Rect(0f, 0f, 1f, 1f);
@@ -422,10 +422,11 @@ internal string uwfDrawPasswordField(string s, Font font, Color color, float x,
422422
GUI_SetFont(UE.GUI.skin.textField, font);
423423

424424
UE.GUI.color = color.ToUnityColor();
425-
UE.GUI.skin.textField.hover.background = null;
426-
UE.GUI.skin.textField.active.background = null;
427-
UE.GUI.skin.textField.focused.background = null;
428-
UE.GUI.skin.textField.normal.background = null;
425+
426+
UE.GUI.skin.textField.hover.background = UnityWinForms.TransparentTexture;
427+
UE.GUI.skin.textField.active.background = UnityWinForms.TransparentTexture;
428+
UE.GUI.skin.textField.focused.background = UnityWinForms.TransparentTexture;
429+
UE.GUI.skin.textField.normal.background = UnityWinForms.TransparentTexture;
429430

430431
return UE.GUI.PasswordField(new UE.Rect(x, y, width, height), s, '*');
431432
}
@@ -440,10 +441,10 @@ internal string uwfDrawTextArea(string s, Font font, Color color, float x, float
440441

441442
GUI_SetFont(UE.GUI.skin.textArea, font);
442443

443-
UE.GUI.skin.textArea.hover.background = null;
444-
UE.GUI.skin.textArea.active.background = null;
445-
UE.GUI.skin.textArea.focused.background = null;
446-
UE.GUI.skin.textArea.normal.background = null;
444+
UE.GUI.skin.textArea.hover.background = UnityWinForms.TransparentTexture;
445+
UE.GUI.skin.textArea.active.background = UnityWinForms.TransparentTexture;
446+
UE.GUI.skin.textArea.focused.background = UnityWinForms.TransparentTexture;
447+
UE.GUI.skin.textArea.normal.background = UnityWinForms.TransparentTexture;
447448

448449
return UE.GUI.TextArea(new UE.Rect(x, y, width, height), s);
449450
}
@@ -468,10 +469,11 @@ internal string uwfDrawTextField(string s, Font font, Color color, float x, floa
468469
GUI_SetFont(UE.GUI.skin.textField, font);
469470

470471
UE.GUI.color = color.ToUnityColor();
471-
UE.GUI.skin.textField.hover.background = null;
472-
UE.GUI.skin.textField.active.background = null;
473-
UE.GUI.skin.textField.focused.background = null;
474-
UE.GUI.skin.textField.normal.background = null;
472+
473+
UE.GUI.skin.textField.hover.background = UnityWinForms.TransparentTexture;
474+
UE.GUI.skin.textField.active.background = UnityWinForms.TransparentTexture;
475+
UE.GUI.skin.textField.focused.background = UnityWinForms.TransparentTexture;
476+
UE.GUI.skin.textField.normal.background = UnityWinForms.TransparentTexture;
475477

476478
return UE.GUI.TextField(new UE.Rect(x, y, width, height), s);
477479
}

Unity/UnityWinForms.cs

+18-15
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public sealed class UnityWinForms : UE.MonoBehaviour
1818
[UE.Tooltip("Delay between KeyDown events")]
1919
public float ShiftDownTime = .05f;
2020

21-
private static UE.Texture2D defaultSprite;
21+
private static UE.Texture2D defaultTexture;
22+
private static UE.Texture2D transparentTexture;
2223

2324
private Application controller;
2425

@@ -28,23 +29,17 @@ public sealed class UnityWinForms : UE.MonoBehaviour
2829
private float shiftDownTimer;
2930
private float shiftDownDelayTimer; // Shift pressing is really fast.
3031
private bool paused;
31-
32-
internal static UE.Texture2D DefaultSprite
32+
33+
internal static UE.Texture2D DefaultTexture
3334
{
34-
get
35-
{
36-
if (defaultSprite != null) return defaultSprite;
37-
38-
defaultSprite = new UE.Texture2D(1, 1);
35+
get { return defaultTexture; }
36+
}
3937

40-
for (int y = 0; y < defaultSprite.height; y++)
41-
for (int x = 0; x < defaultSprite.width; x++)
42-
defaultSprite.SetPixel(x, y, UE.Color.white);
43-
44-
defaultSprite.Apply();
45-
return defaultSprite;
46-
}
38+
internal static UE.Texture2D TransparentTexture
39+
{
40+
get { return transparentTexture; }
4741
}
42+
4843
internal static AppGdiImages GdiImages { get; private set; }
4944
internal static AppResources gResources { get; private set; }
5045

@@ -59,6 +54,14 @@ internal static void Inspect(object obj)
5954

6055
private void Awake()
6156
{
57+
defaultTexture = new UE.Texture2D(1, 1);
58+
defaultTexture.SetPixel(0, 0, UE.Color.white);
59+
defaultTexture.Apply();
60+
61+
transparentTexture = new UE.Texture2D(1, 1);
62+
transparentTexture.SetPixel(0, 0, UE.Color.clear);
63+
transparentTexture.Apply();
64+
6265
ApiHolder.Graphics = new UnityGdi();
6366
ApiHolder.Input = new UnityInput();
6467
ApiHolder.System = new UnitySystem();

Unity/Views/ObjectDesigner.cs

+42-47
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ public virtual object Draw(int width, int height)
3939

4040
private class PropertyEditor
4141
{
42-
public readonly List<ObjectEditor> arrayEditors = new List<ObjectEditor>();
4342
public readonly PropertyInfo info;
4443
public readonly bool canSet;
4544

4645
public ObjectEditor editor;
47-
public bool expanded;
4846

4947
public PropertyEditor(PropertyInfo propertyInfo, bool instanceIsValueType)
5048
{
@@ -59,6 +57,7 @@ public PropertyEditor(PropertyInfo propertyInfo, bool instanceIsValueType)
5957
private class ObjectEditor
6058
{
6159
public bool toggleEditor;
60+
public readonly List<ObjectEditor> arrayEditors = new List<ObjectEditor>();
6261

6362
private readonly List<FieldInfo> fields;
6463
private readonly List<MethodInfo> methods;
@@ -67,6 +66,7 @@ private class ObjectEditor
6766
private readonly string name;
6867

6968
private int backgroundRGB = 255;
69+
private bool enumerableExpanded;
7070

7171
public ObjectEditor(object o, string objName)
7272
{
@@ -163,6 +163,46 @@ public object Draw()
163163

164164
Editor.EndVertical();
165165
}
166+
167+
// Array & List.
168+
if (!(target is string) && (target.GetType().IsArray || target is IEnumerable))
169+
{
170+
Editor.BeginVertical();
171+
172+
{
173+
var arrayIndex = 0;
174+
var e = target as IEnumerable;
175+
176+
foreach (var item in e)
177+
{
178+
var control = item as Control;
179+
if (control != null)
180+
{
181+
if (Editor.Button(" " + arrayIndex, control.ToString()))
182+
return control;
183+
}
184+
else
185+
{
186+
if (arrayIndex >= arrayEditors.Count)
187+
{
188+
var itemText = "null";
189+
if (item != null) itemText = item.ToString();
190+
191+
var aEditor = new ObjectEditor(item, arrayIndex + " (" + itemText + ")");
192+
aEditor.backgroundRGB = MathHelper.Clamp(backgroundRGB - 25, 128, 255);
193+
194+
arrayEditors.Add(aEditor);
195+
}
196+
197+
arrayEditors[arrayIndex].Draw();
198+
}
199+
200+
arrayIndex++;
201+
}
202+
}
203+
204+
Editor.EndVertical();
205+
}
166206
}
167207

168208
Editor.EndVertical();
@@ -183,12 +223,6 @@ public object Draw(PropertyEditor propertyEditor)
183223
return null;
184224
}
185225

186-
// Array & List.
187-
if (!(value is string) && (value.GetType().IsArray || value is IEnumerable))
188-
{
189-
return PropertyEditorEnumerable(propertyEditor, (IEnumerable) value);
190-
}
191-
192226
// Base editors.
193227
if (value is bool) return PropertyEditorBool(propertyEditor, (bool) value);
194228
if (value is byte) return PropertyEditorUInt8(propertyEditor, (byte) value);
@@ -332,45 +366,6 @@ public object PropertyEditorEnum(PropertyEditor propertyEditor, Enum value)
332366

333367
return null;
334368
}
335-
public object PropertyEditorEnumerable(PropertyEditor propertyEditor, IEnumerable value)
336-
{
337-
Editor.BeginVertical();
338-
propertyEditor.expanded = Editor.Foldout(propertyEditor.info.Name, propertyEditor.expanded);
339-
if (propertyEditor.expanded)
340-
{
341-
var arrayIndex = 0;
342-
343-
foreach (var item in value)
344-
{
345-
var control = item as Control;
346-
if (control != null)
347-
{
348-
if (Editor.Button(" " + arrayIndex, control.ToString()))
349-
return control;
350-
}
351-
else
352-
{
353-
if (arrayIndex >= propertyEditor.arrayEditors.Count)
354-
{
355-
var itemText = "null";
356-
if (item != null) itemText = item.ToString();
357-
358-
var aEditor = new ObjectEditor(item, arrayIndex + " (" + itemText + ")");
359-
aEditor.backgroundRGB = MathHelper.Clamp(backgroundRGB - 25, 128, 255);
360-
361-
propertyEditor.arrayEditors.Add(aEditor);
362-
}
363-
364-
propertyEditor.arrayEditors[arrayIndex].Draw();
365-
}
366-
367-
arrayIndex++;
368-
}
369-
}
370-
371-
Editor.EndVertical();
372-
return null;
373-
}
374369
public object PropertyEditorInt8(PropertyEditor propertyEditor, sbyte value)
375370
{
376371
if (!propertyEditor.canSet)

Utility/ControlHelper.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ public static void AddDialogButtons(this Control f, Control buttonOk, Control bu
1515
buttonCancel.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
1616
buttonCancel.Location = new Point(f.Width - buttonCancel.Width - 12, f.Height - buttonCancel.Height - 15);
1717
buttonCancel.Text = "Cancel";
18-
f.Controls.Add(buttonCancel);
18+
19+
if (!f.Controls.Contains(buttonCancel))
20+
f.Controls.Add(buttonCancel);
1921

2022
buttonOk.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
2123
buttonOk.Location = new Point(buttonCancel.Location.X - buttonOk.Width - 9, buttonCancel.Location.Y);
2224
buttonOk.Text = "Ok";
23-
f.Controls.Add(buttonOk);
25+
26+
if (!f.Controls.Contains(buttonOk))
27+
f.Controls.Add(buttonOk);
2428

2529
if (additionalButtons != null)
2630
{

0 commit comments

Comments
 (0)