Skip to content

Commit 6e0de83

Browse files
authored
Merge pull request #2 from AnnulusGames/add-enum-field
Add: enum field
2 parents c0783be + f533118 commit 6e0de83

5 files changed

Lines changed: 132 additions & 5 deletions

File tree

sandbox/DebugUI.Sandbox/Assets/Sandbox/DebugUISandbox/DebugUISandbox.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
using System;
12
using UnityEngine;
23

34
namespace DebugUI.Sandbox
45
{
6+
public enum EnumExample
7+
{
8+
Alpha,
9+
Beta,
10+
Gamma
11+
}
12+
513
public sealed class DebugUISandbox : DebugUIBuilderBase
614
{
715
[Header("Fields")]
@@ -14,6 +22,7 @@ public sealed class DebugUISandbox : DebugUIBuilderBase
1422
[SerializeField] Vector2Int vector2IntValue;
1523
[SerializeField] Vector3Int vector3IntValue;
1624
[SerializeField] Bounds boundsValue;
25+
[SerializeField] EnumExample enumValue;
1726

1827
[Header("Slider")]
1928
[SerializeField, Range(0f, 10f)] float sliderValue;
@@ -69,6 +78,7 @@ protected override void Configure(IDebugUIBuilder builder)
6978
builder.AddField("Vector2Int", () => vector2IntValue, x => vector2IntValue = x);
7079
builder.AddField("Vector3Int", () => vector3IntValue, x => vector3IntValue = x);
7180
builder.AddField("Bounds", () => boundsValue, x => boundsValue = x);
81+
builder.AddField("Enum", () => enumValue, x => enumValue = x);
7282
});
7383

7484
builder.AddFoldout("Sliders", builder =>

sandbox/DebugUI.Sandbox/Assets/Sandbox/UXML/UXMLSandbox.uxml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<ui:ProgressBar value="22" title="my-progress" />
1010
<DebugUI.UIElements.FillSlider label="Slider" />
1111
<ui:Vector3Field label="Vec3 Field" />
12+
<ui:EnumField label="Enum" type="UnityEngine.TextAlignment, UnityEngine.TextRenderingModule" value="Center" />
1213
<ui:Foldout text="Foldout" />
1314
</DebugUI.UIElements.DebugWindow>
1415
</ui:UXML>

src/DebugUI/Assets/DebugUI/Package Resources/Debug UI.uss

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@
233233
}
234234

235235
.unity-scroller--vertical {
236+
scale: 0.25 1;
236237
}
237238

238239
.unity-scroll-view__content-viewport {
@@ -268,6 +269,67 @@
268269
-unity-background-image-tint-color: rgb(255, 255, 255);
269270
}
270271

272+
.unity-enum-field {
273+
padding-top: 1px;
274+
padding-bottom: 1px;
275+
margin-top: 2px;
276+
margin-right: 0;
277+
margin-bottom: 2px;
278+
margin-left: 0;
279+
}
280+
281+
.unity-enum-field__arrow {
282+
-unity-background-image-tint-color: rgb(255, 255, 255);
283+
}
284+
285+
.unity-enum-field__input {
286+
border-top-width: 0;
287+
border-right-width: 0;
288+
border-bottom-width: 0;
289+
border-left-width: 0;
290+
border-top-left-radius: 4px;
291+
border-top-right-radius: 4px;
292+
border-bottom-right-radius: 4px;
293+
border-bottom-left-radius: 4px;
294+
background-color: rgb(105, 105, 133);
295+
height: 32px;
296+
margin-top: 0;
297+
margin-right: 0;
298+
margin-bottom: 0;
299+
margin-left: 0;
300+
}
301+
302+
.unity-enum-field__text {
303+
color: rgb(255, 255, 255);
304+
}
305+
306+
.unity-base-dropdown__container-outer {
307+
border-left-color: rgba(255, 255, 255, 0.05);
308+
border-right-color: rgba(255, 255, 255, 0.05);
309+
border-top-color: rgba(255, 255, 255, 0.05);
310+
border-bottom-color: rgba(255, 255, 255, 0.05);
311+
border-top-left-radius: 3px;
312+
border-top-right-radius: 3px;
313+
border-bottom-right-radius: 3px;
314+
border-bottom-left-radius: 3px;
315+
border-top-width: 1px;
316+
border-right-width: 1px;
317+
border-bottom-width: 1px;
318+
border-left-width: 1px;
319+
}
320+
321+
.unity-base-dropdown__container-inner {
322+
border-top-width: 0;
323+
border-right-width: 0;
324+
border-bottom-width: 0;
325+
border-left-width: 0;
326+
border-top-left-radius: 3px;
327+
border-top-right-radius: 3px;
328+
border-bottom-right-radius: 3px;
329+
border-bottom-left-radius: 3px;
330+
background-color: rgb(86, 86, 115);
331+
}
332+
271333
.debug-ui-slider__label {
272334
color: rgb(255, 255, 255);
273335
-unity-text-align: middle-right;
@@ -321,7 +383,6 @@
321383
}
322384

323385
.debug-ui-scroller-vertical {
324-
scale: 0.2 1;
325386
margin-right: -12px;
326387
margin-left: -6px;
327388
margin-top: 1px;
@@ -349,4 +410,4 @@
349410
margin-right: 3px;
350411
margin-bottom: 3px;
351412
margin-left: 3px;
352-
}
413+
}

src/DebugUI/Assets/DebugUI/Runtime/DebugUIBuilderExtensions.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ public static IDebugUIBuilder AddButton(this IDebugUIBuilder builder, string tex
4747
return builder;
4848
}
4949

50+
public static IDebugUIBuilder AddField<TEnum>(this IDebugUIBuilder builder, string label, Func<TEnum> getter)
51+
where TEnum : Enum
52+
{
53+
builder.Factories.Add(new DebugEnumFieldFactory<TEnum>()
54+
{
55+
Label = label,
56+
Getter = getter,
57+
});
58+
return builder;
59+
}
60+
61+
62+
public static IDebugUIBuilder AddField<TEnum>(this IDebugUIBuilder builder, string label, Func<TEnum> getter, Action<TEnum> setter)
63+
where TEnum : Enum
64+
{
65+
builder.Factories.Add(new DebugEnumFieldFactory<TEnum>()
66+
{
67+
Label = label,
68+
Getter = getter,
69+
Setter = setter,
70+
});
71+
return builder;
72+
}
73+
5074
public static IDebugUIBuilder AddField(this IDebugUIBuilder builder, string label, Func<bool> getter)
5175
{
5276
builder.Factories.Add(new DebugFieldFactory<bool, Toggle>()
@@ -68,7 +92,6 @@ public static IDebugUIBuilder AddField(this IDebugUIBuilder builder, string labe
6892
return builder;
6993
}
7094

71-
7295
public static IDebugUIBuilder AddField(this IDebugUIBuilder builder, string label, Func<int> getter)
7396
{
7497
builder.Factories.Add(new DebugFieldFactory<int, IntegerField>()

src/DebugUI/Assets/DebugUI/Runtime/DebugUIElementFactory.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,41 @@ public VisualElement CreateVisualElement(ICollection<IDisposable> disposables)
212212
}
213213
}
214214

215+
internal sealed class DebugEnumFieldFactory<TEnum> : IDebugUIElementFactory
216+
where TEnum : Enum
217+
{
218+
public string Label { get; set; }
219+
public Func<TEnum> Getter { get; set; }
220+
public Action<TEnum> Setter { get; set; }
221+
222+
public VisualElement CreateVisualElement(ICollection<IDisposable> disposables)
223+
{
224+
var field = new EnumField(Getter())
225+
{
226+
label = Label,
227+
};
228+
229+
if (Setter == null)
230+
{
231+
VisualElementHelper.SetInputFieldsEnabled(field, false);
232+
}
233+
else
234+
{
235+
field.RegisterValueChangedCallback(x => Setter((TEnum)x.newValue));
236+
}
237+
238+
MinimalRx.EveryValueChanged(this, x => x.Getter())
239+
.Subscribe(x =>
240+
{
241+
field.value = x;
242+
})
243+
.AddTo(disposables);
244+
245+
return field;
246+
}
247+
}
248+
215249
internal sealed class DebugFieldFactory<TValue, TField> : IDebugUIElementFactory
216-
where TValue : IEquatable<TValue>
217250
where TField : BaseField<TValue>, new()
218251
{
219252
public string Label { get; set; }
@@ -249,7 +282,6 @@ public VisualElement CreateVisualElement(ICollection<IDisposable> disposables)
249282
}
250283

251284
internal sealed class DebugCompositeFieldFactory<TValue, TCompositeField, TField, TFieldValue> : IDebugUIElementFactory
252-
where TValue : IEquatable<TValue>
253285
where TCompositeField : BaseCompositeField<TValue, TField, TFieldValue>, new()
254286
where TField : TextValueField<TFieldValue>, new()
255287
{

0 commit comments

Comments
 (0)