se.theodor.quiz.screen.ChooseQuiz.java Source code

Java tutorial

Introduction

Here is the source code for se.theodor.quiz.screen.ChooseQuiz.java

Source

/********************************************************************************
 * 
 *   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.QUESTION_TEXT_BUTTON_STYLE;
import static se.theodor.quiz.util.Objects.RED_TEXT_BUTTON_STYLE;

import se.theodor.quiz.Quiz;
import se.theodor.quiz.Team;
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 ChooseQuiz implements Screen {

    public ChooseQuiz(Quiz quiz, String gameName, Array<Team> teams) {
        this.quiz = quiz;
        this.teams = teams;
        this.gameName = gameName;
    }

    private Quiz quiz;
    private Array<Team> teams;
    private Array<TextButton> buttons;
    private TextButton currentButtonSelected;
    private TextButton load;
    private Stage stage;
    private String gameName;

    public void load() {
        Array<String> acceptedFiles = Util.loadQuizesNameNoExtension();
        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 b = new TextButton(s, QUESTION_TEXT_BUTTON_STYLE);
            b.setBounds(0, (i * buttonHeight) + yOffset, quiz.getWidth(), buttonHeight);
            buttons.add(b);
            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(RED_TEXT_BUTTON_STYLE);
                }
            });
        }
    }

    @Override
    public void render(float delta) {
        if (teams.size > 0) {
            int yOffset = 100;
            int teamsOnWidth = 3;
            float var = ((float) teams.size / (float) teamsOnWidth);
            if (var <= 0) {
                var = 1;
            }
            float previousVar = var;
            var = Math.round(var + 0.5f);
            if (((var - previousVar) == 1.0f)) {
                var--;
            }
            int backgroundWidth = (Gdx.graphics.getWidth()) / teamsOnWidth,
                    backgroundHeight = (int) ((Gdx.graphics.getHeight() - yOffset) / var);
            for (int i = 0, x = 0, y = 0; i < teams.size; i++, x++, y++) {
                if (x >= teamsOnWidth) {
                    x = 0;
                }
                teams.get(i).updateButton(x * backgroundWidth, ((y / 3) * backgroundHeight) + yOffset,
                        backgroundWidth, backgroundHeight);
            }
        }
        stage.act(delta);
        stage.draw();

    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void show() {
        float[] xOffsets = new float[] { 0, Gdx.graphics.getWidth() / 2, };

        TextButton back = new TextButton("Tillbaka", QUESTION_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 CreateTeams(quiz, teams));
            }

        });

        load = new TextButton("Ladda valda quiz", QUESTION_TEXT_BUTTON_STYLE);
        load.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, gameName, currentButtonSelected.getText().toString(), teams));
            }

        });

        Table table = new Table();
        table.setBounds(0, 0, quiz.getWidth(), 100);
        stage = Objects.STAGE;
        stage.clear();

        table.add(back).pad(Values.PAD);
        table.add(load).pad(Values.PAD);

        stage.addActor(table);

        load();
    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }

}