· 5 years ago · May 17, 2020, 01:42 AM
1package com.virtual_hex.engine.screens;
2
3import com.badlogic.gdx.Gdx;
4import com.badlogic.gdx.InputProcessor;
5import com.badlogic.gdx.assets.AssetManager;
6import com.kotcrab.vis.ui.VisUI;
7import com.virtual_hex.engine.VirtualHexApp;
8import com.virtual_hex.engine.app.AppLauncher;
9import com.virtual_hex.engine.screens.defaults.DefaultMenuScreen;
10import com.virtual_hex.engine.screens.defaults.DefaultMenuSystemAsset;
11import org.slf4j.Logger;
12import org.slf4j.LoggerFactory;
13
14/**
15 * Screen Inputs:
16 * This screen class will provide each screen with uniform API for being able to have defaults or
17 * override functionality. Each screen occupies Index 3 of the array backing the InputMultiplexer
18 * this will enable each screen to be able to provide catch all handling for anything not caught
19 * by other InputProcessors. By default the following table will list keys handled by the abstract.
20 *
21 * Screen Defaults:
22 * Each class that extends AbstractVirtualHex will have access to a default menu "ESC" by default, which will provide
23 * default navigation between the current screen and the starting screen, as well as the option to return to current
24 * screen and exit application. The menu can be overridden in full or specific menu items can be dropped in
25 * to the default menu by using the extending the appropriate function.
26 *
27 *
28 *
29 * Each default component will be made of default components to allow expansion as possible with ease
30 * but providing a out of the box functionality for a default editor and game frame work
31 *
32 *
33 *
34 */
35public abstract class AbstractVirtualHexScreen implements VirtualHexScreen, InputProcessor {
36
37 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractVirtualHexScreen.class);
38
39 protected final VirtualHexApp parentApp;
40 protected final ScreenMetaData metaData;
41
42 protected final ScreenControlManager controlManager;
43 protected final MenuSystemAsset menuSystemAsset;
44
45 protected AssetManager assetManager;
46
47 protected boolean loaded;
48 protected float loadingProgress;
49
50 public AbstractVirtualHexScreen(AppLauncher<VirtualHexApp> appLauncher, ScreenMetaData metaData){
51 this.parentApp = appLauncher.getApp();
52 this.metaData = metaData;
53
54 this.assetManager = new AssetManager();
55
56 this.controlManager = new ScreenControlManager();
57
58 this.menuSystemAsset = getMenuSystemAsset();
59
60 try {
61 this.controlManager.addProcessor(3, this);
62 } catch (Exception e) {
63 e.printStackTrace();
64 }
65 }
66
67 protected MenuSystemAsset getMenuSystemAsset() {
68 return new DefaultMenuSystemAsset(parentApp, this);
69 }
70
71 @Override
72 public ScreenMetaData getMetaData() {
73 return metaData;
74 }
75
76 @Override
77 public void resize(int width, int height) {
78 LOGGER.info("Entering method resize(); : width, height ({}, {})", width, height);
79 }
80
81 @Override
82 public void show() {
83 LOGGER.info("Entering method show();");
84 Gdx.input.setInputProcessor(controlManager.inputMultiplexer);
85 }
86
87 @Override
88 public VirtualHexApp getParentApp() {
89 return parentApp;
90 }
91
92
93 @Override
94 public void hide() {
95 LOGGER.info("Entering method hide();");
96 }
97
98 @Override
99 public void pause() {
100 LOGGER.info("Entering method pause();");
101 }
102
103 @Override
104 public void resume() {
105 LOGGER.info("Entering method resume();");
106 }
107
108
109 @Override
110 public void dispose() {
111 LOGGER.info("Entering method dispose();");
112 assetManager.dispose();
113 VisUI.dispose();
114 }
115
116 @Override
117 public boolean isLoaded(){
118 return loaded;
119 }
120
121 @Override
122 public AssetManager getAssetManager() {
123 return assetManager;
124 }
125
126 /**
127 *
128 * @return a ScreenChangeScreenAction that is called upon
129 * when the default menu key inputs are caught at the last
130 * inout handling layer which is the abstract default
131 * implementations. Override this to provide a new
132 * action when the default menu catches are used.
133 * Can do anything, to include replacing the menu with a new one,
134 * returning to the main screen or another screen
135 */
136 public ScreenAction getMenuScreenAction(){
137 return new ScreenChangeScreenAction(new DefaultMenuScreen(parentApp.appLauncher, this, menuSystemAsset));
138 }
139
140 /**
141 * If this method is overwritten then if super.keyDown() is not called then the menu screen will not
142 * be triggered
143 *
144 * By default the AbstractVirtualHexScreen uses Input.Keys.ESC on PC, Input.Keys.BACK and Input.Keys.MENU for
145 * android to trigger the menu this screen has. If the screen has no menu a default will allow the screen
146 * to trigger an application exit, back to the main screen, or back to the game.
147 *
148 * https://github.com/libgdx/libgdx/wiki/Back-and-menu-key-catching
149 *
150 * {@inheritDoc}
151 */
152 @Override
153 public boolean keyDown(int keycode) {
154 boolean hasCatchKey = menuSystemAsset.hasCatchKey(keycode);
155 // Menu
156 if(hasCatchKey){
157 ScreenAction menuScreenAction = getMenuScreenAction();
158 menuScreenAction.doAction(parentApp, this);
159 }
160 return false;
161 }
162
163 @Override
164 public boolean keyUp(int keycode) {
165 return false;
166 }
167
168 @Override
169 public boolean keyTyped(char character) {
170 return false;
171 }
172
173 @Override
174 public boolean touchDown(int screenX, int screenY, int pointer, int button) {
175 return false;
176 }
177
178 @Override
179 public boolean touchUp(int screenX, int screenY, int pointer, int button) {
180 return false;
181 }
182
183 @Override
184 public boolean touchDragged(int screenX, int screenY, int pointer) {
185 return false;
186 }
187
188 @Override
189 public boolean mouseMoved(int screenX, int screenY) {
190 return false;
191 }
192
193 @Override
194 public boolean scrolled(int amount) {
195 return false;
196 }
197}