HtmlInput.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using UnityEngine;
  2. namespace FairyGUI.Utils
  3. {
  4. /// <summary>
  5. ///
  6. /// </summary>
  7. public class HtmlInput : IHtmlObject
  8. {
  9. public GTextInput textInput { get; private set; }
  10. RichTextField _owner;
  11. HtmlElement _element;
  12. bool _hidden;
  13. public static int defaultBorderSize = 2;
  14. public static Color defaultBorderColor = ToolSet.ColorFromRGB(0xA9A9A9);
  15. public static Color defaultBackgroundColor = Color.clear;
  16. public HtmlInput()
  17. {
  18. textInput = (GTextInput)UIObjectFactory.NewObject(ObjectType.InputText);
  19. textInput.gameObjectName = "HtmlInput";
  20. textInput.verticalAlign = VertAlignType.Middle;
  21. }
  22. public DisplayObject displayObject
  23. {
  24. get { return textInput.displayObject; }
  25. }
  26. public HtmlElement element
  27. {
  28. get { return _element; }
  29. }
  30. public float width
  31. {
  32. get { return _hidden ? 0 : textInput.width; }
  33. }
  34. public float height
  35. {
  36. get { return _hidden ? 0 : textInput.height; }
  37. }
  38. public void Create(RichTextField owner, HtmlElement element)
  39. {
  40. _owner = owner;
  41. _element = element;
  42. string type = element.GetString("type");
  43. if (type != null)
  44. type = type.ToLower();
  45. _hidden = type == "hidden";
  46. if (!_hidden)
  47. {
  48. int width = element.GetInt("width", 0);
  49. int height = element.GetInt("height", 0);
  50. int borderSize = element.GetInt("border", defaultBorderSize);
  51. Color borderColor = element.GetColor("border-color", defaultBorderColor);
  52. Color backgroundColor = element.GetColor("background-color", defaultBackgroundColor);
  53. if (width == 0)
  54. {
  55. width = element.space;
  56. if (width > _owner.width / 2 || width < 100)
  57. width = (int)_owner.width / 2;
  58. }
  59. if (height == 0)
  60. height = element.format.size + 10;
  61. textInput.textFormat = element.format;
  62. textInput.displayAsPassword = type == "password";
  63. textInput.maxLength = element.GetInt("maxlength", int.MaxValue);
  64. textInput.border = borderSize;
  65. textInput.borderColor = borderColor;
  66. textInput.backgroundColor = backgroundColor;
  67. textInput.SetSize(width, height);
  68. }
  69. textInput.text = element.GetString("value");
  70. }
  71. public void SetPosition(float x, float y)
  72. {
  73. if (!_hidden)
  74. textInput.SetXY(x, y);
  75. }
  76. public void Add()
  77. {
  78. if (!_hidden)
  79. _owner.AddChild(textInput.displayObject);
  80. }
  81. public void Remove()
  82. {
  83. if (!_hidden && textInput.displayObject.parent != null)
  84. _owner.RemoveChild(textInput.displayObject);
  85. }
  86. public void Release()
  87. {
  88. textInput.RemoveEventListeners();
  89. textInput.text = null;
  90. _owner = null;
  91. _element = null;
  92. }
  93. public void Dispose()
  94. {
  95. textInput.Dispose();
  96. }
  97. }
  98. }