GLabel.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using UnityEngine;
  2. using FairyGUI.Utils;
  3. namespace FairyGUI
  4. {
  5. /// <summary>
  6. /// GLabel class.
  7. /// </summary>
  8. public class GLabel : GComponent, IColorGear
  9. {
  10. protected GObject _titleObject;
  11. protected GObject _iconObject;
  12. public GLabel()
  13. {
  14. }
  15. /// <summary>
  16. /// Icon of the label.
  17. /// </summary>
  18. override public string icon
  19. {
  20. get
  21. {
  22. if (_iconObject != null)
  23. return _iconObject.icon;
  24. else
  25. return null;
  26. }
  27. set
  28. {
  29. if (_iconObject != null)
  30. _iconObject.icon = value;
  31. UpdateGear(7);
  32. }
  33. }
  34. /// <summary>
  35. /// Title of the label.
  36. /// </summary>
  37. public string title
  38. {
  39. get
  40. {
  41. if (_titleObject != null)
  42. return _titleObject.text;
  43. else
  44. return null;
  45. }
  46. set
  47. {
  48. if (_titleObject != null)
  49. _titleObject.text = value;
  50. UpdateGear(6);
  51. }
  52. }
  53. /// <summary>
  54. /// Same of the title.
  55. /// </summary>
  56. override public string text
  57. {
  58. get { return this.title; }
  59. set { this.title = value; }
  60. }
  61. /// <summary>
  62. /// If title is input text.
  63. /// </summary>
  64. public bool editable
  65. {
  66. get
  67. {
  68. if (_titleObject is GTextInput)
  69. return _titleObject.asTextInput.editable;
  70. else
  71. return false;
  72. }
  73. set
  74. {
  75. if (_titleObject is GTextInput)
  76. _titleObject.asTextInput.editable = value;
  77. }
  78. }
  79. /// <summary>
  80. /// Title color of the label
  81. /// </summary>
  82. public Color titleColor
  83. {
  84. get
  85. {
  86. GTextField tf = GetTextField();
  87. if (tf != null)
  88. return tf.color;
  89. else
  90. return Color.black;
  91. }
  92. set
  93. {
  94. GTextField tf = GetTextField();
  95. if (tf != null)
  96. {
  97. tf.color = value;
  98. UpdateGear(4);
  99. }
  100. }
  101. }
  102. /// <summary>
  103. ///
  104. /// </summary>
  105. public int titleFontSize
  106. {
  107. get
  108. {
  109. GTextField tf = GetTextField();
  110. if (tf != null)
  111. return tf.textFormat.size;
  112. else
  113. return 0;
  114. }
  115. set
  116. {
  117. GTextField tf = GetTextField();
  118. if (tf != null)
  119. {
  120. TextFormat format = tf.textFormat;
  121. format.size = value;
  122. tf.textFormat = format;
  123. }
  124. }
  125. }
  126. /// <summary>
  127. ///
  128. /// </summary>
  129. public Color color
  130. {
  131. get { return this.titleColor; }
  132. set { this.titleColor = value; }
  133. }
  134. /// <summary>
  135. ///
  136. /// </summary>
  137. /// <returns></returns>
  138. public GTextField GetTextField()
  139. {
  140. if (_titleObject is GTextField)
  141. return (GTextField)_titleObject;
  142. else if (_titleObject is GLabel)
  143. return ((GLabel)_titleObject).GetTextField();
  144. else if (_titleObject is GButton)
  145. return ((GButton)_titleObject).GetTextField();
  146. else
  147. return null;
  148. }
  149. override protected void ConstructExtension(ByteBuffer buffer)
  150. {
  151. _titleObject = GetChild("title");
  152. _iconObject = GetChild("icon");
  153. }
  154. override public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
  155. {
  156. base.Setup_AfterAdd(buffer, beginPos);
  157. if (!buffer.Seek(beginPos, 6))
  158. return;
  159. if ((ObjectType)buffer.ReadByte() != packageItem.objectType)
  160. return;
  161. string str;
  162. str = buffer.ReadS();
  163. if (str != null)
  164. this.title = str;
  165. str = buffer.ReadS();
  166. if (str != null)
  167. this.icon = str;
  168. if (buffer.ReadBool())
  169. this.titleColor = buffer.ReadColor();
  170. int iv = buffer.ReadInt();
  171. if (iv != 0)
  172. this.titleFontSize = iv;
  173. if (buffer.ReadBool())
  174. {
  175. GTextInput input = GetTextField() as GTextInput;
  176. if (input != null)
  177. {
  178. str = buffer.ReadS();
  179. if (str != null)
  180. input.promptText = str;
  181. str = buffer.ReadS();
  182. if (str != null)
  183. input.restrict = str;
  184. iv = buffer.ReadInt();
  185. if (iv != 0)
  186. input.maxLength = iv;
  187. iv = buffer.ReadInt();
  188. if (iv != 0)
  189. input.keyboardType = iv;
  190. if (buffer.ReadBool())
  191. input.displayAsPassword = true;
  192. }
  193. else
  194. buffer.Skip(13);
  195. }
  196. if (buffer.version >= 5)
  197. {
  198. string sound = buffer.ReadS();
  199. if (!string.IsNullOrEmpty(sound))
  200. {
  201. float volumeScale = buffer.ReadFloat();
  202. displayObject.onClick.Add(() =>
  203. {
  204. NAudioClip audioClip = UIPackage.GetItemAssetByURL(sound) as NAudioClip;
  205. if (audioClip != null && audioClip.nativeClip != null)
  206. Stage.inst.PlayOneShotSound(audioClip.nativeClip, volumeScale);
  207. });
  208. }
  209. else
  210. buffer.Skip(4);
  211. }
  212. }
  213. }
  214. }