tabberui.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. class TabberUI extends ScriptedWidgetEventHandler
  2. {
  3. protected bool m_FirstInit = true;
  4. protected Widget m_Root;
  5. protected Widget m_TabControlsRoot;
  6. protected int m_TabsCount;
  7. protected ref map<int, Widget> m_TabControls;
  8. protected ref map<int, Widget> m_Tabs;
  9. protected int m_SelectedIndex;
  10. protected float m_ResolutionMultiplier;
  11. protected bool m_CanSwitch;
  12. ref ScriptInvoker m_OnTabSwitch = new ScriptInvoker();
  13. ref ScriptInvoker m_OnAttemptTabSwitch = new ScriptInvoker();
  14. ref Timer m_InitTimer;
  15. protected void OnInputPresetChanged()
  16. {
  17. #ifdef PLATFORM_CONSOLE
  18. UpdateControlsElements();
  19. #endif
  20. }
  21. protected void OnInputDeviceChanged(EInputDeviceType pInputDeviceType)
  22. {
  23. switch (pInputDeviceType)
  24. {
  25. case EInputDeviceType.CONTROLLER:
  26. UpdateControlsElements();
  27. m_TabControlsRoot.FindAnyWidget("ConsoleControls").Show(true);
  28. break;
  29. default:
  30. if (GetGame().GetInput().IsEnabledMouseAndKeyboardEvenOnServer())
  31. {
  32. m_TabControlsRoot.FindAnyWidget("ConsoleControls").Show(false);
  33. }
  34. break;
  35. }
  36. }
  37. void Init()
  38. {
  39. int x, y;
  40. GetScreenSize( x, y );
  41. m_ResolutionMultiplier = y / 1080;
  42. m_CanSwitch = true;
  43. m_TabsCount = 0;
  44. Widget tab_controls = m_Root.FindAnyWidget("Tab_Control_Container");
  45. if ( tab_controls )
  46. {
  47. Widget tab_child = tab_controls.GetChildren();
  48. while ( tab_child )
  49. {
  50. m_TabsCount++;
  51. tab_child = tab_child.GetSibling();
  52. }
  53. for ( int i = 0; i < m_TabsCount; i++ )
  54. {
  55. Widget tab_control = tab_controls.FindAnyWidget("Tab_Control_" + i);
  56. Widget tab_widget = m_Root.FindAnyWidget("Tab_" + i);
  57. if (tab_control && tab_widget)
  58. {
  59. tab_control.SetHandler(this);
  60. m_TabControls.Insert(i, tab_control);
  61. m_Tabs.Insert(i, tab_widget);
  62. }
  63. else
  64. {
  65. Error("Tabber could not find correctly named tab or control at index " + i);
  66. }
  67. }
  68. AlignTabbers();
  69. #ifdef PLATFORM_CONSOLE
  70. UpdateControlsElements();
  71. #endif
  72. SelectTabControl(0);
  73. m_InitTimer.Run(0.01, this, "AlignTabbers");
  74. }
  75. GetGame().GetMission().GetOnInputPresetChanged().Insert(OnInputPresetChanged);
  76. GetGame().GetMission().GetOnInputDeviceChanged().Insert(OnInputDeviceChanged);
  77. OnInputDeviceChanged(GetGame().GetInput().GetCurrentInputDevice());
  78. }
  79. void OnWidgetScriptInit( Widget w )
  80. {
  81. m_TabsCount = 0;
  82. m_TabControls = new map<int, Widget>;
  83. m_Tabs = new map<int, Widget>;
  84. m_Root = w;
  85. m_InitTimer = new Timer();
  86. m_TabControlsRoot = m_Root.FindAnyWidget("TabControls");
  87. Init();
  88. }
  89. void AlignTabbers()
  90. {
  91. float total_size;
  92. float x, y;
  93. Widget tab_controls_container = m_TabControlsRoot.FindAnyWidget( "Tab_Control_Container" );
  94. Widget tab_controls_scroller = m_TabControlsRoot.FindAnyWidget( "Tab_Control_Scroller" );
  95. m_TabControlsRoot.Update();
  96. tab_controls_container.Update();
  97. Widget tab_child = tab_controls_container.GetChildren();
  98. while ( tab_child )
  99. {
  100. if ( tab_child.IsVisible() )
  101. {
  102. TextWidget tab_text = TextWidget.Cast( tab_child.FindAnyWidget( tab_child.GetName() + "_Title" ) );
  103. int t_x, t_y;
  104. tab_text.Update();
  105. tab_text.GetTextSize( t_x, t_y );
  106. tab_child.SetSize( t_x + 10 * m_ResolutionMultiplier, 1 );
  107. tab_controls_container.Update();
  108. total_size += ( t_x + 10 * m_ResolutionMultiplier );
  109. }
  110. tab_child = tab_child.GetSibling();
  111. }
  112. tab_child = tab_controls_container.GetChildren();
  113. float x_f_c, y_f_c;
  114. tab_controls_container.GetScreenPos( x_f_c, y_f_c );
  115. while ( tab_child )
  116. {
  117. Widget tab_bg = tab_child.FindAnyWidget( tab_child.GetName() + "_Background" );
  118. tab_child.GetScreenPos( x, y );
  119. tab_bg.SetPos( ( x_f_c - x ), 0 );
  120. tab_bg.SetSize( total_size, 1 );
  121. tab_child = tab_child.GetSibling();
  122. }
  123. m_TabControlsRoot.GetSize( x, y );
  124. m_TabControlsRoot.SetSize( total_size, y );
  125. tab_controls_container.Update();
  126. if (tab_controls_scroller)
  127. tab_controls_scroller.Update();
  128. m_TabControlsRoot.Update();
  129. }
  130. int AddTab( string name )
  131. {
  132. int new_index = m_Tabs.Count();
  133. Widget tab = GetGame().GetWorkspace().CreateWidgets( "gui/layouts/new_ui/tabber_prefab/tab.layout", m_Root );
  134. Widget control = GetGame().GetWorkspace().CreateWidgets( "gui/layouts/new_ui/tabber_prefab/tab_control.layout", m_Root.FindAnyWidget( "Tab_Control_Container" ) );
  135. TextWidget control_text = TextWidget.Cast( control.FindAnyWidget( "Tab_Control_x_Title" ) );
  136. tab.SetName( "Tab_" + new_index );
  137. control.SetName( "Tab_Control_" + new_index );
  138. control_text.SetName( "Tab_Control_" + new_index + "_Title" );
  139. control.FindAnyWidget( "Tab_Control_x_Background" ).SetName( "Tab_Control_" + new_index + "_Background" );
  140. control_text.SetText( name );
  141. control.SetHandler( this );
  142. m_TabControls.Insert( new_index, control );
  143. m_Tabs.Insert( new_index, tab );
  144. AlignTabbers();
  145. return new_index;
  146. }
  147. void RemoveTab( int index )
  148. {
  149. }
  150. Widget GetTab( int index )
  151. {
  152. return m_Tabs.Get( index );
  153. }
  154. int GetTabCount()
  155. {
  156. return m_Tabs.Count();
  157. }
  158. bool CanSwitchTab()
  159. {
  160. return m_CanSwitch;
  161. }
  162. void SetCanSwitch(bool value)
  163. {
  164. m_CanSwitch = value;
  165. }
  166. void PerformSwitchTab(int index)
  167. {
  168. DeselectTabControl( m_SelectedIndex );
  169. DeselectTabPanel( m_SelectedIndex );
  170. SelectTabControl( index );
  171. SelectTabPanel( index );
  172. m_SelectedIndex = index;
  173. m_OnTabSwitch.Invoke( m_SelectedIndex );
  174. if ( m_FirstInit )
  175. {
  176. AlignTabbers();
  177. m_FirstInit = false;
  178. }
  179. }
  180. override bool OnMouseEnter( Widget w, int x, int y )
  181. {
  182. int index = m_TabControls.GetKeyByValue( w );
  183. if ( m_SelectedIndex == index )
  184. {
  185. return false;
  186. }
  187. Widget tab_control = m_TabControls.Get( index );
  188. if ( tab_control )
  189. {
  190. Widget tab_title = TextWidget.Cast(tab_control.FindAnyWidget( tab_control.GetName() + "_Title" ));
  191. tab_title.SetColor( ARGB(255, 255, 0, 0) );
  192. tab_control.SetColor( ARGB(255, 0, 0 ,0) );
  193. }
  194. return false;
  195. }
  196. override bool OnMouseLeave( Widget w, Widget enterW, int x, int y )
  197. {
  198. int index = m_TabControls.GetKeyByValue( w );
  199. if ( m_SelectedIndex == index )
  200. {
  201. return false;
  202. }
  203. Widget tab_control = m_TabControls.Get( index );
  204. if ( tab_control )
  205. {
  206. Widget tab_title = TextWidget.Cast(tab_control.FindAnyWidget( tab_control.GetName() + "_Title" ));
  207. tab_title.SetColor( ARGB(255, 255, 255, 255) );
  208. tab_control.SetColor( ARGB(0, 0, 0 ,0) );
  209. }
  210. return false;
  211. }
  212. override bool OnMouseButtonUp( Widget w, int x, int y, int button )
  213. {
  214. if ( button == MouseState.LEFT )
  215. {
  216. int index = m_TabControls.GetKeyByValue( w );
  217. if ( m_SelectedIndex != index )
  218. {
  219. m_OnAttemptTabSwitch.Invoke( m_SelectedIndex, index );
  220. if ( CanSwitchTab() )
  221. {
  222. PerformSwitchTab(index);
  223. return true;
  224. }
  225. }
  226. }
  227. return false;
  228. }
  229. override bool OnChildAdd( Widget w, Widget child )
  230. {
  231. if ( w == m_Root.FindAnyWidget( "Tab_Control_Container" ) )
  232. {
  233. AlignTabbers();
  234. return true;
  235. }
  236. return false;
  237. }
  238. override bool OnChildRemove( Widget w, Widget child )
  239. {
  240. if ( w == m_Root.FindAnyWidget( "Tab_Control_Container" ) )
  241. {
  242. AlignTabbers();
  243. return true;
  244. }
  245. return false;
  246. }
  247. void EnableTabControl( int index, bool enable )
  248. {
  249. Widget tab_control = m_Root.FindAnyWidget( "Tab_Control_" + index );
  250. if ( tab_control )
  251. tab_control.Show( enable );
  252. AlignTabbers();
  253. }
  254. void SelectTabControl( int index )
  255. {
  256. Widget tab_control = m_TabControls.Get( index );
  257. if ( tab_control )
  258. {
  259. /*
  260. Widget tab_bg = tab_control.FindAnyWidget( tab_control.GetName() + "_Background" );
  261. if( tab_bg )
  262. {
  263. tab_bg.Show( true );
  264. }
  265. */
  266. Widget tab_title = TextWidget.Cast(tab_control.FindAnyWidget( tab_control.GetName() + "_Title" ));
  267. int color_title = ARGB(255, 255, 0, 0);
  268. int color_backg = ARGB(255, 0, 0 ,0);
  269. #ifdef PLATFORM_CONSOLE
  270. color_title = ARGB(255, 255, 255, 255);
  271. color_backg = ARGB(255, 200, 0 ,0);
  272. #endif
  273. tab_title.SetColor( color_title );
  274. tab_control.SetColor( color_backg );
  275. }
  276. }
  277. void SelectTabPanel( int index )
  278. {
  279. Widget tab = m_Tabs.Get( index );
  280. if ( tab )
  281. {
  282. tab.Show( true );
  283. }
  284. }
  285. void DeselectTabControl( int index )
  286. {
  287. Widget tab_control = m_TabControls.Get( index );
  288. if ( tab_control )
  289. {
  290. /*
  291. Widget tab_bg = tab_control.FindAnyWidget( tab_control.GetName() + "_Background" );
  292. if( tab_bg )
  293. {
  294. tab_bg.Show( false );
  295. }
  296. */
  297. Widget tab_title = TextWidget.Cast(tab_control.FindAnyWidget( tab_control.GetName() + "_Title" ));
  298. tab_title.SetColor( ARGB(255, 255, 255,255) );
  299. tab_control.SetColor( ARGB(0, 0, 0 ,0) );
  300. }
  301. }
  302. void DeselectTabPanel( int index )
  303. {
  304. Widget tab = m_Tabs.Get( index );
  305. if ( tab )
  306. {
  307. tab.Show( false );
  308. }
  309. }
  310. void DeselectAll()
  311. {
  312. for (int i = 0; i < m_TabControls.Count(); i++)
  313. {
  314. DeselectTabControl(i);
  315. DeselectTabPanel(i);
  316. }
  317. }
  318. void NextTab()
  319. {
  320. int next_index = m_SelectedIndex + 1;
  321. while ( next_index < m_Tabs.Count() && !m_TabControls[next_index].IsVisible() )
  322. {
  323. next_index++;
  324. }
  325. if ( next_index >= m_Tabs.Count() )
  326. {
  327. next_index = 0;
  328. }
  329. if ( m_SelectedIndex != next_index )
  330. {
  331. m_OnAttemptTabSwitch.Invoke( m_SelectedIndex, next_index );
  332. if ( CanSwitchTab() )
  333. {
  334. PerformSwitchTab(next_index);
  335. }
  336. }
  337. if ( m_FirstInit )
  338. {
  339. AlignTabbers();
  340. m_FirstInit = false;
  341. }
  342. }
  343. void PreviousTab()
  344. {
  345. int next_index = m_SelectedIndex - 1;
  346. if ( next_index < 0 )
  347. {
  348. next_index = m_TabControls.Count() - 1;
  349. }
  350. while ( next_index > 0 && !m_TabControls[next_index].IsVisible() )
  351. {
  352. next_index--;
  353. }
  354. if ( m_SelectedIndex != next_index )
  355. {
  356. m_OnAttemptTabSwitch.Invoke( m_SelectedIndex, next_index );
  357. if ( CanSwitchTab() )
  358. {
  359. PerformSwitchTab(next_index);
  360. }
  361. }
  362. if ( m_FirstInit )
  363. {
  364. AlignTabbers();
  365. m_FirstInit = false;
  366. }
  367. }
  368. int GetSelectedIndex()
  369. {
  370. return m_SelectedIndex;
  371. }
  372. void RefreshTab(bool performInitAlignment = false)
  373. {
  374. m_FirstInit = performInitAlignment;
  375. DeselectAll();
  376. PerformSwitchTab(m_SelectedIndex);
  377. }
  378. protected void UpdateControlsElements()
  379. {
  380. Widget xbControls = m_Root.FindAnyWidget("ConsoleControls");
  381. if (xbControls)
  382. {
  383. xbControls.Show(m_TabsCount > 1);
  384. RichTextWidget toolbar_lb = RichTextWidget.Cast(xbControls.FindAnyWidget("TabLeftControl"));
  385. RichTextWidget toolbar_rb = RichTextWidget.Cast(xbControls.FindAnyWidget("TabRightControl"));
  386. if (toolbar_lb)
  387. {
  388. toolbar_lb.SetText(InputUtils.GetRichtextButtonIconFromInputAction("UAUITabLeft", "", EUAINPUT_DEVICE_CONTROLLER, InputUtils.ICON_SCALE_TOOLBAR));
  389. }
  390. if (toolbar_rb)
  391. {
  392. toolbar_rb.SetText(InputUtils.GetRichtextButtonIconFromInputAction("UAUITabRight", "", EUAINPUT_DEVICE_CONTROLLER, InputUtils.ICON_SCALE_TOOLBAR));
  393. }
  394. }
  395. }
  396. //! useful if we want to disable actual tabs for whatever reason
  397. void DisableTabs(bool disable)
  398. {
  399. foreach (Widget w : m_Tabs)
  400. {
  401. if (disable)
  402. {
  403. w.SetFlags( WidgetFlags.IGNOREPOINTER );
  404. }
  405. else
  406. {
  407. w.ClearFlags( WidgetFlags.IGNOREPOINTER );
  408. }
  409. w.Enable(!disable);
  410. }
  411. }
  412. }