Added ui to the engine and buttons and implemented them in the game fixed also a mouse position problem

This commit is contained in:
Ziver Koc 2007-04-24 21:41:31 +00:00
parent 515281351c
commit 728a68cc48
35 changed files with 436 additions and 23 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 223 KiB

BIN
src/data/ui/apu_button.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 229 KiB

BIN
src/data/ui/start.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 945 B

BIN
src/data/ui/start_ontop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4 KiB

BIN
src/data/ui/tank_button.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 167 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 196 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

View file

@ -75,7 +75,7 @@ public abstract class MouseInput extends Input{
if(cursor != null){
GL11.glPushMatrix();
Vector3f v = LWJGLGameWindow.getCamera().getLocation();
GL11.glTranslatef(v.getX(),v.getY(), v.getZ());
GL11.glTranslatef(v.getX()+cursor.getSize().getX()/2,v.getY()+cursor.getSize().getY()/2, v.getZ());
cursor.render();
GL11.glPopMatrix();
}

View file

@ -43,6 +43,24 @@ public class Node extends Sprite {
return null;
}
/**
* Removes all the entities in this node
*
*/
public void clear(){
entities.clear();
}
/**
* Checks if the given entity is present in the node
*
* @param e The entity to look for
* @return True if the entity exists else false
*/
public boolean contains(Entity e){
return entities.contains(e);
}
/**
* remove a Entity
* @param name the name of Entity to remove

View file

@ -13,6 +13,8 @@ public class AnimatedTexture extends Texture {
private HashMap<String,Texture[]> textures;
private String currentAnimation;
private int textureId;
private int delay;
private int delayTimer;
/**
* Create a new empty AnimatedSprite
@ -24,7 +26,8 @@ public class AnimatedTexture extends Texture {
super();
textures = new HashMap<String,Texture[]>();
currentAnimation = null;
textureId = -1;
textureId = 0;
delay = 1;
}
/**
@ -36,6 +39,7 @@ public class AnimatedTexture extends Texture {
*/
public boolean addAnimation(String name, Texture[] t){
if(!textures.containsKey(name)){
if(textures.isEmpty())currentAnimation = name;
textures.put(name,t);
return true;
}
@ -81,10 +85,14 @@ public class AnimatedTexture extends Texture {
*/
public void bindGL() {
if(currentAnimation != null){
delayTimer++;
textures.get(currentAnimation)[textureId].bindGL();
textureId++;
if(textures.get(currentAnimation).length >= textureId){
textureId = 0;
if(delayTimer > delay){
textureId++;
if(textures.get(currentAnimation).length <= textureId){
textureId = 0;
}
delayTimer = 0;
}
}
}
@ -169,4 +177,13 @@ public class AnimatedTexture extends Texture {
public void setTextureWidth(float texWidth) {
textures.get(currentAnimation)[textureId].setTextureWidth(texWidth);
}
/**
* Set the delay between texures in frame count
*
* @param d The number of frames to whait until changing texture
*/
public void setDelay(int d){
delay = d;
}
}

View file

@ -0,0 +1,69 @@
package ei.engine.ui;
import ei.engine.math.Vector2f;
import ei.engine.math.Vector3f;
import ei.engine.scene.Node;
import ei.engine.scene.Sprite;
public class Button extends UiComponent{
private Sprite normalButton;
private Sprite ontopButton;
private Sprite presedButton;
private Node buttonNode;
private Vector2f size;
public Button(String name, Vector2f s){
super(name);
buttonNode = new Node(name+"Node");
size = s;
}
public void setButtonSprite(Sprite s){
normalButton = s;
}
public void setOnTopButtonSprite(Sprite s){
ontopButton = s;
}
public void setPressedButtonSprite(Sprite s){
presedButton = s;
}
public void mousePos(int x, int y){
if(size != null){
Vector3f pos = buttonNode.getLocation();
if( x >= (pos.getX()-(size.getX()/2)) && x <= (pos.getX()+(size.getX()/2))
&& y >= (pos.getY()-(size.getY()/2)) && y <= (pos.getY()+(size.getY()/2))){
if(ontopButton != null && !buttonNode.contains(ontopButton)){
buttonNode.clear();
buttonNode.add(ontopButton);
}
}
else{
if(normalButton != null && !buttonNode.contains(normalButton)){
buttonNode.clear();
buttonNode.add(normalButton);
}
}
}
}
public void mouseDown(int x, int y, int event){
if(size != null){
Vector3f pos = buttonNode.getLocation();
if( x >= (pos.getX()-(size.getX()/2)) && x <= (pos.getX()+(size.getX()/2))
&& y >= (pos.getY()-(size.getY()/2)) && y <= (pos.getY()+(size.getY()/2))){
if(presedButton != null && !buttonNode.contains(presedButton)){
buttonNode.clear();
buttonNode.add(presedButton);
}
castEvent();
}
}
}
public Node getNode(){
return buttonNode;
}
}

View file

@ -0,0 +1,61 @@
package ei.engine.ui;
import java.util.ArrayList;
import ei.engine.scene.Entity;
public abstract class UiComponent {
/** The list of listeners for this component */
private ArrayList<UiListener> listeners;
/** The name of the component */
private String name;
public UiComponent(String name){
this.name = name;
listeners = new ArrayList<UiListener>();
}
/**
* Add a listener to this component
*
* @param l The listener to add
*/
public void addListener(UiListener l) {
listeners.add(l);
}
/**
* Remove a listener from this component
*
* @param l The listener to remove
*/
public void removeListener(UiListener l) {
listeners.remove(l);
}
/**
* Casts a event to all the listeners
*
*/
protected void castEvent(){
for (int i=0;i<listeners.size();i++) {
listeners.get(i).ActionEvent(this);
}
}
public void mousePos(int x, int y){
}
public void mouseDown(int x, int y, int event){
}
public void keyDown(int event){
}
public String getName(){
return name;
}
public abstract Entity getNode();
}

View file

@ -0,0 +1,111 @@
package ei.engine.ui;
import java.util.ArrayList;
import ei.engine.scene.Node;
/**
* Handles all the ui assign to it
* @author Ziver
*
*/
public class UiHandler {
private ArrayList<UiComponent> ui;
private Node uiNode;
/**
* Creates a ui handler
*
* @param name The name of the ui handler
*/
public UiHandler(String name){
ui = new ArrayList<UiComponent>();
uiNode = new Node(name);
}
/**
* Add An ui component to the handler
*
* @param u The component to add
* @return True if successfully added else false
*/
public boolean addUi(UiComponent u){
if(!ui.contains(u)){
ui.add(u);
uiNode.add(u.getNode());
return true;
}
return false;
}
/**
* Removes a component from the handler
*
* @param u The component to remove
* @return True if successfully removed else false
*/
public boolean removeUi(UiComponent u){
if(ui.contains(u)){
ui.remove(u);
uiNode.remove(u.getNode());
return true;
}
return false;
}
/**
* Removes a component from the handler by its name
*
* @param name The name of the component to remove
* @return True if successfully removed else false
*/
public boolean addUi(String name){
for(int i=0; i<ui.size() ;i++){
if(ui.get(i).getName().equals(name)){
uiNode.remove(ui.get(i).getNode());
ui.remove(i);
return true;
}
}
return false;
}
/**
* Get the UiNode
*
* @return The UiNode
*/
public Node getNode(){
return uiNode;
}
/**
* Should be cald from a mouselistener
*
* @param x The x pos of the cursor
* @param y The y pos of the cursor
*/
public void mousePos(int x, int y){
for(int i=0; i<ui.size() ;i++){
ui.get(i).mousePos(x, y);
}
}
/**
* Should be cald from a mouselistener when mouse is down
*
* @param x The x pos of the cursor
* @param y The y pos of the cursor
*/
public void mouseDown(int x, int y, int event){
for(int i=0; i<ui.size() ;i++){
ui.get(i).mouseDown(x, y, event);
}
}
public void keyDown(int event){
for(int i=0; i<ui.size() ;i++){
ui.get(i).keyDown(event);
}
}
}

View file

@ -0,0 +1,11 @@
package ei.engine.ui;
/**
* A Listener class for the ui
* @author Ziver
*
*/
public interface UiListener {
public void ActionEvent(UiComponent source);
}

View file

@ -47,11 +47,13 @@ public class InGameState extends GameState{
rootNode.add(map.getMapNode());
rootNode.add(PlayerHandler.getInstance().getNode());
rootNode.add(WeaponHandler.getInstance().getNode());
music = new Sound("music", "data/sounds/ei.ogg");
music.loop();
hud = new InGameHud(player);
mouse.setHud(hud);
rootNode.add(hud.getNode());
music = new Sound("music", "data/sounds/ei.ogg");
music.loop();
}
/**

View file

@ -0,0 +1,77 @@
package ei.game.hud;
import ei.engine.math.Vector2f;
import ei.engine.scene.Node;
import ei.engine.scene.Sprite;
import ei.engine.ui.Button;
import ei.engine.ui.UiComponent;
import ei.engine.ui.UiHandler;
import ei.engine.ui.UiListener;
/**
* This class handels the build buttons for the units
* @author Ziver
*
*/
public class InGameBuildHud {
private UiHandler ui;
public InGameBuildHud(int x ,int y){
ui = new UiHandler("BuildMenu");
Vector2f size = new Vector2f(40,40);
Button apu = new Button("TankButton", size);
Sprite s1 = new Sprite("ApuButton","data/ui/apu_button.png");
s1.setSize(size);
apu.setButtonSprite(s1);
Sprite s2 = new Sprite("ApuButton_Selected","data/ui/apu_button_selected.png");
s2.setSize(size);
apu.setOnTopButtonSprite(s2);
apu.getNode().setLocation(new Vector2f(x,y));
apu.addListener(new UiListener(){
public void ActionEvent(UiComponent source){
System.out.println("apu");
}
});
ui.addUi(apu);
Button tank = new Button("TankButton", size);
Sprite s3 = new Sprite("TankButton","data/ui/tank_button.png");
s3.setSize(size);
tank.setButtonSprite(s3);
Sprite s4 = new Sprite("TankButton_Selected","data/ui/tank_button_selected.png");
s4.setSize(size);
tank.setOnTopButtonSprite(s4);
tank.getNode().setLocation(new Vector2f(x+(size.getX()*2),y));
tank.addListener(new UiListener(){
public void ActionEvent(UiComponent source){
System.out.println("tank");
}
});
ui.addUi(tank);
Button bomber = new Button("TankButton", size);
Sprite s5 = new Sprite("BomberButton","data/ui/bomber_button.png");
s5.setSize(size);
bomber.setButtonSprite(s5);
Sprite s6 = new Sprite("BomberButton_Selected","data/ui/bomber_button_selected.png");
s6.setSize(size);
bomber.setOnTopButtonSprite(s6);
bomber.getNode().setLocation(new Vector2f(x+(size.getX()*4),y));
bomber.addListener(new UiListener(){
public void ActionEvent(UiComponent source){
System.out.println("bomber");
}
});
ui.addUi(bomber);
}
public UiHandler getUi(){
return ui;
}
public Node getNode(){
return ui.getNode();
}
}

View file

@ -9,11 +9,18 @@ import ei.engine.scene.Node;
import ei.engine.scene.Sprite;
import ei.game.player.Player;
/**
* This clas handles the hud of the game
* @author Ziver
*
*/
public class InGameHud {
private Node hudNode;
private BitmapText money;
private Player player;
private ProgressBar buildBar;
private InGameBuildHud buildHud;
private Sprite buildBack;
public InGameHud(Player p){
player = p;
@ -30,7 +37,7 @@ public class InGameHud {
money.setLocation(new Vector2f(LWJGLGameWindow.getWidth()-money.getBound().width,5));
hudNode.add(money);
Sprite buildBack = new Sprite("BuildBackground","data/hud/buildmenu.png");
buildBack = new Sprite("BuildBackground","data/hud/buildmenu.png");
buildBack.setLocation(new Vector2f(
LWJGLGameWindow.getWidth()/2,
LWJGLGameWindow.getHeight()-buildBack.getSize().getY()/2));
@ -43,6 +50,10 @@ public class InGameHud {
LWJGLGameWindow.getWidth()/2+50,
LWJGLGameWindow.getHeight()-55));
hudNode.add(buildBar.getNode());
buildHud = new InGameBuildHud((int)(buildBack.getLocation().getX()-buildBack.getSize().getX()/4),
(int)(buildBack.getLocation().getY()+10));
hudNode.add(buildHud.getNode());
}
public void update(){
@ -59,4 +70,12 @@ public class InGameHud {
public Node getNode(){
return hudNode;
}
public InGameBuildHud getBuildHud(){
return buildHud;
}
public Sprite getBuildBar(){
return buildBack;
}
}

View file

@ -10,6 +10,7 @@ import ei.engine.math.Vector4f;
import ei.engine.scene.Box;
import ei.engine.scene.Sprite;
import ei.engine.texture.Texture;
import ei.game.hud.InGameHud;
import ei.game.scene.GameEntity;
import ei.game.scene.Map;
@ -22,6 +23,7 @@ public class InGameMouseInput extends MouseInput{
private Vector2f leftKlickPos;
private Box markingBox;
private InGameHud hud;
private Map map;
public InGameMouseInput(Map map) {
@ -40,6 +42,7 @@ public class InGameMouseInput extends MouseInput{
@Override
public void mouseUpdate(int x, int y, int w) {
if(hud != null)hud.getBuildHud().getUi().mousePos(x, y);
removeDead();
// mov cam to the left
@ -55,15 +58,21 @@ public class InGameMouseInput extends MouseInput{
LWJGLGameWindow.getCamera().getLocation().add(0,-CAMERA_MOVE_SPEED,0);
}
// mov cam down
if(y > LWJGLGameWindow.getHeight()-CAMERA_MOVE_BORDER){
if(y > LWJGLGameWindow.getHeight()-10){
LWJGLGameWindow.getCamera().getLocation().add(0,CAMERA_MOVE_SPEED,0);
}
Vector2i pos = Map.getPosByPixel(
LWJGLGameWindow.getCamera().getLocation().getX()+x,
LWJGLGameWindow.getCamera().getLocation().getY()+y);
LWJGLGameWindow.getCamera().getLocation().getX()+x+Map.POS_SIZE/2,
LWJGLGameWindow.getCamera().getLocation().getY()+y+Map.POS_SIZE/2);
if(map.posExist(pos.getX(), pos.getY())){
//checks if the mous is on the buildbar
if(x >= (LWJGLGameWindow.getWidth()/2)-hud.getBuildBar().getSize().getX()/2 &&
x <= (LWJGLGameWindow.getWidth()/2)+hud.getBuildBar().getSize().getX()/2 &&
y >= LWJGLGameWindow.getHeight()-hud.getBuildBar().getSize().getY() &&
y <= LWJGLGameWindow.getHeight()){}
// checks wich position the mouse is on top of
else if(map.posExist(pos.getX(), pos.getY())){
// The marking box is sized and positioned
if(leftKlickPos != null){
float markingSizeX = 0;
@ -83,7 +92,7 @@ public class InGameMouseInput extends MouseInput{
markingSizeY = -Math.abs(leftKlickPos.getY()-(LWJGLGameWindow.getCamera().getLocation().getY()+y));
}
markingBox.setSize(new Vector2f(markingSizeX+getSprite().getSize().getX()/2,markingSizeY+getSprite().getSize().getY()/2));
markingBox.setSize(new Vector2f(markingSizeX,markingSizeY));
markingBox.setLocation(new Vector2f(
leftKlickPos.getX()-markingBox.getSize().getX()/2,
leftKlickPos.getY()-markingBox.getSize().getY()/2));
@ -109,11 +118,17 @@ public class InGameMouseInput extends MouseInput{
@Override
public void mouseDown(int event,int x, int y) {
Vector2i pos = Map.getPosByPixel(
LWJGLGameWindow.getCamera().getLocation().getX()+x,
LWJGLGameWindow.getCamera().getLocation().getY()+y);
LWJGLGameWindow.getCamera().getLocation().getX()+x+Map.POS_SIZE/2,
LWJGLGameWindow.getCamera().getLocation().getY()+y+Map.POS_SIZE/2);
removeDead();
//map.printAllUnits();
if(map.posExist(pos.getX(), pos.getY())){
//checks if the mous is on the buildbar
if(x >= (LWJGLGameWindow.getWidth()/2)-hud.getBuildBar().getSize().getX()/2 &&
x <= (LWJGLGameWindow.getWidth()/2)+hud.getBuildBar().getSize().getX()/2 &&
y >= LWJGLGameWindow.getHeight()-hud.getBuildBar().getSize().getY() &&
y <= LWJGLGameWindow.getHeight()){}
// checks wich position the mouse presed
else if(map.posExist(pos.getX(), pos.getY())){
//selecting unit.
if(event==LEFT_MOUSE_BUTTON){
if(leftKlickPos == null){
@ -159,13 +174,14 @@ public class InGameMouseInput extends MouseInput{
@Override
public void mouseUp(int event,int x, int y) {
Vector2i pos = Map.getPosByPixel(
LWJGLGameWindow.getCamera().getLocation().getX()+x,
LWJGLGameWindow.getCamera().getLocation().getY()+y);
LWJGLGameWindow.getCamera().getLocation().getX()+x+Map.POS_SIZE/2,
LWJGLGameWindow.getCamera().getLocation().getY()+y+Map.POS_SIZE/2);
if(hud != null)hud.getBuildHud().getUi().mouseDown(x, y, event);
removeDead();
if(map.posExist(pos.getX(), pos.getY())){
if(leftKlickPos != null){
deselectAllUnits();
selectUnits(Map.getPosByPixel(leftKlickPos.getX(), leftKlickPos.getY()),pos);
selectUnits(Map.getPosByPixel((leftKlickPos.getX()+Map.POS_SIZE/2), (leftKlickPos.getY())+Map.POS_SIZE/2),pos);
leftKlickPos = null;
getNode().remove(markingBox);
}
@ -207,5 +223,9 @@ public class InGameMouseInput extends MouseInput{
}
selected.clear();
}
public void setHud(InGameHud u){
hud = u;
}
}

View file

@ -23,7 +23,7 @@ public class Map {
public static final int OBJ_STONE = 1;
private static final int POS_SIZE = 50;
public static final int POS_SIZE = 50;
private int width;
private int hight;
private GameEntity[][] map;

View file

@ -30,7 +30,6 @@ public abstract class Building extends GameEntity{
buildQueue = new LinkedList<Unit>();
unitNode = new Node("UnitNode");
unitNode.setLocation(Map.getPixelByPos(pos.getX(), pos.getY(), this.size));
System.out.println("location: "+unitNode.getLocation());
setPos(pos.getX(), pos.getY(), this.size);
}

View file

@ -15,7 +15,7 @@ public class CommandCenter extends Building{
public CommandCenter(int x, int y, Player p){
super(1000, new Vector2i(x,y), p, 4);
this.sprite = new Sprite("APU", "data/buildings/commandcenter.png");
this.sprite = new Sprite("APU", "data/buildings/cc/cc.png");
sprite.setSize(new Vector2f(200,200));
getNode().add(sprite);

View file

@ -4,6 +4,7 @@ import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i;
import ei.engine.scene.Sprite;
import ei.engine.sound.Sound;
import ei.engine.texture.AnimatedTexture;
import ei.game.player.Player;
import ei.game.scene.SelectBox;
import ei.game.scene.weapons.BomberWeapon;
@ -24,7 +25,15 @@ public class Bomber extends Unit{
public Bomber(int x, int y, Player p){
super(200, new Vector2i(x,y), p);
this.sprite = new Sprite("Bomber", "data/units/bomber/bomber0000.png");
AnimatedTexture tex = new AnimatedTexture("BomberTexture");
tex.addAnimation("normal", new String[]{
"data/units/bomber/bomber0000.png",
"data/units/bomber/bomber0001.png",
"data/units/bomber/bomber0002.png",
"data/units/bomber/bomber0003.png"
});
tex.setDelay(5);
sprite = new Sprite("Bomber",tex);
sprite.setSize(new Vector2f(50,60));
getNode().add(sprite);
setBuildTime(40);