SelectionTextBox.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Bitsplash.DatePicker
  6. {
  7. public class SelectionTextBox : DatePickerText , IDatePickerSettingsItem
  8. {
  9. public string DateFormat = "ddd, MMM d";
  10. public string Seperator = " ... ";
  11. public string NothingSelectedString = "";
  12. public int Order { get { return 8; } }
  13. protected override void SetContent(DatePickerContent content)
  14. {
  15. if (Content != null)
  16. Content.OnSelectionChanged.RemoveListener(SelectionChanged);
  17. base.SetContent(content);
  18. if (Content != null)
  19. {
  20. Content.OnSelectionChanged.AddListener(SelectionChanged);
  21. SelectionChanged();
  22. }
  23. }
  24. void RefreshText()
  25. {
  26. if (Content == null)
  27. return;
  28. try
  29. {
  30. string text = NothingSelectedString;
  31. if (Content.Selection.Count > 0)
  32. {
  33. text = Content.Selection.GetItem(0).ToString(DateFormat);
  34. if (Content.Selection.Count > 1)
  35. {
  36. text += Seperator + Content.Selection.GetItem(Content.Selection.Count - 1).ToString(DateFormat);
  37. }
  38. }
  39. Text = text;
  40. }
  41. catch (Exception)
  42. {
  43. Debug.LogWarning("Invalid date format for text box - " + DateFormat);
  44. }
  45. }
  46. void SelectionChanged()
  47. {
  48. RefreshText();
  49. }
  50. void OnDestroy()
  51. {
  52. if (Content != null)
  53. Content.OnSelectionChanged.RemoveListener(SelectionChanged);
  54. }
  55. public string EditorTitle
  56. {
  57. get { return "Selection Text Box - " + gameObject.name; }
  58. }
  59. }
  60. }