com.grillecube.engine.renderer.gui.GuiRenderer.java Source code

Java tutorial

Introduction

Here is the source code for com.grillecube.engine.renderer.gui.GuiRenderer.java

Source

/**
**   This file is part of the project https://github.com/toss-dev/VoxelEngine
**
**   License is available here: https://raw.githubusercontent.com/toss-dev/VoxelEngine/master/LICENSE.md
**
**   PEREIRA Romain
**                                       4-----7          
**                                      /|    /|
**                                     0-----3 |
**                                     | 5___|_6
**                                     |/    | /
**                                     1-----2
*/

package com.grillecube.engine.renderer.gui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.lwjgl.opengl.GL11;

import com.grillecube.engine.Taskable;
import com.grillecube.engine.VoxelEngine;
import com.grillecube.engine.VoxelEngine.Callable;
import com.grillecube.engine.opengl.GLFWWindow;
import com.grillecube.engine.renderer.MainRenderer;
import com.grillecube.engine.renderer.MainRenderer.GLTask;
import com.grillecube.engine.renderer.Renderer;
import com.grillecube.engine.renderer.gui.components.GuiLabel;
import com.grillecube.engine.renderer.gui.font.Font;
import com.grillecube.engine.renderer.gui.font.FontModel;
import com.grillecube.engine.resources.R;

public class GuiRenderer extends Renderer {
    /** rendering program */
    private ProgramFont _program_font;
    private ProgramQuad _program_quad;

    /** fonts */
    public static Font DEFAULT_FONT;

    /** list to render */
    private ArrayList<FontModel> _fonts_model;
    private ArrayList<Gui> _guis;

    /** Fonts */
    private Map<String, Font> _fonts;

    public GuiRenderer(MainRenderer renderer) {
        super(renderer);
    }

    @Override
    public void initialize() {

        this._fonts_model = new ArrayList<FontModel>();
        this._guis = new ArrayList<Gui>();
        this._fonts = new HashMap<String, Font>();
        this._program_quad = new ProgramQuad();
        this._program_font = new ProgramFont();

        this.loadFonts();
    }

    @Override
    public void deinitialize() {
        while (this._guis.size() > 0) {
            Gui gui = this._guis.remove(0);
            gui.onRemoved(this);
        }
    }

    /** load every fonts */
    private void loadFonts() {

        this.registerFont("Banana");
        this.registerFont("Calibri");
        this.registerFont("CalibriLight");
        this.registerFont("Cambria");
        this.registerFont("ComicSansMS");
        this.registerFont("Consolas");
        this.registerFont("Corbel");
        this.registerFont("David");
        this.registerFont("EDBWT");
        this.registerFont("Kirbyss");
        this.registerFont("Pirate");
        this.registerFont("LucidaBlack");
        this.registerFont("Pokemon");
        this.registerFont("PokemonBold");
        this.registerFont("TeleMarines");

        // DEFAULT_FONT = this.getFont("Kirbyss");
        DEFAULT_FONT = this.getFont("Calibri");
    }

    public Font registerFont(String name) {
        Font font = new Font(R.getResPath("font/" + name));
        this._fonts.put(name, font);
        return (font);
    }

    public Font getFont(String fontname) {
        Font font = this._fonts.get(fontname);
        if (font == null) {
            return (this.registerFont(fontname));
        }
        return (font);
    }

    @Override
    public void preRender() {
        GL11.glDisable(GL11.GL_DEPTH_TEST);
    }

    @Override
    public void render() {
        this.updateEvents();
        this.renderQuads();
        this.renderFonts();
    }

    /** update gui mouse events */
    private void updateEvents() {
        GLFWWindow window = this.getParent().getGLFWWindow();

        float mx = (float) (window.getMouseX() / window.getWidth() * 2 - 1);
        float my = (float) (window.getMouseY() / window.getHeight() * 2 - 1);
        boolean pressed = window.isMouseLeftPressed();

        for (int i = 0; i < this._guis.size(); i++) {
            Gui gui = this._guis.get(i);
            gui.update(mx, -my, pressed);
        }
    }

    /** add gui to the renderer */
    public void addGui(Gui gui) {
        if (gui == null) {
            return;
        }
        this._guis.add(gui);
        gui.onAdded(this);
    }

    /** remove gui from the renderer */
    public void removeGui(Gui gui) {
        if (gui == null || this._guis.size() == 0) {
            return;
        }
        this._guis.remove(gui);
        gui.onRemoved(this);
    }

    /** remove gui from the renderer */
    public void removeGui() {
        if (this._guis.size() == 0) {
            return;
        }
        this.removeGui(this._guis.get(this._guis.size() - 1));
    }

    /** remove every guis */
    public void clean() {
        for (Gui gui : this._guis) {
            gui.onRemoved(this);
        }
        this._guis.clear();
        this._fonts_model.clear();
    }

    /** add the font model to the rendering pipeline */
    public FontModel addFontModel(FontModel model) {
        this._fonts_model.add(model);
        return (model);
    }

    /** remove the font model from the rendering pipeline */
    public void removeFontModel(FontModel model) {
        if (model == null) {
            return;
        }
        this._fonts_model.remove(model);
    }

    /** render guis */
    private void renderQuads() {
        this._program_quad.useStart();
        this.getParent().getDefaultVAO().bind();

        for (Gui gui : this._guis) {
            gui.render(this._program_quad);
        }
        this._program_quad.useStop();
    }

    /** render every fonts models */
    private void renderFonts() {
        this._program_font.useStart();

        for (int i = 0; i < this._fonts_model.size(); i++) {
            FontModel model = this._fonts_model.get(i);
            this._program_font.bindFontModel(model);
            model.render();
        }

        this._program_font.useStop();
    }

    // TODO fix toasts
    /** toast a message on the screen */
    public void toast(String text, Font font, float r, float g, float b, float a, int time) {

        GuiLabel gui = new GuiLabel() {
            int _timer = time;

            @Override
            public void render(ProgramQuad program) {
                super.render(program);
                _timer--;
                if (_timer <= 0) {
                    final GuiLabel label = this;

                    getParent().addGLTask(new GLTask() {
                        @Override
                        public void run() {
                            getParent().getGuiRenderer().removeGui(label);
                        }
                    });
                }
            }
        };
        gui.setFontColor(r, g, b, a);
        gui.setFontSize(1.0f, 1.0f);
        gui.setText(text);
        gui.setCenter(0.0f, 0.0f);
        gui.addParameters(GuiLabel.PARAM_AUTO_ADJUST_RECT);
        gui.addParameters(GuiLabel.PARAM_CENTER);
        // gui.startAnimation(new GuiAnimationTextHoverScale<GuiLabel>(1.1f));
        this.addGui(gui);

    }

    public void toast(String str, float r, float g, float b, float a) {
        this.toast(str, r, g, b, a, 30);
    }

    public void toast(String str) {
        this.toast(str, 0, 1, 0, 1, 30);
    }

    public void toast(String str, int time) {
        this.toast(str, 0, 1, 0, 1, time);
    }

    public void toast(String str, boolean good) {
        if (good) {
            this.toast(str, 0, 1, 0, 1, 90);
        } else {
            this.toast(str, 1, 0, 0, 1, 90);
        }
    }

    public void toast(String str, float r, float g, float b, float a, int time) {
        this.toast(str, DEFAULT_FONT, r, g, b, a, time);
    }

    @Override
    public void postRender() {
        // TODO Auto-generated method stub

    }

    @Override
    public void getTasks(VoxelEngine engine, ArrayList<Callable<Taskable>> tasks) {
        // TODO Auto-generated method stub

    }
}