GButton.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. using UnityEngine;
  2. using FairyGUI.Utils;
  3. namespace FairyGUI
  4. {
  5. /// <summary>
  6. /// GButton class.
  7. /// </summary>
  8. public class GButton : GComponent, IColorGear
  9. {
  10. /// <summary>
  11. /// Play sound when button is clicked.
  12. /// </summary>
  13. public NAudioClip sound;
  14. /// <summary>
  15. /// Volume of the click sound. (0-1)
  16. /// </summary>
  17. public float soundVolumeScale;
  18. /// <summary>
  19. /// For radio or checkbox. if false, the button will not change selected status on click. Default is true.
  20. /// 如果为true,对于单选和多选按钮,当玩家点击时,按钮会自动切换状态。设置为false,则不会。默认为true。
  21. /// </summary>
  22. public bool changeStateOnClick;
  23. /// <summary>
  24. /// Show a popup on click.
  25. /// 可以为按钮设置一个关联的组件,当按钮被点击时,此组件被自动弹出。
  26. /// </summary>
  27. public GObject linkedPopup;
  28. protected GObject _titleObject;
  29. protected GObject _iconObject;
  30. protected Controller _relatedController;
  31. protected string _relatedPageId;
  32. ButtonMode _mode;
  33. bool _selected;
  34. string _title;
  35. string _icon;
  36. string _selectedTitle;
  37. string _selectedIcon;
  38. Controller _buttonController;
  39. int _downEffect;
  40. float _downEffectValue;
  41. bool _downScaled;
  42. bool _down;
  43. bool _over;
  44. EventListener _onChanged;
  45. public const string UP = "up";
  46. public const string DOWN = "down";
  47. public const string OVER = "over";
  48. public const string SELECTED_OVER = "selectedOver";
  49. public const string DISABLED = "disabled";
  50. public const string SELECTED_DISABLED = "selectedDisabled";
  51. public GButton()
  52. {
  53. sound = UIConfig.buttonSound;
  54. soundVolumeScale = UIConfig.buttonSoundVolumeScale;
  55. changeStateOnClick = true;
  56. _downEffectValue = 0.8f;
  57. _title = string.Empty;
  58. }
  59. /// <summary>
  60. /// Dispatched when the button status was changed.
  61. /// 如果为单选或多选按钮,当按钮的选中状态发生改变时,此事件触发。
  62. /// </summary>
  63. public EventListener onChanged
  64. {
  65. get { return _onChanged ?? (_onChanged = new EventListener(this, "onChanged")); }
  66. }
  67. /// <summary>
  68. /// Icon of the button.
  69. /// </summary>
  70. override public string icon
  71. {
  72. get
  73. {
  74. return _icon;
  75. }
  76. set
  77. {
  78. _icon = value;
  79. value = (_selected && _selectedIcon != null) ? _selectedIcon : _icon;
  80. if (_iconObject != null)
  81. _iconObject.icon = value;
  82. UpdateGear(7);
  83. }
  84. }
  85. /// <summary>
  86. /// Title of the button
  87. /// </summary>
  88. public string title
  89. {
  90. get
  91. {
  92. return _title;
  93. }
  94. set
  95. {
  96. _title = value;
  97. if (_titleObject != null)
  98. _titleObject.text = (_selected && _selectedTitle != null) ? _selectedTitle : _title;
  99. UpdateGear(6);
  100. }
  101. }
  102. /// <summary>
  103. /// Same of the title.
  104. /// </summary>
  105. override public string text
  106. {
  107. get { return this.title; }
  108. set { this.title = value; }
  109. }
  110. /// <summary>
  111. /// Icon value on selected status.
  112. /// </summary>
  113. public string selectedIcon
  114. {
  115. get
  116. {
  117. return _selectedIcon;
  118. }
  119. set
  120. {
  121. _selectedIcon = value;
  122. value = (_selected && _selectedIcon != null) ? _selectedIcon : _icon;
  123. if (_iconObject != null)
  124. _iconObject.icon = value;
  125. }
  126. }
  127. /// <summary>
  128. /// Title value on selected status.
  129. /// </summary>
  130. public string selectedTitle
  131. {
  132. get
  133. {
  134. return _selectedTitle;
  135. }
  136. set
  137. {
  138. _selectedTitle = value;
  139. if (_titleObject != null)
  140. _titleObject.text = (_selected && _selectedTitle != null) ? _selectedTitle : _title;
  141. }
  142. }
  143. /// <summary>
  144. /// Title color.
  145. /// </summary>
  146. public Color titleColor
  147. {
  148. get
  149. {
  150. GTextField tf = GetTextField();
  151. if (tf != null)
  152. return tf.color;
  153. else
  154. return Color.black;
  155. }
  156. set
  157. {
  158. GTextField tf = GetTextField();
  159. if (tf != null)
  160. {
  161. tf.color = value;
  162. UpdateGear(4);
  163. }
  164. }
  165. }
  166. /// <summary>
  167. ///
  168. /// </summary>
  169. public Color color
  170. {
  171. get { return this.titleColor; }
  172. set { this.titleColor = value; }
  173. }
  174. /// <summary>
  175. ///
  176. /// </summary>
  177. public int titleFontSize
  178. {
  179. get
  180. {
  181. GTextField tf = GetTextField();
  182. if (tf != null)
  183. return tf.textFormat.size;
  184. else
  185. return 0;
  186. }
  187. set
  188. {
  189. GTextField tf = GetTextField();
  190. if (tf != null)
  191. {
  192. TextFormat format = tf.textFormat;
  193. format.size = value;
  194. tf.textFormat = format;
  195. }
  196. }
  197. }
  198. /// <summary>
  199. /// If the button is in selected status.
  200. /// </summary>
  201. public bool selected
  202. {
  203. get
  204. {
  205. return _selected;
  206. }
  207. set
  208. {
  209. if (_mode == ButtonMode.Common)
  210. return;
  211. if (_selected != value)
  212. {
  213. _selected = value;
  214. SetCurrentState();
  215. if (_selectedTitle != null && _titleObject != null)
  216. _titleObject.text = _selected ? _selectedTitle : _title;
  217. if (_selectedIcon != null)
  218. {
  219. string str = _selected ? _selectedIcon : _icon;
  220. if (_iconObject != null)
  221. _iconObject.icon = str;
  222. }
  223. if (_relatedController != null
  224. && parent != null
  225. && !parent._buildingDisplayList)
  226. {
  227. if (_selected)
  228. {
  229. _relatedController.selectedPageId = _relatedPageId;
  230. if (_relatedController.autoRadioGroupDepth)
  231. parent.AdjustRadioGroupDepth(this, _relatedController);
  232. }
  233. else if (_mode == ButtonMode.Check && _relatedController.selectedPageId == _relatedPageId)
  234. _relatedController.oppositePageId = _relatedPageId;
  235. }
  236. }
  237. }
  238. }
  239. /// <summary>
  240. /// Button mode.
  241. /// </summary>
  242. /// <seealso cref="ButtonMode"/>
  243. public ButtonMode mode
  244. {
  245. get
  246. {
  247. return _mode;
  248. }
  249. set
  250. {
  251. if (_mode != value)
  252. {
  253. if (value == ButtonMode.Common)
  254. this.selected = false;
  255. _mode = value;
  256. }
  257. }
  258. }
  259. /// <summary>
  260. /// A controller is connected to this button, the activate page of this controller will change while the button status changed.
  261. /// 对应编辑器中的单选控制器。
  262. /// </summary>
  263. public Controller relatedController
  264. {
  265. get
  266. {
  267. return _relatedController;
  268. }
  269. set
  270. {
  271. if (value != _relatedController)
  272. {
  273. _relatedController = value;
  274. _relatedPageId = null;
  275. }
  276. }
  277. }
  278. /// <summary>
  279. ///
  280. /// </summary>
  281. public string relatedPageId
  282. {
  283. get
  284. {
  285. return _relatedPageId;
  286. }
  287. set
  288. {
  289. _relatedPageId = value;
  290. }
  291. }
  292. /// <summary>
  293. /// Simulates a click on this button.
  294. /// 模拟点击这个按钮。
  295. /// </summary>
  296. /// <param name="downEffect">If the down effect will simulate too.</param>
  297. public void FireClick(bool downEffect, bool clickCall = false)
  298. {
  299. if (downEffect && _mode == ButtonMode.Common)
  300. {
  301. SetState(OVER);
  302. Timers.inst.Add(0.1f, 1, (object param) => { SetState(DOWN); });
  303. Timers.inst.Add(0.2f, 1,
  304. (object param) =>
  305. {
  306. SetState(UP);
  307. if (clickCall)
  308. {
  309. onClick.Call();
  310. }
  311. }
  312. );
  313. }
  314. else
  315. {
  316. if (clickCall)
  317. {
  318. onClick.Call();
  319. }
  320. }
  321. __click();
  322. }
  323. /// <summary>
  324. ///
  325. /// </summary>
  326. /// <returns></returns>
  327. public GTextField GetTextField()
  328. {
  329. if (_titleObject is GTextField)
  330. return (GTextField)_titleObject;
  331. else if (_titleObject is GLabel)
  332. return ((GLabel)_titleObject).GetTextField();
  333. else if (_titleObject is GButton)
  334. return ((GButton)_titleObject).GetTextField();
  335. else
  336. return null;
  337. }
  338. protected void SetState(string val)
  339. {
  340. if (_buttonController != null)
  341. _buttonController.selectedPage = val;
  342. if (_downEffect == 1)
  343. {
  344. int cnt = this.numChildren;
  345. if (val == DOWN || val == SELECTED_OVER || val == SELECTED_DISABLED)
  346. {
  347. Color color = new Color(_downEffectValue, _downEffectValue, _downEffectValue);
  348. for (int i = 0; i < cnt; i++)
  349. {
  350. GObject obj = this.GetChildAt(i);
  351. if ((obj is IColorGear) && !(obj is GTextField))
  352. ((IColorGear)obj).color = color;
  353. }
  354. }
  355. else
  356. {
  357. for (int i = 0; i < cnt; i++)
  358. {
  359. GObject obj = this.GetChildAt(i);
  360. if ((obj is IColorGear) && !(obj is GTextField))
  361. ((IColorGear)obj).color = Color.white;
  362. }
  363. }
  364. }
  365. else if (_downEffect == 2)
  366. {
  367. if (val == DOWN || val == SELECTED_OVER || val == SELECTED_DISABLED)
  368. {
  369. if (!_downScaled)
  370. {
  371. _downScaled = true;
  372. SetScale(this.scaleX * _downEffectValue, this.scaleY * _downEffectValue);
  373. }
  374. }
  375. else
  376. {
  377. if (_downScaled)
  378. {
  379. _downScaled = false;
  380. SetScale(this.scaleX / _downEffectValue, this.scaleY / _downEffectValue);
  381. }
  382. }
  383. }
  384. }
  385. protected void SetCurrentState()
  386. {
  387. if (this.grayed && _buttonController != null && _buttonController.HasPage(DISABLED))
  388. {
  389. if (_selected)
  390. SetState(SELECTED_DISABLED);
  391. else
  392. SetState(DISABLED);
  393. }
  394. else
  395. {
  396. if (_selected)
  397. SetState(_over ? SELECTED_OVER : DOWN);
  398. else
  399. SetState(_over ? OVER : UP);
  400. }
  401. }
  402. override public void HandleControllerChanged(Controller c)
  403. {
  404. base.HandleControllerChanged(c);
  405. if (_relatedController == c)
  406. this.selected = _relatedPageId == c.selectedPageId;
  407. }
  408. override protected void HandleGrayedChanged()
  409. {
  410. if (_buttonController != null && _buttonController.HasPage(DISABLED))
  411. {
  412. if (this.grayed)
  413. {
  414. if (_selected)
  415. SetState(SELECTED_DISABLED);
  416. else
  417. SetState(DISABLED);
  418. }
  419. else
  420. {
  421. if (_selected)
  422. SetState(DOWN);
  423. else
  424. SetState(UP);
  425. }
  426. }
  427. else
  428. base.HandleGrayedChanged();
  429. }
  430. override protected void ConstructExtension(ByteBuffer buffer)
  431. {
  432. buffer.Seek(0, 6);
  433. _mode = (ButtonMode)buffer.ReadByte();
  434. string str = buffer.ReadS();
  435. if (str != null)
  436. sound = UIPackage.GetItemAssetByURL(str) as NAudioClip;
  437. soundVolumeScale = buffer.ReadFloat();
  438. _downEffect = buffer.ReadByte();
  439. _downEffectValue = buffer.ReadFloat();
  440. if (_downEffect == 2)
  441. SetPivot(0.5f, 0.5f, this.pivotAsAnchor);
  442. _buttonController = GetController("button");
  443. _titleObject = GetChild("title");
  444. _iconObject = GetChild("icon");
  445. if (_titleObject != null)
  446. _title = _titleObject.text;
  447. if (_iconObject != null)
  448. _icon = _iconObject.icon;
  449. if (_mode == ButtonMode.Common)
  450. SetState(UP);
  451. displayObject.onRollOver.Add(__rollover);
  452. displayObject.onRollOut.Add(__rollout);
  453. displayObject.onTouchBegin.Add(__touchBegin);
  454. displayObject.onTouchEnd.Add(__touchEnd);
  455. displayObject.onRemovedFromStage.Add(__removedFromStage);
  456. displayObject.onClick.Add(__click);
  457. }
  458. override public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
  459. {
  460. base.Setup_AfterAdd(buffer, beginPos);
  461. if (!buffer.Seek(beginPos, 6))
  462. return;
  463. if ((ObjectType)buffer.ReadByte() != packageItem.objectType)
  464. return;
  465. string str;
  466. str = buffer.ReadS();
  467. if (str != null)
  468. this.title = str;
  469. str = buffer.ReadS();
  470. if (str != null)
  471. this.selectedTitle = str;
  472. str = buffer.ReadS();
  473. if (str != null)
  474. this.icon = str;
  475. str = buffer.ReadS();
  476. if (str != null)
  477. this.selectedIcon = str;
  478. if (buffer.ReadBool())
  479. this.titleColor = buffer.ReadColor();
  480. int iv = buffer.ReadInt();
  481. if (iv != 0)
  482. this.titleFontSize = iv;
  483. iv = buffer.ReadShort();
  484. if (iv >= 0)
  485. _relatedController = parent.GetControllerAt(iv);
  486. _relatedPageId = buffer.ReadS();
  487. str = buffer.ReadS();
  488. if (str != null)
  489. sound = UIPackage.GetItemAssetByURL(str) as NAudioClip;
  490. if (buffer.ReadBool())
  491. soundVolumeScale = buffer.ReadFloat();
  492. this.selected = buffer.ReadBool();
  493. }
  494. private void __rollover()
  495. {
  496. if (_buttonController == null || !_buttonController.HasPage(OVER))
  497. return;
  498. _over = true;
  499. if (_down)
  500. return;
  501. if (this.grayed && _buttonController.HasPage(DISABLED))
  502. return;
  503. SetState(_selected ? SELECTED_OVER : OVER);
  504. }
  505. private void __rollout()
  506. {
  507. if (_buttonController == null || !_buttonController.HasPage(OVER))
  508. return;
  509. _over = false;
  510. if (_down)
  511. return;
  512. if (this.grayed && _buttonController.HasPage(DISABLED))
  513. return;
  514. SetState(_selected ? DOWN : UP);
  515. }
  516. private void __touchBegin(EventContext context)
  517. {
  518. if (context.inputEvent.button != 0)
  519. return;
  520. _down = true;
  521. context.CaptureTouch();
  522. if (_mode == ButtonMode.Common)
  523. {
  524. if (this.grayed && _buttonController != null && _buttonController.HasPage(DISABLED))
  525. SetState(SELECTED_DISABLED);
  526. else
  527. SetState(DOWN);
  528. }
  529. if (linkedPopup != null)
  530. {
  531. if (linkedPopup is Window)
  532. ((Window)linkedPopup).ToggleStatus();
  533. else
  534. this.root.TogglePopup(linkedPopup, this);
  535. }
  536. }
  537. private void __touchEnd()
  538. {
  539. if (_down)
  540. {
  541. _down = false;
  542. if (_mode == ButtonMode.Common)
  543. {
  544. if (this.grayed && _buttonController != null && _buttonController.HasPage(DISABLED))
  545. SetState(DISABLED);
  546. else if (_over)
  547. SetState(OVER);
  548. else
  549. SetState(UP);
  550. }
  551. else
  552. {
  553. if (!_over
  554. && _buttonController != null
  555. && (_buttonController.selectedPage == OVER || _buttonController.selectedPage == SELECTED_OVER))
  556. {
  557. SetCurrentState();
  558. }
  559. }
  560. }
  561. }
  562. private void __removedFromStage()
  563. {
  564. if (_over)
  565. __rollout();
  566. }
  567. private void __click()
  568. {
  569. if (sound != null && sound.nativeClip != null)
  570. Stage.inst.PlayOneShotSound(sound.nativeClip, soundVolumeScale);
  571. if (_mode == ButtonMode.Check)
  572. {
  573. if (changeStateOnClick)
  574. {
  575. this.selected = !_selected;
  576. DispatchEvent("onChanged", null);
  577. }
  578. }
  579. else if (_mode == ButtonMode.Radio)
  580. {
  581. if (changeStateOnClick && !_selected)
  582. {
  583. this.selected = true;
  584. DispatchEvent("onChanged", null);
  585. }
  586. }
  587. else
  588. {
  589. if (_relatedController != null)
  590. _relatedController.selectedPageId = _relatedPageId;
  591. }
  592. }
  593. }
  594. }