Added ui to the engine and buttons and implemented them in the game fixed also a mouse position problem
This commit is contained in:
parent
515281351c
commit
728a68cc48
35 changed files with 436 additions and 23 deletions
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
69
src/ei/engine/ui/Button.java
Normal file
69
src/ei/engine/ui/Button.java
Normal 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;
|
||||
}
|
||||
}
|
||||
61
src/ei/engine/ui/UiComponent.java
Normal file
61
src/ei/engine/ui/UiComponent.java
Normal 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();
|
||||
}
|
||||
111
src/ei/engine/ui/UiHandler.java
Normal file
111
src/ei/engine/ui/UiHandler.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
src/ei/engine/ui/UiListener.java
Normal file
11
src/ei/engine/ui/UiListener.java
Normal 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue