【AndEngine】Progress Bar HUD

http://pastebin.com/u/Jong


package com.jong.rpg;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.hud.HUD;
import org.anddev.andengine.entity.primitive.Line;
import org.anddev.andengine.entity.primitive.Rectangle;

/**
 * @author Jong - Yonatan
 *
 */
public class ProgressBar extends HUD {
	// ===========================================================
	// Constants          
	// ===========================================================
	private static final float FRAME_LINE_WIDTH = 5f;
	// ===========================================================          
	// Fields         
	// =========================================================== 
	private final Line[] mFrameLines = new Line[4];
	private final Rectangle mBackgroundRectangle;
	private final Rectangle mProgressRectangle;
	
	private final float mPixelsPerPercentRatio;
	// ===========================================================          
	// Constructors          
	// =========================================================== 
	public ProgressBar(final Camera pCamera, final float pX, final float pY, final float pWidth, final float pHeight) {
		super();
		super.setCamera(pCamera);
		
		this.mBackgroundRectangle = new Rectangle(pX, pY, pWidth, pHeight);
		
		this.mFrameLines[0] = new Line(pX, pY, pX+pWidth, pY, FRAME_LINE_WIDTH); //Top line.
		this.mFrameLines[1] = new Line(pX + pWidth, pY, pX + pWidth, pY + pHeight, FRAME_LINE_WIDTH); //Right line.
		this.mFrameLines[2] = new Line(pX + pWidth, pY + pHeight, pX, pY + pHeight, FRAME_LINE_WIDTH); //Bottom line.
		this.mFrameLines[3] = new Line(pX, pY + pHeight, pX, pY, FRAME_LINE_WIDTH); //Left line.
		
		this.mProgressRectangle = new Rectangle(pX, pY, pWidth, pHeight);
		
		super.attachChild(this.mBackgroundRectangle); //This one is drawn first.
		super.attachChild(this.mProgressRectangle); //The progress is drawn afterwards.
		for(int i = 0; i < this.mFrameLines.length; i++)
			super.attachChild(this.mFrameLines[i]); //Lines are drawn last, so they'll override everything.
		
		this.mPixelsPerPercentRatio = pWidth / 100;
	}
	// ===========================================================          
	// Getter & Setter          
	// =========================================================== 
	public void setBackColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha) {
		this.mBackgroundRectangle.setColor(pRed, pGreen, pBlue, pAlpha);
	}
	public void setFrameColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha) {
		for(int i = 0; i < this.mFrameLines.length; i++)
			this.mFrameLines[i].setColor(pRed, pGreen, pBlue, pAlpha);
	}
	public void setProgressColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha) {
		this.mProgressRectangle.setColor(pRed, pGreen, pBlue, pAlpha);
	}
	/**
	 * Set the current progress of this progress bar.
	 * @param pProgress is <b> BETWEEN </b> 0 - 100.
	 */
	public void setProgress(final float pProgress) {
		if(pProgress < 0)
			this.mProgressRectangle.setWidth(0); //This is an internal check for my specific game, you can remove it.
		this.mProgressRectangle.setWidth(this.mPixelsPerPercentRatio * pProgress);
	}
	// ===========================================================          
	// Methods for/from SuperClass/Interfaces          
	// ===========================================================  
	
	// ===========================================================          
	// Methods          
	// ===========================================================  
	
	// ===========================================================          
	// Inner and Anonymous Classes          
	// ===========================================================  
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小涵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值