Controller.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. /// <summary>
  124. /// Previouse page index.
  125. /// 获得上次活动页面索引
  126. /// </summary>
  127. public int previsousIndex
  128. {
  129. get { return _previousIndex; }
  130. }
  131. /// <summary>
  132. /// Previous page name.
  133. /// 获得上次活动页面名称。
  134. /// </summary>
  135. public string previousPage
  136. {
  137. get
  138. {
  139. if (_previousIndex == -1)
  140. return null;
  141. else
  142. return _pageNames[_previousIndex];
  143. }
  144. }
  145. /// <summary>
  146. /// Page count of this controller.
  147. /// 获得页面数量。
  148. /// </summary>
  149. public int pageCount
  150. {
  151. get { return _pageIds.Count; }
  152. }
  153. /// <summary>
  154. /// Get page name by an index.
  155. /// 通过页面索引获得页面名称。
  156. /// </summary>
  157. /// <param name="index">Page index</param>
  158. /// <returns>Page Name</returns>
  159. public string GetPageName(int index)
  160. {
  161. return _pageNames[index];
  162. }
  163. /// <summary>
  164. /// Get page id by an index.
  165. /// 通过页面索引获得页面id。
  166. /// </summary>
  167. /// <param name="index">Page index</param>
  168. /// <returns>Page Id</returns>
  169. public string GetPageId(int index)
  170. {
  171. return _pageIds[index];
  172. }
  173. /// <summary>
  174. /// Get page id by name
  175. /// </summary>
  176. /// <param name="aName"></param>
  177. /// <returns></returns>
  178. public string GetPageIdByName(string aName)
  179. {
  180. int i = _pageNames.IndexOf(aName);
  181. if (i != -1)
  182. return _pageIds[i];
  183. else
  184. return null;
  185. }
  186. /// <summary>
  187. /// Add a new page to this controller.
  188. /// </summary>
  189. /// <param name="name">Page name</param>
  190. public void AddPage(string name)
  191. {
  192. if (name == null)
  193. name = string.Empty;
  194. AddPageAt(name, _pageIds.Count);
  195. }
  196. /// <summary>
  197. /// Add a new page to this controller at a certain index.
  198. /// </summary>
  199. /// <param name="name">Page name</param>
  200. /// <param name="index">Insert position</param>
  201. public void AddPageAt(string name, int index)
  202. {
  203. string nid = "_" + (_nextPageId++);
  204. if (index == _pageIds.Count)
  205. {
  206. _pageIds.Add(nid);
  207. _pageNames.Add(name);
  208. }
  209. else
  210. {
  211. _pageIds.Insert(index, nid);
  212. _pageNames.Insert(index, name);
  213. }
  214. }
  215. /// <summary>
  216. /// Remove a page.
  217. /// </summary>
  218. /// <param name="name">Page name</param>
  219. public void RemovePage(string name)
  220. {
  221. int i = _pageNames.IndexOf(name);
  222. if (i != -1)
  223. {
  224. _pageIds.RemoveAt(i);
  225. _pageNames.RemoveAt(i);
  226. if (_selectedIndex >= _pageIds.Count)
  227. this.selectedIndex = _selectedIndex - 1;
  228. else
  229. parent.ApplyController(this);
  230. }
  231. }
  232. /// <summary>
  233. /// Removes a page at a certain index.
  234. /// </summary>
  235. /// <param name="index"></param>
  236. public void RemovePageAt(int index)
  237. {
  238. _pageIds.RemoveAt(index);
  239. _pageNames.RemoveAt(index);
  240. if (_selectedIndex >= _pageIds.Count)
  241. this.selectedIndex = _selectedIndex - 1;
  242. else
  243. parent.ApplyController(this);
  244. }
  245. /// <summary>
  246. /// Remove all pages.
  247. /// </summary>
  248. public void ClearPages()
  249. {
  250. _pageIds.Clear();
  251. _pageNames.Clear();
  252. if (_selectedIndex != -1)
  253. this.selectedIndex = -1;
  254. else
  255. parent.ApplyController(this);
  256. }
  257. /// <summary>
  258. /// Check if the controller has a page.
  259. /// </summary>
  260. /// <param name="aName">Page name.</param>
  261. /// <returns></returns>
  262. public bool HasPage(string aName)
  263. {
  264. return _pageNames.IndexOf(aName) != -1;
  265. }
  266. internal int GetPageIndexById(string aId)
  267. {
  268. return _pageIds.IndexOf(aId);
  269. }
  270. internal string GetPageNameById(string aId)
  271. {
  272. int i = _pageIds.IndexOf(aId);
  273. if (i != -1)
  274. return _pageNames[i];
  275. else
  276. return null;
  277. }
  278. internal string selectedPageId
  279. {
  280. get
  281. {
  282. if (_selectedIndex == -1)
  283. return string.Empty;
  284. else
  285. return _pageIds[_selectedIndex];
  286. }
  287. set
  288. {
  289. int i = _pageIds.IndexOf(value);
  290. if (i != -1)
  291. this.selectedIndex = i;
  292. }
  293. }
  294. internal string oppositePageId
  295. {
  296. set
  297. {
  298. int i = _pageIds.IndexOf(value);
  299. if (i > 0)
  300. this.selectedIndex = 0;
  301. else if (_pageIds.Count > 1)
  302. this.selectedIndex = 1;
  303. }
  304. }
  305. internal string previousPageId
  306. {
  307. get
  308. {
  309. if (_previousIndex == -1)
  310. return null;
  311. else
  312. return _pageIds[_previousIndex];
  313. }
  314. }
  315. public void RunActions()
  316. {
  317. if (_actions != null)
  318. {
  319. int cnt = _actions.Count;
  320. for (int i = 0; i < cnt; i++)
  321. {
  322. _actions[i].Run(this, previousPageId, selectedPageId);
  323. }
  324. }
  325. }
  326. public void Setup(ByteBuffer buffer)
  327. {
  328. int beginPos = buffer.position;
  329. buffer.Seek(beginPos, 0);
  330. name = buffer.ReadS();
  331. autoRadioGroupDepth = buffer.ReadBool();
  332. buffer.Seek(beginPos, 1);
  333. int cnt = buffer.ReadShort();
  334. _pageIds.Capacity = cnt;
  335. _pageNames.Capacity = cnt;
  336. for (int i = 0; i < cnt; i++)
  337. {
  338. _pageIds.Add(buffer.ReadS());
  339. _pageNames.Add(buffer.ReadS());
  340. }
  341. int homePageIndex = 0;
  342. if (buffer.version >= 2)
  343. {
  344. int homePageType = buffer.ReadByte();
  345. switch (homePageType)
  346. {
  347. case 1:
  348. homePageIndex = buffer.ReadShort();
  349. break;
  350. case 2:
  351. homePageIndex = _pageNames.IndexOf(UIPackage.branch);
  352. if (homePageIndex == -1)
  353. homePageIndex = 0;
  354. break;
  355. case 3:
  356. homePageIndex = _pageNames.IndexOf(UIPackage.GetVar(buffer.ReadS()));
  357. if (homePageIndex == -1)
  358. homePageIndex = 0;
  359. break;
  360. }
  361. }
  362. buffer.Seek(beginPos, 2);
  363. cnt = buffer.ReadShort();
  364. if (cnt > 0)
  365. {
  366. if (_actions == null)
  367. _actions = new List<ControllerAction>(cnt);
  368. for (int i = 0; i < cnt; i++)
  369. {
  370. int nextPos = buffer.ReadShort();
  371. nextPos += buffer.position;
  372. ControllerAction action = ControllerAction.CreateAction((ControllerAction.ActionType)buffer.ReadByte());
  373. action.Setup(buffer);
  374. _actions.Add(action);
  375. buffer.position = nextPos;
  376. }
  377. }
  378. if (parent != null && _pageIds.Count > 0)
  379. _selectedIndex = homePageIndex;
  380. else
  381. _selectedIndex = -1;
  382. }
  383. }
  384. }