aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Common/UI/UILayer.cpp
blob: e1c388f54fddb2d3625aea7cff4cba50d3354f57 (plain)
1
2
3
4
5
6
7
8
9
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
#include "stdafx.h"
#include "UI.h"
#include "UILayer.h"
#include "UIScene.h"

UILayer::UILayer(UIGroup *parent)
{
	m_parentGroup = parent;
	m_hasFocus = false;
	m_bMenuDisplayed = false;
	m_bPauseMenuDisplayed = false;
	m_bContainerMenuDisplayed = false;
	m_bIgnoreAutosaveMenuDisplayed = false;
	m_bIgnorePlayerJoinMenuDisplayed = false;
}

void UILayer::tick()
{
	// Delete old scenes - deleting a scene can cause a new scene to be deleted, so we need to make a copy of the scenes that we are going to try and destroy this tick
	vector<UIScene *>scenesToDeleteCopy;
	for(auto& scene : m_scenesToDelete)
	{
		scenesToDeleteCopy.push_back(scene);
	}
	m_scenesToDelete.clear();

	// Delete the scenes in our copy if they are ready to delete, otherwise add back to the ones that are still to be deleted. Actually deleting a scene might also add something back into m_scenesToDelete.
	for(auto& scene : scenesToDeleteCopy)
	{
		if( scene && scene->isReadyToDelete())
		{
			delete scene;
		}
		else
		{
			m_scenesToDelete.push_back(scene);
		}
	}

	while (!m_scenesToDestroy.empty())
	{
		UIScene *scene = m_scenesToDestroy.back();
		m_scenesToDestroy.pop_back();
		scene->destroyMovie();
	}
	m_scenesToDestroy.clear();

	for(auto & component : m_components)
	{
		component->tick();
	}
	// Note: reverse iterator, the last element is the top of the stack
	int sceneIndex = m_sceneStack.size() - 1;
	while( sceneIndex >= 0 && sceneIndex < m_sceneStack.size() )
	{
		//(*it)->tick();
		UIScene *scene = m_sceneStack[sceneIndex];
		scene->tick();
		--sceneIndex;
		// TODO: We may wish to ignore ticking the rest of the stack based on this scene
	}
}

void UILayer::render(S32 width, S32 height, C4JRender::eViewportType viewport)
{
	if(!ui.IsExpectingOrReloadingSkin())
	{
		for(auto& it : m_components)
		{
			auto itRef = m_componentRefCount.find(it->getSceneType());
			if(itRef != m_componentRefCount.end() && itRef->second.second)
			{
				if(it->isVisible() )
				{
					PIXBeginNamedEvent(0, "Rendering component %d", it->getSceneType() );
					it->render(width, height,viewport);
					PIXEndNamedEvent();
				}
			}
		}
	}
	if(!m_sceneStack.empty())
	{
		int lowestRenderable = m_sceneStack.size() - 1;
		for(;lowestRenderable >= 0; --lowestRenderable)
		{
			if(m_sceneStack[lowestRenderable]->hidesLowerScenes()) break;
		}
		if(lowestRenderable < 0) lowestRenderable = 0;
		for(;lowestRenderable < m_sceneStack.size(); ++lowestRenderable)
		{
			if(m_sceneStack[lowestRenderable]->isVisible() && (!ui.IsExpectingOrReloadingSkin() || m_sceneStack[lowestRenderable]->getSceneType()==eUIScene_Timer))
			{
				PIXBeginNamedEvent(0, "Rendering scene %d", m_sceneStack[lowestRenderable]->getSceneType() );
				m_sceneStack[lowestRenderable]->render(width, height,viewport);
				PIXEndNamedEvent();
			}
		}
	}
}

bool UILayer::IsSceneInStack(EUIScene scene)
{
	bool inStack = false;
	for(int i = static_cast<int>(m_sceneStack.size()) - 1; i >= 0; --i)
	{
		if(m_sceneStack[i]->getSceneType() == scene)
		{
			inStack = true;
			break;
		}
	}
	return inStack;
}

bool UILayer::HasFocus(int iPad)
{
	bool hasFocus = false;
	if(m_hasFocus)
	{
		for(int i = (int)m_sceneStack.size() - 1; i >= 0; --i)
		{
			if(m_sceneStack[i]->stealsFocus() )
			{
				if(m_sceneStack[i]->hasFocus(iPad))
				{
					hasFocus = true;
				}
				break;
			}
		}
	}
	return hasFocus;
}

bool UILayer::hidesLowerScenes()
{
	bool hidesScenes = false;
	for(auto& it : m_components)
	{
		if(it->hidesLowerScenes())
		{
			hidesScenes = true;
			break;
		}
	}
	if(!hidesScenes && !m_sceneStack.empty())
	{
		for(int i = static_cast<int>(m_sceneStack.size()) - 1; i >= 0; --i)
		{
			if(m_sceneStack[i]->hidesLowerScenes())
			{
				hidesScenes = true;
				break;
			}
		}
	}
	return hidesScenes;
}

void UILayer::getRenderDimensions(S32 &width, S32 &height)
{
	m_parentGroup->getRenderDimensions(width, height);
}

void UILayer::DestroyAll()
{
	for(auto& it : m_components)
	{
		it->destroyMovie();
	}
	for(auto& it : m_sceneStack)
	{
		it->destroyMovie();
	}
}

void UILayer::ReloadAll(bool force)
{
	for(auto& it : m_components)
	{
		it->reloadMovie(force);
	}
	if(!m_sceneStack.empty())
	{
		for(auto& lowestRenderable : m_sceneStack)
		{
			lowestRenderable->reloadMovie(force);
		}
	}
}

bool UILayer::GetMenuDisplayed()
{
	return m_bMenuDisplayed;
}

bool UILayer::NavigateToScene(int iPad, EUIScene scene, void *initData)
{
	UIScene *newScene = nullptr;
	switch(scene)
	{
		// Debug
#ifdef _DEBUG_MENUS_ENABLED
	case eUIScene_DebugOverlay:
		newScene = new UIScene_DebugOverlay(iPad, initData, this);
		break;
	case eUIScene_DebugSetCamera:
		newScene = new UIScene_DebugSetCamera(iPad, initData, this);
		break;
	case eUIScene_DebugCreateSchematic:
		newScene = new UIScene_DebugCreateSchematic(iPad, initData, this);
		break;
#endif
	case eUIScene_DebugOptions:
		newScene = new UIScene_DebugOptionsMenu(iPad, initData, this);
		break;

		// Containers
	case eUIScene_InventoryMenu:
		newScene = new UIScene_InventoryMenu(iPad, initData, this);
		break;
	case eUIScene_CreativeMenu:
		newScene = new UIScene_CreativeMenu(iPad, initData, this);
		break;
	case eUIScene_ContainerMenu:
	case eUIScene_LargeContainerMenu:
		newScene = new UIScene_ContainerMenu(iPad, initData, this);
		break;
	case eUIScene_BrewingStandMenu:
		newScene = new UIScene_BrewingStandMenu(iPad, initData, this);
		break;
	case eUIScene_DispenserMenu:
		newScene = new UIScene_DispenserMenu(iPad, initData, this);
		break;
	case eUIScene_EnchantingMenu:
		newScene = new UIScene_EnchantingMenu(iPad, initData, this);
		break;
	case eUIScene_FurnaceMenu:
		newScene = new UIScene_FurnaceMenu(iPad, initData, this);
		break;
	case eUIScene_Crafting2x2Menu:
	case eUIScene_Crafting3x3Menu:
		newScene = new UIScene_CraftingMenu(iPad, initData, this);
		break;
	case eUIScene_TradingMenu:
		newScene = new UIScene_TradingMenu(iPad, initData, this);
		break;
	case eUIScene_AnvilMenu:
		newScene = new UIScene_AnvilMenu(iPad, initData, this);
		break;
	case eUIScene_HopperMenu:
		newScene = new UIScene_HopperMenu(iPad, initData, this);
		break;
	case eUIScene_BeaconMenu:
		newScene = new UIScene_BeaconMenu(iPad, initData, this);
		break;
	case eUIScene_HorseMenu:
		newScene = new UIScene_HorseInventoryMenu(iPad, initData, this);
		break;
	case eUIScene_FireworksMenu:
		newScene = new UIScene_FireworksMenu(iPad, initData, this);
		break;

		// Help and Options
	case eUIScene_HelpAndOptionsMenu:
		newScene = new UIScene_HelpAndOptionsMenu(iPad, initData, this);
		break;
	case eUIScene_SettingsMenu:
		newScene = new UIScene_SettingsMenu(iPad, initData, this);
		break;
	case eUIScene_SettingsOptionsMenu:
		newScene = new UIScene_SettingsOptionsMenu(iPad, initData, this);
		break;
	case eUIScene_SettingsAudioMenu:
		newScene = new UIScene_SettingsAudioMenu(iPad, initData, this);
		break;
	case eUIScene_SettingsControlMenu:
		newScene = new UIScene_SettingsControlMenu(iPad, initData, this);
		break;
	case eUIScene_SettingsGraphicsMenu:
		newScene = new UIScene_SettingsGraphicsMenu(iPad, initData, this);
		break;
	case eUIScene_SettingsUIMenu:
		newScene = new UIScene_SettingsUIMenu(iPad, initData, this);
		break;
	case eUIScene_SkinSelectMenu:
		newScene = new UIScene_SkinSelectMenu(iPad, initData, this);
		break;
	case eUIScene_HowToPlayMenu:
		newScene = new UIScene_HowToPlayMenu(iPad, initData, this);
		break;
	case eUIScene_LanguageSelector:
		newScene = new UIScene_LanguageSelector(iPad, initData, this);
		break;
	case eUIScene_HowToPlay:
		newScene = new UIScene_HowToPlay(iPad, initData, this);
		break;
	case eUIScene_ControlsMenu:
		newScene = new UIScene_ControlsMenu(iPad, initData, this);
		break;
	case eUIScene_ReinstallMenu:
		newScene = new UIScene_ReinstallMenu(iPad, initData, this);
		break;
	case eUIScene_Credits:
		newScene = new UIScene_Credits(iPad, initData, this);
		break;


		// Other in-game
	case eUIScene_PauseMenu:
		newScene = new UIScene_PauseMenu(iPad, initData, this);
		break;
	case eUIScene_DeathMenu:
		newScene = new UIScene_DeathMenu(iPad, initData, this);
		break;
	case eUIScene_ConnectingProgress:
		newScene = new UIScene_ConnectingProgress(iPad, initData, this);
		break;
	case eUIScene_SignEntryMenu:
		newScene = new UIScene_SignEntryMenu(iPad, initData, this);
		break;
	case eUIScene_InGameInfoMenu:
		newScene = new UIScene_InGameInfoMenu(iPad, initData, this);
		break;
	case eUIScene_InGameHostOptionsMenu:
		if (IsSceneInStack(eUIScene_InGameHostOptionsMenu)) {
			app.DebugPrintf("Skipped eUIScene_InGameHostOptionsMenu, we have already this tab!");
			return false;
		}
		newScene = new UIScene_InGameHostOptionsMenu(iPad, initData, this);
		break;
	case eUIScene_InGamePlayerOptionsMenu:
		newScene = new UIScene_InGamePlayerOptionsMenu(iPad, initData, this);
		break;
#if defined(_XBOX_ONE) || defined(__ORBIS__)
	case eUIScene_InGameSaveManagementMenu:
		newScene = new UIScene_InGameSaveManagementMenu(iPad, initData, this);
		break;
#endif
	case eUIScene_TeleportMenu:
		newScene = new UIScene_TeleportMenu(iPad, initData, this);
		break;
	case eUIScene_EndPoem:
		if(IsSceneInStack(eUIScene_EndPoem))
		{
			app.DebugPrintf("Skipped EndPoem as one was already showing\n");
			return false;
		}
		else
		{
			newScene = new UIScene_EndPoem(iPad, initData, this);
		}
		break;


		// Frontend
	case eUIScene_TrialExitUpsell:
		newScene = new UIScene_TrialExitUpsell(iPad, initData, this);
		break;
	case eUIScene_Intro:
		newScene = new UIScene_Intro(iPad, initData, this);
		break;
	case eUIScene_SaveMessage:
		newScene = new UIScene_SaveMessage(iPad, initData, this);
		break;
	case eUIScene_MainMenu:
		newScene = new UIScene_MainMenu(iPad, initData, this);
		break;
	case eUIScene_LoadOrJoinMenu:
		newScene = new UIScene_LoadOrJoinMenu(iPad, initData, this);
		break;
	case eUIScene_LoadMenu:
		newScene = new UIScene_LoadMenu(iPad, initData, this);
		break;
	case eUIScene_JoinMenu:
		newScene = new UIScene_JoinMenu(iPad, initData, this);
		break;
	case eUIScene_CreateWorldMenu:
		newScene = new UIScene_CreateWorldMenu(iPad, initData, this);
		break;
	case eUIScene_LaunchMoreOptionsMenu:
		newScene = new UIScene_LaunchMoreOptionsMenu(iPad, initData, this);
		break;
	case eUIScene_FullscreenProgress:
		newScene = new UIScene_FullscreenProgress(iPad, initData, this);
		break;
	case eUIScene_LeaderboardsMenu:
		newScene = new UIScene_LeaderboardsMenu(iPad, initData, this);
		break;
	case eUIScene_DLCMainMenu:
		newScene = new UIScene_DLCMainMenu(iPad, initData, this);
		break;
	case eUIScene_DLCOffersMenu:
		newScene = new UIScene_DLCOffersMenu(iPad, initData, this);
		break;
	case eUIScene_EULA:
		newScene = new UIScene_EULA(iPad, initData, this);
		break;
	case eUIScene_NewUpdateMessage:
		newScene = new UIScene_NewUpdateMessage(iPad, initData, this);
		break;

		// Other
	case eUIScene_Keyboard:
		newScene = new UIScene_Keyboard(iPad, initData, this);
		break;
	case eUIScene_QuadrantSignin:
		newScene = new UIScene_QuadrantSignin(iPad, initData, this);
		break;
	case eUIScene_MessageBox:
		if(IsSceneInStack(eUIScene_MessageBox))
		{
			app.DebugPrintf("Skipped MessageBox as one was already showing\n");
			return false;
		}
		else
		{
			newScene = new UIScene_MessageBox(iPad, initData, this);
		}
		break;
	case eUIScene_Timer:
		newScene = new UIScene_Timer(iPad, initData, this);
		break;
	};

	if(newScene == nullptr)
	{
		app.DebugPrintf("WARNING: Scene %d was not created. Add it to UILayer::NavigateToScene\n", scene);
		return false;
	}

	if(m_sceneStack.size() > 0)
	{
		newScene->setBackScene(m_sceneStack[m_sceneStack.size()-1]);
	}

	m_sceneStack.push_back(newScene);

	updateFocusState();

	newScene->tick();

	return true;
}

bool UILayer::NavigateBack(int iPad, EUIScene eScene)
{
	if(m_sceneStack.size() == 0) return false;

	bool navigated = false;
	if(eScene < eUIScene_COUNT)
	{
		UIScene *scene = nullptr;
		do
		{
			scene = m_sceneStack.back();
			if(scene->getSceneType() == eScene)
			{
				navigated = true;
				break;
			}
			else
			{
				if(scene->hasFocus(iPad))
				{
					removeScene(scene);
				}
				else
				{
					// No focus on the top scene, so this use shouldn't be navigating!
					break;
				}
			}
		} while(m_sceneStack.size() > 0);

	}
	else
	{
		UIScene *scene = m_sceneStack.back();
		if(scene->hasFocus(iPad))
		{
			removeScene(scene);
			navigated = true;
		}
	}
	return navigated;
}

void UILayer::showComponent(int iPad, EUIScene scene, bool show)
{
    auto it = m_componentRefCount.find(scene);
    if(it != m_componentRefCount.end())
	{
		it->second.second = show;
		return;
	}
	if(show) addComponent(iPad,scene);
}

bool UILayer::isComponentVisible(EUIScene scene)
{
	bool visible = false;
    auto it = m_componentRefCount.find(scene);
    if(it != m_componentRefCount.end())
	{
		visible = it->second.second;
	}
	return visible;
}

UIScene *UILayer::addComponent(int iPad, EUIScene scene, void *initData)
{
    auto it = m_componentRefCount.find(scene);
    if(it != m_componentRefCount.end())
	{
		++it->second.first;

		for(auto& itComp : m_components)
		{
			if( itComp->getSceneType() == scene )
			{
				return itComp;
			}
		}
		return nullptr;
	}
	UIScene *newScene = nullptr;

	switch(scene)
	{
	case eUIComponent_Panorama:
		newScene = new UIComponent_Panorama(iPad, initData, this);
		m_componentRefCount[scene] = pair<int,bool>(1,true);
		break;
	case eUIComponent_DebugUIConsole:
		newScene = new UIComponent_DebugUIConsole(iPad, initData, this);
		m_componentRefCount[scene] = pair<int,bool>(1,true);
		break;
	case eUIComponent_DebugUIMarketingGuide:
		newScene = new UIComponent_DebugUIMarketingGuide(iPad, initData, this);
		m_componentRefCount[scene] = pair<int,bool>(1,true);
		break;
	case eUIComponent_Logo:
		newScene = new UIComponent_Logo(iPad, initData, this);
		m_componentRefCount[scene] = pair<int,bool>(1,true);
		break;
	case eUIComponent_Tooltips:
		newScene = new UIComponent_Tooltips(iPad, initData, this);
		m_componentRefCount[scene] = pair<int,bool>(1,true);
		break;
	case eUIComponent_TutorialPopup:
		newScene = new UIComponent_TutorialPopup(iPad, initData, this);
		// Start hidden
		m_componentRefCount[scene] = pair<int,bool>(1,false);
		break;
	case eUIScene_HUD:
		newScene = new UIScene_HUD(iPad, initData, this);
		// Start hidden
		m_componentRefCount[scene] = pair<int,bool>(1,false);
		break;
	case eUIComponent_Chat:
		newScene = new UIComponent_Chat(iPad, initData, this);
		m_componentRefCount[scene] = pair<int,bool>(1,true);
		break;
	case eUIComponent_PressStartToPlay:
		newScene = new UIComponent_PressStartToPlay(iPad, initData, this);
		m_componentRefCount[scene] = pair<int,bool>(1,true);
		break;
	case eUIComponent_MenuBackground:
		newScene = new UIComponent_MenuBackground(iPad, initData, this);
		m_componentRefCount[scene] = pair<int,bool>(1,true);
		break;
	};

	if(newScene == nullptr) return nullptr;

	m_components.push_back(newScene);

	return newScene;
}

void UILayer::removeComponent(EUIScene scene)
{
    auto it = m_componentRefCount.find(scene);
    if(it != m_componentRefCount.end())
	{
		--it->second.first;

		if(it->second.first <= 0)
		{
			m_componentRefCount.erase(it);
            for (auto compIt = m_components.begin(); compIt != m_components.end();)
            {
				if( (*compIt)->getSceneType() == scene)
				{
#ifdef __PSVITA__
					// remove any touchboxes
					ui.TouchBoxesClear((*compIt));
#endif
					m_scenesToDelete.push_back((*compIt));
					(*compIt)->handleDestroy(); // For anything that might require the pointer be valid
					compIt = m_components.erase(compIt);
				}
				else
				{
					++compIt;
				}
			}
		}
	}
}

void UILayer::removeScene(UIScene *scene)
{
#ifdef __PSVITA__
	// remove any touchboxes
	ui.TouchBoxesClear(scene);
#endif

    auto newEnd = std::remove(m_sceneStack.begin(), m_sceneStack.end(), scene);
    m_sceneStack.erase(newEnd, m_sceneStack.end());

	m_scenesToDelete.push_back(scene);

	scene->handleDestroy(); // For anything that might require the pointer be valid

	bool hadFocus = m_hasFocus;
	updateFocusState();

	// If this layer has focus, pass it on
	if (m_hasFocus || hadFocus)
	{
		m_hasFocus = false;
		m_parentGroup->UpdateFocusState();
	}
}

void UILayer::closeAllScenes()
{
	vector<UIScene *> temp;
	temp.insert(temp.end(), m_sceneStack.begin(), m_sceneStack.end());
	m_sceneStack.clear();
	for(auto& it : temp)
	{
#ifdef __PSVITA__
		// remove any touchboxes
		ui.TouchBoxesClear(it);
#endif
		m_scenesToDelete.push_back(it);
		it->handleDestroy(); // For anything that might require the pointer be valid
	}

	updateFocusState();

	// If this layer has focus, pass it on
	if (m_hasFocus)
	{
		m_hasFocus = false;
		m_parentGroup->UpdateFocusState();
	}
}

// Get top scene on stack (or nullptr if stack is empty)
UIScene *UILayer::GetTopScene()
{
	if(m_sceneStack.size() == 0)
	{
		return nullptr;
	}
	else
	{
		return m_sceneStack[m_sceneStack.size()-1];
	}
}

// Updates layer focus state if no error message is present (unless this is the error layer)
bool UILayer::updateFocusState(bool allowedFocus /* = false */)
{
	// If haveFocus is false, request it
	if (!allowedFocus)
	{
		// To update focus in this layer we need to request focus from group
		// Focus will be denied if there's an upper layer that needs focus
		allowedFocus = m_parentGroup->RequestFocus(this);
	}

	m_bMenuDisplayed = false;
	m_bPauseMenuDisplayed = false;
	m_bContainerMenuDisplayed = false;
	m_bIgnoreAutosaveMenuDisplayed = false;
	m_bIgnorePlayerJoinMenuDisplayed = false;

	bool layerFocusSet = false;
    for (auto it = m_sceneStack.rbegin(); it != m_sceneStack.rend(); ++it)
    {
		UIScene *scene = *it;

		// UPDATE FOCUS STATES
		if(!layerFocusSet && allowedFocus && scene->stealsFocus())
		{
			scene->gainFocus();
			layerFocusSet = true;
		}
		else
		{
			scene->loseFocus();
			if(allowedFocus && app.GetGameStarted())
			{
				// 4J Stu - This is a memory optimisation so we don't keep scenes loaded in memory all the time
				// This is required for PS3 (and likely Vita), but I'm removing it on XboxOne so that we can avoid
				// the scene creation time (which can be >0.5s) since we have the memory to spare
#ifndef _XBOX_ONE
				m_scenesToDestroy.push_back(scene);
#endif
			}

			if (scene->getSceneType() == eUIScene_SettingsOptionsMenu)
			{
				scene->loseFocus();
				m_scenesToDestroy.push_back(scene);
			}
		}

		/// UPDATE STACK STATES

		// 4J-PB - this should just be true
		m_bMenuDisplayed=true;

		EUIScene sceneType = scene->getSceneType();
		switch(sceneType)
		{
		case eUIScene_PauseMenu:
			m_bPauseMenuDisplayed = true;
			break;
		case eUIScene_Crafting2x2Menu:
		case eUIScene_Crafting3x3Menu:
		case eUIScene_FurnaceMenu:
		case eUIScene_ContainerMenu:
		case eUIScene_LargeContainerMenu:
		case eUIScene_InventoryMenu:
		case eUIScene_CreativeMenu:
		case eUIScene_DispenserMenu:
		case eUIScene_BrewingStandMenu:
		case eUIScene_EnchantingMenu:
		case eUIScene_TradingMenu:
		case eUIScene_HopperMenu:
		case eUIScene_HorseMenu:
		case eUIScene_FireworksMenu:
		case eUIScene_BeaconMenu:
		case eUIScene_AnvilMenu:
			m_bContainerMenuDisplayed=true;

			// Intentional fall-through
		case eUIScene_DeathMenu:
		case eUIScene_FullscreenProgress:
		case eUIScene_SignEntryMenu:
		case eUIScene_EndPoem:
			m_bIgnoreAutosaveMenuDisplayed = true;
			break;
		}

		switch(sceneType)
		{
		case eUIScene_FullscreenProgress:
		case eUIScene_EndPoem:
		case eUIScene_Credits:
		case eUIScene_LeaderboardsMenu:
			m_bIgnorePlayerJoinMenuDisplayed = true;
			break;
		}
	}
	m_hasFocus = layerFocusSet;

	return m_hasFocus;
}

#ifdef __PSVITA__
UIScene *UILayer::getCurrentScene()
{
	// Note: reverse iterator, the last element is the top of the stack
	for( auto it = m_sceneStack.rbegin(); it != m_sceneStack.rend(); ++it)
	{
		UIScene *scene = *it;
		// 4J-PB - only used on Vita, so iPad 0 is fine
		if(scene->hasFocus(0) && scene->canHandleInput())
		{
			return scene;
		}
}

	return nullptr;
}
#endif

void UILayer::handleInput(int iPad, int key, bool repeat, bool pressed, bool released, bool &handled)
{
	// Note: reverse iterator, the last element is the top of the stack
    for (auto it = m_sceneStack.rbegin(); it != m_sceneStack.rend(); ++it)
    {
		UIScene *scene = *it;
		if(scene->hasFocus(iPad) && scene->canHandleInput())
		{
			// 4J-PB - ignore repeats of action ABXY buttons
			// fix for PS3 213 - [MAIN MENU] Holding down buttons will continue to activate every prompt.
			// 4J Stu - Changed this slightly to add the allowRepeat function so we can allow repeats in the crafting menu
			if(repeat && !scene->allowRepeat(key) )
			{
				return;
			}
			scene->handleInput(iPad, key, repeat, pressed, released, handled);
		}

		// Fix for PS3 #444 - [IN GAME] If the user keeps pressing CROSS while on the 'Save Game' screen the title will crash.
		handled = handled || scene->hidesLowerScenes() || scene->blocksInput();
		if(handled ) break;
	}

	// Components can't take input or focus
}

void UILayer::HandleDLCMountingComplete()
{
    for (auto it = m_sceneStack.rbegin(); it != m_sceneStack.rend(); ++it)
    {
		UIScene *topScene = *it;
		app.DebugPrintf("UILayer::HandleDLCMountingComplete - topScene\n");
		topScene->HandleDLCMountingComplete();
	}
}

void UILayer::HandleDLCInstalled()
{
    for (auto it = m_sceneStack.rbegin(); it != m_sceneStack.rend(); ++it)
    {
		UIScene *topScene = *it;
		topScene->HandleDLCInstalled();
	}
}

#ifdef _XBOX_ONE
void UILayer::HandleDLCLicenseChange()
{
	for( auto it = m_sceneStack.rbegin(); it != m_sceneStack.rend(); ++it)
	{
		UIScene *topScene = *it;
		topScene->HandleDLCLicenseChange();
	}
}
#endif

void UILayer::HandleMessage(EUIMessage message, void *data)
{
    for (auto it = m_sceneStack.rbegin(); it != m_sceneStack.rend(); ++it)
    {
		UIScene *topScene = *it;
		topScene->HandleMessage(message, data);
	}
}

bool UILayer::IsFullscreenGroup()
{
	return m_parentGroup->IsFullscreenGroup();
}

C4JRender::eViewportType UILayer::getViewport()
{
	return m_parentGroup->GetViewportType();
}


void UILayer::handleUnlockFullVersion()
{
	for(auto& it : m_sceneStack)
	{
		it->handleUnlockFullVersion();
	}
}

void UILayer::PrintTotalMemoryUsage(int64_t &totalStatic, int64_t &totalDynamic)
{
	int64_t layerStatic = 0;
	int64_t layerDynamic = 0;
	for(auto& it : m_components)
	{
		it->PrintTotalMemoryUsage(layerStatic, layerDynamic);
	}
	for(auto& it : m_sceneStack)
	{
		it->PrintTotalMemoryUsage(layerStatic, layerDynamic);
	}
	app.DebugPrintf(app.USER_SR, "  \\- Layer static: %d , Layer dynamic: %d\n", layerStatic, layerDynamic);
	totalStatic += layerStatic;
	totalDynamic += layerDynamic;
}

// Returns the first scene of given type if it exists, nullptr otherwise
UIScene *UILayer::FindScene(EUIScene sceneType)
{
	for (size_t i = 0; i < m_sceneStack.size(); i++)
	{
		if (m_sceneStack[i]->getSceneType() == sceneType)
		{
			return m_sceneStack[i];
		}
	}

	return nullptr;
}