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

Java tutorial

Introduction

Here is the source code for se.theodor.quiz.screen.CreateQuiz.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.BATCH;
import static se.theodor.quiz.util.Objects.TEXTURE_ATLAS;
import se.theodor.quiz.Category;
import se.theodor.quiz.EditorPanel;
import se.theodor.quiz.LoadQuizPanel;
import se.theodor.quiz.Question;
import se.theodor.quiz.Quiz;
import se.theodor.quiz.QuizField;
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.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
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.TextButton;
import com.badlogic.gdx.utils.Array;

public class CreateQuiz implements Screen {

    public CreateQuiz(Quiz quiz) {
        this.quiz = quiz;
    }

    private Quiz quiz;
    private EditorPanel editorPanel;
    private LoadQuizPanel loadQuizPanel;
    private OrthographicCamera camera;
    private QuizField quizField;

    @Override
    public void render(float delta) {
        BATCH.begin();
        quizField.draw(quiz, delta);
        editorPanel.setQuizField(quizField);
        editorPanel.render(BATCH, delta);
        loadQuizPanel.render(delta);
        Objects.STAGE.act(delta);
        Objects.STAGE.draw();
        BATCH.end();
    }

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

    @Override
    public void show() {
        camera = Objects.CAMERA;
        editorPanel = new EditorPanel((int) quiz.getWidth(), 300, quizField, loadQuizPanel,
                TEXTURE_ATLAS.createPatch("fallDownWindow"), quiz);
        quizField = new QuizField(quiz, camera, editorPanel, 9, 5);
        loadQuizPanel = new LoadQuizPanel(new Rectangle(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()),
                this);

        // TODO: FIX STAGE
        Stage stage = Objects.STAGE;
        stage.clear();
        Array<Actor> actors = getMainActors();
        for (Actor a : actors) {
            Objects.STAGE.addActor(a);
        }
        initListener();
    }

    // For now, Init save and load buttons
    private void initListener() {

        editorPanel.saveQuiz.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                Object o = quizField.getOpenSquare();
                if (o instanceof Question) {
                    Question q = (Question) o;
                    q.setQuestion(editorPanel.getQuestion().getText());
                    q.setAnswer(editorPanel.getAnswer().getText());
                    q.setTime(Integer.parseInt(editorPanel.getTime().getText()));
                    q.setPoints(Integer.parseInt(editorPanel.getPoints().getText()));
                    q.updateButton();
                } else if (o instanceof Category) {
                    Category c = (Category) o;
                    c.setName(editorPanel.getCategory().getText());
                    c.getButton().setText(c.getName());
                }
                quizField.save(editorPanel.name.getText());
            }

        });

        editorPanel.loadQuiz.addListener(new InputListener() {
            @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) {
                loadQuizPanel.show();
            }
        });

        editorPanel.back.addListener(new InputListener() {
            @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) {
                Object o = quizField.getOpenSquare();
                if (o instanceof Question) {
                    Question q = (Question) o;
                    q.setQuestion(editorPanel.getQuestion().getText());
                    q.setAnswer(editorPanel.getAnswer().getText());
                    q.setTime(Integer.parseInt(editorPanel.getTime().getText()));
                    q.setPoints(Integer.parseInt(editorPanel.getPoints().getText()));
                    q.updateButton();
                } else if (o instanceof Category) {
                    Category c = (Category) o;
                    c.setName(editorPanel.getCategory().getText());
                    c.getButton().setText(c.getName());
                }
                if (!quizField.changesSaved) {
                    // Add popup
                }
                quizField.save(editorPanel.name.getText());
                quiz.setScreen(new MainMenu(quiz));
            }
        });

        TextButton load = loadQuizPanel.getLoadQuizButton();
        load.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                if (button != Buttons.LEFT) {
                    return false;
                } else {
                    return true;
                }
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                String loadedQuiz = loadQuizPanel.getSelectedQuiz();
                if (loadedQuiz != null)
                    load(loadedQuiz);
                loadQuizPanel.hide();
            }
        });

    }

    private Array<Actor> getMainActors() {
        Array<Actor> mainActors = new Array<Actor>();
        mainActors.add(editorPanel.getTable());
        mainActors.addAll(quizField.getActors());
        return mainActors;
    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }

    public EditorPanel getEditorPanel() {
        return editorPanel;
    }

    public void load(String quiz) {
        quizField = new QuizField(this.quiz, camera, editorPanel, quiz);
    }

}