Java tutorial
/******************************************************************************** * * Copyright 2014 Theodor Angergard * * 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 se.theodor.quiz.screen; import static se.theodor.quiz.util.Objects.GREEN_TEXT_BUTTON_STYLE; import static se.theodor.quiz.util.Objects.QUESTION_TEXT_BUTTON_STYLE; import se.theodor.quiz.Quiz; import se.theodor.quiz.util.Objects; import se.theodor.quiz.util.Util; import se.theodor.quiz.util.Values; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input.Buttons; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.utils.Array; public class LoadGame implements Screen { public LoadGame(Quiz quiz) { this.quiz = quiz; } private Quiz quiz; private Stage stage; private OrthographicCamera camera; private Array<TextButton> buttons; private TextButton currentButtonSelected; private TextButton loadGame; @Override public void render(float delta) { stage.act(delta); if (currentButtonSelected != null) { loadGame.setDisabled(false); } stage.draw(); } @Override public void resize(int width, int height) { } @Override public void show() { loadGame = new TextButton("Ladda spelet", GREEN_TEXT_BUTTON_STYLE); loadGame.setDisabled(true); loadGame.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (button == Buttons.LEFT) { return true; } return false; } @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { quiz.setScreen(new PlayQuiz(quiz, currentButtonSelected.getText().toString())); } }); TextButton back = new TextButton("Tillbaka", GREEN_TEXT_BUTTON_STYLE); back.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (button == Buttons.LEFT) { return true; } return false; } @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { quiz.setScreen(new CreateOrLoadQuiz(quiz)); } }); Table table = new Table(); table.setBounds(0, 0, quiz.getWidth(), 100); camera = Objects.CAMERA; stage = Objects.STAGE; stage.clear(); table.add(loadGame).pad(Values.PAD); table.add(back).pad(Values.PAD); stage.addActor(table); load(); } @Override public void hide() { } @Override public void pause() { } @Override public void resume() { } @Override public void dispose() { } public void load() { Array<String> acceptedFiles = Util.loadGamesNameNoExtension(); float yOffset = 100; float buttonHeight = (Gdx.graphics.getHeight() - yOffset) / acceptedFiles.size; buttons = new Array<TextButton>(); for (int i = 0; i < acceptedFiles.size; i++) { final int index = i; String s = acceptedFiles.get(i); TextButton button = new TextButton(s, QUESTION_TEXT_BUTTON_STYLE); button.setBounds(0, i * buttonHeight + yOffset, Gdx.graphics.getWidth(), buttonHeight); buttons.add(button); stage.addActor(buttons.get(i)); buttons.get(i).setName(s); buttons.get(i).addListener(new InputListener() { private TextButton button = buttons.get(index); @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (button != Buttons.LEFT) { return false; } return true; } @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { if (currentButtonSelected != null) { currentButtonSelected.setStyle(QUESTION_TEXT_BUTTON_STYLE); } currentButtonSelected = this.button; currentButtonSelected.setStyle(QUESTION_TEXT_BUTTON_STYLE); } }); } } }