From 9abcacb104c3b357053b77e8151addb7b2f22e85 Mon Sep 17 00:00:00 2001 From: Allexit Date: Tue, 12 May 2015 00:01:59 +0300 Subject: [PATCH] InteractiveNode now recieves "mouseEnter" and "mouseLeave" respectively --- .../saltosion/gladiator/GladiatorBrawler.java | 3 ++ .../saltosion/gladiator/gui/GUIManager.java | 11 +++++ .../gladiator/input/InputHandler.java | 42 +++++++++++++++++++ .../gladiator/systems/RenderingSystem.java | 7 +++- .../com/saltosion/gladiator/util/Global.java | 3 +- 5 files changed, 62 insertions(+), 4 deletions(-) diff --git a/core/src/com/saltosion/gladiator/GladiatorBrawler.java b/core/src/com/saltosion/gladiator/GladiatorBrawler.java index 544d9f9..110d405 100644 --- a/core/src/com/saltosion/gladiator/GladiatorBrawler.java +++ b/core/src/com/saltosion/gladiator/GladiatorBrawler.java @@ -175,6 +175,7 @@ public class GladiatorBrawler extends ApplicationAdapter { public void initializeTestGUI() { Sprite img1 = SpriteLoader.loadSprite(Name.WALLIMG, 0, 0, 32, 64); Sprite img2 = SpriteLoader.loadSprite(Name.WALLIMG, 1, 0, 32, 64); + System.out.println(img1.getRegionHeight() + " - " + img1.getRegionWidth()); ButtonNode button = new ButtonNode("test-button", img1, img2) { @Override public void click(float x, float y, Input.Buttons mouseButton) { @@ -188,6 +189,8 @@ public class GladiatorBrawler extends ApplicationAdapter { @Override public void resize(int width, int height) { super.resize(width, height); + RenderingSystem.screenHeight = height; + RenderingSystem.screenWidth = width; RenderingSystem rs = engine.getSystem(RenderingSystem.class); float aspectratio = ((float) width) / ((float) height); rs.aspectratio = aspectratio; diff --git a/core/src/com/saltosion/gladiator/gui/GUIManager.java b/core/src/com/saltosion/gladiator/gui/GUIManager.java index 92ebd52..f5bf8ef 100644 --- a/core/src/com/saltosion/gladiator/gui/GUIManager.java +++ b/core/src/com/saltosion/gladiator/gui/GUIManager.java @@ -1,5 +1,7 @@ package com.saltosion.gladiator.gui; +import java.util.ArrayList; + public class GUIManager { private final GUINode rootNode; @@ -11,4 +13,13 @@ public class GUIManager { return this.rootNode; } + public ArrayList getAllRecursiveChildren(GUINode guiNode) { + ArrayList list = new ArrayList(); + for (GUINode child : guiNode.getChildren()) { + list.add(child); + list.addAll(getAllRecursiveChildren(child)); + } + return list; + } + } diff --git a/core/src/com/saltosion/gladiator/input/InputHandler.java b/core/src/com/saltosion/gladiator/input/InputHandler.java index 30f82fc..7b2433c 100644 --- a/core/src/com/saltosion/gladiator/input/InputHandler.java +++ b/core/src/com/saltosion/gladiator/input/InputHandler.java @@ -4,11 +4,21 @@ import java.util.HashMap; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.InputProcessor; +import com.badlogic.gdx.graphics.g2d.Sprite; +import com.badlogic.gdx.utils.Array; +import com.saltosion.gladiator.gui.GUINode; +import com.saltosion.gladiator.gui.ImageNode; +import com.saltosion.gladiator.gui.InteractiveNode; +import com.saltosion.gladiator.systems.RenderingSystem; +import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.Global; import com.saltosion.gladiator.util.Name; public class InputHandler implements InputProcessor { public HashMap keys = new HashMap(); + + private Array hoveredUIElements = new Array(); public InputHandler() { keys.put(Keys.A, Name.MOVE_LEFT); @@ -61,6 +71,38 @@ public class InputHandler implements InputProcessor { @Override public boolean mouseMoved(int screenX, int screenY) { + float x = (float)(screenX)/RenderingSystem.screenWidth; + float y = 1-(float)(screenY)/RenderingSystem.screenHeight; + + for (GUINode node : AppUtil.guiManager.getAllRecursiveChildren(AppUtil.guiManager.getRootNode())) { + if (node instanceof ImageNode) { + Sprite s = ((ImageNode) node).getImage(); + float height = (s.getHeight()*Global.SPRITE_SCALE)/AppUtil.VPHEIGHT_CONST; + float width = (s.getWidth()*Global.SPRITE_SCALE)/(AppUtil.VPHEIGHT_CONST*RenderingSystem.aspectratio); + float x0 = node.getPosition().x-width/2; + float x1 = node.getPosition().x+width/2; + float y0 = node.getPosition().y-height/2; + float y1 = node.getPosition().y+height/2; + x += 0.01f; + if (node instanceof InteractiveNode) { + InteractiveNode interactiveNode = (InteractiveNode) node; + + if (x >= x0 && x <= x1 && y >= y0 && y <= y1) { + if (hoveredUIElements.contains(node.getID(), false)) { + continue; + } + hoveredUIElements.add(node.getID()); + interactiveNode.mouseEnter(x, y); + } else { + if (!hoveredUIElements.contains(node.getID(), false)) { + continue; + } + hoveredUIElements.removeValue(node.getID(), false); + interactiveNode.mouseLeave(x, y); + } + } + } + } return false; } diff --git a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java index 4b5ca5f..96a7baa 100644 --- a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java +++ b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java @@ -37,7 +37,10 @@ public class RenderingSystem extends EntitySystem { private BitmapFont font; private ShapeRenderer debugRenderer; private OrthographicCamera camera; - public float aspectratio; + + public static float aspectratio; + public static int screenHeight = 0; + public static int screenWidth = 0; private boolean debug = true; private final Color debugColor = new Color(0, 1, 0, 1); @@ -115,7 +118,7 @@ public class RenderingSystem extends EntitySystem { position.add(node.getPosition()); if (node instanceof ImageNode) { Sprite s = ((ImageNode) node).getImage(); - s.setPosition(position.x*AppUtil.VPHEIGHT_CONST*aspectratio-s.getWidth()/2+camera.position.x, + s.setPosition(position.x*AppUtil.VPHEIGHT_CONST*aspectratio-s.getWidth()/2+camera.position.x, position.y*AppUtil.VPHEIGHT_CONST-s.getHeight()/2+camera.position.y); s.draw(batch); } diff --git a/core/src/com/saltosion/gladiator/util/Global.java b/core/src/com/saltosion/gladiator/util/Global.java index bc4b4b4..16dae75 100644 --- a/core/src/com/saltosion/gladiator/util/Global.java +++ b/core/src/com/saltosion/gladiator/util/Global.java @@ -6,8 +6,7 @@ public class Global { public static final String PLAYERIMG = "PLAYERIMG"; public static final String GAME_NAME = "Gladiator Brawl"; - - public static final int VPHEIGHT_CONST = 14; + public static final float SPRITE_SCALE = 1 / 16f; }