108 lines
2.9 KiB
C#
108 lines
2.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine.Events;
|
|
using BITKit.UX;
|
|
namespace BITKit.UX.Components
|
|
{
|
|
public abstract class StringComponent : DataComponents
|
|
{
|
|
public override void OnStart()
|
|
{
|
|
Data.AddListener<string>(key, OnSet);
|
|
}
|
|
public override void OnStop()
|
|
{
|
|
Data.RemoveListender<string>(key, OnSet);
|
|
}
|
|
protected abstract void OnSet(string value);
|
|
}
|
|
[System.Serializable]
|
|
public sealed class Text : StringComponent
|
|
{
|
|
public Provider output;
|
|
protected override async void OnSet(string value)
|
|
{
|
|
await UniTask.SwitchToMainThread();
|
|
output.Set(value);
|
|
}
|
|
}
|
|
[System.Serializable]
|
|
public sealed class TranslateText : StringComponent
|
|
{
|
|
ITranslator translator;
|
|
public Provider output;
|
|
public TranslateSO so;
|
|
public string source;
|
|
public override void OnStart()
|
|
{
|
|
base.OnStart();
|
|
source = output.Get<string>();
|
|
translator = DI.Get<ITranslator>();
|
|
translator.OnChangeLanguage += OnTranslate;
|
|
}
|
|
protected override async void OnSet(string value)
|
|
{
|
|
source = value;
|
|
value = so?.GetAt(value);
|
|
await UniTask.SwitchToMainThread();
|
|
output.Set(value);
|
|
}
|
|
public override void OnStop()
|
|
{
|
|
translator.OnChangeLanguage -= OnTranslate;
|
|
}
|
|
void OnTranslate(string langs)
|
|
{
|
|
OnSet(source);
|
|
}
|
|
}
|
|
[System.Serializable]
|
|
public sealed class Event : StringComponent
|
|
{
|
|
public UnityEvent<string> output;
|
|
protected override async void OnSet(string value)
|
|
{
|
|
await UniTask.SwitchToMainThread();
|
|
output.Invoke(value);
|
|
}
|
|
}
|
|
[System.Serializable]
|
|
public sealed class BoolEvent : DataComponents
|
|
{
|
|
public UnityEvent<bool> outputEvent;
|
|
public UnityEvent ifTrueEvent;
|
|
public UnityEvent ifFlaseEvent;
|
|
public override void OnStart()
|
|
{
|
|
Data.AddListener<bool>(key, OnSetBool, true);
|
|
}
|
|
public override void OnStop()
|
|
{
|
|
Data.RemoveListender<bool>(key, OnSetBool);
|
|
}
|
|
async void OnSetBool(bool boolean)
|
|
{
|
|
try
|
|
{
|
|
await UniTask.SwitchToMainThread(BITApp.CancellationToken);
|
|
outputEvent.Invoke(boolean);
|
|
|
|
if (boolean is true)
|
|
ifTrueEvent.Invoke();
|
|
else
|
|
ifFlaseEvent.Invoke();
|
|
}
|
|
catch (System.OperationCanceledException)
|
|
{
|
|
|
|
}
|
|
catch (System.Exception)
|
|
{
|
|
throw;
|
|
}
|
|
|
|
}
|
|
}
|
|
} |