the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "UIGroup.h"
3
4UIGroup::UIGroup(EUIGroup group, int iPad)
5{
6 m_group = group;
7 m_iPad = iPad;
8 m_bMenuDisplayed = false;
9 m_bPauseMenuDisplayed = false;
10 m_bContainerMenuDisplayed = false;
11 m_bIgnoreAutosaveMenuDisplayed = false;
12 m_bIgnorePlayerJoinMenuDisplayed = false;
13
14 m_updateFocusStateCountdown = 0;
15
16 m_viewportType = C4JRender::VIEWPORT_TYPE_FULLSCREEN;
17
18 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
19 {
20 m_layers[i] = new UILayer(this);
21#ifdef __PSVITA__
22 m_layers[i]->m_iLayer=(EUILayer)i;
23#endif
24 }
25
26 m_tooltips = (UIComponent_Tooltips *)m_layers[(int)eUILayer_Tooltips]->addComponent(0, eUIComponent_Tooltips);
27
28 m_tutorialPopup = NULL;
29 m_hud = NULL;
30 m_pressStartToPlay = NULL;
31 if(m_group != eUIGroup_Fullscreen)
32 {
33 m_tutorialPopup = (UIComponent_TutorialPopup *)m_layers[(int)eUILayer_Popup]->addComponent(m_iPad, eUIComponent_TutorialPopup);
34
35 m_hud = (UIScene_HUD *)m_layers[(int)eUILayer_HUD]->addComponent(m_iPad, eUIScene_HUD);
36
37 //m_layers[(int)eUILayer_Chat]->addComponent(m_iPad, eUIComponent_Chat);
38 }
39 else
40 {
41 m_pressStartToPlay = (UIComponent_PressStartToPlay *)m_layers[(int)eUILayer_Tooltips]->addComponent(0, eUIComponent_PressStartToPlay);
42 }
43
44 // 4J Stu - Pre-allocate this for cached rendering in scenes. It's horribly slow to do dynamically, but we should only need one
45 // per group as we will only be displaying one of these types of scenes at a time
46 m_commandBufferList = MemoryTracker::genLists(1);
47}
48
49void UIGroup::DestroyAll()
50{
51 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
52 {
53 m_layers[i]->DestroyAll();
54 }
55}
56
57void UIGroup::ReloadAll()
58{
59 // We only need to reload things when they are likely to be rendered
60 int highestRenderable = 0;
61 for(; highestRenderable < eUILayer_COUNT; ++highestRenderable)
62 {
63 if(m_layers[highestRenderable]->hidesLowerScenes()) break;
64 }
65 if(highestRenderable < eUILayer_Fullscreen) highestRenderable = eUILayer_Fullscreen;
66 for(; highestRenderable >= 0; --highestRenderable)
67 {
68 if(highestRenderable < eUILayer_COUNT) m_layers[highestRenderable]->ReloadAll(highestRenderable != (int)eUILayer_Fullscreen);
69 }
70}
71
72void UIGroup::tick()
73{
74 // Ignore this group if the player isn't signed in
75 if(m_iPad >= 0 && !ProfileManager.IsSignedIn(m_iPad)) return;
76 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
77 {
78 m_layers[i]->tick();
79
80 // TODO: May wish to ignore ticking other layers here based on current layer
81 }
82
83 // Handle deferred update focus
84 if (m_updateFocusStateCountdown > 0)
85 {
86 m_updateFocusStateCountdown--;
87 if (m_updateFocusStateCountdown == 0)_UpdateFocusState();
88}
89}
90
91void UIGroup::render()
92{
93 // Ignore this group if the player isn't signed in
94 if(m_iPad >= 0 && !ProfileManager.IsSignedIn(m_iPad)) return;
95 S32 width = 0;
96 S32 height = 0;
97 ui.getRenderDimensions(m_viewportType, width, height);
98 int highestRenderable = 0;
99 for(; highestRenderable < eUILayer_COUNT; ++highestRenderable)
100 {
101 if(m_layers[highestRenderable]->hidesLowerScenes()) break;
102 }
103 for(; highestRenderable >= 0; --highestRenderable)
104 {
105 if(highestRenderable < eUILayer_COUNT) m_layers[highestRenderable]->render(width, height,m_viewportType);
106 }
107}
108
109bool UIGroup::hidesLowerScenes()
110{
111 // Ignore this group if the player isn't signed in
112 if(m_iPad >= 0 && !ProfileManager.IsSignedIn(m_iPad)) return false;
113 bool hidesScenes = false;
114 for(int i = eUILayer_COUNT - 1; i >= 0; --i)
115 {
116 hidesScenes = m_layers[i]->hidesLowerScenes();
117 if(hidesScenes) break;
118 }
119 return hidesScenes;
120}
121
122void UIGroup::getRenderDimensions(S32 &width, S32 &height)
123{
124 ui.getRenderDimensions(m_viewportType, width, height);
125}
126
127// NAVIGATION
128bool UIGroup::NavigateToScene(int iPad, EUIScene scene, void *initData, EUILayer layer)
129{
130 bool succeeded = m_layers[(int)layer]->NavigateToScene(iPad, scene, initData);
131 updateStackStates();
132 return succeeded;
133}
134
135bool UIGroup::NavigateBack(int iPad, EUIScene eScene, EUILayer eLayer)
136{
137 // Keep navigating back on every layer until we hit the target scene
138 bool foundTarget = false;
139 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
140 {
141 if(eLayer < eUILayer_COUNT && eLayer != i) continue;
142 foundTarget = m_layers[i]->NavigateBack(iPad, eScene);
143 if(foundTarget) break;
144 }
145 updateStackStates();
146 return foundTarget;
147}
148
149void UIGroup::closeAllScenes()
150{
151 Minecraft *pMinecraft = Minecraft::GetInstance();
152 if( m_iPad >= 0 )
153 {
154 if(pMinecraft != NULL && pMinecraft->localgameModes[m_iPad] != NULL )
155 {
156 TutorialMode *gameMode = (TutorialMode *)pMinecraft->localgameModes[m_iPad];
157
158 // This just allows it to be shown
159 gameMode->getTutorial()->showTutorialPopup(true);
160 }
161 }
162
163 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
164 {
165 // Ignore the error layer
166 if(i != (int)eUILayer_Error) m_layers[i]->closeAllScenes();
167 }
168 updateStackStates();
169}
170
171UIScene *UIGroup::GetTopScene(EUILayer layer)
172{
173 return m_layers[(int)layer]->GetTopScene();
174}
175
176bool UIGroup::GetMenuDisplayed()
177{
178 return m_bMenuDisplayed;
179}
180
181bool UIGroup::IsSceneInStack(EUIScene scene)
182{
183 bool found = false;
184 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
185 {
186 found = m_layers[i]->IsSceneInStack(scene);
187 if(found) break;
188 }
189 return found;
190}
191
192bool UIGroup::HasFocus(int iPad)
193{
194 bool hasFocus = false;
195 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
196 {
197 if( m_layers[i]->m_hasFocus)
198 {
199 if(m_layers[i]->HasFocus(iPad))
200 {
201 hasFocus = true;
202 }
203 break;
204 }
205 }
206 return hasFocus;
207}
208
209#ifdef __PSVITA__
210UIScene *UIGroup::getCurrentScene()
211{
212 UIScene *pScene;
213 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
214 {
215 pScene=m_layers[i]->getCurrentScene();
216
217 if(pScene!=NULL) return pScene;
218 }
219
220 return NULL;
221}
222#endif
223
224// INPUT
225void UIGroup::handleInput(int iPad, int key, bool repeat, bool pressed, bool released, bool &handled)
226{
227 // Ignore this group if the player isn't signed in
228 if(m_iPad >= 0 && !ProfileManager.IsSignedIn(m_iPad)) return;
229 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
230 {
231 m_layers[i]->handleInput(iPad, key, repeat, pressed, released, handled);
232 if(handled) break;
233 }
234}
235
236// FOCUS
237
238// Check that a layer may recieve focus, specifically that there is no infocus layer above
239bool UIGroup::RequestFocus(UILayer* layerPtr)
240{
241 // Find the layer
242 unsigned int layerIndex = GetLayerIndex(layerPtr);
243
244 // Top layer is always allowed focus
245 if (layerIndex == 0) return true;
246
247 // Check layers above to see if any of them have focus
248 for (int i = layerIndex-1; i >= 0; i--)
249 {
250 if (m_layers[i]->m_hasFocus) return false;
251 }
252
253 return true;
254}
255
256void UIGroup::showComponent(int iPad, EUIScene scene, EUILayer layer, bool show)
257{
258 m_layers[layer]->showComponent(iPad, scene, show);
259}
260
261UIScene *UIGroup::addComponent(int iPad, EUIScene scene, EUILayer layer)
262{
263 return m_layers[layer]->addComponent(iPad, scene);
264}
265
266void UIGroup::removeComponent(EUIScene scene, EUILayer layer)
267{
268 m_layers[layer]->removeComponent(scene);
269}
270
271void UIGroup::SetViewportType(C4JRender::eViewportType type)
272{
273 if(m_viewportType != type)
274 {
275 m_viewportType = type;
276 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
277 {
278 m_layers[i]->ReloadAll(true);
279 }
280 }
281}
282
283C4JRender::eViewportType UIGroup::GetViewportType()
284{
285 return m_viewportType;
286}
287
288void UIGroup::HandleDLCMountingComplete()
289{
290 // Ignore this group if the player isn't signed in
291 if(m_iPad >= 0 && !ProfileManager.IsSignedIn(m_iPad)) return;
292 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
293 {
294 app.DebugPrintf("UIGroup::HandleDLCMountingComplete - m_layers[%d]\n",i);
295 m_layers[i]->HandleDLCMountingComplete();
296 }
297}
298
299void UIGroup::HandleDLCInstalled()
300{
301 // Ignore this group if the player isn't signed in
302 if(m_iPad >= 0 && !ProfileManager.IsSignedIn(m_iPad)) return;
303 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
304 {
305 m_layers[i]->HandleDLCInstalled();
306 }
307}
308
309#ifdef _XBOX_ONE
310void UIGroup::HandleDLCLicenseChange()
311{
312 // Ignore this group if the player isn't signed in
313 if(m_iPad >= 0 && !ProfileManager.IsSignedIn(m_iPad)) return;
314 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
315 {
316 m_layers[i]->HandleDLCLicenseChange();
317 }
318}
319#endif
320
321void UIGroup::HandleMessage(EUIMessage message, void *data)
322{
323 // Ignore this group if the player isn't signed in
324 if(m_iPad >= 0 && !ProfileManager.IsSignedIn(m_iPad)) return;
325 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
326 {
327 m_layers[i]->HandleMessage(message, data);
328 }
329}
330
331bool UIGroup::IsFullscreenGroup()
332{
333 return m_group == eUIGroup_Fullscreen;
334}
335
336
337void UIGroup::handleUnlockFullVersion()
338{
339 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
340 {
341 m_layers[i]->handleUnlockFullVersion();
342 }
343}
344
345void UIGroup::updateStackStates()
346{
347 m_bMenuDisplayed = false;
348 m_bPauseMenuDisplayed = false;
349 m_bContainerMenuDisplayed = false;
350 m_bIgnoreAutosaveMenuDisplayed = false;
351 m_bIgnorePlayerJoinMenuDisplayed = false;
352
353 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
354 {
355 m_bMenuDisplayed = m_bMenuDisplayed || m_layers[i]->m_bMenuDisplayed;
356 m_bPauseMenuDisplayed = m_bPauseMenuDisplayed || m_layers[i]->m_bPauseMenuDisplayed;
357 m_bContainerMenuDisplayed = m_bContainerMenuDisplayed || m_layers[i]->m_bContainerMenuDisplayed;
358 m_bIgnoreAutosaveMenuDisplayed = m_bIgnoreAutosaveMenuDisplayed || m_layers[i]->m_bIgnoreAutosaveMenuDisplayed;
359 m_bIgnorePlayerJoinMenuDisplayed = m_bIgnorePlayerJoinMenuDisplayed || m_layers[i]->m_bIgnorePlayerJoinMenuDisplayed;
360 }
361}
362
363// Defer update focus till for 10 UI ticks
364void UIGroup::UpdateFocusState()
365{
366 m_updateFocusStateCountdown = 10;
367}
368
369// Pass focus to uppermost layer that accepts focus
370void UIGroup::_UpdateFocusState()
371{
372 bool groupFocusSet = false;
373
374 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
375 {
376 groupFocusSet = m_layers[i]->updateFocusState(true);
377 if (groupFocusSet) break;
378 }
379}
380
381// Get the index of the layer
382unsigned int UIGroup::GetLayerIndex(UILayer* layerPtr)
383{
384 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
385 {
386 if (m_layers[i] == layerPtr) return i;
387 }
388
389 // can't get here...
390 return 0;
391}
392
393void UIGroup::PrintTotalMemoryUsage(__int64 &totalStatic, __int64 &totalDynamic)
394{
395 __int64 groupStatic = 0;
396 __int64 groupDynamic = 0;
397 app.DebugPrintf(app.USER_SR, "-- BEGIN GROUP %d\n",m_group);
398 for(unsigned int i = 0; i < eUILayer_COUNT; ++i)
399 {
400 app.DebugPrintf(app.USER_SR, " \\- BEGIN LAYER %d\n",i);
401 m_layers[i]->PrintTotalMemoryUsage(groupStatic, groupDynamic);
402 app.DebugPrintf(app.USER_SR, " \\- END LAYER %d\n",i);
403 }
404 app.DebugPrintf(app.USER_SR, "-- Group static: %d, Group dynamic: %d\n", groupStatic, groupDynamic);
405 totalStatic += groupStatic;
406 totalDynamic += groupDynamic;
407 app.DebugPrintf(app.USER_SR, "-- END GROUP %d\n",m_group);
408}
409
410int UIGroup::getCommandBufferList()
411{
412 return m_commandBufferList;
413}
414
415// Returns the first scene of given type if it exists, NULL otherwise
416UIScene *UIGroup::FindScene(EUIScene sceneType)
417{
418 UIScene *pScene = NULL;
419
420 for (int i = 0; i < eUILayer_COUNT; i++)
421 {
422 pScene = m_layers[i]->FindScene(sceneType);
423#ifdef __PS3__
424 if (pScene != NULL) return pScene;
425#else
426 if (pScene != nullptr) return pScene;
427#endif
428 }
429
430 return pScene;
431}