Java tutorial
/******************************************************************************* * Copyright 2012-Present, MoribitoTech * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package com.moribitotech.mtx; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.scenes.scene2d.utils.Drawable; import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; import com.badlogic.gdx.utils.Scaling; import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.Viewport; import com.moribitotech.mtx.settings.AppSettings; import com.moribitotech.mtx.settings.MtxLogger; public abstract class AbstractScreen implements Screen { // protected static final String logTag = "MtxScreenLog"; public static boolean logActive = true; // Game reference private Game game; // Initial private String screenName = "Untitled Screen"; private final Stage stage; // Screen second counter (1 second tick) private float startTime = System.nanoTime(); public static float SECONDS_TIME = 0; // Animation timer (If any animation is used) private float stateTime = 0; // Custom back button private boolean isBackButtonActive = false; /** * Construct the screen * <p> * -Gives reference to game<br> * -Creates stage<br> * -Centers camera of stage<br> * -Sets Input processor for stage (Gdx.input.setInputProcessor(stage))<br> * -Calls setUpScreenElements (Good place the set views and iniatial * elements) * * @param game * the main game class * @param screenName * the name of the screen * */ public AbstractScreen(Game game, String screenName) { super(); this.game = game; if (screenName.equals("")) { this.screenName = "Untitled Screen"; } else { this.screenName = screenName; } // stage = new Stage(); stage.getCamera().position.set(AppSettings.SCREEN_W / 2, AppSettings.SCREEN_H / 2, 0); // Receive inputs from stage Gdx.input.setInputProcessor(stage); // INFO LOG MtxLogger.log(logActive, true, logTag, "SCREEN CONSTRUCTED: " + getScreenName()); } @Override public void render(float delta) { // Update screen clock (1 second tick) // ############################################################ if (System.nanoTime() - startTime >= 1000000000) { SECONDS_TIME++; startTime = System.nanoTime(); } // Update animation times // ############################################################ stateTime += delta; // Snippet (Clear screen and give red color) // ############################################################ Gdx.gl.glClearColor(0.2f, 0.2f, 1.0f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Update stage/actors logic (update() method in previous games) // ############################################################ stage.act(delta); // Render drawings (draw()/render() methods in previous games) // ############################################################ stage.draw(); // Custom back button // ############################################################ if (isBackButtonActive) { if (Gdx.input.isKeyPressed(Keys.BACK)) { keyBackPressed(); } } } /** * Set stage background. Sets the image (Adds to stage as image) * * @param backgroundTextureRegion * * */ public void setBackgroundTexture(TextureRegion textureBackground) { Drawable tBg = new TextureRegionDrawable(textureBackground); Image imgbg = new Image(tBg, Scaling.stretch); imgbg.setFillParent(true); stage.addActor(imgbg); // MtxLogger.log(logActive, true, logTag, "SCREEN BG IMAGE SET: " + getScreenName()); } /** * Set the back button active for the screen. Sets * "Gdx.input.setCatchBackKey(true)" and override the method * "keyBackPressed" to add desired functionality to back button * * @param isBackButtonActive * to use or not to use the back button * @see keyBackPressed * * */ public void setBackButtonActive(boolean isBackButtonActive) { Gdx.input.setCatchBackKey(true); this.isBackButtonActive = isBackButtonActive; // MtxLogger.log(logActive, true, logTag, "SCREEN BACK BUTTON SET: " + getScreenName()); } /** * Override this method to do some function when back button pressed * */ public void keyBackPressed() { } /** * Get the game class * */ public Game getGame() { return game; } /** * Set the game class * */ public void setGame(Game game) { this.game = game; } /** * Get screen name * */ public String getScreenName() { return screenName; } /** * Get seconds since this screen constructed (EX: 3345 seconds) * */ public float getSecondsTime() { return SECONDS_TIME; } /** * Set or reset sceconds * */ /** * Get stage of the screen * */ public Stage getStage() { return stage; } @Override public void resize(int width, int height) { MtxLogger.log(logActive, true, logTag, "SCREEN RESIZE: " + getScreenName()); } @Override public void show() { MtxLogger.log(logActive, true, logTag, "SCREEN SHOW: " + getScreenName()); } @Override public void hide() { MtxLogger.log(logActive, true, logTag, "SCREEN HIDE: " + getScreenName()); } @Override public void pause() { MtxLogger.log(logActive, true, logTag, "SCREEN PAUSE: " + getScreenName()); } @Override public void resume() { MtxLogger.log(logActive, true, logTag, "SCREEN RESUME: " + getScreenName()); } @Override public void dispose() { stage.dispose(); // Add items here for log String strDisposedItems = "Stage, "; MtxLogger.log(logActive, true, logTag, "$$ :: SCREEN DISPOSING: " + getScreenName() + " DISPOSED: " + strDisposedItems); } }