Java tutorial
/* * Copyright 2015-2016 Telinc1 * * 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.telinc1.rpjg.module; import java.util.TreeSet; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.GL11; import com.telinc1.rpjg.module.gui.Gui; import com.telinc1.rpjg.util.DrawingUtils; public abstract class Module { private TreeSet<Gui> guis; public Module() { this.guis = new TreeSet<>(); } /** * @return The width of the module on the screen. */ public abstract int getWidth(); /** * @return The height of the module on the screen. */ public abstract int getHeight(); /** * @return Whether or not the module is modal. * Modal modules tint everything behind them in black. * The effect also stacks. */ public abstract boolean isModal(); /** * Checks whether or not this module can hide the given * one, which is usually loaded behind it (don't count on this though). * * @param module - The module we're testing against. * @return Whether or not the given module can be hidden by this one. */ public abstract boolean canHide(Module module); /** * Checks whether or not this module can be hidden by * the given one, which is usually about to be loaded in front * of it (don't count on this though). * * @param module - The module we're testing against. * @return Whether or not the given module can hide this one. */ public abstract boolean canBeHiddenBy(Module module); /** * Renders the visual aspects of this module. */ public abstract void renderGraphics(); /** * Renders any user interaction elements for this module. */ public void renderUI() { } /** * Called every frame this module's active to update it. */ public void update() { for (Gui gui : this.getGUIs()) { gui.update(); } } /** * Called whenever the player moves the mouse while this module is active. * * @param x - The new X coordinate of the mouse * @param y - The new Y coordinate of the mouse */ public void mouseMoved(int x, int y) { } /** * Called whenever the player presses a mouse button. * * @param x - The X coordinate of the mouse * @param y - The Y coordinate of the mouse * @param button - The button pressed (0: left; 1: right; 3: middle) */ public void mousePressed(int x, int y, int button) { for (Gui gui : this.getGUIs()) { if (gui.mousePressed(x, y, button)) { return; } } } /** * Called whenever the player releases a mouse button. * * @param x - The X coordinate of the mouse * @param y - The Y coordinate of the mouse * @param button - The button released (0: left; 1: right; 3: middle) */ public void mouseReleased(int x, int y, int button) { for (Gui gui : this.getGUIs()) { if (gui.mouseReleased(x, y, button)) { return; } } } /** * Called whenever the player presses a keyboard key. * * @param key - The key pressed * @see org.lwjgl.input.Keyboard */ public void keyPressed(int key) { for (Gui gui : this.getGUIs()) { if (gui.keyPressed(key)) { return; } } } /** * Called whenever the player releases a keyboard key. * * @param key - The key released * @see org.lwjgl.input.Keyboard */ public void keyReleased(int key) { for (Gui gui : this.getGUIs()) { if (gui.keyReleased(key)) { return; } } } /** * For modules which don't cover the screen, calculates * where the left side of the module is. * * @return The screen coordinates of the left side. */ public int getLeft() { return (Display.getWidth() - this.getWidth()) / 2; } /** * For modules which don't cover the screen, calculates * where the top side of the module is. * * @return The screen coordinates of the top side. */ public int getTop() { return (Display.getHeight() - this.getHeight()) / 2; } /** * @return All the loaded GUIs. */ public TreeSet<Gui> getGUIs() { return this.guis; } /** * Opens a GUI. * * @param gui - The GUI to open. */ public void openGUI(Gui gui) { this.getGUIs().add(gui); } /** * Closes a GUI. * * @param gui - The GUI to close. */ public void closeGUI(Gui gui) { gui.onClose(); this.getGUIs().remove(gui); } /** * Closes all the GUI which are currently open. */ public void closeAllGUIs() { for (Gui gui : this.getGUIs()) { gui.close(); } } /** * Renders the module's graphics and UI elements. * Also applied the screen darkening effect for modal modules.<br /> * Generally not a good idea to override this. */ public void render() { if (this.isModal()) { DrawingUtils.unbindTextures(); // Not going to change this to use DrawingUtils until // I don't make actual modal modules. // Don't want untested code! GL11.glPushAttrib(DrawingUtils.GL_COLOR_BITS); GL11.glColor4f(0f, 0f, 0f, .5f); GL11.glBegin(GL11.GL_QUADS); GL11.glVertex2i(0, 0); GL11.glVertex2i(Display.getWidth(), 0); GL11.glVertex2i(Display.getWidth(), Display.getHeight()); GL11.glVertex2i(0, Display.getHeight()); GL11.glEnd(); GL11.glPopAttrib(); } this.renderGraphics(); this.renderUI(); for (Gui gui : this.getGUIs()) { gui.render(); } } }