Java tutorial
/* * 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.nodes; import com.badlogic.gdx.math.Vector2; import java.util.ArrayList; import org.saltosion.pixelprophecy.gui.InteractionListener; public class GUINode { private String name; private Vector2 localTranslation = new Vector2(0, 0); private Vector2 size = new Vector2(0, 0); private Vector2 alignment = new Vector2(0, 0); private GUINode parent; private ArrayList<GUINode> children = new ArrayList<GUINode>(); private InteractionListener interactionListener; public boolean hovered = false; private boolean visible = true; private float scale; public GUINode(String name) { this.name = name; } /** * Returns the pixel-perfect location of the node * @return */ public Vector2 getWorldLocation() { Vector2 loc = localTranslation.cpy(); Vector2 alignmentOffset = alignment.cpy().scl(getSize()).scl(.5f); if (hasParent()) { loc.scl(parent.getSize().cpy().scl(.5f)).add(parent.getWorldLocation()); } return loc.sub(alignmentOffset); } public void attach(GUINode child) { this.children.add(child); child.parent = this; } public void detach(GUINode child) { this.children.remove(child); child.parent = null; } public void clear() { for (GUINode child : children) { child.parent = null; } this.children.clear(); } public boolean hasParent() { return parent != null; } // Setters public void setLocalTranslation(Vector2 localTranslation) { this.localTranslation = localTranslation; } public void setSize(Vector2 size) { this.size = size; } public void setAlignment(Vector2 alignment) { this.alignment = alignment; } public void setScale(float scale) { this.scale = scale; } public void setInteractionListener(InteractionListener interactionListener) { this.interactionListener = interactionListener; } public void setVisible(boolean visible) { this.visible = visible; } // Getters /** * Returns the location of the node relative to the parent (screen size if no parent) * @return */ public Vector2 getLocalTranslation() { return localTranslation; } /** * Returns the size of the node (in pixels) * @return */ public Vector2 getSize() { return size; } public Vector2 getAlignment() { return alignment; } public float getScale() { return scale; } public String getName() { return name; } public ArrayList<GUINode> getChildren() { return children; } public GUINode getParent() { return parent; } public InteractionListener getInteractionListener() { return interactionListener; } public boolean isInteractive() { return interactionListener != null; } public boolean isVisible() { return visible; } }