All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bladecoder.engine.ui.LoadSaveScreen Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright 2014 Rafael Garcia Moreno.
 *
 * 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 com.bladecoder.engine.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
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.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Container;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Stack;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.SnapshotArray;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.bladecoder.engine.assets.EngineAssetManager;
import com.bladecoder.engine.model.Text;
import com.bladecoder.engine.model.TextManager;
import com.bladecoder.engine.model.World;
import com.bladecoder.engine.serialization.WorldSerialization;
import com.bladecoder.engine.ui.UI.Screens;
import com.bladecoder.engine.ui.defaults.ScreenControllerHandler;
import com.bladecoder.engine.util.DPIUtils;
import com.bladecoder.engine.util.EngineLogger;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class LoadSaveScreen extends ScreenAdapter implements BladeScreen {
    private static final int ROW_SLOTS = 3;
    private static final int COL_SLOTS = 2;

    private UI ui;

    private Stage stage;
    private Texture bgTexFile = null;

    private boolean loadScreenMode = true;

    private int slotWidth = 0;
    private int slotHeight = 0;

    // texture list for final dispose
    private final ArrayList textureList = new ArrayList<>();

    private Pointer pointer;

    private ScreenControllerHandler controller;

    public LoadSaveScreen() {
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act(delta);
        stage.draw();
        controller.update(delta);
    }

    @Override
    public void resize(int width, int height) {
        stage.getViewport().update(width, height, true);
        pointer.resize();
    }

    @Override
    public void dispose() {
        if (stage != null) {
            stage.dispose();
            stage = null;

            if (bgTexFile != null) {
                bgTexFile.dispose();
            }

            bgTexFile = null;

            for (Texture t : textureList)
                t.dispose();

            textureList.clear();
        }
    }

    @Override
    public void show() {
        float size = DPIUtils.getPrefButtonSize();
        float pad = DPIUtils.getMarginSize();
        final Skin skin = ui.getSkin();
        final World world = ui.getWorld();

        // loadScreenMode = ui.getScreen(Screens.LOAD_GAME_SCREEN) == this;
        loadScreenMode = world.getCurrentScene() == null;

        stage = new Stage(new ScreenViewport());

        slotWidth = (int) (stage.getViewport().getWorldWidth() / (ROW_SLOTS + 1) - 2 * pad);
        slotHeight = slotWidth * stage.getViewport().getScreenHeight() / stage.getViewport().getScreenWidth();

        LoadSaveScreenStyle style = skin.get(LoadSaveScreenStyle.class);

        Drawable bg = style.background;

        if (bg == null && style.bgFile != null) {
            bgTexFile = new Texture(EngineAssetManager.getInstance().getResAsset(style.bgFile));
            bgTexFile.setFilter(TextureFilter.Linear, TextureFilter.Linear);

            bg = new TextureRegionDrawable(new TextureRegion(bgTexFile));
        }

        Table table = new Table(skin);
        table.setFillParent(true);
        table.center();
        table.pad(pad);

        Label title = new Label(
                loadScreenMode ? world.getI18N().getString("ui.load") : world.getI18N().getString("ui.save"), skin,
                "title");

        Button back = new Button(skin, "back");

        back.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                ui.setCurrentScreen(Screens.MENU_SCREEN);
            }
        });

        Table header = new Table();
        // header.padBottom(pad);
        Container




© 2015 - 2025 Weber Informatics LLC | Privacy Policy