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