HtmlElement.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace FairyGUI.Utils
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public enum HtmlElementType
  10. {
  11. Text,
  12. Link,
  13. Image,
  14. Input,
  15. Select,
  16. Object,
  17. //internal
  18. LinkEnd,
  19. }
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. public class HtmlElement
  24. {
  25. public HtmlElementType type;
  26. public string name;
  27. public string text;
  28. public TextFormat format;
  29. public int charIndex;
  30. public IHtmlObject htmlObject;
  31. public int status; //1 hidden 2 clipped 4 added
  32. public int space;
  33. public Vector2 position;
  34. Hashtable attributes;
  35. public HtmlElement()
  36. {
  37. format = new TextFormat();
  38. }
  39. public object Get(string attrName)
  40. {
  41. if (attributes == null)
  42. return null;
  43. return attributes[attrName];
  44. }
  45. public void Set(string attrName, object attrValue)
  46. {
  47. if (attributes == null)
  48. attributes = new Hashtable();
  49. attributes[attrName] = attrValue;
  50. }
  51. public string GetString(string attrName)
  52. {
  53. return GetString(attrName, null);
  54. }
  55. public string GetString(string attrName, string defValue)
  56. {
  57. if (attributes == null)
  58. return defValue;
  59. object ret = attributes[attrName];
  60. if (ret != null)
  61. return ret.ToString();
  62. else
  63. return defValue;
  64. }
  65. public int GetInt(string attrName)
  66. {
  67. return GetInt(attrName, 0);
  68. }
  69. public int GetInt(string attrName, int defValue)
  70. {
  71. string value = GetString(attrName);
  72. if (value == null || value.Length == 0)
  73. return defValue;
  74. if (value[value.Length - 1] == '%')
  75. {
  76. int ret;
  77. if (int.TryParse(value.Substring(0, value.Length - 1), out ret))
  78. return Mathf.CeilToInt(ret / 100.0f * defValue);
  79. else
  80. return defValue;
  81. }
  82. else
  83. {
  84. int ret;
  85. if (int.TryParse(value, out ret))
  86. return ret;
  87. else
  88. return defValue;
  89. }
  90. }
  91. public float GetFloat(string attrName)
  92. {
  93. return GetFloat(attrName, 0);
  94. }
  95. public float GetFloat(string attrName, float defValue)
  96. {
  97. string value = GetString(attrName);
  98. if (value == null || value.Length == 0)
  99. return defValue;
  100. float ret;
  101. if (float.TryParse(value, out ret))
  102. return ret;
  103. else
  104. return defValue;
  105. }
  106. public bool GetBool(string attrName)
  107. {
  108. return GetBool(attrName, false);
  109. }
  110. public bool GetBool(string attrName, bool defValue)
  111. {
  112. string value = GetString(attrName);
  113. if (value == null || value.Length == 0)
  114. return defValue;
  115. bool ret;
  116. if (bool.TryParse(value, out ret))
  117. return ret;
  118. else
  119. return defValue;
  120. }
  121. public Color GetColor(string attrName, Color defValue)
  122. {
  123. string value = GetString(attrName);
  124. if (value == null || value.Length == 0)
  125. return defValue;
  126. return ToolSet.ConvertFromHtmlColor(value);
  127. }
  128. public void FetchAttributes()
  129. {
  130. attributes = XMLIterator.GetAttributes(attributes);
  131. }
  132. public bool isEntity
  133. {
  134. get { return type == HtmlElementType.Image || type == HtmlElementType.Select || type == HtmlElementType.Input || type == HtmlElementType.Object; }
  135. }
  136. #region Pool Support
  137. static Stack<HtmlElement> elementPool = new Stack<HtmlElement>();
  138. public static HtmlElement GetElement(HtmlElementType type)
  139. {
  140. HtmlElement ret;
  141. if (elementPool.Count > 0)
  142. ret = elementPool.Pop();
  143. else
  144. ret = new HtmlElement();
  145. ret.type = type;
  146. if (type != HtmlElementType.Text && ret.attributes == null)
  147. ret.attributes = new Hashtable();
  148. return ret;
  149. }
  150. public static void ReturnElement(HtmlElement element)
  151. {
  152. element.name = null;
  153. element.text = null;
  154. element.htmlObject = null;
  155. element.status = 0;
  156. if (element.attributes != null)
  157. element.attributes.Clear();
  158. elementPool.Push(element);
  159. }
  160. public static void ReturnElements(List<HtmlElement> elements)
  161. {
  162. int count = elements.Count;
  163. for (int i = 0; i < count; i++)
  164. {
  165. HtmlElement element = elements[i];
  166. ReturnElement(element);
  167. }
  168. elements.Clear();
  169. }
  170. #endregion
  171. }
  172. }