Made GUIManager more practical and added a few Logs

GUIManager now has "physicsLocationToGUILocation" and "getNode" methods
This commit is contained in:
Allexit 2015-05-12 19:27:46 +03:00
parent adf75ef7a1
commit 59207fb8a0
4 changed files with 52 additions and 16 deletions

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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);
}
}