TextMeshProAsyncExtensions.InputField.tt 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <#@ template debug="false" hostspecific="false" language="C#" #>
  2. <#@ assembly name="System.Core" #>
  3. <#@ import namespace="System.Linq" #>
  4. <#@ import namespace="System.Text" #>
  5. <#@ import namespace="System.Collections.Generic" #>
  6. <#@ output extension=".cs" #>
  7. <#
  8. var handlers = new (string name, string type)[] {
  9. ("ValueChanged", "string"),
  10. ("EndEdit", "string"),
  11. ("EndTextSelection", "(string, int, int)"),
  12. ("TextSelection", "(string, int, int)"),
  13. ("Deselect", "string"),
  14. ("Select", "string"),
  15. ("Submit", "string"),
  16. };
  17. Func<string, bool> shouldConvert = x => x.EndsWith("TextSelection");
  18. Func<string, string> eventName = x => shouldConvert(x) ? $"new TextSelectionEventConverter(inputField.on{x})" : $"inputField.on{x}";
  19. #>
  20. #if UNITASK_TEXTMESHPRO_SUPPORT
  21. using System;
  22. using System.Threading;
  23. using TMPro;
  24. namespace Cysharp.Threading.Tasks
  25. {
  26. public static partial class TextMeshProAsyncExtensions
  27. {
  28. <# foreach(var (name, type) in handlers) { #>
  29. public static IAsync<#= (name) #>EventHandler<<#= type #>> GetAsync<#= (name) #>EventHandler(this TMP_InputField inputField)
  30. {
  31. return new AsyncUnityEventHandler<<#= type #>>(<#= eventName(name) #>, inputField.GetCancellationTokenOnDestroy(), false);
  32. }
  33. public static IAsync<#= (name) #>EventHandler<<#= type #>> GetAsync<#= (name) #>EventHandler(this TMP_InputField inputField, CancellationToken cancellationToken)
  34. {
  35. return new AsyncUnityEventHandler<<#= type #>>(<#= eventName(name) #>, cancellationToken, false);
  36. }
  37. public static UniTask<<#= type #>> On<#= (name) #>Async(this TMP_InputField inputField)
  38. {
  39. return new AsyncUnityEventHandler<<#= type #>>(<#= eventName(name) #>, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
  40. }
  41. public static UniTask<<#= type #>> On<#= (name) #>Async(this TMP_InputField inputField, CancellationToken cancellationToken)
  42. {
  43. return new AsyncUnityEventHandler<<#= type #>>(<#= eventName(name) #>, cancellationToken, true).OnInvokeAsync();
  44. }
  45. public static IUniTaskAsyncEnumerable<<#= type #>> On<#= (name) #>AsAsyncEnumerable(this TMP_InputField inputField)
  46. {
  47. return new UnityEventHandlerAsyncEnumerable<<#= type #>>(<#= eventName(name) #>, inputField.GetCancellationTokenOnDestroy());
  48. }
  49. public static IUniTaskAsyncEnumerable<<#= type #>> On<#= (name) #>AsAsyncEnumerable(this TMP_InputField inputField, CancellationToken cancellationToken)
  50. {
  51. return new UnityEventHandlerAsyncEnumerable<<#= type #>>(<#= eventName(name) #>, cancellationToken);
  52. }
  53. <# } #>
  54. }
  55. }
  56. #endif