From 318f92bc93bdda415dab79c86d7b91065a2ba811 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Thu, 5 Apr 2007 13:30:18 +0000 Subject: [PATCH] Added a progress bar class for future use --- src/ei/engine/effects/ProgressBar.java | 220 ++++++++++++++++++++++++ src/ei/game/gamestate/LoadingState.java | 55 ++---- 2 files changed, 238 insertions(+), 37 deletions(-) create mode 100644 src/ei/engine/effects/ProgressBar.java diff --git a/src/ei/engine/effects/ProgressBar.java b/src/ei/engine/effects/ProgressBar.java new file mode 100644 index 0000000..77da6ec --- /dev/null +++ b/src/ei/engine/effects/ProgressBar.java @@ -0,0 +1,220 @@ +package ei.engine.effects; + +import java.awt.Rectangle; + +import ei.engine.LWJGLGameWindow; +import ei.engine.math.Vector2f; +import ei.engine.math.Vector3f; +import ei.engine.scene.Entity; +import ei.engine.scene.Sprite; + +/** + * This class is a progress meter + * + * @author Ziver + * + */ + +public class ProgressBar extends Entity{ + // Temp things for the loadingbar + private int value; + private int max; + private int min; + // The textures + private Sprite bar; + private Sprite progress; + private Sprite background; + + public ProgressBar(String name){ + super(name); + value = 0; + min = 0; + max = 100; + } + + /** + * Set the texture for the bar + * + * @param s The texture for the bar + */ + public void setBarTexture(Sprite s){ + bar = s; + } + + /** + * Set the texture for the progressbar + * + * @param s The texture for the progressbar + */ + public void setProgressTexture(Sprite s){ + progress = s; + } + + /** + * Set the texture for the background + * + * @param s The texture for the background + */ + public void setBackgroundTexture(Sprite s){ + background = s; + } + + /** + * Centers the bar to the screen + * + */ + public void centerToScreen(){ + setLocation(new Vector3f( + (LWJGLGameWindow.getWidth()/2), + (LWJGLGameWindow.getHeight()/2),0.0f)); + } + + /** + * set the location of this entity in pixels + * + * @param l The location of the entity + */ + public void setLocation(Vector3f l) { + super.setLocation(l); + updateLocation(); + } + + /** + * set the location of this entity in pixels + * + * @param l The location of the entity + */ + public void setLocation(Vector2f l) { + super.setLocation(l); + updateLocation(); + } + + private void updateLocation(){ + if(bar != null){ + bar.setLocation(getLocation()); + } + if(progress != null){ + progress.setLocation(new Vector3f( + (LWJGLGameWindow.getWidth()/2)-progress.getWidth(), + (LWJGLGameWindow.getHeight()/2)+3,0.0f)); + } + if(background != null){ + background.setLocation(getLocation()); + } + } + + /** + * Sets the max value + * + * @param max The max value + */ + public void setMax(int max){ + this.max = max; + if(value > max){ + value = max; + } + } + + /** + * Returns the max value + * + * @return The max value + */ + public int getMax(){ + return max; + } + + /** + * Set the value of progress + * + * @param v The value of progress + */ + public void setValue(int v){ + value = v; + if(min > value){ + value = min; + } + if(value > max){ + value = max; + } + } + + /** + * Returns the value of the progress + * + * @return The value of the progress + */ + public int getValue(){ + return value; + } + + /** + * Set the min value + * + * @param m The min value + */ + public void setMin(int m){ + min = m; + if(min > value){ + value = min; + } + } + + /** + * Returns the min value + * + * @return The min value + */ + public int getMin(){ + return min; + } + + /** + * Returns the per cent of the progress + * + * @return The per cent of the progress + */ + public int getProcent(){ + return (int)(((float)(value-min)/(max-min))*100); + } + + /** + * Updates the progressbar + */ + public void update(){ + updateLocation(); + if(background != null)background.update(); + if(progress != null)progress.update(); + if(bar != null)bar.update(); + } + + /** + * Render the load bar + */ + public void render() { + // Calculate the procentage + float procent = (float)(value-min)/(max-min); + //render the bar + if(background != null)background.render(); + if(progress != null){ + progress.setLocation(new Vector3f( + getLocation().getX()-progress.getWidth()+(progress.getWidth()*procent), + getLocation().getY()+6,0.0f)); + progress.render(); + } + if(bar != null) bar.render(); + } + + @Override + public Rectangle getBound() { + if(bar != null){ + return bar.getBound(); + } + else if(progress != null){ + return progress.getBound(); + } + else{ + return null; + } + } +} diff --git a/src/ei/game/gamestate/LoadingState.java b/src/ei/game/gamestate/LoadingState.java index 756ccf5..d7ae0c8 100644 --- a/src/ei/game/gamestate/LoadingState.java +++ b/src/ei/game/gamestate/LoadingState.java @@ -4,6 +4,7 @@ import java.util.LinkedList; import java.util.Queue; import ei.engine.LWJGLGameWindow; +import ei.engine.effects.ProgressBar; import ei.engine.math.Vector3f; import ei.engine.scene.Sprite; import ei.engine.sound.SoundLoader; @@ -23,12 +24,8 @@ public class LoadingState extends GameState{ private Queue loadSounds; // The name of the next state to activate after loading private String nextState; - - // Temp things for the loadingbar - private int status; - private Sprite bar; - private Sprite loadBar; - private Sprite background; + private ProgressBar progress; + /** * Creates a loadingstate @@ -37,21 +34,14 @@ public class LoadingState extends GameState{ public LoadingState(String name,String nextState) { super(name); this.nextState = nextState; - status = 0; loadTextures = new LinkedList(); loadSounds = new LinkedList(); - bar = new Sprite("Bar","data/loadbar_front.png"); - bar.setLocation(new Vector3f( - (LWJGLGameWindow.getWidth()/2), - (LWJGLGameWindow.getHeight()/2),0.0f)); - loadBar = new Sprite("LoadingBar","data/loadbar.png"); - loadBar.setLocation(new Vector3f( - (LWJGLGameWindow.getWidth()/2)-loadBar.getWidth(), - (LWJGLGameWindow.getHeight()/2)+3,0.0f)); - background = new Sprite("Bar","data/loadbar_back.png"); - background.setLocation(new Vector3f( - (LWJGLGameWindow.getWidth()/2), - (LWJGLGameWindow.getHeight()/2),0.0f)); + + progress = new ProgressBar("Loading"); + progress.centerToScreen(); + progress.setBarTexture(new Sprite("ProgressBar","data/loadbar_front.png")); + progress.setProgressTexture(new Sprite("Progress","data/loadbar.png")); + progress.setBackgroundTexture(new Sprite("progressBackground","data/loadbar_back.png")); } /** @@ -60,6 +50,7 @@ public class LoadingState extends GameState{ */ public void addTexture(String url){ loadTextures.add(url); + progress.setMax(loadTextures.size()+loadSounds.size()); } /** @@ -68,6 +59,7 @@ public class LoadingState extends GameState{ */ public void addSound(String url){ loadSounds.add(url); + progress.setMax(loadTextures.size()+loadSounds.size()); } /** @@ -76,39 +68,28 @@ public class LoadingState extends GameState{ public void update() { if(!loadTextures.isEmpty()){ TextureLoader.getTextureLoaderInstance().getTexture(loadTextures.poll()); - status++; + progress.setValue(progress.getValue()+1); } else if(!loadSounds.isEmpty()){ SoundLoader.getInstnace().loadSound(loadSounds.poll()); - status++; + progress.setValue(progress.getValue()+1); } else{ - status++; - if(status >= 100){ + progress.setValue(progress.getValue()+1); + if(progress.getProcent() >= 100){ //deactivate this state and activate the next one GameStateManager.getInstance().removeState(this); GameStateManager.getInstance().setActive(nextState); - } - + } } - background.update(); - loadBar.update(); - bar.update(); + progress.update(); } /** * Render the load bar */ public void render() { - // Calculate the procentage - float procent = (float)status/100;//(loadTextures.size()+loadSounds.size()); - loadBar.setLocation(new Vector3f( - (LWJGLGameWindow.getWidth()/2)-loadBar.getWidth()+(loadBar.getWidth()*procent), - (LWJGLGameWindow.getHeight()/2)+6,0.0f)); - //render the bar - background.render(); - loadBar.render(); - bar.render(); + progress.render(); } }