Controller.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using System.Collections.Generic;
  2. using FairyGUI.Utils;
  3. using System;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. /// Controller class.
  8. /// 控制器类。控制器的创建和设计需通过编辑器完成,不建议使用代码创建。
  9. /// 最常用的方法是通过selectedIndex获得或改变控制器的活动页面。如果要获得控制器页面改变的通知,使用onChanged事件。
  10. /// </summary>
  11. public class Controller : EventDispatcher
  12. {
  13. /// <summary>
  14. /// Name of the controller
  15. /// 控制器名称。
  16. /// </summary>
  17. public string name;
  18. internal GComponent parent;
  19. internal bool autoRadioGroupDepth;
  20. internal bool changing;
  21. int _selectedIndex;
  22. int _previousIndex;
  23. List<string> _pageIds;
  24. List<string> _pageNames;
  25. List<ControllerAction> _actions;
  26. EventListener _onChanged;
  27. static uint _nextPageId;
  28. public Controller()
  29. {
  30. _pageIds = new List<string>();
  31. _pageNames = new List<string>();
  32. _selectedIndex = -1;
  33. _previousIndex = -1;
  34. }
  35. public void Dispose()
  36. {
  37. RemoveEventListeners();
  38. }
  39. /// <summary>
  40. /// When controller page changed.
  41. /// 当控制器活动页面改变时,此事件被触发。
  42. /// </summary>
  43. public EventListener onChanged
  44. {
  45. get { return _onChanged ?? (_onChanged = new EventListener(this, "onChanged")); }
  46. }
  47. /// <summary>
  48. /// Current page index.
  49. /// 获得或设置当前活动页面索引。
  50. /// </summary>
  51. public int selectedIndex
  52. {
  53. get
  54. {
  55. return _selectedIndex;
  56. }
  57. set
  58. {
  59. if (_selectedIndex != value)
  60. {
  61. if (value > _pageIds.Count - 1)
  62. throw new IndexOutOfRangeException("" + value);
  63. changing = true;
  64. _previousIndex = _selectedIndex;
  65. _selectedIndex = value;
  66. parent.ApplyController(this);
  67. DispatchEvent("onChanged", null);
  68. changing = false;
  69. }
  70. }
  71. }
  72. /// <summary>
  73. /// Set current page index, no onChanged event.
  74. /// 通过索引设置当前活动页面,和selectedIndex的区别在于,这个方法不会触发onChanged事件。
  75. /// </summary>
  76. /// <param name="value">Page index</param>
  77. public void SetSelectedIndex(int value)
  78. {
  79. if (_selectedIndex != value)
  80. {
  81. if (value > _pageIds.Count - 1)
  82. throw new IndexOutOfRangeException("" + value);
  83. changing = true;
  84. _previousIndex = _selectedIndex;
  85. _selectedIndex = value;
  86. parent.ApplyController(this);
  87. changing = false;
  88. }
  89. }
  90. /// <summary>
  91. /// Set current page by name, no onChanged event.
  92. /// 通过页面名称设置当前活动页面,和selectedPage的区别在于,这个方法不会触发onChanged事件。
  93. /// </summary>
  94. /// <param name="value">Page name</param>
  95. public void SetSelectedPage(string value)
  96. {
  97. int i = _pageNames.IndexOf(value);
  98. if (i == -1)
  99. i = 0;
  100. this.SetSelectedIndex(i);
  101. }
  102. /// <summary>
  103. /// Current page name.
  104. /// 获得当前活动页面名称
  105. /// </summary>
  106. public string selectedPage
  107. {
  108. get
  109. {
  110. if (_selectedIndex == -1)
  111. return null;
  112. else
  113. return _pageNames[_selectedIndex];
  114. }
  115. set
  116. {
  117. int i = _pageNames.IndexOf(value);
  118. if (i == -1)
  119. i = 0;
  120. this.selectedIndex = i;
  121. }
  122. }
  123. [Obsolete("Use previousIndex")]
  124. public int previsousIndex
  125. {
  126. get { return _previousIndex; }
  127. }
  128. /// <summary>
  129. /// Previouse page index.
  130. /// 获得上次活动页面索引
  131. /// </summary>
  132. public int previousIndex
  133. {
  134. get { return _previousIndex; }
  135. }
  136. /// <summary>
  137. /// Previous page name.
  138. /// 获得上次活动页面名称。
  139. /// </summary>
  140. public string previousPage
  141. {
  142. get
  143. {
  144. if (_previousIndex == -1)
  145. return null;
  146. else
  147. return _pageNames[_previousIndex];
  148. }
  149. }
  150. /// <summary>
  151. /// Page count of this controller.
  152. /// 获得页面数量。
  153. /// </summary>
  154. public int pageCount
  155. {
  156. get { return _pageIds.Count; }
  157. }
  158. /// <summary>
  159. /// Get page name by an index.
  160. /// 通过页面索引获得页面名称。
  161. /// </summary>
  162. /// <param name="index">Page index</param>
  163. /// <returns>Page Name</returns>
  164. public string GetPageName(int index)
  165. {
  166. return _pageNames[index];
  167. }
  168. /// <summary>
  169. /// Get page id by an index.
  170. /// 通过页面索引获得页面id。
  171. /// </summary>
  172. /// <param name="index">Page index</param>
  173. /// <returns>Page Id</returns>
  174. public string GetPageId(int index)
  175. {
  176. return _pageIds[index];
  177. }
  178. /// <summary>
  179. /// Get page id by name
  180. /// </summary>
  181. /// <param name="aName"></param>
  182. /// <returns></returns>
  183. public string GetPageIdByName(string aName)
  184. {
  185. int i = _pageNames.IndexOf(aName);
  186. if (i != -1)
  187. return _pageIds[i];
  188. else
  189. return null;
  190. }
  191. /// <summary>
  192. /// Add a new page to this controller.
  193. /// </summary>
  194. /// <param name="name">Page name</param>
  195. public void AddPage(string name)
  196. {
  197. if (name == null)
  198. name = string.Empty;
  199. AddPageAt(name, _pageIds.Count);
  200. }
  201. /// <summary>
  202. /// Add a new page to this controller at a certain index.
  203. /// </summary>
  204. /// <param name="name">Page name</param>
  205. /// <param name="index">Insert position</param>
  206. public void AddPageAt(string name, int index)
  207. {
  208. string nid = "_" + (_nextPageId++);
  209. if (index == _pageIds.Count)
  210. {
  211. _pageIds.Add(nid);
  212. _pageNames.Add(name);
  213. }
  214. else
  215. {
  216. _pageIds.Insert(index, nid);
  217. _pageNames.Insert(index, name);
  218. }
  219. }
  220. /// <summary>
  221. /// Remove a page.
  222. /// </summary>
  223. /// <param name="name">Page name</param>
  224. public void RemovePage(string name)
  225. {
  226. int i = _pageNames.IndexOf(name);
  227. if (i != -1)
  228. {
  229. _pageIds.RemoveAt(i);
  230. _pageNames.RemoveAt(i);
  231. if (_selectedIndex >= _pageIds.Count)
  232. this.selectedIndex = _selectedIndex - 1;
  233. else
  234. parent.ApplyController(this);
  235. }
  236. }
  237. /// <summary>
  238. /// Removes a page at a certain index.
  239. /// </summary>
  240. /// <param name="index"></param>
  241. public void RemovePageAt(int index)
  242. {
  243. _pageIds.RemoveAt(index);
  244. _pageNames.RemoveAt(index);
  245. if (_selectedIndex >= _pageIds.Count)
  246. this.selectedIndex = _selectedIndex - 1;
  247. else
  248. parent.ApplyController(this);
  249. }
  250. /// <summary>
  251. /// Remove all pages.
  252. /// </summary>
  253. public void ClearPages()
  254. {
  255. _pageIds.Clear();
  256. _pageNames.Clear();
  257. if (_selectedIndex != -1)
  258. this.selectedIndex = -1;
  259. else
  260. parent.ApplyController(this);
  261. }
  262. /// <summary>
  263. /// Check if the controller has a page.
  264. /// </summary>
  265. /// <param name="aName">Page name.</param>
  266. /// <returns></returns>
  267. public bool HasPage(string aName)
  268. {
  269. return _pageNames.IndexOf(aName) != -1;
  270. }
  271. internal int GetPageIndexById(string aId)
  272. {
  273. return _pageIds.IndexOf(aId);
  274. }
  275. internal string GetPageNameById(string aId)
  276. {
  277. int i = _pageIds.IndexOf(aId);
  278. if (i != -1)
  279. return _pageNames[i];
  280. else
  281. return null;
  282. }
  283. internal string selectedPageId
  284. {
  285. get
  286. {
  287. if (_selectedIndex == -1)
  288. return string.Empty;
  289. else
  290. return _pageIds[_selectedIndex];
  291. }
  292. set
  293. {
  294. int i = _pageIds.IndexOf(value);
  295. if (i != -1)
  296. this.selectedIndex = i;
  297. }
  298. }
  299. internal string oppositePageId
  300. {
  301. set
  302. {
  303. int i = _pageIds.IndexOf(value);
  304. if (i > 0)
  305. this.selectedIndex = 0;
  306. else if (_pageIds.Count > 1)
  307. this.selectedIndex = 1;
  308. }
  309. }
  310. internal string previousPageId
  311. {
  312. get
  313. {
  314. if (_previousIndex == -1)
  315. return null;
  316. else
  317. return _pageIds[_previousIndex];
  318. }
  319. }
  320. public void RunActions()
  321. {
  322. if (_actions != null)
  323. {
  324. int cnt = _actions.Count;
  325. for (int i = 0; i < cnt; i++)
  326. {
  327. _actions[i].Run(this, previousPageId, selectedPageId);
  328. }
  329. }
  330. }
  331. public void Setup(ByteBuffer buffer)
  332. {
  333. int beginPos = buffer.position;
  334. buffer.Seek(beginPos, 0);
  335. name = buffer.ReadS();
  336. autoRadioGroupDepth = buffer.ReadBool();
  337. buffer.Seek(beginPos, 1);
  338. int cnt = buffer.ReadShort();
  339. _pageIds.Capacity = cnt;
  340. _pageNames.Capacity = cnt;
  341. for (int i = 0; i < cnt; i++)
  342. {
  343. _pageIds.Add(buffer.ReadS());
  344. _pageNames.Add(buffer.ReadS());
  345. }
  346. int homePageIndex = 0;
  347. if (buffer.version >= 2)
  348. {
  349. int homePageType = buffer.ReadByte();
  350. switch (homePageType)
  351. {
  352. case 1:
  353. homePageIndex = buffer.ReadShort();
  354. break;
  355. case 2:
  356. homePageIndex = _pageNames.IndexOf(UIPackage.branch);
  357. if (homePageIndex == -1)
  358. homePageIndex = 0;
  359. break;
  360. case 3:
  361. homePageIndex = _pageNames.IndexOf(UIPackage.GetVar(buffer.ReadS()));
  362. if (homePageIndex == -1)
  363. homePageIndex = 0;
  364. break;
  365. }
  366. }
  367. buffer.Seek(beginPos, 2);
  368. cnt = buffer.ReadShort();
  369. if (cnt > 0)
  370. {
  371. if (_actions == null)
  372. _actions = new List<ControllerAction>(cnt);
  373. for (int i = 0; i < cnt; i++)
  374. {
  375. int nextPos = buffer.ReadUshort();
  376. nextPos += buffer.position;
  377. ControllerAction action = ControllerAction.CreateAction((ControllerAction.ActionType)buffer.ReadByte());
  378. action.Setup(buffer);
  379. _actions.Add(action);
  380. buffer.position = nextPos;
  381. }
  382. }
  383. if (parent != null && _pageIds.Count > 0)
  384. _selectedIndex = homePageIndex;
  385. else
  386. _selectedIndex = -1;
  387. }
  388. }
  389. }