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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
class TabberUI extends ScriptedWidgetEventHandler
{
	protected bool					m_FirstInit = true;
	protected Widget				m_Root;
	protected Widget				m_TabControlsRoot;
	
	protected int 					m_TabsCount;
	protected ref map<int, Widget>	m_TabControls;
	protected ref map<int, Widget>	m_Tabs;
	
	protected int					m_SelectedIndex;
	protected float					m_ResolutionMultiplier;
	protected bool					m_CanSwitch;
	
	ref ScriptInvoker				m_OnTabSwitch = new ScriptInvoker();
	ref ScriptInvoker				m_OnAttemptTabSwitch = new ScriptInvoker();
	ref Timer						m_InitTimer;
	
	protected void OnInputPresetChanged()
	{
		#ifdef PLATFORM_CONSOLE
		UpdateControlsElements();
		#endif
	}
	
	protected void OnInputDeviceChanged(EInputDeviceType pInputDeviceType)
	{
		switch (pInputDeviceType)
		{
		case EInputDeviceType.CONTROLLER:
			UpdateControlsElements();
			m_TabControlsRoot.FindAnyWidget("ConsoleControls").Show(true);
		break;

		default:
			if (GetGame().GetInput().IsEnabledMouseAndKeyboardEvenOnServer())
			{
				m_TabControlsRoot.FindAnyWidget("ConsoleControls").Show(false);
			}
		break;
		}
	}
	
	void Init()
	{
		int x, y;
		GetScreenSize( x, y );
		m_ResolutionMultiplier	= y / 1080;
		m_CanSwitch = true;
		m_TabsCount	= 0;
		
		Widget tab_controls	= m_Root.FindAnyWidget("Tab_Control_Container");
		if ( tab_controls )
		{
			Widget tab_child	= tab_controls.GetChildren();
			
			while ( tab_child )
			{
				m_TabsCount++;
				tab_child = tab_child.GetSibling();
			}
			
			for ( int i = 0; i < m_TabsCount; i++ )
			{
				Widget tab_control	= tab_controls.FindAnyWidget("Tab_Control_" + i);
				Widget tab_widget	= m_Root.FindAnyWidget("Tab_" + i);
				if (tab_control && tab_widget)
				{
					tab_control.SetHandler(this);
					m_TabControls.Insert(i, tab_control);
					m_Tabs.Insert(i, tab_widget);
				}
				else
				{
					Error("Tabber could not find correctly named tab or control at index " + i);
				}
			}
			
			AlignTabbers();
			#ifdef PLATFORM_CONSOLE
			UpdateControlsElements();
			#endif
			
			SelectTabControl(0);
			
			m_InitTimer.Run(0.01, this, "AlignTabbers");
		}
		
		GetGame().GetMission().GetOnInputPresetChanged().Insert(OnInputPresetChanged);
		GetGame().GetMission().GetOnInputDeviceChanged().Insert(OnInputDeviceChanged);
		
		OnInputDeviceChanged(GetGame().GetInput().GetCurrentInputDevice());
	}

	void OnWidgetScriptInit( Widget w )
	{
		m_TabsCount				= 0;
		m_TabControls			= new map<int, Widget>;
		m_Tabs					= new map<int, Widget>;
		
		m_Root					= w;
		m_InitTimer				= new Timer();
		m_TabControlsRoot		= m_Root.FindAnyWidget("TabControls");
		
		Init();
	}
	
	void AlignTabbers()
	{
		float total_size;
		float x, y;
		
		Widget tab_controls_container = m_TabControlsRoot.FindAnyWidget( "Tab_Control_Container" );
		Widget tab_controls_scroller = m_TabControlsRoot.FindAnyWidget( "Tab_Control_Scroller" );
		
		m_TabControlsRoot.Update();
		tab_controls_container.Update();
		
		Widget tab_child = tab_controls_container.GetChildren();
		while ( tab_child )
		{
			if ( tab_child.IsVisible() )
			{
				TextWidget tab_text = TextWidget.Cast( tab_child.FindAnyWidget( tab_child.GetName() + "_Title" ) );
				int t_x, t_y;
				tab_text.Update();
				tab_text.GetTextSize( t_x, t_y );
				tab_child.SetSize( t_x + 10 * m_ResolutionMultiplier, 1 );
				tab_controls_container.Update();
				
				total_size += ( t_x + 10 * m_ResolutionMultiplier );
			}
			
			tab_child = tab_child.GetSibling();
		}
		
		tab_child = tab_controls_container.GetChildren();
		
		float x_f_c, y_f_c;
		tab_controls_container.GetScreenPos( x_f_c, y_f_c );
		
		while ( tab_child )
		{
			Widget tab_bg = tab_child.FindAnyWidget( tab_child.GetName() + "_Background" );
			tab_child.GetScreenPos( x, y );
			tab_bg.SetPos( ( x_f_c - x ), 0 );
			tab_bg.SetSize( total_size, 1 );
			
			tab_child = tab_child.GetSibling();
		}
		
		m_TabControlsRoot.GetSize( x, y );
		
		m_TabControlsRoot.SetSize( total_size, y );
		tab_controls_container.Update();
		if (tab_controls_scroller)
			tab_controls_scroller.Update();
		m_TabControlsRoot.Update();
	}
	
	int AddTab( string name )
	{
		int new_index = m_Tabs.Count();
		Widget tab = GetGame().GetWorkspace().CreateWidgets( "gui/layouts/new_ui/tabber_prefab/tab.layout", m_Root );
		Widget control = GetGame().GetWorkspace().CreateWidgets( "gui/layouts/new_ui/tabber_prefab/tab_control.layout", m_Root.FindAnyWidget( "Tab_Control_Container" ) );
		TextWidget control_text = TextWidget.Cast( control.FindAnyWidget( "Tab_Control_x_Title" ) );
		
		tab.SetName( "Tab_" + new_index );
		control.SetName( "Tab_Control_" + new_index );
		control_text.SetName( "Tab_Control_" + new_index + "_Title" );
		control.FindAnyWidget( "Tab_Control_x_Background" ).SetName( "Tab_Control_" + new_index + "_Background" );
		
		control_text.SetText( name );
		
		control.SetHandler( this );
		m_TabControls.Insert( new_index, control );
		m_Tabs.Insert( new_index, tab );
		
		AlignTabbers();
		
		return new_index;
	}
	
	void RemoveTab( int index )
	{

	}
	
	
	Widget GetTab( int index )
	{
		return m_Tabs.Get( index );
	}
	
	int GetTabCount()
	{
		return m_Tabs.Count();
	}
	
	bool CanSwitchTab()
	{
		return m_CanSwitch;
	}
	
	void SetCanSwitch(bool value)
	{
		m_CanSwitch = value;
	}
	
	void PerformSwitchTab(int index)
	{
		DeselectTabControl( m_SelectedIndex );
		DeselectTabPanel( m_SelectedIndex );
		
		SelectTabControl( index );
		SelectTabPanel( index );
		
		m_SelectedIndex = index;
		m_OnTabSwitch.Invoke( m_SelectedIndex );
		
		if ( m_FirstInit )
		{
			AlignTabbers();
			m_FirstInit = false;
		}
	}
	
	override bool OnMouseEnter( Widget w, int x, int y )
	{
		int index = m_TabControls.GetKeyByValue( w );
		if ( m_SelectedIndex == index )
		{
			return false;
		}
		
		Widget tab_control = m_TabControls.Get( index );
		if ( tab_control )
		{			
			Widget tab_title = TextWidget.Cast(tab_control.FindAnyWidget( tab_control.GetName() + "_Title" ));
			tab_title.SetColor( ARGB(255, 255, 0, 0) );
			tab_control.SetColor( ARGB(255, 0, 0 ,0) );
		}
		
		return false;
	}
	
	override bool OnMouseLeave( Widget w, Widget enterW, int x, int y )
	{
		int index = m_TabControls.GetKeyByValue( w );
		if ( m_SelectedIndex == index )
		{
			return false;
		}
		
		Widget tab_control = m_TabControls.Get( index );
		if ( tab_control )
		{			
			Widget tab_title = TextWidget.Cast(tab_control.FindAnyWidget( tab_control.GetName() + "_Title" ));
			tab_title.SetColor( ARGB(255, 255, 255, 255) );
			tab_control.SetColor( ARGB(0, 0, 0 ,0) );
		}
		return false;
	}
	
	override bool OnMouseButtonUp( Widget w, int x, int y, int button )
	{
		if ( button == MouseState.LEFT )
		{
			int index = m_TabControls.GetKeyByValue( w );
			if ( m_SelectedIndex != index )
			{
				m_OnAttemptTabSwitch.Invoke( m_SelectedIndex, index );
				if ( CanSwitchTab() )
				{
					PerformSwitchTab(index);
					
					return true;
				}
			}
		}
		
		return false;
	}
	
	override bool OnChildAdd( Widget w, Widget child )
	{
		if ( w == m_Root.FindAnyWidget( "Tab_Control_Container" ) )
		{
			AlignTabbers();
			return true;
		}
		return false;
	}
	
	override bool OnChildRemove( Widget w, Widget child )
	{
		if ( w == m_Root.FindAnyWidget( "Tab_Control_Container" ) )
		{
			AlignTabbers();
			return true;
		}
		return false;
	}
	
	void EnableTabControl( int index, bool enable )
	{
		Widget tab_control = m_Root.FindAnyWidget( "Tab_Control_" + index );
		if ( tab_control )
			tab_control.Show( enable );
		AlignTabbers();
	}
	
	void SelectTabControl( int index )
	{
		Widget tab_control = m_TabControls.Get( index );
		if ( tab_control )
		{
			/*
			Widget tab_bg = tab_control.FindAnyWidget( tab_control.GetName() + "_Background" );
			if( tab_bg )
			{
				tab_bg.Show( true );
			}
			*/
			
			Widget tab_title = TextWidget.Cast(tab_control.FindAnyWidget( tab_control.GetName() + "_Title" ));
			
			int color_title = ARGB(255, 255, 0, 0);
			int color_backg = ARGB(255, 0, 0 ,0);
			
			#ifdef PLATFORM_CONSOLE
				color_title = ARGB(255, 255, 255, 255);
				color_backg = ARGB(255, 200, 0 ,0);
			#endif
			
			tab_title.SetColor( color_title );
			tab_control.SetColor( color_backg );
		}
	}
	
	void SelectTabPanel( int index )
	{
		Widget tab = m_Tabs.Get( index );
		if ( tab )
		{
			tab.Show( true );
		}
	}
	
	void DeselectTabControl( int index )
	{
		Widget tab_control = m_TabControls.Get( index );
		if ( tab_control )
		{
			/*
			Widget tab_bg = tab_control.FindAnyWidget( tab_control.GetName() + "_Background" );
			if( tab_bg )
			{
				tab_bg.Show( false );
			}
			*/
			Widget tab_title = TextWidget.Cast(tab_control.FindAnyWidget( tab_control.GetName() + "_Title" ));
			tab_title.SetColor( ARGB(255, 255, 255,255) );
			tab_control.SetColor( ARGB(0, 0, 0 ,0) );
		}
	}
	
	void DeselectTabPanel( int index )
	{
		Widget tab = m_Tabs.Get( index );
		if ( tab )
		{
			tab.Show( false );
		}
	}
	
	void DeselectAll()
	{
		for (int i = 0; i < m_TabControls.Count(); i++)
		{
			DeselectTabControl(i);
			DeselectTabPanel(i);
		}
	}
	
	void NextTab()
	{
		int next_index = m_SelectedIndex + 1;
		
		while ( next_index < m_Tabs.Count() && !m_TabControls[next_index].IsVisible() )
		{
			next_index++;
		}
		
		if ( next_index >= m_Tabs.Count() )
		{
			next_index = 0;
		}
		
		if ( m_SelectedIndex != next_index )
		{
			m_OnAttemptTabSwitch.Invoke( m_SelectedIndex, next_index );
			if ( CanSwitchTab() )
			{
				PerformSwitchTab(next_index);
			}
		}
		
		if ( m_FirstInit )
		{
			AlignTabbers();
			m_FirstInit = false;
		}
	}
	
	void PreviousTab()
	{
		int next_index = m_SelectedIndex - 1;
		
		if ( next_index < 0 )
		{
			next_index = m_TabControls.Count() - 1;
		}
		
		while ( next_index > 0 && !m_TabControls[next_index].IsVisible() )
		{
			next_index--;
		}
		
		if ( m_SelectedIndex != next_index )
		{
			m_OnAttemptTabSwitch.Invoke( m_SelectedIndex, next_index );
			if ( CanSwitchTab() )
			{
				PerformSwitchTab(next_index);
			}
		}
		
		if ( m_FirstInit )
		{
			AlignTabbers();
			m_FirstInit = false;
		}
	}
	
	int GetSelectedIndex()
	{
		return m_SelectedIndex;
	}
	
	void RefreshTab(bool performInitAlignment = false)
	{
		m_FirstInit = performInitAlignment;
		DeselectAll();
		PerformSwitchTab(m_SelectedIndex);
	}
	
	protected void UpdateControlsElements()
	{
		Widget xbControls = m_Root.FindAnyWidget("ConsoleControls");
		if (xbControls)
		{
			xbControls.Show(m_TabsCount > 1);
	
			RichTextWidget toolbar_lb = RichTextWidget.Cast(xbControls.FindAnyWidget("TabLeftControl"));
			RichTextWidget toolbar_rb = RichTextWidget.Cast(xbControls.FindAnyWidget("TabRightControl"));
			if (toolbar_lb)
			{
				toolbar_lb.SetText(InputUtils.GetRichtextButtonIconFromInputAction("UAUITabLeft", "", EUAINPUT_DEVICE_CONTROLLER, InputUtils.ICON_SCALE_TOOLBAR));
			}

			if (toolbar_rb)
			{
				toolbar_rb.SetText(InputUtils.GetRichtextButtonIconFromInputAction("UAUITabRight", "", EUAINPUT_DEVICE_CONTROLLER, InputUtils.ICON_SCALE_TOOLBAR));
			}
		}
	}
	
	//! useful if we want to disable actual tabs for whatever reason
	void DisableTabs(bool disable)
	{
		foreach (Widget w : m_Tabs)
		{
			if (disable)
			{
				w.SetFlags( WidgetFlags.IGNOREPOINTER );
			}
			else
			{
				w.ClearFlags( WidgetFlags.IGNOREPOINTER );
			}
			w.Enable(!disable);
		}
	}
}