InteractiveNode now recieves "mouseEnter" and "mouseLeave" respectively

This commit is contained in:
Allexit 2015-05-12 00:01:59 +03:00
parent a20e960c19
commit 9abcacb104
5 changed files with 62 additions and 4 deletions

View File

@ -175,6 +175,7 @@ public class GladiatorBrawler extends ApplicationAdapter {
public void initializeTestGUI() { public void initializeTestGUI() {
Sprite img1 = SpriteLoader.loadSprite(Name.WALLIMG, 0, 0, 32, 64); Sprite img1 = SpriteLoader.loadSprite(Name.WALLIMG, 0, 0, 32, 64);
Sprite img2 = SpriteLoader.loadSprite(Name.WALLIMG, 1, 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) { ButtonNode button = new ButtonNode("test-button", img1, img2) {
@Override @Override
public void click(float x, float y, Input.Buttons mouseButton) { public void click(float x, float y, Input.Buttons mouseButton) {
@ -188,6 +189,8 @@ public class GladiatorBrawler extends ApplicationAdapter {
@Override @Override
public void resize(int width, int height) { public void resize(int width, int height) {
super.resize(width, height); super.resize(width, height);
RenderingSystem.screenHeight = height;
RenderingSystem.screenWidth = width;
RenderingSystem rs = engine.getSystem(RenderingSystem.class); RenderingSystem rs = engine.getSystem(RenderingSystem.class);
float aspectratio = ((float) width) / ((float) height); float aspectratio = ((float) width) / ((float) height);
rs.aspectratio = aspectratio; rs.aspectratio = aspectratio;

View File

@ -1,5 +1,7 @@
package com.saltosion.gladiator.gui; package com.saltosion.gladiator.gui;
import java.util.ArrayList;
public class GUIManager { public class GUIManager {
private final GUINode rootNode; private final GUINode rootNode;
@ -11,4 +13,13 @@ public class GUIManager {
return this.rootNode; return this.rootNode;
} }
public ArrayList<GUINode> getAllRecursiveChildren(GUINode guiNode) {
ArrayList<GUINode> list = new ArrayList<GUINode>();
for (GUINode child : guiNode.getChildren()) {
list.add(child);
list.addAll(getAllRecursiveChildren(child));
}
return list;
}
} }

View File

@ -4,11 +4,21 @@ import java.util.HashMap;
import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor; 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; import com.saltosion.gladiator.util.Name;
public class InputHandler implements InputProcessor { public class InputHandler implements InputProcessor {
public HashMap<Integer, String> keys = new HashMap<Integer, String>(); public HashMap<Integer, String> keys = new HashMap<Integer, String>();
private Array<String> hoveredUIElements = new Array<String>();
public InputHandler() { public InputHandler() {
keys.put(Keys.A, Name.MOVE_LEFT); keys.put(Keys.A, Name.MOVE_LEFT);
@ -61,6 +71,38 @@ public class InputHandler implements InputProcessor {
@Override @Override
public boolean mouseMoved(int screenX, int screenY) { 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; return false;
} }

View File

@ -37,7 +37,10 @@ public class RenderingSystem extends EntitySystem {
private BitmapFont font; private BitmapFont font;
private ShapeRenderer debugRenderer; private ShapeRenderer debugRenderer;
private OrthographicCamera camera; 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 boolean debug = true;
private final Color debugColor = new Color(0, 1, 0, 1); private final Color debugColor = new Color(0, 1, 0, 1);
@ -115,7 +118,7 @@ public class RenderingSystem extends EntitySystem {
position.add(node.getPosition()); position.add(node.getPosition());
if (node instanceof ImageNode) { if (node instanceof ImageNode) {
Sprite s = ((ImageNode) node).getImage(); 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); position.y*AppUtil.VPHEIGHT_CONST-s.getHeight()/2+camera.position.y);
s.draw(batch); s.draw(batch);
} }

View File

@ -6,8 +6,7 @@ public class Global {
public static final String PLAYERIMG = "PLAYERIMG"; public static final String PLAYERIMG = "PLAYERIMG";
public static final String GAME_NAME = "Gladiator Brawl"; public static final String GAME_NAME = "Gladiator Brawl";
public static final int VPHEIGHT_CONST = 14;
public static final float SPRITE_SCALE = 1 / 16f; public static final float SPRITE_SCALE = 1 / 16f;
} }