00001
#include "StdAfx.h"
00002
#include "GUI.h"
00003
00004 CGUIProgressBar::CGUIProgressBar()
00005 {
00006
Initialize();
00007 }
00008
00009 CGUIProgressBar::~CGUIProgressBar()
00010 {
00011
00012 }
00013
00014 void CGUIProgressBar::Initialize()
00015 {
00016
m_iCurPos = 1;
00017
m_iMinPos = 0;
00018
m_iMaxPos = 2;
00019
m_fCurPos = 0;
00020
m_fPixelPerValue = 0.0;
00021
m_bMinToMax =
true;
00022
m_bAutoCoordCalc =
true;
00023
SetType(GUI_HorizontalProgressBar);
00024
00025 m_strElementType =
"ProgressBar";
00026 }
00027
00028 int CGUIProgressBar::Create(
CGUIElement *pParent, tRect WidgetRect, eGUIControlType ProgressBarType,
CGUIElement *pPosElement,
CGUIElement *pNegElement,
CCustomFont *pFont,
CTexture *pTexture,
CMaterial *pMaterial,
bool bBorder)
00029 {
00030
if(!
CGUIStatic::Create(pParent, WidgetRect, pFont, pTexture, pMaterial, bBorder))
00031
return 0;
00032
00033
SetType(ProgressBarType);
00034
00035
CGUIElement *pPositiveElement = pPosElement;
00036
CGUIElement *pNegativeElement = pNegElement;
00037
00038
if(pPositiveElement == NULL)
00039 {
00040 pPositiveElement =
new CGUIElement();
00041 pPositiveElement->
Create(
this, tRect());
00042 }
00043
00044
if(pNegativeElement == NULL)
00045 {
00046 pNegativeElement =
new CGUIElement();
00047 pNegativeElement->
Create(
this, tRect());
00048 }
00049
00050
if(pPositiveElement !=NULL)
00051 {
00052
if((!
IsChild(pPositiveElement)) && (
GetChild(GUI_ProgressBar_PositiveElement) == NULL))
00053 {
00054 pPositiveElement->
SetType(GUI_ProgressBar_PositiveElement);
00055 pPositiveElement->
SetName(CString(
"Positive Element"));
00056 pPositiveElement->
SetParent(
this);
00057
00058 pPositiveElement->
SetFlag(GUI_Flag_RestrictMovementX,
true);
00059 pPositiveElement->
SetFlag(GUI_Flag_RestrictMovementY,
true);
00060
00061 pPositiveElement->
RemoveChild(pPositiveElement->
GetChild(GUI_Border));
00062
push_back(pPositiveElement);
00063 }
00064 }
00065
00066
if(pNegativeElement !=NULL)
00067 {
00068
if((!
IsChild(pNegativeElement)) && (
GetChild(GUI_ProgressBar_NegativeElement) == NULL))
00069 {
00070 pNegativeElement->
SetType(GUI_ProgressBar_NegativeElement);
00071 pNegativeElement->
SetName(CString(
"Negative Element"));
00072 pNegativeElement->
SetParent(
this);
00073
00074 pNegativeElement->
SetFlag(GUI_Flag_RestrictMovementX,
true);
00075 pNegativeElement->
SetFlag(GUI_Flag_RestrictMovementY,
true);
00076
00077 pNegativeElement->
RemoveChild(pNegativeElement->
GetChild(GUI_Border));
00078
push_back(pNegativeElement);
00079 }
00080 }
00081
00082
SetRange(
m_iMinPos,
m_iMaxPos);
00083
SetCurPos(
m_iCurPos);
00084
00085
return Resize(WidgetRect);
00086 }
00087
00088 int CGUIProgressBar::LoadXML(
TiXmlNode *pDataNode, CString strFilename)
00089 {
00090
TiXmlElement* pXMLElement = NULL;
00091
const char *pcValue = NULL;
00092
00093
int iRetValue =
CGUIStatic::LoadXML(pDataNode, strFilename);
00094
if(iRetValue !=1)
00095
return iRetValue;
00096
00097 pXMLElement = m_pXMLElement;
00098
00099 pcValue = pXMLElement->
Attribute(
"Type");
00100
if(pcValue !=NULL)
00101 {
00102
if(stricmp(pcValue,
"horizontal") == 0)
00103
SetType(GUI_HorizontalProgressBar);
00104
else if(stricmp(pcValue,
"vertical") == 0)
00105
SetType(GUI_VerticalProgressBar);
00106
else
00107 {
00108
CGlobalLogger::GetSingleton().
Write(
"\n\t\t!!!!!!!!!!General Error!!!!!!!!!!!!\n");
00109
CGlobalLogger::GetSingleton().
Write(
"ProgressBar (Name: %s, ID: %d) doesn't have Type (Type) Attribute specified.",
GetName(),
GetID());
00110 }
00111 pcValue = NULL;
00112 }
00113
00114 pcValue = pXMLElement->
Attribute(
"MinMaxCur");
00115
if(pcValue !=NULL)
00116 {
00117 sscanf(pcValue,
"%d,%d,%d", &
m_iMinPos, &
m_iMaxPos, &
m_iCurPos);
00118 pcValue = NULL;
00119 }
00120
00121 pcValue = pXMLElement->
Attribute(
"uiMin");
00122
if(pcValue !=NULL)
00123 {
00124
m_iMinPos = atoi(pcValue);
00125 pcValue = NULL;
00126 }
00127
00128 pcValue = pXMLElement->
Attribute(
"uiMax");
00129
if(pcValue !=NULL)
00130 {
00131
m_iMaxPos = atoi(pcValue);
00132 pcValue = NULL;
00133 }
00134
00135 pcValue = pXMLElement->
Attribute(
"Cur");
00136
if(pcValue !=NULL)
00137 {
00138
m_iCurPos = atoi(pcValue);
00139
m_fCurPos = (
float)
m_iCurPos;
00140 pcValue = NULL;
00141 }
00142
00143 UINT Count = 0;
00144
for (UINT i=0; i<
GetChildCount(); i++)
00145 {
00146
CGUIElement *pChild =
GetChild(i);
00147
if(pChild->
GetType() == GUI_Element)
00148 {
00149 pChild->
SetType(eGUIControlType(GUI_ProgressBar_NegativeElement - Count));
00150 Count++;
00151 }
00152
00153
if(Count == 2)
00154
break;
00155 }
00156
00157
CGUIElement *pPositiveElement =
GetChild(GUI_ProgressBar_PositiveElement);
00158
CGUIElement *pNegativeElement =
GetChild(GUI_ProgressBar_NegativeElement);
00159
00160
if(pPositiveElement !=NULL)
00161 {
00162 pPositiveElement->
SetFlag(GUI_Flag_RestrictMovementX,
true);
00163 pPositiveElement->
SetFlag(GUI_Flag_RestrictMovementY,
true);
00164 }
00165
00166
if(pNegativeElement !=NULL)
00167 {
00168 pNegativeElement->
SetFlag(GUI_Flag_RestrictMovementX,
true);
00169 pNegativeElement->
SetFlag(GUI_Flag_RestrictMovementY,
true);
00170 }
00171
00172
return Create(
GetParent(),
GetRect(),
GetType(), pPositiveElement, pNegativeElement);
00173 }
00174
00175 int CGUIProgressBar::SaveXML(
TiXmlNode *pDataNode, CString strFilename)
00176 {
00177
00178
if(pDataNode == NULL && strFilename ==
"")
00179
return -1;
00180
00181
char Buf[128] =
"";
00182
TiXmlElement *pSaveXMLElement = NULL;
00183
00184
if(pDataNode != NULL)
00185 pSaveXMLElement = pDataNode->
ToElement();
00186
else
00187 pSaveXMLElement =
new TiXmlElement(
"ProgressBar");
00188
00189
if(
GetType() == GUI_HorizontalProgressBar)
00190 pSaveXMLElement->
SetAttribute(
"Type",
"Horizontal");
00191
else if(
GetType() == GUI_VerticalProgressBar)
00192 pSaveXMLElement->
SetAttribute(
"Type",
"Vertical");
00193
00194 pSaveXMLElement->
SetAttribute(
"uiMin",
m_iMinPos);
00195 pSaveXMLElement->
SetAttribute(
"Cur",
m_iCurPos);
00196 pSaveXMLElement->
SetAttribute(
"uiMax",
m_iMaxPos);
00197
00198
int iRetValue =
CGUIStatic::SaveXML(pSaveXMLElement, strFilename);
00199
00200
if(pDataNode == NULL && strFilename !=
"")
00201 {
00202
TiXmlDocument SaveDoc;
00203 SaveDoc.
InsertEndChild(*pSaveXMLElement);
00204
int iRetValue = SaveDoc.
SaveFile(strFilename);
00205 SAFEDEL(pSaveXMLElement)
00206
00207
return iRetValue;
00208 }
00209
00210
return iRetValue;
00211 }
00212
00213 void CGUIProgressBar::Draw()
00214 {
00215
if(!
Visible())
00216
return;
00217
00218
00219
00220
00221
CGUIElement *pPositiveElement =
GetChild(GUI_ProgressBar_PositiveElement);
00222
CGUIElement *pNegativeElement =
GetChild(GUI_ProgressBar_NegativeElement);
00223
00224 pNegativeElement->
RemoveChild(pNegativeElement->
GetChild(GUI_Border));
00225 pPositiveElement->
RemoveChild(pPositiveElement->
GetChild(GUI_Border));
00226
00227
if(
m_bAutoCoordCalc)
00228 {
00229
if(
GetType() == GUI_HorizontalProgressBar)
00230 {
00231
if(pPositiveElement !=NULL)
00232 {
00233
CTexture *pTexture = pPositiveElement->
GetTexture();
00234
if(pTexture !=NULL)
00235 {
00236 pPositiveElement->
SetTexCoord(0, 0, 1);
00237 pPositiveElement->
SetTexCoord(1, (
float)pPositiveElement->
GetWidth() / (
float)pTexture->
GetWidth(), 1);
00238 pPositiveElement->
SetTexCoord(2, (
float)pPositiveElement->
GetWidth() / (
float)pTexture->
GetWidth(), 0);
00239 pPositiveElement->
SetTexCoord(3, 0, 0);
00240 }
00241 }
00242
00243
if(pNegativeElement !=NULL)
00244 {
00245
CTexture *pTexture = pNegativeElement->
GetTexture();
00246
if(pTexture !=NULL)
00247 {
00248 pNegativeElement->
SetTexCoord(0, 0, 1);
00249 pNegativeElement->
SetTexCoord(1, (
float)
GetWidth() / (
float)pTexture->
GetWidth(), 1);
00250 pNegativeElement->
SetTexCoord(2, (
float)
GetWidth() / (
float)pTexture->
GetWidth(), 0);
00251 pNegativeElement->
SetTexCoord(3, 0, 0);
00252 }
00253 }
00254 }
00255
else if(
GetType() == GUI_VerticalProgressBar)
00256 {
00257
if(pPositiveElement !=NULL)
00258 {
00259
CTexture *pTexture = pPositiveElement->
GetTexture();
00260
if(pTexture !=NULL)
00261 {
00262 pPositiveElement->
SetTexCoord(0, 0, (
float)pPositiveElement->
GetHeight() / (
float)pTexture->
GetHeight());
00263 pPositiveElement->
SetTexCoord(1, 1, (
float)pPositiveElement->
GetHeight() / (
float)pTexture->
GetHeight());
00264 pPositiveElement->
SetTexCoord(2, 1, 0);
00265 pPositiveElement->
SetTexCoord(3, 0, 0);
00266 }
00267 }
00268
00269
if(pNegativeElement !=NULL)
00270 {
00271
CTexture *pTexture = pNegativeElement->
GetTexture();
00272
if(pTexture !=NULL)
00273 {
00274 pNegativeElement->
SetTexCoord(0, 0, (
float)
GetHeight() / (
float)pTexture->
GetHeight());
00275 pNegativeElement->
SetTexCoord(1, 1, (
float)
GetHeight() / (
float)pTexture->
GetHeight());
00276 pNegativeElement->
SetTexCoord(2, 1, 0);
00277 pNegativeElement->
SetTexCoord(3, 0, 0);
00278 }
00279 }
00280 }
00281 }
00282
00283
CGUIStatic::Draw();
00284
00285 CString pcValue;
00286 pcValue.Format(
"%d",
m_iCurPos);
00287
SetText(pcValue);
00288
00289
DrawText();
00290 }
00291
00292 int CGUIProgressBar::Resize(tRect& NewRect)
00293 {
00294
int iRetValue =
CGUIStatic::Resize(NewRect);
00295
00296
CGUIElement *pPositiveElement =
GetChild(GUI_ProgressBar_PositiveElement);
00297
CGUIElement *pNegativeElement =
GetChild(GUI_ProgressBar_NegativeElement);
00298
00299 UINT Temp_IntCurPos =
m_iCurPos, Temp_FloatCurPos =
m_fCurPos;
00300
if(!
m_bMinToMax)
00301 {
00302 m_iCurPos =
m_iMaxPos - m_iCurPos;
00303 m_fCurPos = float(
m_iMaxPos) - m_fCurPos;
00304 }
00305
00306 tRect PositiveRect, NegativeRect;
00307
if(
GetType() == GUI_HorizontalProgressBar)
00308 {
00309
if(pPositiveElement !=NULL)
00310 {
00311
00312 PositiveRect.left =
GetRect().left + DEFAULT_PROGRESSBAR_SPACINGX;
00313 PositiveRect.right = PositiveRect.left +
m_fPixelPerValue * m_iCurPos;
00314 PositiveRect.top =
GetRect().top - DEFAULT_PROGRESSBAR_SPACINGY;
00315 PositiveRect.bottom =
GetRect().bottom + DEFAULT_PROGRESSBAR_SPACINGY;
00316 }
00317
00318
if(pNegativeElement !=NULL)
00319 {
00320 NegativeRect.left = PositiveRect.right;
00321 NegativeRect.right =
GetRect().right - DEFAULT_PROGRESSBAR_SPACINGX;
00322 NegativeRect.top =
GetRect().top - DEFAULT_PROGRESSBAR_SPACINGY;
00323 NegativeRect.bottom =
GetRect().bottom + DEFAULT_PROGRESSBAR_SPACINGY;
00324 }
00325 }
00326
else if(
GetType() == GUI_VerticalProgressBar)
00327 {
00328
if(pPositiveElement !=NULL)
00329 {
00330
00331 PositiveRect.left =
GetRect().left + DEFAULT_PROGRESSBAR_SPACINGX;
00332 PositiveRect.right =
GetRect().right - DEFAULT_PROGRESSBAR_SPACINGX;
00333 PositiveRect.top =
GetRect().top - DEFAULT_PROGRESSBAR_SPACINGY;
00334 PositiveRect.bottom = PositiveRect.top -
m_fPixelPerValue*m_iCurPos;
00335 }
00336
00337
if(pNegativeElement !=NULL)
00338 {
00339 NegativeRect.left =
GetRect().left + DEFAULT_PROGRESSBAR_SPACINGX;
00340 NegativeRect.right =
GetRect().right - DEFAULT_PROGRESSBAR_SPACINGX;
00341 NegativeRect.top = PositiveRect.bottom;
00342 NegativeRect.bottom =
GetRect().bottom + DEFAULT_PROGRESSBAR_SPACINGY;
00343 }
00344 }
00345
00346
if(pPositiveElement !=NULL)
00347 pPositiveElement->
SetRect(PositiveRect);
00348
00349
if(pNegativeElement !=NULL)
00350 pNegativeElement->
SetRect(NegativeRect);
00351
00352
if(!
m_bMinToMax)
00353 {
00354 m_iCurPos = Temp_IntCurPos;
00355 m_fCurPos = Temp_FloatCurPos;
00356 }
00357
00358
SetRange(
m_iMinPos,
m_iMaxPos,
m_bMinToMax);
00359
00360
return iRetValue;
00361 }
00362
00363 UINT
CGUIProgressBar::GetCurPos()
00364 {
00365
return m_iCurPos;
00366 }
00367
00368 void CGUIProgressBar::SetCurPos(
float pcValue)
00369 {
00370
if(pcValue >=
m_iMaxPos)
00371
m_iCurPos = (
int)
m_iMaxPos;
00372
else if(pcValue <=
m_iMinPos)
00373
m_iCurPos = (
int)
m_iMinPos;
00374
if(pcValue >
m_iMinPos && pcValue <
m_iMaxPos)
00375
m_iCurPos = (
int)pcValue;
00376
00377
Resize(
GetRect());
00378 }
00379
00380 void CGUIProgressBar::SetRange(UINT uiMin, UINT uiMax,
bool MinToMax)
00381 {
00382
m_iMinPos = uiMin;
00383
m_iMaxPos = uiMax;
00384
m_bMinToMax = MinToMax;
00385
00386
if(
GetType() == GUI_HorizontalProgressBar)
00387
m_fPixelPerValue = float(
GetWidth() - 2 * DEFAULT_PROGRESSBAR_SPACINGX) / (
m_iMaxPos -
m_iMinPos);
00388
else if(
GetType() == GUI_VerticalProgressBar)
00389
m_fPixelPerValue = float(
GetHeight() - 2 * DEFAULT_PROGRESSBAR_SPACINGY) / (
m_iMaxPos -
m_iMinPos);
00390 }
00391
00392 tVERTEX2d&
CGUIProgressBar::GetRange()
00393 {
00394
return tVERTEX2d(
m_iMinPos,
m_iMaxPos);
00395 }
00396
00397 void CGUIProgressBar::ProcessMessage(
tGUIMessage& Message)
00398 {
00399
switch(Message.uiMessage)
00400 {
00401
case GUI_Message_MoveX:
00402
case GUI_Message_MoveY:
00403
case GUI_Message_MoveXY:
00404 {
00405
if(Message.
pSender ==
this)
00406
Resize(
GetRect());
00407
00408
CGUIElement *pIterElement =
begin();
00409
set_ptr(pIterElement);
00410
00411
while(pIterElement !=NULL)
00412 {
00413
if((Message.
pSender == pIterElement) && (Message.
pSender->
GetType() == GUI_Border))
00414
Resize(
GetRect());
00415
00416 pIterElement =
next();
00417 }
00418
00419
break;
00420 }
00421 }
00422 }
00423
00424 bool CGUIProgressBar::IsOfType(eEntityType eType)
00425 {
00426
if(eType == Entity_GUIProgressBar)
00427
return true;
00428
00429
return CGUIStatic::IsOfType(eType);
00430 }
00431
00432 bool CGUIProgressBar::PropertyChanging(
const void* pvProperty ,
void* pvNewValue )
00433 {
00434
bool bChangeOK =
CGUIStatic::PropertyChanging(pvProperty, pvNewValue);
00435
00436
if(pvProperty == &
m_iMinPos)
00437 {
00438
m_iMinPos = *(
int *)pvNewValue;
00439
SetRange(
m_iMinPos,
m_iMaxPos);
00440 }
00441
00442
if(pvProperty == &
m_iMaxPos)
00443 {
00444
m_iMaxPos = *(
int *)pvNewValue;
00445
SetRange(
m_iMinPos,
m_iMaxPos);
00446 }
00447
00448
if(pvProperty == &
m_iCurPos)
00449 {
00450
m_iCurPos = *(
int *)pvNewValue;
00451
m_fCurPos = (
float)
m_iCurPos;
00452
00453
SetCurPos(
m_fCurPos);
00454 }
00455
00456
return bChangeOK;
00457 }
00458
00459 void CGUIProgressBar::GetProperties( EPropList& PropList )
00460 {
00461
CGUIStatic::GetProperties(PropList);
00462
00463 PropList.AddTab(
"ProgressBar");
00464 PropList.AddPropInt(
this,
"Minimum", &
m_iMinPos,
"",
true)->SetComment(
"Minimum scroll value");
00465 PropList.AddPropInt(
this,
"Maximum", &
m_iMaxPos,
"",
true)->SetComment(
"Maximum scroll value");
00466 PropList.AddPropInt(
this,
"Current", &
m_iCurPos,
"",
true)->SetComment(
"Current scroll value");
00467 PropList.AddPropCheck(
this,
"uiMin-To-uiMax", &
m_bMinToMax)->SetComment(
"Progress left-to-right or other way around");
00468 PropList.AddPropCheck(
this,
"Auto-Coord Calc.", &
m_bAutoCoordCalc)->SetComment(
"Automatic texture coords calculation");
00469 }