HtmlSelect.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace FairyGUI.Utils
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class HtmlSelect : IHtmlObject
  10. {
  11. public GComboBox comboBox { get; private set; }
  12. public const string CHANGED_EVENT = "OnHtmlSelectChanged";
  13. public static string resource;
  14. RichTextField _owner;
  15. HtmlElement _element;
  16. EventCallback0 _changeHandler;
  17. public HtmlSelect()
  18. {
  19. if (resource != null)
  20. {
  21. comboBox = UIPackage.CreateObjectFromURL(resource).asComboBox;
  22. _changeHandler = () =>
  23. {
  24. _owner.DispatchEvent(CHANGED_EVENT, null, this);
  25. };
  26. }
  27. else
  28. Debug.LogWarning("FairyGUI: Set HtmlSelect.resource first");
  29. }
  30. public DisplayObject displayObject
  31. {
  32. get { return comboBox.displayObject; }
  33. }
  34. public HtmlElement element
  35. {
  36. get { return _element; }
  37. }
  38. public float width
  39. {
  40. get { return comboBox != null ? comboBox.width : 0; }
  41. }
  42. public float height
  43. {
  44. get { return comboBox != null ? comboBox.height : 0; }
  45. }
  46. public void Create(RichTextField owner, HtmlElement element)
  47. {
  48. _owner = owner;
  49. _element = element;
  50. if (comboBox == null)
  51. return;
  52. comboBox.onChanged.Add(_changeHandler);
  53. int width = element.GetInt("width", comboBox.sourceWidth);
  54. int height = element.GetInt("height", comboBox.sourceHeight);
  55. comboBox.SetSize(width, height);
  56. comboBox.items = (string[])element.Get("items");
  57. comboBox.values = (string[])element.Get("values");
  58. comboBox.value = element.GetString("value");
  59. }
  60. public void SetPosition(float x, float y)
  61. {
  62. if (comboBox != null)
  63. comboBox.SetXY(x, y);
  64. }
  65. public void Add()
  66. {
  67. if (comboBox != null)
  68. _owner.AddChild(comboBox.displayObject);
  69. }
  70. public void Remove()
  71. {
  72. if (comboBox != null && comboBox.displayObject.parent != null)
  73. _owner.RemoveChild(comboBox.displayObject);
  74. }
  75. public void Release()
  76. {
  77. if (comboBox != null)
  78. comboBox.RemoveEventListeners();
  79. _owner = null;
  80. _element = null;
  81. }
  82. public void Dispose()
  83. {
  84. if (comboBox != null)
  85. comboBox.Dispose();
  86. }
  87. }
  88. }