Crazy Eddies GUI System  0.7.8
CEGUIRenderedStringWordWrapper.h
1 /***********************************************************************
2  filename: CEGUIRenderedStringWordWrapper.h
3  created: 25/05/2009
4  author: Paul Turner
5  *************************************************************************/
6 /***************************************************************************
7  * Copyright (C) 2004 - 2009 Paul D Turner & The CEGUI Development Team
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  ***************************************************************************/
28 #ifndef _CEGUIRenderedStringWordWrapper_h_
29 #define _CEGUIRenderedStringWordWrapper_h_
30 
31 #include "CEGUIFormattedRenderedString.h"
32 #include "CEGUIJustifiedRenderedString.h"
33 #include "CEGUIVector.h"
34 #include <vector>
35 
36 // Start of CEGUI namespace section
37 namespace CEGUI
38 {
44 template <typename T>
46 {
47 public:
52 
53  // implementation of base interface
54  void format(const Size& area_size);
55  void draw(GeometryBuffer& buffer, const Vector2& position,
56  const ColourRect* mod_colours, const Rect* clip_rect) const;
57  size_t getFormattedLineCount() const;
58  float getHorizontalExtent() const;
59  float getVerticalExtent() const;
60 
61 protected:
63  void deleteFormatters();
65  typedef std::vector<FormattedRenderedString*> LineList;
68 };
69 
71 template <> CEGUIEXPORT
73 
74 //----------------------------------------------------------------------------//
75 template <typename T>
77  const RenderedString& string) :
79 {
80 }
81 
82 //----------------------------------------------------------------------------//
83 template <typename T>
85 {
86  deleteFormatters();
87 }
88 
89 //----------------------------------------------------------------------------//
90 template <typename T>
91 void RenderedStringWordWrapper<T>::format(const Size& area_size)
92 {
93  deleteFormatters();
94 
95  RenderedString rstring, lstring;
96  rstring = *d_renderedString;
97  float rs_width;
98 
99  T* frs;
100 
101  for (size_t line = 0; line < rstring.getLineCount(); ++line)
102  {
103  while ((rs_width = rstring.getPixelSize(line).d_width) > 0)
104  {
105  // skip line if no wrapping occurs
106  if (rs_width <= area_size.d_width)
107  break;
108 
109  // split rstring at width into lstring and remaining rstring
110  rstring.split(line, area_size.d_width, lstring);
111  frs = new T(*new RenderedString(lstring));
112  frs->format(area_size);
113  d_lines.push_back(frs);
114  line = 0;
115  }
116  }
117 
118  // last line.
119  frs = new T(*new RenderedString(rstring));
120  frs->format(area_size);
121  d_lines.push_back(frs);
122 }
123 
124 //----------------------------------------------------------------------------//
125 template <typename T>
126 void RenderedStringWordWrapper<T>::draw(GeometryBuffer& buffer,
127  const Vector2& position,
128  const ColourRect* mod_colours,
129  const Rect* clip_rect) const
130 {
131  Vector2 line_pos(position);
132  typename LineList::const_iterator i = d_lines.begin();
133  for (; i != d_lines.end(); ++i)
134  {
135  (*i)->draw(buffer, line_pos, mod_colours, clip_rect);
136  line_pos.d_y += (*i)->getVerticalExtent();
137  }
138 }
139 
140 //----------------------------------------------------------------------------//
141 template <typename T>
142 size_t RenderedStringWordWrapper<T>::getFormattedLineCount() const
143 {
144  return d_lines.size();
145 }
146 
147 //----------------------------------------------------------------------------//
148 template <typename T>
149 float RenderedStringWordWrapper<T>::getHorizontalExtent() const
150 {
151  // TODO: Cache at format time.
152 
153  float w = 0;
154  typename LineList::const_iterator i = d_lines.begin();
155  for (; i != d_lines.end(); ++i)
156  {
157  const float cur_width = (*i)->getHorizontalExtent();
158  if (cur_width > w)
159  w = cur_width;
160  }
161 
162  return w;
163 }
164 
165 //----------------------------------------------------------------------------//
166 template <typename T>
167 float RenderedStringWordWrapper<T>::getVerticalExtent() const
168 {
169  // TODO: Cache at format time.
170 
171  float h = 0;
172  typename LineList::const_iterator i = d_lines.begin();
173  for (; i != d_lines.end(); ++i)
174  h += (*i)->getVerticalExtent();
175 
176  return h;
177 }
178 
179 //----------------------------------------------------------------------------//
180 template <typename T>
182 {
183  for (size_t i = 0; i < d_lines.size(); ++i)
184  {
185  // get the rendered string back from rthe formatter
186  const RenderedString* rs = &d_lines[i]->getRenderedString();
187  // delete the formatter
188  delete d_lines[i];
189  // delete the rendered string.
190  delete rs;
191  }
192 
193  d_lines.clear();
194 }
195 
196 //----------------------------------------------------------------------------//
197 
198 } // End of CEGUI namespace section
199 
200 #endif // end of guard _CEGUIRenderedStringWordWrapper_h_