org.saltosion.pixelprophecy.gui.GUIManager.java Source code

Java tutorial

Introduction

Here is the source code for org.saltosion.pixelprophecy.gui.GUIManager.java

Source

/*
 * Copyright (C) 2016 Saltosion
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNUss General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.saltosion.pixelprophecy.gui;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Color;
import org.saltosion.pixelprophecy.gui.nodes.GUINode;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import org.saltosion.pixelprophecy.gui.properties.ImageProperty;
import org.saltosion.pixelprophecy.gui.properties.TextProperty;
import org.saltosion.pixelprophecy.util.Globals;

public class GUIManager {

    private GUINode rootNode = new GUINode("root");

    private OrthographicCamera guiCamera;
    private SpriteBatch spriteBatch;
    private ShapeRenderer guiDebugRenderer;

    private static final Vector2 screenSize = new Vector2(0, 0);

    private static BitmapFont font;
    public static float guiScale = 1;

    public GUIManager() {
        guiCamera = new OrthographicCamera();
        spriteBatch = new SpriteBatch();
        guiDebugRenderer = new ShapeRenderer();
        guiDebugRenderer.setAutoShapeType(true);
    }

    public GUINode getRootNode() {
        return rootNode;
    }

    public void drawGUI() {
        spriteBatch.setProjectionMatrix(guiCamera.combined);
        guiDebugRenderer.setProjectionMatrix(guiCamera.combined);
        spriteBatch.begin();
        guiDebugRenderer.begin();
        drawGUINode(rootNode);
        spriteBatch.end();
        guiDebugRenderer.end();
    }

    /**
     * Renders the GUINode (and all it's children) on screen.
     */
    private void drawGUINode(GUINode node) {
        if (!node.isVisible()) {
            return;
        }
        Vector2 pos = node.getWorldLocation();
        if (node instanceof ImageProperty) {
            Sprite sprite = ((ImageProperty) node).getSprite();
            sprite.setScale(node.getScale() * guiScale);
            sprite.setPosition(pos.x - sprite.getWidth() / 2, pos.y - sprite.getHeight() / 2);
            sprite.draw(spriteBatch);
        }
        if (node instanceof TextProperty) {
            String text = ((TextProperty) node).getText();
            font.setColor(Color.WHITE);
            font.getData().setScale(node.getScale() * guiScale);
            font.setColor(Color.WHITE);
            font.draw(spriteBatch, text, pos.x - (getTextWidth(text) / 2) * node.getScale() * guiScale,
                    pos.y + getTextHeight() / 2);
        }
        if (Globals.GUI_DEBUG) {
            guiDebugRenderer.setColor(Globals.GUI_DEBUG_COLOR);
            if (node.hovered) {
                guiDebugRenderer.setColor(Globals.GUI_DEBUG_HOVER_COLOR);
            }
            guiDebugRenderer.line(pos.cpy().add(20 * guiScale, 20 * guiScale),
                    pos.cpy().sub(20 * guiScale, 20 * guiScale));
            guiDebugRenderer.line(pos.cpy().add(-20 * guiScale, 20 * guiScale),
                    pos.cpy().add(20 * guiScale, -20 * guiScale));
            guiDebugRenderer.circle(pos.x, pos.y, 20 * guiScale);
        }
        for (GUINode child : node.getChildren()) {
            drawGUINode(child);
        }
    }

    /**
     * Called when window is resized
     *
     * @param width
     * @param height
     */
    public void resize(int width, int height) {
        guiCamera.setToOrtho(false, width, height);
        screenSize.set(width, height);
        rootNode.setSize(screenSize.cpy().scl(2));
        guiScale = Math.min(width / 1920f, height / 1080f);
    }

    public void loadFont(AssetManager assetManager) {
        font = assetManager.get("fonts/font.fnt", BitmapFont.class);
        font.setUseIntegerPositions(true);
    }

    public static float getTextWidth(String text) {
        float w = 0;
        for (char c : text.toCharArray()) {
            w += font.getData().getGlyph(c).xadvance;
        }
        return w;
    }

    public static float getTextHeight() {
        return font.getCapHeight();
    }

    /**
     * Clears the GUI from all nodes (except root)
     */
    public void clear() {
        rootNode.clear();
    }

}