GProgressBar.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using System;
  2. using UnityEngine;
  3. using FairyGUI.Utils;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. /// GProgressBar class.
  8. /// </summary>
  9. public class GProgressBar : GComponent
  10. {
  11. double _min;
  12. double _max;
  13. double _value;
  14. ProgressTitleType _titleType;
  15. bool _reverse;
  16. GObject _titleObject;
  17. GMovieClip _aniObject;
  18. GObject _barObjectH;
  19. GObject _barObjectV;
  20. float _barMaxWidth;
  21. float _barMaxHeight;
  22. float _barMaxWidthDelta;
  23. float _barMaxHeightDelta;
  24. float _barStartX;
  25. float _barStartY;
  26. public GProgressBar()
  27. {
  28. _value = 50;
  29. _max = 100;
  30. }
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. public ProgressTitleType titleType
  35. {
  36. get
  37. {
  38. return _titleType;
  39. }
  40. set
  41. {
  42. if (_titleType != value)
  43. {
  44. _titleType = value;
  45. Update(_value);
  46. }
  47. }
  48. }
  49. /// <summary>
  50. ///
  51. /// </summary>
  52. public double min
  53. {
  54. get
  55. {
  56. return _min;
  57. }
  58. set
  59. {
  60. if (_min != value)
  61. {
  62. _min = value;
  63. Update(_value);
  64. }
  65. }
  66. }
  67. /// <summary>
  68. ///
  69. /// </summary>
  70. public double max
  71. {
  72. get
  73. {
  74. return _max;
  75. }
  76. set
  77. {
  78. if (_max != value)
  79. {
  80. _max = value;
  81. Update(_value);
  82. }
  83. }
  84. }
  85. /// <summary>
  86. ///
  87. /// </summary>
  88. public double value
  89. {
  90. get
  91. {
  92. return _value;
  93. }
  94. set
  95. {
  96. if (_value != value)
  97. {
  98. GTween.Kill(this, TweenPropType.Progress, false);
  99. _value = value;
  100. Update(_value);
  101. }
  102. }
  103. }
  104. public bool reverse
  105. {
  106. get { return _reverse; }
  107. set { _reverse = value; }
  108. }
  109. /// <summary>
  110. /// 动态改变进度值。
  111. /// </summary>
  112. /// <param name="value"></param>
  113. /// <param name="duration"></param>
  114. public GTweener TweenValue(double value, float duration)
  115. {
  116. double oldValule;
  117. GTweener twener = GTween.GetTween(this, TweenPropType.Progress);
  118. if (twener != null)
  119. {
  120. oldValule = twener.value.d;
  121. twener.Kill(false);
  122. }
  123. else
  124. oldValule = _value;
  125. _value = value;
  126. return GTween.ToDouble(oldValule, _value, duration)
  127. .SetEase(EaseType.Linear)
  128. .SetTarget(this, TweenPropType.Progress);
  129. }
  130. /// <summary>
  131. ///
  132. /// </summary>
  133. /// <param name="newValue"></param>
  134. public void Update(double newValue)
  135. {
  136. float percent = Mathf.Clamp01((float)((newValue - _min) / (_max - _min)));
  137. if (_titleObject != null)
  138. {
  139. switch (_titleType)
  140. {
  141. case ProgressTitleType.Percent:
  142. if (RTLSupport.BaseDirection == RTLSupport.DirectionType.RTL)
  143. _titleObject.text = "%" + Mathf.FloorToInt(percent * 100);
  144. else
  145. _titleObject.text = Mathf.FloorToInt(percent * 100) + "%";
  146. break;
  147. case ProgressTitleType.ValueAndMax:
  148. if (RTLSupport.BaseDirection == RTLSupport.DirectionType.RTL)
  149. _titleObject.text = Math.Round(max) + "/" + Math.Round(newValue);
  150. else
  151. _titleObject.text = Math.Round(newValue) + "/" + Math.Round(max);
  152. break;
  153. case ProgressTitleType.Value:
  154. _titleObject.text = "" + Math.Round(newValue);
  155. break;
  156. case ProgressTitleType.Max:
  157. _titleObject.text = "" + Math.Round(_max);
  158. break;
  159. }
  160. }
  161. float fullWidth = this.width - _barMaxWidthDelta;
  162. float fullHeight = this.height - _barMaxHeightDelta;
  163. if (!_reverse)
  164. {
  165. if (_barObjectH != null)
  166. {
  167. if (!SetFillAmount(_barObjectH, percent))
  168. _barObjectH.width = Mathf.RoundToInt(fullWidth * percent);
  169. }
  170. if (_barObjectV != null)
  171. {
  172. if (!SetFillAmount(_barObjectV, percent))
  173. _barObjectV.height = Mathf.RoundToInt(fullHeight * percent);
  174. }
  175. }
  176. else
  177. {
  178. if (_barObjectH != null)
  179. {
  180. if (!SetFillAmount(_barObjectH, 1 - percent))
  181. {
  182. _barObjectH.width = Mathf.RoundToInt(fullWidth * percent);
  183. _barObjectH.x = _barStartX + (fullWidth - _barObjectH.width);
  184. }
  185. }
  186. if (_barObjectV != null)
  187. {
  188. if (!SetFillAmount(_barObjectV, 1 - percent))
  189. {
  190. _barObjectV.height = Mathf.RoundToInt(fullHeight * percent);
  191. _barObjectV.y = _barStartY + (fullHeight - _barObjectV.height);
  192. }
  193. }
  194. }
  195. if (_aniObject != null)
  196. _aniObject.frame = Mathf.RoundToInt(percent * 100);
  197. InvalidateBatchingState(true);
  198. }
  199. bool SetFillAmount(GObject bar, float amount)
  200. {
  201. if ((bar is GImage) && ((GImage)bar).fillMethod != FillMethod.None)
  202. ((GImage)bar).fillAmount = amount;
  203. else if ((bar is GLoader) && ((GLoader)bar).fillMethod != FillMethod.None)
  204. ((GLoader)bar).fillAmount = amount;
  205. else
  206. return false;
  207. return true;
  208. }
  209. override protected void ConstructExtension(ByteBuffer buffer)
  210. {
  211. buffer.Seek(0, 6);
  212. _titleType = (ProgressTitleType)buffer.ReadByte();
  213. _reverse = buffer.ReadBool();
  214. _titleObject = GetChild("title");
  215. _barObjectH = GetChild("bar");
  216. _barObjectV = GetChild("bar_v");
  217. _aniObject = GetChild("ani") as GMovieClip;
  218. if (_barObjectH != null)
  219. {
  220. _barMaxWidth = _barObjectH.width;
  221. _barMaxWidthDelta = this.width - _barMaxWidth;
  222. _barStartX = _barObjectH.x;
  223. }
  224. if (_barObjectV != null)
  225. {
  226. _barMaxHeight = _barObjectV.height;
  227. _barMaxHeightDelta = this.height - _barMaxHeight;
  228. _barStartY = _barObjectV.y;
  229. }
  230. }
  231. override public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
  232. {
  233. base.Setup_AfterAdd(buffer, beginPos);
  234. if (!buffer.Seek(beginPos, 6))
  235. {
  236. Update(_value);
  237. return;
  238. }
  239. if ((ObjectType)buffer.ReadByte() != packageItem.objectType)
  240. {
  241. Update(_value);
  242. return;
  243. }
  244. _value = buffer.ReadInt();
  245. _max = buffer.ReadInt();
  246. if (buffer.version >= 2)
  247. _min = buffer.ReadInt();
  248. if (buffer.version >= 5)
  249. {
  250. string sound = buffer.ReadS();
  251. if (!string.IsNullOrEmpty(sound))
  252. {
  253. float volumeScale = buffer.ReadFloat();
  254. displayObject.onClick.Add(() =>
  255. {
  256. NAudioClip audioClip = UIPackage.GetItemAssetByURL(sound) as NAudioClip;
  257. if (audioClip != null && audioClip.nativeClip != null)
  258. Stage.inst.PlayOneShotSound(audioClip.nativeClip, volumeScale);
  259. });
  260. }
  261. else
  262. buffer.Skip(4);
  263. }
  264. Update(_value);
  265. }
  266. override protected void HandleSizeChanged()
  267. {
  268. base.HandleSizeChanged();
  269. if (_barObjectH != null)
  270. _barMaxWidth = this.width - _barMaxWidthDelta;
  271. if (_barObjectV != null)
  272. _barMaxHeight = this.height - _barMaxHeightDelta;
  273. if (!this.underConstruct)
  274. Update(_value);
  275. }
  276. }
  277. }