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

Java tutorial

Introduction

Here is the source code for se.theodor.quiz.screen.CreateTeams.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.GREEN_TEXT_BUTTON_STYLE;
import static se.theodor.quiz.util.Objects.GREEN_TEXT_FIELD_STYLE;
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 com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Screen;
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.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.utils.Array;

public class CreateTeams implements Screen {

    public CreateTeams(Quiz quiz, Array<Team> teams) {
        this.quiz = quiz;
        this.teams = teams;
        alreadyHasTeam = true;
    }

    public CreateTeams(Quiz quiz) {
        this.quiz = quiz;
        alreadyHasTeam = false;
    }

    private boolean alreadyHasTeam;
    private Quiz quiz;
    private Stage stage;
    private Array<Team> teams;
    private Team selectedTeam;

    private TextField teamNameField;
    private TextField gameName;

    @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);
            }
        }
        if (selectedTeam != null) {
            selectedTeam.getButton().setText(teamNameField.getText());
        }
        stage.act(delta);
        stage.draw();
    }

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

    }

    @Override
    public void show() {
        TextButton createTeam = new TextButton("Skapa nytt lag", GREEN_TEXT_BUTTON_STYLE);
        createTeam.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) {
                teams.add(new Team(quiz));
                stage.addActor(teams.get(teams.size - 1).getButton());
                teams.get(teams.size - 1).getButton().addListener(new InputListener() {
                    private Team team = teams.get(teams.size - 1);

                    @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) {
                        if (selectedTeam != null) {
                            selectedTeam.getButton().setStyle(QUESTION_TEXT_BUTTON_STYLE);
                            selectedTeam.setTeamName(teamNameField.getText());
                        }
                        teamNameField.setDisabled(false);
                        selectedTeam = team;
                        selectedTeam.getButton().setStyle(RED_TEXT_BUTTON_STYLE);
                        teamNameField.setText(team.getTeamName());
                    }
                });
            }
        });
        TextButton deleteTeam = new TextButton("Ta bort det \n valda lag", GREEN_TEXT_BUTTON_STYLE);
        deleteTeam.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) {
                if (selectedTeam != null) {
                    teams.removeValue(selectedTeam, true);
                    stage.getRoot().removeActor(selectedTeam.getButton());
                }
            }
        });
        teamNameField = new TextField("", GREEN_TEXT_FIELD_STYLE);
        TextButton next = new TextButton("Vlj quiz", GREEN_TEXT_BUTTON_STYLE);
        next.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) {
                if (selectedTeam != null) {
                    selectedTeam.setTeamName(teamNameField.getText());
                }
                quiz.setScreen(new ChooseQuiz(quiz, gameName.getText(), teams));
            }
        });

        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));
            }
        });
        gameName = new TextField("", Objects.GREEN_TEXT_FIELD_STYLE);
        gameName.setMessageText("Game Name");

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

        table.add(back).expand().fill();
        table.add(createTeam).expand().fill();
        table.add(deleteTeam).expand().fill();
        table.add(teamNameField).expand().fill();
        table.add(gameName).expand().fill();
        table.add(next).expand().fill();

        stage.addActor(table);

        if (!alreadyHasTeam) {
            teams = new Array<Team>();
        } else {
            for (int i = 0; i < teams.size; i++) {
                stage.addActor(teams.get(i).getButton());
            }
        }
    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }

}