Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

D:/Programming/GUI Editor (Source)/GUIScrollBar.cpp

00001 #include "StdAfx.h" 00002 #include "GUI.h" 00003 00005 // Construction/Destruction 00007 00008 CGUIScrollBar::CGUIScrollBar() 00009 { 00010 Initialize(); 00011 } 00012 00013 CGUIScrollBar::~CGUIScrollBar() 00014 { 00015 00016 } 00017 00018 void CGUIScrollBar::Initialize() 00019 { 00020 // A default is a horizontal scrollbar (byte 1 is 1 if LMouse was initiated over a scroll element) 00021 SetType(GUI_HorizontalScrollBar); 00022 m_iMinPos = 0; m_iMaxPos = 1; m_iPrevPos = 0; m_iBtnInc = 1; 00023 m_fCurPos = 0; m_fPixelPerValue = 0; 00024 m_iScrlInc = 5; 00025 00026 m_strElementType = "ScrollBar"; 00027 } 00028 00029 int CGUIScrollBar::Create(CGUIElement *pParent, tRect WidgetRect, eGUIControlType ScrollBarType, CGUIButton *pIncButton, CGUIButton *pDecButton, CGUIButton *pThumbButton, CCustomFont *pFont, CTexture *pTexture, CMaterial *pMaterial, bool bBorder) 00030 { 00031 if(!CGUIStatic::Create(pParent, WidgetRect, pFont, pTexture, pMaterial, bBorder)) 00032 return 0; 00033 00034 SetType(ScrollBarType); 00035 00036 CGUIButton *pIncrementButton = pIncButton; 00037 CGUIButton *pDecrementButton = pDecButton; 00038 CGUIButton *pDragButton = pThumbButton; 00039 00040 if(pIncrementButton == NULL) 00041 { 00042 pIncrementButton = new CGUIButton; 00043 pIncrementButton->Create(this, tRect()); 00044 } 00045 00046 if(pDecrementButton == NULL) 00047 { 00048 pDecrementButton = new CGUIButton; 00049 pDecrementButton->Create(this, tRect()); 00050 } 00051 00052 if(pDragButton == NULL) 00053 { 00054 pDragButton = new CGUIButton; 00055 pDragButton->Create(this, tRect()); 00056 } 00057 00058 if(pIncrementButton !=NULL) 00059 { 00060 if((!IsChild(pIncrementButton)) && (GetChild(GUI_ScrollBar_ButtonInc) == NULL)) 00061 { 00062 pIncrementButton->SetType(GUI_ScrollBar_ButtonInc); 00063 pIncrementButton->SetParent(this); 00064 00065 pIncrementButton->RemoveChild(pIncrementButton->GetChild(GUI_Border)); 00066 pIncrementButton->SetFlag(GUI_Flag_RestrictMovementX, true); 00067 pIncrementButton->SetFlag(GUI_Flag_RestrictMovementY, true); 00068 push_back(pIncrementButton); 00069 } 00070 } 00071 00072 if(pDecrementButton !=NULL) // Search by pointer, so user cannot specify same button for all three 00073 { 00074 if((!IsChild(pDecrementButton)) && (GetChild(GUI_ScrollBar_ButtonDec) == NULL)) 00075 { 00076 pDecrementButton->SetType(GUI_ScrollBar_ButtonDec); 00077 pDecrementButton->SetParent(this); 00078 00079 pDecrementButton->RemoveChild(pDecrementButton->GetChild(GUI_Border)); 00080 pDecrementButton->SetFlag(GUI_Flag_RestrictMovementX, true); 00081 pDecrementButton->SetFlag(GUI_Flag_RestrictMovementY, true); 00082 push_back(pDecrementButton); 00083 } 00084 } 00085 00086 if(pDragButton !=NULL) 00087 { 00088 if((!IsChild(pDragButton)) && (GetChild(GUI_ScrollBar_ButtonThumb) == NULL)) 00089 { 00090 pDragButton->SetType(GUI_ScrollBar_ButtonThumb); 00091 pDragButton->SetParent(this); 00092 00093 pDragButton->RemoveChild(pDragButton->GetChild(GUI_Border)); 00094 if(GetType() == GUI_HorizontalScrollBar) 00095 pDragButton->SetFlag(GUI_Flag_RestrictMovementY, true); 00096 else if(GetType() == GUI_VerticalScrollBar) 00097 pDragButton->SetFlag(GUI_Flag_RestrictMovementX, true); 00098 00099 push_back(pDragButton); 00100 } 00101 } 00102 00103 SetRange(m_iMinPos, m_iMaxPos); 00104 SetCurPos(m_iCurPos); 00105 00106 return Resize(WidgetRect); 00107 } 00108 00109 int CGUIScrollBar::LoadXML(TiXmlNode *pDataNode, CString strFilename) 00110 { 00111 TiXmlElement* pXMLElement = NULL; 00112 const char *pcValue = NULL; 00113 00114 int iRetValue = CGUIStatic::LoadXML(pDataNode, strFilename); // LoadXML the data of derivative 00115 if(iRetValue !=1) 00116 return iRetValue; 00117 00118 pXMLElement = m_pXMLElement; 00119 00120 pcValue = pXMLElement->Attribute("Type"); 00121 if(pcValue !=NULL) 00122 { 00123 if(stricmp(pcValue, "horizontal") == 0) 00124 SetType(GUI_HorizontalScrollBar); 00125 else if(stricmp(pcValue, "vertical") == 0) 00126 SetType(GUI_VerticalScrollBar); 00127 else 00128 { 00129 CGlobalLogger::GetSingleton().Write("\n\t\t!!!!!!!!!!General Error!!!!!!!!!!!!\n"); 00130 CGlobalLogger::GetSingleton().Write("ScrollBar (Name: %s, ID: %d) doesn't have Type (Type) Attribute specified.", GetName(), GetID()); 00131 } 00132 pcValue = NULL; 00133 } 00134 00135 pcValue = pXMLElement->Attribute("MinMaxCur"); 00136 if(pcValue !=NULL) 00137 { 00138 sscanf(pcValue, "%d,%d,%d", &m_iMinPos, &m_iMaxPos, &m_iCurPos); 00139 pcValue = NULL; 00140 } 00141 00142 pcValue = pXMLElement->Attribute("uiMin"); 00143 if(pcValue !=NULL) 00144 { 00145 m_iMinPos = atoi(pcValue); 00146 pcValue = NULL; 00147 } 00148 00149 pcValue = pXMLElement->Attribute("uiMax"); 00150 if(pcValue !=NULL) 00151 { 00152 m_iMaxPos = atoi(pcValue); 00153 pcValue = NULL; 00154 } 00155 00156 pcValue = pXMLElement->Attribute("Cur"); 00157 if(pcValue !=NULL) 00158 { 00159 m_iCurPos = atoi(pcValue); 00160 m_fCurPos = (float)m_iCurPos; 00161 pcValue = NULL; 00162 } 00163 00164 pcValue = pXMLElement->Attribute("Inc"); 00165 if(pcValue !=NULL) 00166 { 00167 m_iBtnInc = atoi(pcValue); 00168 pcValue = NULL; 00169 } 00170 00171 // START PARSING ELEMENT PROPERTIES 00172 UINT Count = 0; 00173 for (int i=0; i<GetChildCount(); i++) 00174 { 00175 CGUIElement *pElement = GetChild(i); 00176 if(pElement->GetType() == GUI_Button) 00177 { 00178 pElement->SetType(eGUIControlType(GUI_ScrollBar_ButtonInc + Count)); 00179 Count++; 00180 00181 if(Count == 3) 00182 break; 00183 } 00184 } 00185 00186 CGUIButton *pDragButton = (CGUIButton *)GetChild(GUI_ScrollBar_ButtonThumb); 00187 CGUIButton *pIncButton = (CGUIButton *)GetChild(GUI_ScrollBar_ButtonInc); 00188 CGUIButton *pDecButton = (CGUIButton *)GetChild(GUI_ScrollBar_ButtonDec); 00189 00190 return Create(GetParent(), GetRect(), GetType(), pIncButton, pDecButton, pDragButton); 00191 } 00192 00193 int CGUIScrollBar::SaveXML(TiXmlNode *pDataNode, CString strFilename) 00194 { 00195 // Nowhere to save! Nothing to save! 00196 if(pDataNode == NULL && strFilename == "") 00197 return -1; 00198 00199 char Buf[128] = ""; 00200 TiXmlElement *pSaveXMLElement = NULL; 00201 00202 if(pDataNode != NULL) 00203 pSaveXMLElement = pDataNode->ToElement(); 00204 else 00205 pSaveXMLElement = new TiXmlElement("ScrollBar"); 00206 00207 if(GetType() == GUI_HorizontalScrollBar) 00208 pSaveXMLElement->SetAttribute("Type", "Horizontal"); 00209 else if(GetType() == GUI_VerticalScrollBar) 00210 pSaveXMLElement->SetAttribute("Type", "Vertical"); 00211 00212 pSaveXMLElement->SetAttribute("uiMin", m_iMinPos); 00213 pSaveXMLElement->SetAttribute("Cur", m_iCurPos); 00214 pSaveXMLElement->SetAttribute("uiMax", m_iMaxPos); 00215 pSaveXMLElement->SetAttribute("Inc", m_iBtnInc); 00216 00217 int iRetValue = CGUIStatic::SaveXML(pSaveXMLElement, strFilename); 00218 00219 if(pDataNode == NULL && strFilename !="") 00220 { 00221 TiXmlDocument SaveDoc; 00222 SaveDoc.InsertEndChild(*pSaveXMLElement); 00223 int iRetValue = SaveDoc.SaveFile(strFilename); 00224 SAFEDEL(pSaveXMLElement) 00225 00226 return iRetValue; 00227 } 00228 00229 return iRetValue; 00230 } 00231 00232 void CGUIScrollBar::Draw() 00233 { 00234 CString pcValue; 00235 pcValue.Format("%d", m_iCurPos); 00236 SetText(pcValue); 00237 00238 CGUIStatic::Draw(); 00239 } 00240 00241 int CGUIScrollBar::Resize(tRect& NewRect) 00242 { 00243 CGUIElement *pDragButton = GetChild(GUI_ScrollBar_ButtonThumb); 00244 CGUIElement *pIncButton = GetChild(GUI_ScrollBar_ButtonInc); 00245 CGUIElement *pDecButton = GetChild(GUI_ScrollBar_ButtonDec); 00246 00247 if(pDragButton !=NULL) 00248 pDragButton->RemoveChild(pDragButton->GetChild(GUI_Border)); 00249 00250 if(pIncButton !=NULL) 00251 pIncButton->RemoveChild(pIncButton->GetChild(GUI_Border)); 00252 00253 if(pDecButton !=NULL) 00254 pDecButton->RemoveChild(pDecButton->GetChild(GUI_Border)); 00255 00256 tRect CurrentRect = GetRect(); 00257 // All went OK, we can resize to this rectangle 00258 int iRetValue = CGUIStatic::Resize(NewRect); 00259 00260 int IncrementSize = 0, DecrementSize = 0; 00261 00262 // Set increment/decrement buttons rectangles (if any) 00263 if(GetType() == GUI_HorizontalScrollBar) 00264 { 00265 tRect DecButtonRect, IncButtonRect; 00266 00267 if(pDecButton !=NULL) 00268 { 00269 DecButtonRect.left = GetRect().left; 00270 DecButtonRect.right = GetRect().left + GetHeight(); 00271 DecButtonRect.top = GetRect().top; 00272 DecButtonRect.bottom = GetRect().bottom; 00273 00274 pDecButton->SetRect(DecButtonRect); 00275 } 00276 00277 if(pIncButton !=NULL) 00278 { 00279 IncButtonRect.right = GetRect().right; 00280 IncButtonRect.left = GetRect().right - GetHeight(); 00281 IncButtonRect.top = GetRect().top; 00282 IncButtonRect.bottom = GetRect().bottom; 00283 00284 pIncButton->SetRect(IncButtonRect); 00285 } 00286 } 00287 else if(GetType() == GUI_VerticalScrollBar) 00288 { 00289 tRect DecButtonRect, IncButtonRect; 00290 00291 if(pDecButton !=NULL) 00292 { 00293 DecButtonRect.left = GetRect().left; 00294 DecButtonRect.right = GetRect().right; 00295 DecButtonRect.top = GetRect().top; 00296 DecButtonRect.bottom = GetRect().top - GetWidth(); 00297 00298 pDecButton->SetRect(DecButtonRect); 00299 } 00300 00301 if(pIncButton !=NULL) 00302 { 00303 IncButtonRect.left = GetRect().left; 00304 IncButtonRect.right = GetRect().right; 00305 IncButtonRect.bottom = GetRect().bottom; 00306 IncButtonRect.top = IncButtonRect.bottom + GetWidth(); 00307 00308 pIncButton->SetRect(IncButtonRect); 00309 } 00310 } 00311 00312 UINT TotalWidth = 0, TotalHeight = 0; 00313 00314 if(GetType() == GUI_HorizontalScrollBar) 00315 { 00316 if(pDragButton !=NULL) 00317 TotalWidth+= pDragButton->GetWidth(); 00318 if(pIncButton !=NULL) 00319 TotalWidth+= pIncButton->GetWidth(); 00320 if(pDecButton !=NULL) 00321 TotalWidth+= pDecButton->GetWidth(); 00322 00323 if(NewRect.right - NewRect.left < TotalWidth) 00324 { 00325 CGUIStatic::Resize(CurrentRect); 00326 return 0; 00327 } 00328 } 00329 else if(GetType() == GUI_VerticalScrollBar) 00330 { 00331 if(pDragButton !=NULL) 00332 TotalHeight+= pDragButton->GetHeight(); 00333 if(pIncButton !=NULL) 00334 TotalHeight+= pIncButton->GetHeight(); 00335 if(pDecButton !=NULL) 00336 TotalHeight+= pDecButton->GetHeight(); 00337 00338 if((NewRect.top - NewRect.bottom) < TotalHeight) 00339 { 00340 CGUIStatic::Resize(CurrentRect); 00341 return 0; 00342 } 00343 } 00344 00345 SetCurPos(m_fCurPos); 00346 return iRetValue; 00347 } 00348 00349 void CGUIScrollBar::SetRange(UINT uiMin, UINT uiMax) 00350 { 00351 m_iMinPos = uiMin; 00352 m_iMaxPos = uiMax; 00353 if(GetType() == GUI_HorizontalScrollBar) // If this is a horizontal scroll bar 00354 m_fPixelPerValue = float(GetWidth()) / (m_iMaxPos - m_iMinPos); 00355 else if(GetType() == GUI_VerticalScrollBar) // If its a vertical scroll bar 00356 m_fPixelPerValue = float(GetHeight()) / (m_iMaxPos - m_iMinPos); 00357 00358 Resize(GetRect()); 00359 } 00360 00361 void CGUIScrollBar::SetCurPos(float fPos) 00362 { 00363 if(fPos >= m_iMaxPos) 00364 { 00365 m_fCurPos = m_iMaxPos; 00366 m_iCurPos = (int)m_iMaxPos; 00367 } 00368 else if(fPos <= m_iMinPos) 00369 { 00370 m_fCurPos = m_iMinPos; 00371 m_iCurPos = (int)m_iMinPos; 00372 } 00373 if(fPos > m_iMinPos && fPos < m_iMaxPos) 00374 { 00375 m_fCurPos = fPos; 00376 m_iCurPos = (int)fPos; 00377 } 00378 00379 CGUIElement *pDragButton = GetChild(GUI_ScrollBar_ButtonThumb); 00380 CGUIElement *pIncButton = GetChild(GUI_ScrollBar_ButtonInc); 00381 CGUIElement *pDecButton = GetChild(GUI_ScrollBar_ButtonDec); 00382 00383 // Set drag button rectangle 00384 if(GetType() == GUI_HorizontalScrollBar) 00385 { 00386 if(pDragButton == NULL) 00387 return; 00388 00389 UINT AdditionalWidth = 0; 00390 00391 if(pDecButton !=NULL) 00392 AdditionalWidth+= GetHeight(); 00393 00394 if(pIncButton !=NULL) 00395 AdditionalWidth+= GetHeight(); 00396 00397 UINT TotalWidth = GetWidth() - AdditionalWidth - GetHeight(); 00398 m_fPixelPerValue = (float)TotalWidth / ((float)m_iMaxPos - (float)m_iMinPos); 00399 00400 UINT ScrollWidth = GetHeight(); 00401 00402 UINT LeftExtend = GetRect().left; 00403 if(pDecButton !=NULL) 00404 LeftExtend = pDecButton->GetRect().right; 00405 00406 tRect ThumbBtnRect; 00407 00408 ThumbBtnRect.left = LeftExtend + m_fCurPos * m_fPixelPerValue; 00409 ThumbBtnRect.right = LeftExtend + ScrollWidth + m_fCurPos * m_fPixelPerValue; 00410 ThumbBtnRect.top = GetRect().top; 00411 ThumbBtnRect.bottom = GetRect().bottom; 00412 00413 pDragButton->SetRect(ThumbBtnRect); 00414 00415 } 00416 if(GetType() == GUI_VerticalScrollBar) 00417 { 00418 if(pDragButton == NULL) 00419 return; 00420 00421 UINT AdditionalHeight = 0; 00422 00423 if(pDecButton !=NULL) 00424 AdditionalHeight+= GetWidth(); 00425 00426 if(pIncButton !=NULL) 00427 AdditionalHeight+= GetWidth(); 00428 00429 UINT TotalHeight = GetHeight() - AdditionalHeight - GetWidth(); 00430 m_fPixelPerValue = (float)TotalHeight / ((float)m_iMaxPos - (float)m_iMinPos); 00431 00432 UINT ScrollHeight = GetWidth(); 00433 00434 UINT TopExtend = GetRect().top; 00435 if(pDecButton !=NULL) 00436 TopExtend = pDecButton->GetRect().bottom; 00437 00438 tRect ThumbBtnRect; 00439 00440 ThumbBtnRect.top = TopExtend - m_fCurPos * m_fPixelPerValue; 00441 ThumbBtnRect.bottom = TopExtend - ScrollHeight - m_fCurPos * m_fPixelPerValue; 00442 ThumbBtnRect.left = GetRect().left; 00443 ThumbBtnRect.right = GetRect().right; 00444 00445 pDragButton->SetRect(ThumbBtnRect); 00446 00447 } 00448 00449 GetGUI()->PostMessage(this, NULL, GUI_Message_Scrolled, m_fCurPos, m_iPrevPos); 00450 } 00451 00452 UINT CGUIScrollBar::GetCurPos() 00453 { 00454 return m_iCurPos; 00455 } 00456 00457 void CGUIScrollBar::ProcessMessage(tGUIMessage& Message) 00458 { 00459 switch(Message.uiMessage) 00460 { 00461 case GUI_Message_MoveX: 00462 case GUI_Message_MoveY: 00463 case GUI_Message_MoveXY: 00464 { 00465 if(Message.pSender->GetType() == GUI_ScrollBar_ButtonThumb && IsChild(Message.pSender)) 00466 { 00467 float ValuePerPixel = 1.0f / m_fPixelPerValue; 00468 int Displacement = 0; 00469 if(GetType() == GUI_HorizontalScrollBar) 00470 Displacement = CInputEngine::GetSingleton().GetMouse()->GetCursorPos().x - CInputEngine::GetSingleton().GetMouse()->GetPrevCursorPos().x; 00471 else if(GetType() == GUI_VerticalScrollBar) 00472 Displacement = -(CInputEngine::GetSingleton().GetMouse()->GetCursorPos().y - CInputEngine::GetSingleton().GetMouse()->GetPrevCursorPos().y); 00473 00474 float ValueChange = Displacement * ValuePerPixel; 00475 00476 if(m_fCurPos + ValueChange <= m_iMinPos) 00477 SetCurPos(m_iMinPos); 00478 else if(m_fCurPos + ValueChange >= m_iMaxPos) 00479 SetCurPos(m_iMaxPos); 00480 else 00481 SetCurPos(m_fCurPos + ValueChange); 00482 } 00483 else if(Message.pSender == this) 00484 Resize(GetRect()); 00485 00486 break; 00487 } 00488 00489 case GUI_Message_ButtonPressed: 00490 { 00491 if(Message.pSender->GetType() == GUI_ScrollBar_ButtonInc && IsChild(Message.pSender)) 00492 SetCurPos(m_fCurPos + (float)m_iBtnInc); 00493 else if(Message.pSender->GetType() == GUI_ScrollBar_ButtonDec && IsChild(Message.pSender)) 00494 SetCurPos(m_fCurPos - (float)m_iBtnInc); 00495 } 00496 } 00497 } 00498 00499 int CGUIScrollBar::OnLMouseUp(UINT x, UINT y) 00500 { 00501 int iRetValue = CGUIStatic::OnLMouseUp(x, y); 00502 if(iRetValue == 1) 00503 { 00504 CGUIElement *pDragButton = GetChild(GUI_ScrollBar_ButtonThumb); 00505 CGUIElement *pIncButton = GetChild(GUI_ScrollBar_ButtonInc); 00506 CGUIElement *pDecButton = GetChild(GUI_ScrollBar_ButtonDec); 00507 00508 if(PointInRect(pDragButton->GetRect(), x, y) || 00509 PointInRect(pIncButton->GetRect(), x, y) || 00510 PointInRect(pDecButton->GetRect(), x, y)) 00511 return 1; 00512 00513 if(pDragButton !=NULL) 00514 { 00515 if(GetType() == GUI_HorizontalScrollBar) 00516 { 00517 if(x > pDragButton->GetRect().right) 00518 { 00519 m_iCurPos+= m_iScrlInc; 00520 SetCurPos((float)m_iCurPos); 00521 } 00522 else if(x < pDragButton->GetRect().left) 00523 { 00524 m_iCurPos-= m_iScrlInc; 00525 SetCurPos((float)m_iCurPos); 00526 } 00527 } 00528 else if(GetType() == GUI_VerticalScrollBar) 00529 { 00530 if(y > pDragButton->GetRect().top) 00531 { 00532 m_iCurPos-= m_iScrlInc; 00533 SetCurPos((float)m_iCurPos); 00534 } 00535 else if(y < pDragButton->GetRect().bottom) 00536 { 00537 m_iCurPos+= m_iScrlInc; 00538 SetCurPos((float)m_iCurPos); 00539 } 00540 } 00541 } 00542 } 00543 00544 return iRetValue; 00545 } 00546 00547 tVERTEX2d& CGUIScrollBar::GetRange() 00548 { 00549 return tVERTEX2d(m_iMinPos, m_iMaxPos); 00550 } 00551 00552 bool CGUIScrollBar::IsOfType(eEntityType eType) 00553 { 00554 if(eType == Entity_GUIScrollBar) 00555 return true; 00556 00557 return CGUIStatic::IsOfType(eType); 00558 } 00559 00560 bool CGUIScrollBar::PropertyChanging( const void* pvProperty , void* pvNewValue ) 00561 { 00562 bool bChangeOK = CGUIStatic::PropertyChanging(pvProperty, pvNewValue); 00563 00564 if(pvProperty == &m_iMinPos) 00565 { 00566 m_iMinPos = *(int *)pvNewValue; 00567 SetRange(m_iMinPos, m_iMaxPos); 00568 } 00569 00570 if(pvProperty == &m_iMaxPos) 00571 { 00572 m_iMaxPos = *(int *)pvNewValue; 00573 SetRange(m_iMinPos, m_iMaxPos); 00574 } 00575 00576 if(pvProperty == &m_iCurPos) 00577 { 00578 m_iCurPos = *(int *)pvNewValue; 00579 m_fCurPos = (float)m_iCurPos; 00580 00581 SetCurPos(m_fCurPos); 00582 } 00583 00584 return bChangeOK; 00585 } 00586 00587 void CGUIScrollBar::GetProperties( EPropList& PropList ) 00588 { 00589 CGUIStatic::GetProperties(PropList); 00590 00591 PropList.AddTab("ScrollBar"); 00592 PropList.AddPropInt(this, "Minimum", &m_iMinPos, "", true)->SetComment("Minimum scroll value"); 00593 PropList.AddPropInt(this, "Maximum", &m_iMaxPos, "", true)->SetComment("Maximum scroll value"); 00594 PropList.AddPropInt(this, "Current", &m_iCurPos, "", true)->SetComment("Current scroll value"); 00595 PropList.AddPropInt(this, "Increment Val.", &m_iBtnInc, "", true)->SetComment("Increment value when button(s) are clicked"); 00596 PropList.AddPropInt(this, "Scroll Val.", &m_iScrlInc, "", true)->SetComment("Increment value when scrollbar is clicked"); 00597 }

Generated on Sun Jul 17 21:34:27 2005 for OpenGL GUI by doxygen 1.3.8