00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gui/widgets/progressbar.h"
00023
00024 #include "gui/gui.h"
00025 #include "gui/palette.h"
00026 #include "gui/textrenderer.h"
00027
00028 #include "configuration.h"
00029 #include "graphics.h"
00030
00031 #include "resources/image.h"
00032 #include "resources/resourcemanager.h"
00033
00034 #include <guichan/font.hpp>
00035
00036 ImageRect ProgressBar::mBorder;
00037 int ProgressBar::mInstances = 0;
00038 float ProgressBar::mAlpha = 1.0;
00039
00040 ProgressBar::ProgressBar(float progress,
00041 unsigned int width, unsigned int height,
00042 Uint8 red, Uint8 green, Uint8 blue):
00043 gcn::Widget(),
00044 mRed(red), mGreen(green), mBlue(blue),
00045 mRedToGo(red), mGreenToGo(green), mBlueToGo(blue)
00046 {
00047 mProgressToGo = mProgress = 0.0f;
00048 mSmoothProgress = mSmoothColorChange = true;
00049
00050 setProgress(progress);
00051 setWidth(width);
00052 setHeight(height);
00053
00054 if (mInstances == 0)
00055 {
00056 ResourceManager *resman = ResourceManager::getInstance();
00057 Image *dBorders = resman->getImage("graphics/gui/vscroll_grey.png");
00058 mBorder.grid[0] = dBorders->getSubImage(0, 0, 4, 4);
00059 mBorder.grid[1] = dBorders->getSubImage(4, 0, 3, 4);
00060 mBorder.grid[2] = dBorders->getSubImage(7, 0, 4, 4);
00061 mBorder.grid[3] = dBorders->getSubImage(0, 4, 4, 10);
00062 mBorder.grid[4] = resman->getImage("graphics/gui/bg_quad_dis.png");
00063 mBorder.grid[5] = dBorders->getSubImage(7, 4, 4, 10);
00064 mBorder.grid[6] = dBorders->getSubImage(0, 15, 4, 4);
00065 mBorder.grid[7] = dBorders->getSubImage(4, 15, 3, 4);
00066 mBorder.grid[8] = dBorders->getSubImage(7, 15, 4, 4);
00067
00068 for (int i = 0; i < 9; i++)
00069 {
00070 mBorder.grid[i]->setAlpha(mAlpha);
00071 }
00072
00073 dBorders->decRef();
00074 }
00075
00076 mInstances++;
00077 }
00078
00079 ProgressBar::~ProgressBar()
00080 {
00081 mInstances--;
00082
00083 if (mInstances == 0)
00084 {
00085 delete mBorder.grid[0];
00086 delete mBorder.grid[1];
00087 delete mBorder.grid[2];
00088 delete mBorder.grid[3];
00089 mBorder.grid[4]->decRef();
00090 delete mBorder.grid[5];
00091 delete mBorder.grid[6];
00092 delete mBorder.grid[7];
00093 delete mBorder.grid[8];
00094 }
00095 }
00096
00097 void ProgressBar::logic()
00098 {
00099 if (mSmoothColorChange)
00100 {
00101
00102 if (mRedToGo > mRed) mRed++;
00103 if (mRedToGo < mRed) mRed--;
00104 if (mGreenToGo > mGreen) mGreen++;
00105 if (mGreenToGo < mGreen) mGreen--;
00106 if (mBlueToGo > mBlue) mBlue++;
00107 if (mBlueToGo < mBlue) mBlue--;
00108 }
00109 else
00110 {
00111 mRed = mRedToGo;
00112 mGreen = mGreenToGo;
00113 mBlue = mBlueToGo;
00114 }
00115
00116 if (mSmoothProgress)
00117 {
00118
00119 if (mProgressToGo > mProgress) mProgress = mProgress + 0.005f;
00120 if (mProgressToGo < mProgress) mProgress = mProgress - 0.005f;
00121 }
00122 else
00123 mProgress = mProgressToGo;
00124 }
00125
00126 void ProgressBar::draw(gcn::Graphics *graphics)
00127 {
00128 if (config.getValue("guialpha", 0.8) != mAlpha)
00129 {
00130 mAlpha = config.getValue("guialpha", 0.8);
00131 for (int i = 0; i < 9; i++)
00132 {
00133 mBorder.grid[i]->setAlpha(mAlpha);
00134 }
00135 }
00136
00137 static_cast<Graphics*>(graphics)->
00138 drawImageRect(0, 0, getWidth(), getHeight(), mBorder);
00139
00140 const int alpha = (int)(mAlpha * 255.0f);
00141
00142
00143 if (mProgress > 0)
00144 {
00145 graphics->setColor(gcn::Color(mRed, mGreen, mBlue, alpha));
00146 graphics->fillRectangle(gcn::Rectangle(4, 4,
00147 (int) (mProgress * (getWidth() - 8)),
00148 getHeight() - 8));
00149 }
00150
00151
00152 if (!mText.empty())
00153 {
00154 const int textX = getWidth() / 2;
00155 const int textY = (getHeight() - boldFont->getHeight()) / 2;
00156
00157 TextRenderer::renderText(graphics, mText, textX, textY,
00158 gcn::Graphics::CENTER,
00159 guiPalette->getColor(Palette::PROGRESS_BAR,
00160 alpha), boldFont, true, false);
00161 }
00162 }
00163
00164 void ProgressBar::setProgress(float progress)
00165 {
00166 if (progress < 0.0f) mProgressToGo = 0.0;
00167 else if (progress > 1.0f) mProgressToGo = 1.0;
00168 else mProgressToGo = progress;
00169 }
00170
00171 void ProgressBar::setColor(Uint8 red, Uint8 green, Uint8 blue)
00172 {
00173 mRedToGo = red;
00174 mGreenToGo = green;
00175 mBlueToGo = blue;
00176 }