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.input; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.InputAdapter; import com.badlogic.gdx.math.Vector2; import java.util.ArrayList; import org.saltosion.pixelprophecy.gui.GUIManager; import org.saltosion.pixelprophecy.gui.nodes.GUINode; import org.saltosion.pixelprophecy.util.Globals; public class GUIInputAdapter extends InputAdapter { private GUINode rootNode; private GUINode hovered; public GUIInputAdapter(GUIManager guiManager) { this.rootNode = guiManager.getRootNode(); } @Override public boolean mouseMoved(int screenX, int screenY) { updateHovered(screenX, screenY); return true; } @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { if (hovered == null) { return false; } hovered.getInteractionListener().clickDown(screenX, screenY, button); return true; } @Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { if (hovered == null) { return false; } hovered.getInteractionListener().clickUp(screenX, screenY, button); return true; } @Override public boolean scrolled(int amount) { return super.scrolled(amount); //To change body of generated methods, choose Tools | Templates. } @Override public boolean touchDragged(int screenX, int screenY, int pointer) { updateHovered(screenX, screenY); if (hovered == null) { return false; } hovered.getInteractionListener().dragged(screenX, screenY, pointer); return true; } private ArrayList<GUINode> getInteractiveNodes(GUINode node, int screenX, int screenY, boolean interactive) { return getInteractiveNodes(node, screenX, screenY, new ArrayList<GUINode>(), interactive); } private ArrayList<GUINode> getInteractiveNodes(GUINode node, int screenX, int screenY, ArrayList<GUINode> interactives, boolean interactive) { Vector2 loc = node.getWorldLocation().cpy().sub(node.getSize().cpy().scl(.5f)); Vector2 locSize = loc.cpy().add(node.getSize()); float x1 = loc.x; float y1 = loc.y; float x2 = locSize.x; float y2 = locSize.y; if (x1 <= screenX && y1 <= screenY && x2 >= screenX && y2 >= screenY && (node.isVisible()) && (node.isInteractive() || node.isInteractive() == interactive)) { interactives.add(0, node); } for (GUINode child : node.getChildren()) { interactives = getInteractiveNodes(child, screenX, screenY, interactives, interactive); } return interactives; } private void updateHovered(int screenX, int screenY) { if (Globals.GUI_DEBUG) { removeHovered(rootNode); ArrayList<GUINode> hovered = getInteractiveNodes(rootNode, screenX, Gdx.graphics.getHeight() - screenY, false); for (GUINode node : hovered) { node.hovered = true; } } ArrayList<GUINode> interactives = getInteractiveNodes(rootNode, screenX, Gdx.graphics.getHeight() - screenY, true); GUINode currHovered = null; if (interactives.size() > 0) { currHovered = interactives.get(0); } if (hovered != currHovered) { // previously hovered element is not hovered anymore! if (hovered != null) { // Hovered wasn't null hovered.getInteractionListener().mouseLeave(screenY, screenY); } if (currHovered != null) { currHovered.getInteractionListener().mouseEnter(screenY, screenY); } hovered = currHovered; } } private void removeHovered(GUINode node) { node.hovered = false; for (GUINode child : node.getChildren()) { removeHovered(child); } } }