UEGauge.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. using CommonLang.Xml;
  2. using CommonUnity3D.UGUI;
  3. using System;
  4. using System.Xml;
  5. using UnityEngine;
  6. using CommonUI.Data;
  7. using TextAnchor = CommonUI.Data.TextAnchor;
  8. using FontStyle = CommonUI.Data.FontStyle;
  9. using CommonLang.Property;
  10. namespace CommonUnity3D.UGUIEditor.UI
  11. {
  12. public class UEGauge : UIComponent
  13. {
  14. protected readonly UIComponent mStrip;
  15. protected DisplayText mTextSprite;
  16. private double mGaugeMinValue = 0, mGaugeMaxValue = 100, mGaugeValue = 50;
  17. private GaugeOrientation mOrientation = GaugeOrientation.LEFT_2_RIGHT;
  18. private string mText = "";
  19. private bool mIsShowPercent = false;
  20. protected readonly bool mUseBitmapFont;
  21. public UEGauge(bool use_bitmap)
  22. {
  23. this.mUseBitmapFont = use_bitmap;
  24. this.mStrip = new UIComponent("strip");
  25. this.mStrip.Layout = UILayout.CreateUILayoutColor(new Color(1f, 1f, 1f, 0.5f), Color.black);
  26. this.mStrip.Enable = false;
  27. this.mStrip.EnableChildren = false;
  28. this.AddChild(mStrip);
  29. this.Enable = false;
  30. this.EnableChildren = false;
  31. }
  32. public UEGauge() : this(UIEditor.GlobalUseBitmapText)
  33. {
  34. }
  35. public UIComponent Strip
  36. {
  37. get { return mStrip; }
  38. }
  39. public UILayout StripLayout
  40. {
  41. get { return mStrip.Layout; }
  42. set { mStrip.Layout = value; ; }
  43. }
  44. public DisplayText TextSprite
  45. {
  46. get { return mTextSprite; }
  47. }
  48. public GaugeOrientation Orientation
  49. {
  50. get { return mOrientation; }
  51. set
  52. {
  53. mOrientation = value;
  54. ChangeFillMode(this.mOrientation);
  55. }
  56. }
  57. public double Value
  58. {
  59. get { return mGaugeValue; }
  60. set
  61. {
  62. if (value > mGaugeMaxValue) throw new Exception(string.Format("Gauge value [{0}] out of range [{1}-{2}]", value, mGaugeMinValue, mGaugeMaxValue));
  63. if (value < mGaugeMinValue) throw new Exception(string.Format("Gauge value [{0}] out of range [{1}-{2}]", value, mGaugeMinValue, mGaugeMaxValue));
  64. if (mGaugeValue != value)
  65. {
  66. mGaugeValue = value;
  67. DoValueChanged();
  68. }
  69. }
  70. }
  71. public float ValuePercent
  72. {
  73. get
  74. {
  75. if (mGaugeMaxValue - mGaugeMinValue == 0) { return 0; }
  76. else { return (float)((mGaugeValue - mGaugeMinValue) / (mGaugeMaxValue - mGaugeMinValue) * 100f); }
  77. }
  78. set
  79. {
  80. if (value < 0 || value > 100) throw new Exception(string.Format("Gauge ValuePercent [{0}] out of range [0-100]", value));
  81. Value = mGaugeMinValue + (mGaugeMaxValue - mGaugeMinValue) * value * 0.01;
  82. }
  83. }
  84. public bool IsShowPercent
  85. {
  86. get { return mIsShowPercent; }
  87. set
  88. {
  89. if (mIsShowPercent != value)
  90. {
  91. mIsShowPercent = value;
  92. DoTextChanged();
  93. }
  94. }
  95. }
  96. //-----------------------------------------------------------------------------------------------
  97. public string Text
  98. {
  99. get { return mText; }
  100. set
  101. {
  102. if (IsDispose) return;
  103. if (!string.Equals(mText, value))
  104. {
  105. mText = value;
  106. DoTextChanged();
  107. }
  108. }
  109. }
  110. public int FontSize
  111. {
  112. get { return mTextSprite.FontSize; }
  113. set { mTextSprite.FontSize = value; }
  114. }
  115. public UnityEngine.Color FontColor
  116. {
  117. get { return mTextSprite.FontColor; }
  118. set { mTextSprite.FontColor = value; }
  119. }
  120. public TextAnchor EditTextAnchor
  121. {
  122. get { return mTextSprite.Anchor; }
  123. set { mTextSprite.Anchor = value; }
  124. }
  125. public Vector2 TextOffset
  126. {
  127. get { return mTextSprite.TextOffset; }
  128. set { mTextSprite.TextOffset = value; }
  129. }
  130. //-----------------------------------------------------------------------------------------------
  131. public double GaugeMinValue
  132. {
  133. get { return mGaugeMinValue; }
  134. }
  135. public double GaugeMaxValue
  136. {
  137. get { return mGaugeMaxValue; }
  138. }
  139. public void SetGaugeMinMax(double min, double max)
  140. {
  141. if (min > max) CommonLang.CUtils.Swap<double>(ref min, ref max);
  142. bool needDoChange = (mGaugeMinValue != min) || (mGaugeMaxValue != min);
  143. mGaugeMaxValue = max;
  144. mGaugeMinValue = min;
  145. double value = this.Value;
  146. value = Math.Max(min, value);
  147. value = Math.Min(max, value);
  148. if (needDoChange && value == this.Value)
  149. {
  150. DoValueChanged();
  151. }
  152. this.Value = value;
  153. }
  154. /// <summary>
  155. /// 设置填充模式.
  156. /// EG.SetFillMode(UnityEngine.UI.Image.FillMethod.Radial360,
  157. /// (int)UnityEngine.UI.Image.Origin360.Left)
  158. /// </summary>
  159. /// <param name="fill"></param>
  160. /// <param name="fillOrigin"></param>
  161. /// <param name="fillClockwise"></param>
  162. /// <param name="fillCenter"></param>
  163. public void SetFillMode(UnityEngine.UI.Image.FillMethod fill, int fillOrigin, bool fillClockwise = false, bool fillCenter = true)
  164. {
  165. if (mStrip != null && mStrip.Graphics != null)
  166. {
  167. mStrip.Graphics.SetFillMode(fill, fillOrigin, fillClockwise, fillCenter);
  168. }
  169. }
  170. private void ChangeFillMode(GaugeOrientation Orientation)
  171. {
  172. if (mStrip != null && mStrip.Layout != null)
  173. {
  174. if (mStrip.Layout.Style == UILayoutStyle.IMAGE_STYLE_BACK_4_CENTER ||
  175. mStrip.Layout.Style == UILayoutStyle.IMAGE_STYLE_BACK_4)
  176. {
  177. switch (Orientation)
  178. {
  179. case GaugeOrientation.BOTTOM_2_TOP:
  180. this.SetFillMode(UnityEngine.UI.Image.FillMethod.Vertical,
  181. (int)UnityEngine.UI.Image.OriginVertical.Bottom);
  182. break;
  183. case GaugeOrientation.TOP_2_BOTTOM:
  184. this.SetFillMode(UnityEngine.UI.Image.FillMethod.Vertical,
  185. (int)UnityEngine.UI.Image.OriginVertical.Top);
  186. break;
  187. case GaugeOrientation.LEFT_2_RIGHT:
  188. this.SetFillMode(UnityEngine.UI.Image.FillMethod.Horizontal,
  189. (int)UnityEngine.UI.Image.OriginHorizontal.Left);
  190. break;
  191. case GaugeOrientation.RIGTH_2_LEFT:
  192. this.SetFillMode(UnityEngine.UI.Image.FillMethod.Horizontal,
  193. (int)UnityEngine.UI.Image.OriginHorizontal.Right);
  194. break;
  195. }
  196. }
  197. }
  198. }
  199. //-----------------------------------------------------------------------------------------------
  200. protected override void OnStart()
  201. {
  202. if (mTextSprite == null)
  203. {
  204. if (mUseBitmapFont)
  205. {
  206. mTextSprite = new BitmapTextSprite("bitmap_text");
  207. }
  208. else
  209. {
  210. mTextSprite = new TextSprite("bitmap_text");
  211. }
  212. }
  213. base.OnStart();
  214. this.DoValueChanged();
  215. }
  216. protected override void OnUpdate()
  217. {
  218. base.OnUpdate();
  219. if (mStrip.Graphics.IsShowUILayout)
  220. {
  221. UIUtils.AdjustGaugeOrientation(Orientation, this, mStrip, ValuePercent);
  222. }
  223. else
  224. {
  225. mStrip.Position2D = Vector2.zero;
  226. mStrip.Size2D = this.Size2D;
  227. }
  228. if (mTextSprite != null)
  229. {
  230. mTextSprite.Size2D = this.Size2D;
  231. }
  232. }
  233. protected override void DecodeEnd(UIEditor.Decoder editor, UIComponentMeta e)
  234. {
  235. base.DecodeEnd(editor, e);
  236. {
  237. if (!string.IsNullOrEmpty((e as UEGaugeMeta).ImageFont))
  238. {
  239. this.Decode_ImageFont(editor, e as UEGaugeMeta);
  240. }
  241. else if (mUseBitmapFont)
  242. {
  243. this.Decode_BitmapText(editor, e as UEGaugeMeta);
  244. }
  245. else
  246. {
  247. this.Decode_Text(editor, e as UEGaugeMeta);
  248. }
  249. }
  250. this.Decode_Gauge(editor, e as UEGaugeMeta);
  251. this.Enable = false;
  252. this.EnableChildren = false;
  253. }
  254. private void Decode_Gauge(UIEditor.Decoder editor, UEGaugeMeta e)
  255. {
  256. this.Text = e.text;
  257. this.mGaugeMaxValue = e.gaugeMax;
  258. this.mGaugeMinValue = e.gaugeMin;
  259. this.mGaugeValue = e.gaugeValue;
  260. this.Orientation = e.render_orientation;
  261. this.IsShowPercent = e.showPercent;
  262. UILayout custom_layout_up = editor.CreateLayout(e.custom_layout_up);
  263. if (custom_layout_up != null)
  264. {
  265. mStrip.Layout = custom_layout_up;
  266. ChangeFillMode(this.Orientation);
  267. }
  268. mTextSprite.Text = this.Text;
  269. mTextSprite.Size2D = this.Size2D;
  270. mTextSprite.Anchor = e.text_anchor;
  271. mTextSprite.TextOffset = new Vector2(e.text_offset_x, e.text_offset_y);
  272. }
  273. private void Decode_Text(UIEditor.Decoder editor, UETextComponentMeta e)
  274. {
  275. var text = new TextSprite("text");
  276. this.mTextSprite = text;
  277. this.AddChild(mTextSprite);
  278. if (!string.IsNullOrEmpty(e.textFontName))
  279. {
  280. text.SetTextFont(editor.editor.CreateFont(e.textFontName), this.FontSize, (UnityEngine.FontStyle)e.textFontStyle);
  281. }
  282. else
  283. {
  284. text.SetTextFont(editor.editor.CreateFont(null), editor.editor.DefaultFont.fontSize, UnityEngine.FontStyle.Normal);
  285. }
  286. if (e.textFontSize > 0)
  287. {
  288. text.FontSize = e.textFontSize;
  289. text.Graphics.resizeTextForBestFit = false;
  290. }
  291. else
  292. {
  293. text.Graphics.resizeTextForBestFit = true;
  294. }
  295. text.FontColor = UIUtils.UInt32_ARGB_To_Color(e.textColor);
  296. if (e.textBorderAlpha > 0)
  297. {
  298. Color border_color = UIUtils.UInt32_ARGB_To_Color(e.textBorderColor);
  299. border_color.a = e.textBorderAlpha / 100f;
  300. text.AddBorder(border_color, new Vector2(1, -1));
  301. }
  302. }
  303. private void Decode_BitmapText(UIEditor.Decoder editor, UETextComponentMeta e)
  304. {
  305. var text = new BitmapTextSprite("bitmap_text");
  306. this.mTextSprite = text;
  307. this.AddChild(mTextSprite);
  308. text.Graphics.Style = e.textFontStyle;
  309. text.Graphics.FontColor = UIUtils.UInt32_ARGB_To_Color(e.textColor);
  310. if (e.textFontSize > 0)
  311. {
  312. text.FontSize = e.textFontSize;
  313. }
  314. if (e.textBorderAlpha > 0)
  315. {
  316. Color border_color = UIUtils.UInt32_ARGB_To_Color(e.textBorderColor);
  317. border_color.a = e.textBorderAlpha / 100f;
  318. text.Graphics.BorderTime = TextBorderCount.Border;
  319. text.Graphics.BorderColor = border_color;
  320. }
  321. }
  322. private void Decode_ImageFont(UIEditor.Decoder editor, UETextComponentMeta e)
  323. {
  324. var text = new ImageFontSprite("image_font");
  325. this.mTextSprite = text;
  326. this.AddChild(mTextSprite);
  327. editor.editor.ParseImageFont(e.ImageFont, e.text, text.Graphics);
  328. }
  329. private void DoTextChanged()
  330. {
  331. string text = this.Text;
  332. if (IsShowPercent)
  333. {
  334. text = string.Format("{0}%", ((int)ValuePercent));
  335. mTextSprite.Text = text;
  336. }
  337. else
  338. {
  339. mTextSprite.Text = text;
  340. }
  341. }
  342. private void DoValueChanged()
  343. {
  344. string text = this.Text;
  345. if (IsShowPercent)
  346. {
  347. text = string.Format("{0}%", ((int)ValuePercent));
  348. mTextSprite.Text = text;
  349. }
  350. else
  351. {
  352. mTextSprite.Text = text;
  353. }
  354. mStrip.Graphics.SetFillPercent(ValuePercent);
  355. if (event_ValueChanged != null)
  356. {
  357. event_ValueChanged.Invoke(this, mGaugeValue);
  358. }
  359. }
  360. protected override void OnDisposeEvents()
  361. {
  362. base.OnDisposeEvents();
  363. event_ValueChanged = null;
  364. }
  365. public delegate void ValueChangedHandler(UEGauge sender, double value);
  366. [Desc("值改变")]
  367. public ValueChangedHandler event_ValueChanged;
  368. [Desc("值改变")]
  369. public event ValueChangedHandler ValueChanged { add { event_ValueChanged += value; } remove { event_ValueChanged -= value; } }
  370. }
  371. }