Skip to content

Commit

Permalink
feat: #6 FieldOption.delayInput. This can delay updating the value in…
Browse files Browse the repository at this point in the history
… UI.Field().
  • Loading branch information
fuqunaga committed Jan 17, 2023
1 parent b7f9128 commit 508862f
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 35 deletions.
24 changes: 21 additions & 3 deletions Assets/Example/Runtime/Categories/FieldExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,25 @@ public Element CreateElement(LabelElement _)
UI.FieldReadOnly(() => simpleClass),
UI.FieldReadOnly(() => classList)
),
ExampleTemplate.CodeElementSetsTab("Codes", "Attributes",
(@"public class AttributeExampleClass
ExampleTemplate.Tab("Codes",
ExampleTemplate.CodeElementSets("Argument",
"If FieldOption.delayInput == true, the value isn't updated until Enter is pressed or the focus is lost.",
(@"UI.Field(
() => intValue,
new FieldOption() { delayInput = true }
).RegisterValueChangeCallback(
() => Debug.Log($""OnValueChanged[{intValue}]"")
);",
UI.Field(
() => intValue,
new FieldOption() { delayInput = true }
).RegisterValueChangeCallback(
() => Debug.Log($"OnValueChanged[{intValue}]")
)
)
),
ExampleTemplate.CodeElementSets("Attribute",
(@"public class AttributeExampleClass
{
[Range(0f,100f)]
public float rangeFloat;
Expand All @@ -116,7 +133,8 @@ public Element CreateElement(LabelElement _)
UI.Field(() => attributeExampleClass).Open();
",
UI.Field(() => attributeExampleClass).Open()
UI.Field(() => attributeExampleClass).Open()
)
)
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine;
using UnityEngine.UIElements;

namespace RosettaUI.UIToolkit.Builder
Expand Down Expand Up @@ -54,6 +52,11 @@ private void Bind_Field<TValue, TField>(FieldBaseElement<TValue> element, TField
where TField : BaseField<TValue>, new()
{
element.Bind(field);

if (field is TextInputBaseField<TValue> textInputBaseField)
{
textInputBaseField.isDelayed = element.Option?.delayInput ?? false;
}

if (labelEnable)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
/// </summary>
public abstract class FieldBaseElement<T> : ReadOnlyFieldElement<T>
{
public FieldOption Option { get; protected set; }
private readonly IBinder<T> _binder;

protected FieldBaseElement(LabelElement label, IBinder<T> binder) : base(label, binder)
protected FieldBaseElement(LabelElement label, IBinder<T> binder, FieldOption option = null) : base(label, binder)
{
_binder = binder;
Option = option;
Interactable = !binder.IsReadOnly;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace RosettaUI
{
public class FieldOption
{
public bool delayInput = false;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public class FloatFieldElement : FieldBaseElement<float>
{
public FloatFieldElement(LabelElement label, IBinder<float> binder) : base(label, binder) { }
public FloatFieldElement(LabelElement label, IBinder<float> binder, FieldOption option) : base(label, binder, option) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class IntFieldElement : FieldBaseElement<int>
{
public IntFieldElement(LabelElement label, IBinder<int> binder) : base(label, binder)
public IntFieldElement(LabelElement label, IBinder<int> binder, FieldOption option) : base(label, binder, option)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public class TextFieldElement : FieldBaseElement<string>
{
public bool IsMultiLine { get; internal set; }

public TextFieldElement(LabelElement label, IBinder<string> binder) : base(label, binder) { }
public TextFieldElement(LabelElement label, IBinder<string> binder, FieldOption option) : base(label, binder, option) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class UIntFieldElement : FieldBaseElement<uint>
{
public UIntFieldElement(LabelElement label, IBinder<uint> binder) : base(label, binder)
public UIntFieldElement(LabelElement label, IBinder<uint> binder, FieldOption option) : base(label, binder, option)
{}
}
}
24 changes: 12 additions & 12 deletions Packages/RosettaUI/Runtime/UI/BinderToElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class BinderToElement
{
#region Field

public static Element CreateFieldElement(LabelElement label, IBinder binder)
public static Element CreateFieldElement(LabelElement label, IBinder binder, FieldOption option = null)
{
var valueType = binder.ValueType;

Expand All @@ -26,20 +26,20 @@ public static Element CreateFieldElement(LabelElement label, IBinder binder)
_ when UICustom.GetElementCreationMethod(valueType) is { } creationFunc => InvokeCreationFunc(label,
binder, creationFunc),

IBinder<int> ib => new IntFieldElement(label, ib),
IBinder<uint> ib => new UIntFieldElement(label, ib),
IBinder<float> ib => new FloatFieldElement(label, ib),
IBinder<string> ib => new TextFieldElement(label, ib),
IBinder<int> ib => new IntFieldElement(label, ib, option),
IBinder<uint> ib => new UIntFieldElement(label, ib, option),
IBinder<float> ib => new FloatFieldElement(label, ib, option),
IBinder<string> ib => new TextFieldElement(label, ib, option),
IBinder<bool> ib => new ToggleElement(label, ib),
IBinder<Color> ib => new ColorFieldElement(label, ib),
_ when valueType.IsEnum => CreateEnumElement(label, binder),
_ when TypeUtility.IsNullable(valueType) => CreateNullableFieldElement(label, binder),
_ when TypeUtility.IsNullable(valueType) => CreateNullableFieldElement(label, binder, option),

_ when binder.GetObject() is IElementCreator elementCreator => WrapNullGuard(() =>
elementCreator.CreateElement(label)),
_ when ListBinder.IsListBinder(binder) => CreateListView(label, binder),

_ => WrapNullGuard(() => CreateMemberFieldElement(label, binder))
_ => WrapNullGuard(() => CreateMemberFieldElement(label, binder, option))
};

Element WrapNullGuard(Func<Element> func) => UI.NullGuardIfNeed(label, binder, func);
Expand All @@ -59,10 +59,10 @@ private static Element CreateEnumElement(LabelElement label, IBinder binder)
return new DropdownElement(label, enumToIdxBinder, Enum.GetNames(valueType));
}

private static Element CreateNullableFieldElement(LabelElement label, IBinder binder)
private static Element CreateNullableFieldElement(LabelElement label, IBinder binder, FieldOption option)
{
var valueBinder = NullableToValueBinder.Create(binder);
return UI.NullGuard(label, binder, () => CreateFieldElement(label, valueBinder));
return UI.NullGuard(label, binder, () => CreateFieldElement(label, valueBinder, option));
}

private static Element CreateListView(LabelElement label, IBinder binder)
Expand All @@ -77,7 +77,7 @@ private static Element CreateListView(LabelElement label, IBinder binder)
return UI.List(label, binder, null, option);
}

private static Element CreateMemberFieldElement(LabelElement label, IBinder binder)
private static Element CreateMemberFieldElement(LabelElement label, IBinder binder, FieldOption option)
{
var valueType = binder.ValueType;

Expand All @@ -93,7 +93,7 @@ private static Element CreateMemberFieldElement(LabelElement label, IBinder bind
return UI.Slider(fieldLabel, fieldBinder, minGetter, maxGetter);
}
var field = UI.Field(fieldLabel, fieldBinder);
var field = UI.Field(fieldLabel, fieldBinder, option);
if (TypeUtility.IsMultiline(valueType, fieldName) && field is TextFieldElement textField)
Expand Down Expand Up @@ -204,7 +204,7 @@ Element CreateElementFunc()
{
if (TypeUtility.IsSingleLine(binder.ValueType))
{
var titleField = CreateMemberFieldElement(new LabelElement(label), binder);
var titleField = CreateMemberFieldElement(new LabelElement(label), binder, null);

// Foldが閉じてるときは titleField を、開いているときは label を表示
// UI.Row(label, titleField) だと titleField のラベルがPrefixLabel判定されないので
Expand Down
20 changes: 10 additions & 10 deletions Packages/RosettaUI/Runtime/UI/UI_Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ namespace RosettaUI
{
public static partial class UI
{
public static Element Field<T>(Expression<Func<T>> targetExpression)
public static Element Field<T>(Expression<Func<T>> targetExpression, FieldOption option = null)
{
return Field(ExpressionUtility.CreateLabelString(targetExpression), targetExpression);
return Field(ExpressionUtility.CreateLabelString(targetExpression), targetExpression, option);
}

public static Element Field<T>(LabelElement label, Expression<Func<T>> targetExpression)
public static Element Field<T>(LabelElement label, Expression<Func<T>> targetExpression, FieldOption option = null)
{
var binder = UIInternalUtility.CreateBinder(targetExpression);
return Field(label, binder);
return Field(label, binder, option);
}

public static Element Field<T>(Expression<Func<T>> targetExpression, Action<T> writeValue)
=> Field(ExpressionUtility.CreateLabelString(targetExpression), targetExpression.Compile(), writeValue);
public static Element Field<T>(Expression<Func<T>> targetExpression, Action<T> writeValue, FieldOption option = null)
=> Field(ExpressionUtility.CreateLabelString(targetExpression), targetExpression.Compile(), writeValue, option);

public static Element Field<T>(LabelElement label, Func<T> readValue, Action<T> writeValue)
=> Field(label, Binder.Create(readValue, writeValue));
public static Element Field<T>(LabelElement label, Func<T> readValue, Action<T> writeValue, FieldOption option = null)
=> Field(label, Binder.Create(readValue, writeValue), option);

public static Element Field(LabelElement label, IBinder binder)
public static Element Field(LabelElement label, IBinder binder, FieldOption option = null)
{
var element = BinderToElement.CreateFieldElement(label, binder);
var element = BinderToElement.CreateFieldElement(label, binder, option);
if (element != null) UIInternalUtility.SetInteractableWithBinder(element, binder);

return element;
Expand Down
2 changes: 1 addition & 1 deletion Packages/RosettaUI/Runtime/UI/UI_NullGuard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static Element NullGuard(LabelElement label, IGetter getter, Func<Element
return DynamicElement.Create(
() => getter.IsNull,
isNull => isNull
? new TextFieldElement(label, NullStrBinder).SetInteractable(false)
? new TextFieldElement(label, NullStrBinder, null).SetInteractable(false)
: createElement(),
$"NullGuard({nameof(DynamicElement)})"
);
Expand Down

0 comments on commit 508862f

Please sign in to comment.