Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CEGUIFalTextComponent.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002     filename:   CEGUIFalTextComponent.cpp
00003     created:    Sun Jun 19 2005
00004     author:     Paul D Turner <paul@cegui.org.uk>
00005 *************************************************************************/
00006 /*************************************************************************
00007     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00008     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00009  
00010     This library is free software; you can redistribute it and/or
00011     modify it under the terms of the GNU Lesser General Public
00012     License as published by the Free Software Foundation; either
00013     version 2.1 of the License, or (at your option) any later version.
00014  
00015     This library is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018     Lesser General Public License for more details.
00019  
00020     You should have received a copy of the GNU Lesser General Public
00021     License along with this library; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 *************************************************************************/
00024 #include "falagard/CEGUIFalTextComponent.h"
00025 #include "falagard/CEGUIFalXMLEnumHelper.h"
00026 #include "CEGUIFontManager.h"
00027 #include "CEGUIExceptions.h"
00028 #include "CEGUIPropertyHelper.h"
00029 #include <iostream>
00030 
00031 // Start of CEGUI namespace section
00032 namespace CEGUI
00033 {
00034     TextComponent::TextComponent() :
00035         d_vertFormatting(VTF_TOP_ALIGNED),
00036         d_horzFormatting(HTF_LEFT_ALIGNED)
00037     {}
00038 
00039     const String& TextComponent::getText() const
00040     {
00041         return d_text;
00042     }
00043 
00044     void TextComponent::setText(const String& text)
00045     {
00046         d_text = text;
00047     }
00048 
00049     const String& TextComponent::getFont() const
00050     {
00051         return d_font;
00052     }
00053 
00054     void TextComponent::setFont(const String& font)
00055     {
00056         d_font = font;
00057     }
00058 
00059     VerticalTextFormatting TextComponent::getVerticalFormatting() const
00060     {
00061         return d_vertFormatting;
00062     }
00063 
00064     void TextComponent::setVerticalFormatting(VerticalTextFormatting fmt)
00065     {
00066         d_vertFormatting = fmt;
00067     }
00068 
00069     HorizontalTextFormatting TextComponent::getHorizontalFormatting() const
00070     {
00071         return d_horzFormatting;
00072     }
00073 
00074     void TextComponent::setHorizontalFormatting(HorizontalTextFormatting fmt)
00075     {
00076         d_horzFormatting = fmt;
00077     }
00078 
00079     void TextComponent::render_impl(Window& srcWindow, Rect& destRect, float base_z, const CEGUI::ColourRect* modColours, const Rect* clipper, bool clipToDisplay) const
00080     {
00081         // get font to use
00082         const Font* font;
00083 
00084         try
00085         {
00086             font = d_font.empty() ? srcWindow.getFont() : FontManager::getSingleton().getFont(d_font);
00087         }
00088         catch (UnknownObjectException)
00089         {
00090             font = 0;
00091         }
00092 
00093         // exit if we have no font to use.
00094         if (!font)
00095             return;
00096 
00097         HorizontalTextFormatting horzFormatting = d_horzFormatPropertyName.empty() ? d_horzFormatting :
00098             FalagardXMLHelper::stringToHorzTextFormat(srcWindow.getProperty(d_horzFormatPropertyName));
00099 
00100         VerticalTextFormatting vertFormatting = d_vertFormatPropertyName.empty() ? d_vertFormatting :
00101             FalagardXMLHelper::stringToVertTextFormat(srcWindow.getProperty(d_vertFormatPropertyName));
00102 
00103         // calculate final colours to be used
00104         ColourRect finalColours;
00105         initColoursRect(srcWindow, modColours, finalColours);
00106 
00107         // decide which string to render.
00108         const String& renderString = d_text.empty() ? srcWindow.getText() : d_text;
00109 
00110         // calculate height of formatted text
00111         float textHeight = font->getFormattedLineCount(renderString, destRect, (TextFormatting)horzFormatting) * font->getLineSpacing();
00112 
00113         // handle dest area adjustments for vertical formatting.
00114         switch(vertFormatting)
00115         {
00116         case VTF_CENTRE_ALIGNED:
00117             destRect.d_top += (destRect.getHeight() - textHeight) * 0.5f;
00118             break;
00119 
00120         case VTF_BOTTOM_ALIGNED:
00121             destRect.d_top = destRect.d_bottom - textHeight;
00122             break;
00123 
00124         default:
00125             // default is VTF_TOP_ALIGNED, for which we take no action.
00126             break;
00127         }
00128 
00129         // add text to the rendering cache for the target window.
00130         srcWindow.getRenderCache().cacheText(renderString, font, (TextFormatting)horzFormatting, destRect, base_z, finalColours, clipper, clipToDisplay);
00131     }
00132 
00133     void TextComponent::writeXMLToStream(OutStream& out_stream) const
00134     {
00135         // opening tag
00136         out_stream << "<TextComponent>" << std::endl;
00137         // write out area
00138         d_area.writeXMLToStream(out_stream);
00139 
00140         // write text element
00141         out_stream << "<Text font=\"" << d_font << "\" string=\"" << d_text << "\" />" << std::endl;
00142 
00143         // get base class to write colours
00144         writeColoursXML(out_stream);
00145 
00146         // write vert format, allowing base class to do this for us if a propety is in use
00147         if (!writeVertFormatXML(out_stream))
00148         {
00149             // was not a property, so write out explicit formatting in use
00150             out_stream << "<VertFormat type=\"" << FalagardXMLHelper::vertTextFormatToString(d_vertFormatting) << "\" />" << std::endl;
00151         }
00152 
00153         // write horz format, allowing base class to do this for us if a propety is in use
00154         if (!writeHorzFormatXML(out_stream))
00155         {
00156             // was not a property, so write out explicit formatting in use
00157             out_stream << "<HorzFormat type=\"" << FalagardXMLHelper::horzTextFormatToString(d_horzFormatting) << "\" />" << std::endl;
00158         }
00159 
00160         // closing tag
00161         out_stream << "</TextComponent>" << std::endl;
00162     }
00163 
00164 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:31 2005 for Crazy Eddies GUI System by  doxygen 1.4.3