Skip to content

Commit 5e487d4

Browse files
committed
Add BoolToFormattedString adapter
1 parent ea08df8 commit 5e487d4

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public class ViewBindingSample : MonoBehaviour
7878

7979
#### Builtin adapters:
8080
- [Bool To String](./Runtime/Applicators/Adapters/BoolToStringAdapter.cs)
81+
- [Bool To Formatted String](./Runtime/Applicators/Adapters/BoolToFormattedStringAdapter.cs)
8182
- [Compare String](./Runtime/Applicators/Adapters/CompareStringAdapter.cs)
8283
- [Float Format](./Runtime/Applicators/Adapters/FloatFormatAdapter.cs)
8384
- [Float Ratio](./Runtime/Applicators/Adapters/FloatRatioAdapter.cs)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
3+
namespace CodeWriter.ViewBinding.Applicators.Adapters {
4+
using UnityEngine;
5+
6+
[AddComponentMenu("View Binding/Adapters/[Binding] Bool To Formatted String Adapter")]
7+
public class BoolToFormattedStringAdapter : SingleResultAdapterBase<string, ViewVariableString> {
8+
[Space]
9+
[SerializeField]
10+
private ViewVariableBool source;
11+
12+
[SerializeField]
13+
private string trueFormat = "TRUE";
14+
15+
[SerializeField]
16+
private string falseFormat = "FALSE";
17+
18+
[SerializeField]
19+
private ViewContextBase[] extraContexts = Array.Empty<ViewContextBase>();
20+
21+
protected override string Adapt() {
22+
var textBuilder = new ValueTextBuilder(ValueTextBuilder.DefaultCapacity);
23+
try {
24+
if (source.Value) {
25+
textBuilder.AppendFormat(trueFormat, extraContexts);
26+
}
27+
else {
28+
textBuilder.AppendFormat(falseFormat, extraContexts);
29+
}
30+
31+
return new string(textBuilder.RawCharArray, 0, textBuilder.Length);
32+
}
33+
finally {
34+
textBuilder.Dispose();
35+
}
36+
}
37+
}
38+
}

Runtime/Applicators/Adapters/BoolToFormattedStringAdapter.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)