Progress Bar HUD

  ... the case you feel the need for a new feature or want to submit one.

Progress Bar HUD

Postby Jong » Mon Dec 19, 2011 10:25 pm

Just a simple class I created now in barely 20 minutes :)
I tested it with different parameters, works fine. I never saw any class like that in AndEngine to fill this purpose, and I have found a few requests when googling it. So, here it is: http://pastebin.com/wxsgXpRj

Or:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.jong.rpg;
  2.  
  3. import org.anddev.andengine.engine.camera.Camera;
  4. import org.anddev.andengine.engine.camera.hud.HUD;
  5. import org.anddev.andengine.entity.primitive.Line;
  6. import org.anddev.andengine.entity.primitive.Rectangle;
  7.  
  8. /**
  9.  * @author Jong - Yonatan
  10.  *
  11.  */
  12. public class ProgressBar extends HUD {
  13.         // ===========================================================
  14.         // Constants          
  15.         // ===========================================================
  16.         private static final float FRAME_LINE_WIDTH = 5f;
  17.         // ===========================================================          
  18.         // Fields        
  19.         // ===========================================================
  20.         private final Line[] mFrameLines = new Line[4];
  21.         private final Rectangle mBackgroundRectangle;
  22.         private final Rectangle mProgressRectangle;
  23.        
  24.         private final float mPixelsPerPercentRatio;
  25.         // ===========================================================          
  26.         // Constructors          
  27.         // ===========================================================
  28.         public ProgressBar(final Camera pCamera, final float pX, final float pY, final float pWidth, final float pHeight) {
  29.                 super();
  30.                 super.setCamera(pCamera);
  31.                
  32.                 this.mBackgroundRectangle = new Rectangle(pX, pY, pWidth, pHeight);
  33.                
  34.                 this.mFrameLines[0] = new Line(pX, pY, pX+pWidth, pY, FRAME_LINE_WIDTH); //Top line.
  35.                 this.mFrameLines[1] = new Line(pX + pWidth, pY, pX + pWidth, pY + pHeight, FRAME_LINE_WIDTH); //Right line.
  36.                 this.mFrameLines[2] = new Line(pX + pWidth, pY + pHeight, pX, pY + pHeight, FRAME_LINE_WIDTH); //Bottom line.
  37.                 this.mFrameLines[3] = new Line(pX, pY + pHeight, pX, pY, FRAME_LINE_WIDTH); //Left line.
  38.                
  39.                 this.mProgressRectangle = new Rectangle(pX, pY, pWidth, pHeight);
  40.                
  41.                 super.attachChild(this.mBackgroundRectangle); //This one is drawn first.
  42.                 super.attachChild(this.mProgressRectangle); //The progress is drawn afterwards.
  43.                 for(int i = 0; i < this.mFrameLines.length; i++)
  44.                         super.attachChild(this.mFrameLines[i]); //Lines are drawn last, so they'll override everything.
  45.                
  46.                 this.mPixelsPerPercentRatio = pWidth / 100;
  47.         }
  48.         // ===========================================================          
  49.         // Getter & Setter          
  50.         // ===========================================================
  51.         public void setBackColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha) {
  52.                 this.mBackgroundRectangle.setColor(pRed, pGreen, pBlue, pAlpha);
  53.         }
  54.         public void setFrameColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha) {
  55.                 for(int i = 0; i < this.mFrameLines.length; i++)
  56.                         this.mFrameLines[i].setColor(pRed, pGreen, pBlue, pAlpha);
  57.         }
  58.         public void setProgressColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha) {
  59.                 this.mProgressRectangle.setColor(pRed, pGreen, pBlue, pAlpha);
  60.         }
  61.         /**
  62.          * Set the current progress of this progress bar.
  63.          * @param pProgress is <b> BETWEEN </b> 0 - 100.
  64.          */
  65.         public void setProgress(final float pProgress) {
  66.                 if(pProgress < 0)
  67.                         this.mProgressRectangle.setWidth(0); //This is an internal check for my specific game, you can remove it.
  68.                 this.mProgressRectangle.setWidth(this.mPixelsPerPercentRatio * pProgress);
  69.         }
  70.         // ===========================================================          
  71.         // Methods for/from SuperClass/Interfaces          
  72.         // ===========================================================  
  73.        
  74.         // ===========================================================          
  75.         // Methods          
  76.         // ===========================================================  
  77.        
  78.         // ===========================================================          
  79.         // Inner and Anonymous Classes          
  80.         // ===========================================================  
  81.        
  82. }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


I'm using it as an HP / EXP bar in a RPG game, but I guess it could have other uses too.
Jong
 
Posts: 49
Joined: Sat Sep 03, 2011 10:46 pm

Re: Progress Bar HUD

Postby rizzythedon » Sun Dec 25, 2011 2:32 am

Thanks, anyway to put rgb colors? I don't seem to get float values :P and is it possible to add :

-- MAX PROGRESS lets say 100 is max and we can change it to 200 or more and it will increment slower ;)
-- MIN PROGRESS

You get the idea :P

Thanks again :) :color:
Games with AndEngine:
## Flop Ball [new]
## Tiny Elite Commando
## Copter
and more... click link and play
Market Link: https://market.android.com/developer?pub=Fihox
rizzythedon
 
Posts: 85
Joined: Thu Dec 22, 2011 2:45 am

Re: Progress Bar HUD

Postby spaine » Wed Dec 28, 2011 4:40 am

Used this for my character's HP and MP bar... Works great! Thanks!! :D
spaine
 
Posts: 58
Joined: Tue Aug 30, 2011 10:26 am

Re: Progress Bar HUD

Postby Nonejm » Thu Dec 29, 2011 2:10 pm

Just thanks for sharing.
Bojan Klabjan ( K-LAB )
Image Burn The Popcorn
Download -> https://market.android.com/details?id=com.klabjan.burnthepopcorn
Nonejm
 
Posts: 99
Joined: Mon Sep 05, 2011 8:14 pm
Location: Slovenia

Re: Progress Bar HUD

Postby morlicando » Fri Dec 30, 2011 9:58 am

Nice work, bro! I think i'll use it. :D
Best regards, Morlicando
morlicando
 
Posts: 85
Joined: Wed Nov 23, 2011 5:35 pm

Re: Progress Bar HUD

Postby Pinoyden » Fri Jan 06, 2012 10:17 am

Thanks for sharing your code! :)
Pinoyden
 
Posts: 2
Joined: Fri Jan 06, 2012 10:12 am

Re: Progress Bar HUD

Postby spaine » Sat Jan 07, 2012 6:44 am

Question on this. I want my to increase my bar capacity without increasing he bar height. Just like HP bars when a character level ups, it increase capacity, but not the width of the bar. cause when I i increase the capacity dynamically, i goes off the screen.
spaine
 
Posts: 58
Joined: Tue Aug 30, 2011 10:26 am

Re: Progress Bar HUD

Postby timothyleerussell » Wed Jan 18, 2012 8:56 am

Thanks peep! I was looking for some examples about how to extend the HUD. Appreciate it!
timothyleerussell
 
Posts: 19
Joined: Wed Sep 08, 2010 5:57 pm

Re: Progress Bar HUD

Postby Muddasir » Wed Jan 25, 2012 6:54 am

hmm i am new at and engine would some body tell me how to use it .. :( ...
Thankx In Advance
Muddasir
 
Posts: 9
Joined: Tue Dec 13, 2011 8:08 am

Re: Progress Bar HUD

Postby spaine » Thu Jan 26, 2012 4:22 am

Got my problem solved on increasing Bar capacity without increasing Bar width:
Add this function to your ProgressBar class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  public void setWidth(float width1, float width2){
  2.                         this.mPixelsPerPercentRatio = width1/width2;
  3.                
  4.         }
  5.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Then calling it would be like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. object.setWidth(float original_width, float intended_capacity);
  2.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Somthing like that... :lol:
spaine
 
Posts: 58
Joined: Tue Aug 30, 2011 10:26 am

Next

Return to Features

Who is online

Users browsing this forum: Exabot [Bot], Google Feedfetcher and 14 guests