com.w67clement.openw67render.gui.screen.OpenScreen.java Source code

Java tutorial

Introduction

Here is the source code for com.w67clement.openw67render.gui.screen.OpenScreen.java

Source

/*
 * Copyright  2016 Clment "w67clement" Wagner
 *
 * This file is part of OpenW67Render.
 *
 * OpenW67Render is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * OpenW67Render 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
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with OpenW67Render.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.w67clement.openw67render.gui.screen;

import com.w67clement.openw67render.gui.components.OpenComponent;
import com.w67clement.openw67render.gui.components.ParentComponent;
import com.w67clement.openw67render.gui.graphics.OpenGraphics;
import com.w67clement.openw67render.utils.OpenUtils;
import com.w67clement.openw67render.window.OpenWindow;
import java.awt.Color;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWCursorPosCallback;
import org.lwjgl.glfw.GLFWMouseButtonCallback;
import org.lwjgl.opengl.GL11;

import static com.w67clement.openw67render.registry.ComponentRegistry.getComponentRegistry;

/**
 * Created by w67clement on 27/04/2016.
 * <p>
 * Class of project: openw67render
 * <p>
 * Warning: <b>The visible property is disabled.</b>
 * </p>
 */
public abstract class OpenScreen extends ParentComponent {
    private Color background;

    public OpenScreen(OpenWindow window) {
        super();
        setGraphics(new OpenGraphics(window));
    }

    /**
     * Gets the background color of the screen.
     *
     * @return Background color.
     */
    public Color getBackground() {
        return background;
    }

    /**
     * Sets the background color of the screen.
     *
     * @param background Background color.
     */
    public void setBackground(Color background) {
        this.background = background;
    }

    @Override
    public void paint(OpenGraphics graphics) {
        paintBackground(graphics);
        if (components.size() != 0) {
            graphics.setOrthoOn();
            components.forEach(comp -> comp.paint(comp.getGraphics()));
        }
    }

    /**
     * Method to paint the background of the screen.
     *
     * @param graphics Graphics for drawing.
     */
    public void paintBackground(OpenGraphics graphics) {
        if (background != null)
            GL11.glClearColor((float) background.getRed() / 255f, (float) background.getGreen() / 255f,
                    (float) background.getBlue() / 255f, 1f);
    }

    @Override
    public void update() {
        components.forEach(OpenComponent::update);
    }

    @Override
    @Deprecated
    public final boolean isVisible() {
        return true;
    }

    @Override
    @Deprecated
    public final void setVisible(boolean visible) {
    }

    @Override
    public final int getWidth() {
        return getGraphics().getWindow().getSize()[0];
    }

    @Override
    public final void setWidth(int width) {
    }

    @Override
    public final int getHeight() {
        return getGraphics().getWindow().getSize()[1];
    }

    @Override
    public void setHeight(int height) {
    }

    @Override
    public int getX() {
        return 0;
    }

    @Override
    public void setX(int x) {
    }

    @Override
    public int getY() {
        return 0;
    }

    @Override
    public void setY(int y) {
    }

    public static class ButtonMouseCallback extends GLFWCursorPosCallback {

        public ButtonMouseCallback() {

        }

        @Override
        public void invoke(long window, double xpos, double ypos) {
            getComponentRegistry().list().parallel().forEach(component -> {
                if (component.getGraphics() != null
                        && component.getGraphics().getWindow().getWindowId() == window) {
                    component.mouseListeners().forEach(action -> {
                        boolean hover = OpenUtils.pointIsInRectangle((int) component.getGraphics().getComplementX(),
                                (int) component.getGraphics().getComplementY(), component.getWidth(),
                                component.getHeight(), xpos, ypos);
                        if (component.isHover()) {
                            if (!hover)
                                action.exited();
                        } else {
                            if (hover && (!component.isDisabled()))
                                action.hovered();
                        }
                    });
                }
            });
        }
    }

    public static class ButtonClickMouseCallback extends GLFWMouseButtonCallback {

        @Override
        public void invoke(long window, int button, int action, int mods) {
            if (button == 0 && action == GLFW.GLFW_PRESS) {
                getComponentRegistry().list().forEach(component -> {
                    if (component.getGraphics() != null
                            && component.getGraphics().getWindow().getWindowId() == window) {
                        component.actionListeners().forEach(actionListener -> {
                            if (component.isHover() && (!component.isDisabled())) {
                                actionListener.action();
                            }
                        });
                    }
                });
            }
        }
    }
}