Skip to content

Commit

Permalink
Add Text Format Value Binder
Browse files Browse the repository at this point in the history
  • Loading branch information
ls9512 committed Aug 19, 2021
1 parent 56377fa commit eae49c3
Show file tree
Hide file tree
Showing 10 changed files with 860 additions and 131 deletions.
4 changes: 3 additions & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ Used to cache all the binding structure information of the objects marked by **B
|-|-|-|-|
|Text Binder|Text|text|string|
|Text FontSize Binder|Text|fontSize|int|
|Text Format Value Binder|Text|text|float|
|InputField Binder|InputField|text|string|
|Slider Binder|Slider|value|float|
|Scrollbar Binder|Scrollbar|value|float|
Expand Down Expand Up @@ -377,4 +378,5 @@ DataConverter.Register((sourceType, targetType), new CustomDataConverter());
***

## 8. <a name='Extension--TextMeshPro'></a>Extension -- TextMeshPro
* TMP Text Binder
* TMP Text Binder
* TMP Text Format Value Binder
4 changes: 3 additions & 1 deletion .github/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ DataConvert.Register(Type sourceType, Type targetType, DataConverter dataConvert
|-|-|-|-|
|Text Binder|Text|text|string|
|Text FontSize Binder|Text|fontSize|int|
|Text Format Value Binder|Text|text|float|
|InputField Binder|InputField|text|string|
|Slider Binder|Slider|value|float|
|Scrollbar Binder|Scrollbar|value|float|
Expand Down Expand Up @@ -378,4 +379,5 @@ DataConverter.Register((sourceType, targetType), new CustomDataConverter());
***

## 8. <a name='--TextMeshPro'></a>扩展 -- TextMeshPro
* TMP Text Binder
* TMP Text Binder
* TMP Text Format Value Binder
10 changes: 5 additions & 5 deletions Editor/Script/ComponentBinderEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public abstract class ComponentBinderEditor<TComponent, TValue, TDataBinder> : B

public virtual void OnEnable()
{
ContextKeyProperty = serializedObject.FindProperty("Context");
DataKeyProperty = serializedObject.FindProperty("Key");
DirectionProperty = serializedObject.FindProperty("Direction");
UpdateTypeProperty = serializedObject.FindProperty("UpdateType");
TargetProperty = serializedObject.FindProperty("Target");
ContextKeyProperty = serializedObject.FindProperty(nameof(ComponentBinder.Context));
DataKeyProperty = serializedObject.FindProperty(nameof(ComponentBinder.Key));
DirectionProperty = serializedObject.FindProperty(nameof(ComponentBinder.Direction));
UpdateTypeProperty = serializedObject.FindProperty(nameof(ComponentBinder.UpdateType));
TargetProperty = serializedObject.FindProperty(nameof(ComponentBinder.Target));
}

public override void OnInspectorGUI()
Expand Down
75 changes: 75 additions & 0 deletions Extension/TextMeshPro/Script/TMPTextFormatValueBinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using TMPro;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Aya.DataBinding
{
[AddComponentMenu("Data Binding/TMP Text Format Value Binder")]
public class TMPTextFormatValueBinder : ComponentBinder<TMP_Text, float, RuntimeTMPTextFormatValueBinder>
{
public override bool NeedUpdate => true;
public string Text;

public override RuntimeTMPTextFormatValueBinder CreateDataBinder()
{
var dataBinder = new RuntimeTMPTextFormatValueBinder
{
Target = Target,
Context = Context,
Direction = Direction,
Key = Key,
Text = Text
};

return dataBinder;
}

public override void Reset()
{
Text = "{0:F2}";
}
}

public class RuntimeTMPTextFormatValueBinder : DataBinder<TMP_Text, float>
{
public override bool NeedUpdate => true;
public string Text;

public override float Value
{
get => _value;
set
{
_value = value;
var str = string.Format(Text, _value);
Target.text = str;
}
}

private float _value;
}

#if UNITY_EDITOR

[CustomEditor(typeof(TMPTextFormatValueBinder)), CanEditMultipleObjects]
public class TMPTextFormatValueBinderEditor : ComponentBinderEditor<TMP_Text, float, RuntimeTMPTextFormatValueBinder>
{
public TMPTextFormatValueBinder Binder => target as TMPTextFormatValueBinder;
protected SerializedProperty TextProperty;

public override void OnEnable()
{
base.OnEnable();
TextProperty = serializedObject.FindProperty(nameof(Binder.Text));
}

public override void DrawBody()
{
EditorGUILayout.PropertyField(TextProperty);
}
}

#endif
}
11 changes: 11 additions & 0 deletions Extension/TextMeshPro/Script/TMPTextFormatValueBinder.cs.meta

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

75 changes: 75 additions & 0 deletions Runtime/Script/Component/UI/TextFormatValueBinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Aya.DataBinding
{
[AddComponentMenu("Data Binding/Text Format Value Binder")]
public class TextFormatValueBinder : ComponentBinder<Text, float, RuntimeTextFormatValueBinder>
{
public override bool NeedUpdate => true;
public string Text;

public override RuntimeTextFormatValueBinder CreateDataBinder()
{
var dataBinder = new RuntimeTextFormatValueBinder
{
Target = Target,
Context = Context,
Direction = Direction,
Key = Key,
Text = Text
};

return dataBinder;
}

public override void Reset()
{
Text = "{0:F2}";
}
}

public class RuntimeTextFormatValueBinder : DataBinder<Text, float>
{
public override bool NeedUpdate => true;
public string Text;

public override float Value
{
get => _value;
set
{
_value = value;
var str = string.Format(Text, _value);
Target.text = str;
}
}

private float _value;
}

#if UNITY_EDITOR

[CustomEditor(typeof(TextFormatValueBinder)), CanEditMultipleObjects]
public class TextFormatValueBinderEditor : ComponentBinderEditor<Text, float, RuntimeTextFormatValueBinder>
{
public TextFormatValueBinder Binder => target as TextFormatValueBinder;
protected SerializedProperty TextProperty;

public override void OnEnable()
{
base.OnEnable();
TextProperty = serializedObject.FindProperty(nameof(Binder.Text));
}

public override void DrawBody()
{
EditorGUILayout.PropertyField(TextProperty);
}
}

#endif
}
11 changes: 11 additions & 0 deletions Runtime/Script/Component/UI/TextFormatValueBinder.cs.meta

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

14 changes: 11 additions & 3 deletions Runtime/Script/Converter/CommonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@ public override object To(object data, Type convertType)
{
return data;
}
else if (dataType == typeof(string))
else if (convertType == typeof(string))
{
var convertData = data.ToString();
return convertData;
}
else
{
var convertData = Convert.ChangeType(data, convertType, CultureInfo.InvariantCulture);
return convertData;
try
{
var convertData = Convert.ChangeType(data, convertType, CultureInfo.InvariantCulture);
return convertData;
}
catch
{
var result = convertType.IsValueType ? Activator.CreateInstance(convertType) : null;
return result;
}
}
}
}
Expand Down
Loading

0 comments on commit eae49c3

Please sign in to comment.