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

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

00001 #include "StdAfx.h" 00002 #include "GUI.h" 00003 00004 CGUIStatic::CGUIStatic() 00005 { 00006 SetEntityType(Entity_GUIStatic); 00007 SetType(GUI_Static); 00008 Initialize(); 00009 } 00010 00011 CGUIStatic::~CGUIStatic() 00012 { 00013 Destroy(); 00014 } 00015 00016 void CGUIStatic::Initialize() 00017 { 00018 m_iHorizontalAlign = 0; 00019 m_iVerticalAlign = 0; 00020 m_strText = ""; 00021 m_TextPos = tVERTEX2d(0, 0); 00022 m_strFontName = ""; 00023 m_strFontFilename = ""; 00024 m_iFontIndex = 0; 00025 m_pFont = NULL; 00026 m_bTextVisible = true; 00027 00028 m_strElementType = "Static"; 00029 } 00030 00031 int CGUIStatic::Create(CGUIElement *pParent, tRect WidgetRect, CCustomFont *pFont, CTexture *pTexture, CMaterial *pMaterial, bool bBorder) 00032 { 00033 if(!CGUIElement::Create(pParent, WidgetRect, pTexture, pMaterial, bBorder)) 00034 return 0; 00035 00036 if(pFont !=NULL && m_pFont == NULL) 00037 m_pFont = pFont; 00038 00039 return 1; 00040 } 00041 00042 int CGUIStatic::LoadXML(TiXmlNode *pDataNode, CString strFilename) 00043 { 00044 TiXmlElement* pXMLElement = NULL; 00045 const char *pcValue = NULL; 00046 00047 int iRetValue = CGUIElement::LoadXML(pDataNode, strFilename); // LoadXML the data of derivative 00048 if(iRetValue !=1) 00049 return iRetValue; 00050 00051 pXMLElement = m_pXMLElement; 00052 00053 pcValue = pXMLElement->Attribute("Text"); 00054 if(pcValue !=NULL) 00055 { 00056 m_strText = CString(pcValue); 00057 pcValue = NULL; 00058 } 00059 00060 pcValue = pXMLElement->Attribute("VAlign"); 00061 if(pcValue !=NULL) 00062 { 00063 if(stricmp(pcValue, "top") == 0) 00064 m_iVerticalAlign = Vertical_Align_Top; 00065 else if(stricmp(pcValue, "center") == 0) 00066 m_iVerticalAlign = Vertical_Align_Center; 00067 else if(stricmp(pcValue, "bottom") == 0) 00068 m_iVerticalAlign = Vertical_Align_Bottom; 00069 00070 pcValue = NULL; 00071 } 00072 else 00073 { 00074 CGlobalLogger::GetSingleton().Write("\n\t\t**********General Warning**********\n"); 00075 CGlobalLogger::GetSingleton().Write("Static Control (Name: %s, ID: %d) doesn't have Vertical Alignment (VAlign) Attribute specified. Assuming default...", GetName(), GetID()); 00076 } 00077 00078 pcValue = pXMLElement->Attribute("HAlign"); 00079 if(pcValue !=NULL) 00080 { 00081 if(stricmp(pcValue, "left") == 0) 00082 m_iHorizontalAlign = Horizontal_Align_Left; 00083 else if(stricmp(pcValue, "center") == 0) 00084 m_iHorizontalAlign = Horizontal_Align_Center; 00085 else if(stricmp(pcValue, "right") == 0) 00086 m_iHorizontalAlign = Horizontal_Align_Right; 00087 00088 pcValue = NULL; 00089 } 00090 else 00091 { 00092 CGlobalLogger::GetSingleton().Write("\n\t\t**********General Warning**********\n"); 00093 CGlobalLogger::GetSingleton().Write("Static Control (Name: %s, ID: %d) doesn't have Horizontal Alignment (HAlign) Attribute specified. Assuming default...", GetName(), GetID()); 00094 } 00095 00096 CCustomFont *pNewFont = CFontManager::GetSingleton().LoadResource(pXMLElement, ""); 00097 if(pNewFont !=NULL) 00098 m_pFont = pNewFont; 00099 else 00100 { 00101 CGlobalLogger::GetSingleton().Write("\t**********General Warning\t**********\n"); 00102 CGlobalLogger::GetSingleton().Write("Static Control (Name: %s, ID: %d) doesn't have Font Attribute (FontByName, FontByID) specified. Assuming default...", GetName(), GetID()); 00103 } 00104 00105 return 1; // All went OK, return 1 00106 } 00107 00108 int CGUIStatic::SaveXML(TiXmlNode *pDataNode, CString strFilename) 00109 { 00110 // Nowhere to save! Nothing to save! 00111 if(pDataNode == NULL && strFilename == "") 00112 return -1; 00113 00114 char Buf[128] = ""; 00115 TiXmlElement *pSaveXMLElement = NULL; 00116 00117 if(pDataNode != NULL) 00118 pSaveXMLElement = pDataNode->ToElement(); 00119 else 00120 pSaveXMLElement = new TiXmlElement("Static"); 00121 00122 if(m_pFont !=NULL) 00123 pSaveXMLElement->SetAttribute("FontByName", m_pFont->GetName()); 00124 00125 if(m_strText !="") 00126 pSaveXMLElement->SetAttribute("Text", m_strText); 00127 00128 if(m_iHorizontalAlign == Horizontal_Align_Left) 00129 pSaveXMLElement->SetAttribute("HAlign", "Left"); 00130 else if(m_iHorizontalAlign == Horizontal_Align_Center) 00131 pSaveXMLElement->SetAttribute("HAlign", "Center"); 00132 else if(m_iHorizontalAlign == Horizontal_Align_Right) 00133 pSaveXMLElement->SetAttribute("HAlign", "Right"); 00134 00135 if(m_iVerticalAlign == Vertical_Align_Top) 00136 pSaveXMLElement->SetAttribute("VAlign", "Top"); 00137 else if(m_iVerticalAlign == Horizontal_Align_Center) 00138 pSaveXMLElement->SetAttribute("VAlign", "Center"); 00139 else if(m_iVerticalAlign == Vertical_Align_Bottom) 00140 pSaveXMLElement->SetAttribute("VAlign", "Bottom"); 00141 00142 int iRetValue = CGUIElement::SaveXML(pSaveXMLElement, strFilename); 00143 00144 if(pDataNode == NULL && strFilename !="") 00145 { 00146 TiXmlDocument SaveDoc; 00147 SaveDoc.InsertEndChild(*pSaveXMLElement); 00148 int iRetValue = SaveDoc.SaveFile(strFilename); 00149 SAFEDEL(pSaveXMLElement) 00150 00151 return iRetValue; 00152 } 00153 00154 return iRetValue; 00155 } 00156 00157 int CGUIStatic::Resize(tRect& NewRect) 00158 { 00159 int iRetValue = CGUIElement::Resize(NewRect); 00160 00161 return iRetValue; 00162 } 00163 00164 void CGUIStatic::Draw() 00165 { 00166 CGUIElement::Draw(); 00167 00168 if(m_bTextVisible) 00169 DrawText(); 00170 } 00171 00172 void CGUIStatic::DrawText() 00173 { 00174 if(m_strText == "" || m_pFont == NULL) 00175 return; 00176 00177 if(m_pFont->GetFontType() == Bitmap_Font) 00178 { 00179 CBitmapFont *pBitmapFont = (CBitmapFont *)m_pFont; 00180 00181 for (int i=0; i<GetLineCount(); i++) 00182 { 00183 CString Line = ""; 00184 00185 UINT TextWidth = GetLineLength(i) * pBitmapFont->GetQuadLength(); 00186 00187 if(TextWidth >=GetWidth()) 00188 Line = CString(GetLine(i, GetWidth() / pBitmapFont->GetQuadLength())); 00189 else 00190 Line = GetLine(i, GetLineLength(i)); 00191 00192 // This part is the most important one: 00193 // Here we are aquiring alignment units (x & y) 00194 int AlignXPixels = 0, AlignYPixels = 0; 00195 00196 int MaxWidthCharacters = (GetWidth() / pBitmapFont->GetQuadLength()) + 1; 00197 int MaxHeightCharacters = (GetHeight() / pBitmapFont->GetQuadHeight()) + 1; 00198 00199 // If horizontal alignment is 'left'(0), there is no displacement for text then (alignx = 0) 00200 if(m_iHorizontalAlign == Horizontal_Align_Left) 00201 AlignXPixels = 0; 00202 00203 // If horizontal alignment is for center, then we calculate the displacement as: 00204 // alignx = (Total Control Width - (Number Of Chars In Line * Character Width) / 2) 00205 // because we want text to be in horizontal center of the control 00206 if(m_iHorizontalAlign == Horizontal_Align_Center) 00207 AlignXPixels = (GetWidth()- (Line.GetLength() * pBitmapFont->GetQuadLength())) / 2; 00208 00209 // If horizontal alignment is right, the displacement is: 00210 // alignx = Total Control Width - (Number Of Chars * Character Width) 00211 // because we want text to be on the right - e.g rightmost character will be right near 00212 // right side of control 00213 if(m_iHorizontalAlign == Horizontal_Align_Right) 00214 AlignXPixels = GetWidth() - (Line.GetLength() * pBitmapFont->GetQuadLength()); 00215 00216 // If the vertical alignment is top(0), we do not displace text down at all 00217 if(m_iVerticalAlign == Vertical_Align_Top) 00218 AlignYPixels = 0; 00219 00220 // If the vertical alignment is middle(1), we displace each Line by: 00221 // aligny = (Total Number of Lines - Line Count) / 2 * Character Height 00222 // If the total lines visible is 1, or there's only 1 Line, the align is in pixels, not lines 00223 if(m_iVerticalAlign == Vertical_Align_Center) 00224 { 00225 UINT LineCount = GetLineCount(); 00226 UINT FontHeight = pBitmapFont->GetQuadHeight(); 00227 UINT TextHeight = LineCount * FontHeight; 00228 if(TextHeight >=GetHeight()) // If there is more lines of text that total height of static, do not align 00229 break; 00230 else 00231 AlignYPixels = (GetHeight() - TextHeight) / 2; 00232 00233 // A one Line text, align in pixels! 00234 //if((GetHeight() > pBitmapFont->GetQuadHeight()) && (GetHeight() <= 2*pBitmapFont->GetQuadHeight())) 00235 //AlignYPixels = (GetHeight() - pBitmapFont->GetQuadHeight()) / 2; 00236 //else // More than one Line, align in lines! 00237 // AlignYPixels = ((MaxHeightCharacters - GetLineCount()) / 2) * pBitmapFont->GetQuadHeight(); 00238 } 00239 00240 // If the vertical alignment is bottom(2), we displace each Line by: 00241 // aligny = (Total Number of Lines - Line Count) * Character Height 00242 if(m_iVerticalAlign == Vertical_Align_Bottom) 00243 AlignYPixels = (MaxHeightCharacters - GetLineCount()) * pBitmapFont->GetQuadHeight(); 00244 00245 // Now we align each Line by assigning text position 00246 m_TextPos.x = GetRect().left + AlignXPixels; 00247 m_TextPos.y = GetRect().top - (i+1)*pBitmapFont->GetQuadHeight() - AlignYPixels; 00248 00249 // Ensure that we don't print text that's outside control (top & bottom side check) 00250 if((m_TextPos.y) >= GetRect().bottom && (m_TextPos.y + pBitmapFont->GetQuadHeight()) <= GetRect().top) 00251 { 00252 pBitmapFont->SetTextPos(m_TextPos.x, m_TextPos.y); 00253 pBitmapFont->Draw(Line.GetBuffer()); 00254 } 00255 } 00256 } 00257 } 00258 00259 void CGUIStatic::SetText(CString& strText) 00260 { 00261 m_strText = strText; 00262 } 00263 00264 00265 CString& CGUIStatic::GetText() 00266 { 00267 return m_strText; 00268 } 00269 00270 UINT CGUIStatic::GetLineLength(UINT uiIndex) 00271 { 00272 UINT uiLineNumber = 0; 00273 UINT uiCharacterCount = 0; 00274 00275 for (UINT i=0; i<m_strText.GetLength(); i++) 00276 { 00277 if(m_strText.GetAt(i) == '\n') 00278 uiLineNumber++; 00279 00280 if(uiIndex == uiLineNumber) 00281 { 00282 if(m_strText.GetAt(i) !='\n') 00283 uiCharacterCount++; 00284 } 00285 00286 if(uiCharacterCount > 0 && m_strText.GetAt(i) == '\n') 00287 return uiCharacterCount; 00288 } 00289 00290 return uiCharacterCount; 00291 } 00292 00293 const CCustomFont *CGUIStatic::GetFont() 00294 { 00295 return m_pFont; 00296 } 00297 00298 void CGUIStatic::SetFont(CBitmapFont *pFont) 00299 { 00300 m_pFont = pFont; 00301 } 00302 00303 UINT CGUIStatic::GetTextLength() 00304 { 00305 return m_strText.GetLength(); 00306 } 00307 00308 UINT CGUIStatic::GetLineCount() 00309 { 00310 UINT LineCount = 0; 00311 for (UINT i=0; i<m_strText.GetLength(); i++) 00312 { 00313 if(m_strText.GetAt(i) == '\n') 00314 LineCount++; 00315 } 00316 00317 return LineCount + 1; 00318 } 00319 00320 CString CGUIStatic::GetLine(UINT uiLine, UINT uiCharacterCount) 00321 { 00322 UINT uiLineIndex = 0; 00323 UINT LineLength = GetLineLength(uiLine); 00324 00325 UINT CharPtr = 0; 00326 char buf[256] = ""; 00327 for (UINT i=0; i<m_strText.GetLength(); i++) 00328 { 00329 if(m_strText.GetAt(i) == '\n') 00330 uiLineIndex++; 00331 00332 if(uiLineIndex == uiLine) 00333 { 00334 CharPtr = i; 00335 break; 00336 } 00337 } 00338 00339 if(uiLineIndex !=0) 00340 CharPtr+=uiLineIndex; 00341 00342 UINT Count = 0; 00343 for (UINT j=CharPtr; j<CharPtr + uiCharacterCount; j++) 00344 { 00345 buf[Count] = m_strText.GetAt(j); 00346 Count++; 00347 } 00348 00349 return CString(buf); 00350 } 00351 00352 void CGUIStatic::ShowText() 00353 { 00354 m_bTextVisible = true; 00355 } 00356 00357 void CGUIStatic::HideText() 00358 { 00359 m_bTextVisible = false; 00360 } 00361 00362 bool CGUIStatic::IsOfType(eEntityType eType) 00363 { 00364 if(eType == Entity_GUIStatic) 00365 return true; 00366 00367 return CGUIElement::IsOfType(eType); 00368 } 00369 00370 bool CGUIStatic::PropertyChanging( const void* pvProperty , void* pvNewValue ) 00371 { 00372 bool bChangeOK = CGUIElement::PropertyChanging(pvProperty, pvNewValue); 00373 00374 if(pvProperty == &m_iFontIndex) 00375 { 00376 if(m_iFontIndex !=1000) 00377 { 00378 m_iFontIndex = *(int *)pvNewValue; 00379 m_pFont = CFontManager::GetSingleton().GetResource(m_iFontIndex); 00380 } 00381 } 00382 00383 return bChangeOK; 00384 } 00385 00386 void CGUIStatic::GetProperties( EPropList& PropList ) 00387 { 00388 CGUIElement::GetProperties(PropList); 00389 00390 PropList.AddTab("Static/Textbox"); 00391 PropList.AddPropSeparator(this, "Font"); 00392 EPropertyCombo *pFontCombo = (EPropertyCombo *)PropList.AddPropCombo(this, "From Database...", &m_iFontIndex)->SetComment("Font selection"); 00393 for (int i=0; i<CFontManager::GetSingleton().size(); i++) 00394 { 00395 CCustomFont *pFont = (CCustomFont *)CFontManager::GetSingleton().at(i); 00396 pFontCombo->AddString(pFont->GetName(), i); 00397 } 00398 if(m_iFontIndex > 0) 00399 { 00400 PropList.AddPropString(this, "strFilename", &m_strFontFilename, false)->SetComment("Font' strFilename"); 00401 PropList.AddPropString(this, "strName", &m_strFontName, false)->SetComment("Font' name"); 00402 00403 } 00404 else 00405 { 00406 PropList.AddPropFile(this, "strFilename", &m_strFontFilename, "XML Files (*.xml)|*.xml|All Files (*.*)|*.*||")->SetComment("Font' strFilename"); 00407 PropList.AddPropString(this, "strName", &m_strFontName, true)->SetComment("Font' name"); 00408 } 00409 00410 EPropertyCombo *pHAlignCombo = PropList.AddPropCombo(this, "H. Align", &m_iHorizontalAlign); 00411 pHAlignCombo->AddString("Left"); 00412 pHAlignCombo->AddString("Center"); 00413 pHAlignCombo->AddString("Right"); 00414 00415 EPropertyCombo *pVAlignCombo = PropList.AddPropCombo(this, "V. Align", &m_iVerticalAlign); 00416 pVAlignCombo->AddString("Top"); 00417 pVAlignCombo->AddString("Middle"); 00418 pVAlignCombo->AddString("bottom"); 00419 00420 PropList.AddPropCheck(this, "Show Text", &m_bTextVisible); 00421 PropList.AddPropTextblock(this, "Text", &m_strText, true); 00422 } 00423 00424 void CGUIStatic::Destroy() 00425 { 00426 CGUIElement::Destroy(); 00427 }

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