From 59207fb8a09bd8dd9d00973a38675d826ba2f704 Mon Sep 17 00:00:00 2001 From: Allexit Date: Tue, 12 May 2015 19:27:46 +0300 Subject: [PATCH] Made GUIManager more practical and added a few Logs GUIManager now has "physicsLocationToGUILocation" and "getNode" methods --- .../saltosion/gladiator/GladiatorBrawler.java | 7 +++-- .../saltosion/gladiator/gui/GUIManager.java | 31 +++++++++++++++++++ .../gladiator/input/InputHandler.java | 25 +++++++-------- .../gladiator/systems/RenderingSystem.java | 5 +++ 4 files changed, 52 insertions(+), 16 deletions(-) diff --git a/core/src/com/saltosion/gladiator/GladiatorBrawler.java b/core/src/com/saltosion/gladiator/GladiatorBrawler.java index a203fd1..00384e1 100644 --- a/core/src/com/saltosion/gladiator/GladiatorBrawler.java +++ b/core/src/com/saltosion/gladiator/GladiatorBrawler.java @@ -41,6 +41,8 @@ public class GladiatorBrawler extends ApplicationAdapter { @Override public void create() { + Log.info("Starting up the game"); + // Initialize the Engine engine = new Engine(); AppUtil.engine = engine; @@ -93,11 +95,13 @@ public class GladiatorBrawler extends ApplicationAdapter { // Initialize input inputHandler = new InputHandler(); Gdx.input.setInputProcessor(inputHandler); + + Log.info("Successfully started the game."); } @Override public void render() { - engine.update(Gdx.graphics.getDeltaTime()); + engine.update(Gdx.graphics.getDeltaTime()); } public void initializePlayer() { @@ -185,7 +189,6 @@ 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 pressed(int x, int y, int mouseButton) { diff --git a/core/src/com/saltosion/gladiator/gui/GUIManager.java b/core/src/com/saltosion/gladiator/gui/GUIManager.java index f5bf8ef..b8670a5 100644 --- a/core/src/com/saltosion/gladiator/gui/GUIManager.java +++ b/core/src/com/saltosion/gladiator/gui/GUIManager.java @@ -2,6 +2,11 @@ package com.saltosion.gladiator.gui; import java.util.ArrayList; +import com.badlogic.gdx.math.Vector2; +import com.saltosion.gladiator.systems.RenderingSystem; +import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.Global; + public class GUIManager { private final GUINode rootNode; @@ -22,4 +27,30 @@ public class GUIManager { return list; } + public GUINode getNode(String id) { + for (GUINode node : getAllRecursiveChildren(rootNode)) { + if (node.getID().equals(id)) { + return node; + } + } + return null; + } + + public static Vector2 physicsLocationToGUILocation(Vector2 physicslocation ) { + RenderingSystem rs = AppUtil.engine.getSystem(RenderingSystem.class); + float cameraY = 1-(rs.getCameraLocation().y/AppUtil.VPHEIGHT_CONST + +.5f); + float cameraX = 1-(rs.getCameraLocation().x/(AppUtil.VPHEIGHT_CONST*RenderingSystem.aspectratio) + +.5f); + System.out.println(cameraX + ":" + cameraY); + float y = physicslocation.y/AppUtil.VPHEIGHT_CONST + + cameraY; + float x = physicslocation.x/(AppUtil.VPHEIGHT_CONST*RenderingSystem.aspectratio) + + cameraX; + + Vector2 v = new Vector2(x, y); + + return v; + } + } diff --git a/core/src/com/saltosion/gladiator/input/InputHandler.java b/core/src/com/saltosion/gladiator/input/InputHandler.java index 473ee67..4943aae 100644 --- a/core/src/com/saltosion/gladiator/input/InputHandler.java +++ b/core/src/com/saltosion/gladiator/input/InputHandler.java @@ -12,6 +12,7 @@ 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.Log; import com.saltosion.gladiator.util.Name; public class InputHandler implements InputProcessor { @@ -57,13 +58,11 @@ public class InputHandler implements InputProcessor { @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { for (String id : hoveredUIElements) { - for (GUINode node : AppUtil.guiManager.getAllRecursiveChildren(AppUtil.guiManager.getRootNode())) { - if (node.getID().equals(id)) { - if (node instanceof InteractiveNode) { - ((InteractiveNode) node).pressed(screenX, screenY, button); - } - } - } + GUINode node = AppUtil.guiManager.getNode(id); + + if (node instanceof InteractiveNode) { + ((InteractiveNode) node).pressed(screenX, screenY, button); + } else { Log.error("Attempted to call 'pressed' on a non-interactive node!"); } } return true; } @@ -71,13 +70,11 @@ public class InputHandler implements InputProcessor { @Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { for (String id : hoveredUIElements) { - for (GUINode node : AppUtil.guiManager.getAllRecursiveChildren(AppUtil.guiManager.getRootNode())) { - if (node.getID().equals(id)) { - if (node instanceof InteractiveNode) { - ((InteractiveNode) node).released(screenX, screenY, button); - } - } - } + GUINode node = AppUtil.guiManager.getNode(id); + + if (node instanceof InteractiveNode) { + ((InteractiveNode) node).released(screenX, screenY, button); + } else { Log.error("Attempted to call 'released' on a non-interactive node!"); } } return true; } diff --git a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java index 96a7baa..13bf026 100644 --- a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java +++ b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java @@ -16,6 +16,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.math.Vector3; import com.saltosion.gladiator.components.CPhysics; import com.saltosion.gladiator.components.CRenderedObject; import com.saltosion.gladiator.gui.GUINode; @@ -138,5 +139,9 @@ public class RenderingSystem extends EntitySystem { public void setDebug(boolean debug) { this.debug = debug; } + + public Vector2 getCameraLocation() { + return new Vector2(this.camera.position.x, this.camera.position.y); + } }