Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package scenes; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; import com.circle.game.Config; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; import java.util.List; import java.util.Properties; import networking.GameClient; /** * * @author <a href="mailto:martin.drost@student.fontys.nl">Martin Dorst</a>, Edited by <a href="mailto:kevin.vanderburg@student.fontys.nl">Kevin van der Burg</a> */ public class SettingsScreen extends ApplicationAdapter { private Stage stage; //done private TextureAtlas atlas; //done private Skin skin; //done private Table table; // Align all buttons private BitmapFont font60, font30, font25; //done private Label heading; public SettingsScreen() { create(); } @Override public void create() { stage = new Stage(); Gdx.input.setInputProcessor(stage); atlas = new TextureAtlas("ui/buttonPack.pack"); skin = new Skin(atlas); table = new Table(skin); table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); //initialize fonts FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/MunroSmall.ttf")); FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter(); parameter.size = 60; font60 = generator.generateFont(parameter); // font size 12 pixels font60.setColor(Color.BLACK); parameter.size = 40; font30 = generator.generateFont(parameter); // font size 12 pixels font30.setColor(0.5f, 0.5f, 0.5f, 255); parameter.size = 25; font25 = generator.generateFont(parameter); // font size 12 pixels font25.setColor(Color.BLACK); generator.dispose(); // don't forget to dispose to avoid memory leaks! final TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle(); textButtonStyle.downFontColor = new Color(0f, 0.57f, 0.92f, 1f); textButtonStyle.overFontColor = new Color(0f, 0.57f, 1f, 1f); textButtonStyle.font = font30; textButtonStyle.fontColor = Color.BLACK; Label.LabelStyle headingStyle = new Label.LabelStyle(font60, Color.BLACK); Label.LabelStyle labelStyle = new Label.LabelStyle(font30, Color.BLACK); //Add head table.add(new Label("", headingStyle)).width(300); table.add(new Label("", headingStyle)).width(300); table.row(); table.add(new Label("Settings", headingStyle)).colspan(2).align(Align.center); //Add fullscreen config TextButton toggleFullscreen = new TextButton(Config.fullscreen ? "True" : "False", textButtonStyle); toggleFullscreen.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { Config.fullscreen = !Config.fullscreen; Gdx.graphics.setDisplayMode(Config.resolutionWidth, Config.resolutionHeight, Config.fullscreen); Config.page = new SettingsScreen(); } }); table.row().padBottom(10); table.add(new Label("Fullscreen", labelStyle)).align(Align.left); table.add(toggleFullscreen).align(Align.right); //Add fullscreen config TextButton toggleResolution = new TextButton(Config.resolutionWidth + "x" + Config.resolutionHeight, textButtonStyle); toggleResolution.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { List<String> resolutions = Arrays.asList("1024x768", "1600x900", "1680x1050", "1920x1080"); String currentResolution = Config.resolutionWidth + "x" + Config.resolutionHeight; int index = 0; for (String resolution : resolutions) { index++; if (currentResolution.equals(resolution)) { break; } } if (resolutions.size() <= index) { index = 0; } String[] newResolution = resolutions.get(index).split("x"); Config.resolutionWidth = Integer.parseInt(newResolution[0]); Config.resolutionHeight = Integer.parseInt(newResolution[1]); Gdx.graphics.setDisplayMode(Config.resolutionWidth, Config.resolutionHeight, Config.fullscreen); Config.page = new SettingsScreen(); } }); table.row().padBottom(10); table.add(new Label("Resolution", labelStyle)).align(Align.left); table.add(toggleResolution).align(Align.right); //Add Music config TextButton toggleSound = new TextButton(Config.sound ? "True" : "False", textButtonStyle); toggleSound.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { Config.sound = !Config.sound; if (!Config.sound) { Config.music.stop(); } else { long id = Config.music.loop(); Config.music.setVolume(id, 0.25f); } Config.page = new SettingsScreen(); } }); table.row().padBottom(10); table.add(new Label("Music", labelStyle)).align(Align.left); table.add(toggleSound).align(Align.right); //Add Sound Effect config TextButton toggleSoundfx = new TextButton(Config.soundfx ? "True" : "False", textButtonStyle); toggleSoundfx.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { Config.soundfx = !Config.soundfx; Config.page = new SettingsScreen(); } }); table.row().padBottom(10); table.add(new Label("Sound Effect", labelStyle)).align(Align.left); table.add(toggleSoundfx).align(Align.right); //Add debug config TextButton toggleDebug = new TextButton(Config.debug ? "True" : "False", textButtonStyle); toggleDebug.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { Config.debug = !Config.debug; if (Config.debug) GameClient.getClient().updateReturnTripTime(); Config.page = new SettingsScreen(); } }); table.row().padBottom(10); table.add(new Label("Debug", labelStyle)).align(Align.left); table.add(toggleDebug).align(Align.right); // Add back button TextButton buttonBack = new TextButton("Back", textButtonStyle); buttonBack.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { Config.page = new MainMenu(); } }); table.row().padBottom(10); table.add(buttonBack); final TextButton.TextButtonStyle textSaveButtonStyle = new TextButton.TextButtonStyle(); textSaveButtonStyle.downFontColor = new Color(0f, 0.57f, 0.92f, 1f); textSaveButtonStyle.overFontColor = new Color(0f, 0.57f, 1f, 1f); textSaveButtonStyle.font = font30; textSaveButtonStyle.fontColor = Color.BLACK; TextButton buttonApply = new TextButton("Save", textSaveButtonStyle); buttonApply.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { saveProperties(); textSaveButtonStyle.fontColor = new Color(0.15f, 0.68f, 0.37f, 1f); textSaveButtonStyle.downFontColor = new Color(0.15f, 0.68f, 0.37f, 1f); textSaveButtonStyle.overFontColor = new Color(0.15f, 0.68f, 0.37f, 1f); } }); table.add(buttonApply); //table.debug(); stage.addActor(table); } @Override public void render() { stage.act(); stage.draw(); } private void saveProperties() { Properties prop = new Properties(); OutputStream output = null; try { output = new FileOutputStream("config.properties"); // set the properties value prop.setProperty("sound", Config.sound ? "true" : "false"); prop.setProperty("soundfx", Config.soundfx ? "true" : "false"); prop.setProperty("fullscreen", Config.fullscreen ? "true" : "false"); prop.setProperty("resolutionWidth", Config.resolutionWidth + ""); prop.setProperty("resolutionHeight", Config.resolutionHeight + ""); prop.setProperty("debug", Config.debug ? "true" : "false"); // save properties to project root folder prop.store(output, null); } catch (IOException io) { io.printStackTrace(); } finally { if (output != null) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } } }