Crazy Eddie's GUI System  0.8.4
RenderedStringWordWrapper.h
00001 /***********************************************************************
00002     created:    25/05/2009
00003     author:     Paul Turner
00004  *************************************************************************/
00005 /***************************************************************************
00006  *   Copyright (C) 2004 - 2009 Paul D Turner & The CEGUI Development Team
00007  *
00008  *   Permission is hereby granted, free of charge, to any person obtaining
00009  *   a copy of this software and associated documentation files (the
00010  *   "Software"), to deal in the Software without restriction, including
00011  *   without limitation the rights to use, copy, modify, merge, publish,
00012  *   distribute, sublicense, and/or sell copies of the Software, and to
00013  *   permit persons to whom the Software is furnished to do so, subject to
00014  *   the following conditions:
00015  *
00016  *   The above copyright notice and this permission notice shall be
00017  *   included in all copies or substantial portions of the Software.
00018  *
00019  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00020  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00021  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00022  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00023  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00024  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00025  *   OTHER DEALINGS IN THE SOFTWARE.
00026  ***************************************************************************/
00027 #ifndef _CEGUIRenderedStringWordWrapper_h_
00028 #define _CEGUIRenderedStringWordWrapper_h_
00029 
00030 #include "CEGUI/FormattedRenderedString.h"
00031 #include "CEGUI/JustifiedRenderedString.h"
00032 #include "CEGUI/Vector.h"
00033 #include <vector>
00034 
00035 // Start of CEGUI namespace section
00036 namespace CEGUI
00037 {
00043 template <typename T>
00044 class RenderedStringWordWrapper : public FormattedRenderedString
00045 {
00046 public:
00048     RenderedStringWordWrapper(const RenderedString& string);
00050     ~RenderedStringWordWrapper();
00051 
00052     // implementation of base interface
00053     void format(const Window* ref_wnd, const Sizef& area_size);
00054     void draw(const Window* ref_wnd, GeometryBuffer& buffer,
00055               const Vector2f& position, const ColourRect* mod_colours,
00056               const Rectf* clip_rect) const;
00057     size_t getFormattedLineCount() const;
00058     float getHorizontalExtent(const Window* ref_wnd) const;
00059     float getVerticalExtent(const Window* ref_wnd) const;
00060 
00061 protected:
00063     void deleteFormatters();
00065     typedef std::vector<FormattedRenderedString*
00066         CEGUI_VECTOR_ALLOC(FormattedRenderedString*)> LineList;
00068     LineList d_lines;
00069 };
00070 
00072 template <> CEGUIEXPORT
00073 void RenderedStringWordWrapper<JustifiedRenderedString>::format(const Window* ref_wnd,
00074                                                                 const Sizef& area_size);
00075 
00076 //----------------------------------------------------------------------------//
00077 template <typename T>
00078 RenderedStringWordWrapper<T>::RenderedStringWordWrapper(
00079         const RenderedString& string) :
00080     FormattedRenderedString(string)
00081 {
00082 }
00083 
00084 //----------------------------------------------------------------------------//
00085 template <typename T>
00086 RenderedStringWordWrapper<T>::~RenderedStringWordWrapper()
00087 {
00088     deleteFormatters();
00089 }
00090 
00091 //----------------------------------------------------------------------------//
00092 template <typename T>
00093 void RenderedStringWordWrapper<T>::format(const Window* ref_wnd,
00094                                           const Sizef& area_size)
00095 {
00096     deleteFormatters();
00097 
00098     RenderedString rstring, lstring;
00099     rstring = *d_renderedString;
00100     float rs_width;
00101 
00102     T* frs;
00103 
00104     for (size_t line = 0; line < rstring.getLineCount(); ++line)
00105     {
00106         while ((rs_width = rstring.getPixelSize(ref_wnd, line).d_width) > 0)
00107         {
00108             // skip line if no wrapping occurs
00109             if (rs_width <= area_size.d_width)
00110                 break;
00111 
00112             // split rstring at width into lstring and remaining rstring
00113             rstring.split(ref_wnd, line, area_size.d_width, lstring);
00114             frs = CEGUI_NEW_AO T(*new RenderedString(lstring));
00115             frs->format(ref_wnd, area_size);
00116             d_lines.push_back(frs);
00117             line = 0;
00118         }
00119     }
00120 
00121     // last line.
00122     frs = CEGUI_NEW_AO T(*new RenderedString(rstring));
00123     frs->format(ref_wnd, area_size);
00124     d_lines.push_back(frs);
00125 }
00126 
00127 //----------------------------------------------------------------------------//
00128 template <typename T>
00129 void RenderedStringWordWrapper<T>::draw(const Window* ref_wnd,
00130                                         GeometryBuffer& buffer,
00131                                         const Vector2f& position,
00132                                         const ColourRect* mod_colours,
00133                                         const Rectf* clip_rect) const
00134 {
00135     Vector2f line_pos(position);
00136     typename LineList::const_iterator i = d_lines.begin();
00137     for (; i != d_lines.end(); ++i)
00138     {
00139         (*i)->draw(ref_wnd, buffer, line_pos, mod_colours, clip_rect);
00140         line_pos.d_y += (*i)->getVerticalExtent(ref_wnd);
00141     }
00142 }
00143 
00144 //----------------------------------------------------------------------------//
00145 template <typename T>
00146 size_t RenderedStringWordWrapper<T>::getFormattedLineCount() const
00147 {
00148     return d_lines.size();
00149 }
00150 
00151 //----------------------------------------------------------------------------//
00152 template <typename T>
00153 float RenderedStringWordWrapper<T>::getHorizontalExtent(const Window* ref_wnd) const
00154 {
00155     // TODO: Cache at format time.
00156 
00157     float w = 0;
00158     typename LineList::const_iterator i = d_lines.begin();
00159     for (; i != d_lines.end(); ++i)
00160     {
00161         const float cur_width = (*i)->getHorizontalExtent(ref_wnd);
00162         if (cur_width > w)
00163             w = cur_width;
00164     }
00165     
00166     return w;
00167 }
00168 
00169 //----------------------------------------------------------------------------//
00170 template <typename T>
00171 float RenderedStringWordWrapper<T>::getVerticalExtent(const Window* ref_wnd) const
00172 {
00173     // TODO: Cache at format time.
00174 
00175     float h = 0;
00176     typename LineList::const_iterator i = d_lines.begin();
00177     for (; i != d_lines.end(); ++i)
00178         h += (*i)->getVerticalExtent(ref_wnd);
00179 
00180     return h;
00181 }
00182 
00183 //----------------------------------------------------------------------------//
00184 template <typename T>
00185 void RenderedStringWordWrapper<T>::deleteFormatters()
00186 {
00187     for (size_t i = 0; i < d_lines.size(); ++i)
00188     {
00189         // get the rendered string back from rthe formatter
00190         const RenderedString* rs = &d_lines[i]->getRenderedString();
00191         // delete the formatter
00192         CEGUI_DELETE_AO d_lines[i];
00193         // delete the rendered string.
00194         CEGUI_DELETE_AO rs;
00195     }
00196 
00197     d_lines.clear();
00198 }
00199 
00200 //----------------------------------------------------------------------------//
00201 
00202 } // End of  CEGUI namespace section
00203 
00204 #endif // end of guard _CEGUIRenderedStringWordWrapper_h_
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends