Crazy Eddie's GUI System  0.8.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
RenderedStringWordWrapper.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 "CEGUI/FormattedRenderedString.h"
32 #include "CEGUI/JustifiedRenderedString.h"
33 #include "CEGUI/Vector.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 Window* ref_wnd, const Sizef& area_size);
55  void draw(const Window* ref_wnd, GeometryBuffer& buffer,
56  const Vector2f& position, const ColourRect* mod_colours,
57  const Rectf* clip_rect) const;
58  size_t getFormattedLineCount() const;
59  float getHorizontalExtent(const Window* ref_wnd) const;
60  float getVerticalExtent(const Window* ref_wnd) const;
61 
62 protected:
64  void deleteFormatters();
66  typedef std::vector<FormattedRenderedString*
67  CEGUI_VECTOR_ALLOC(FormattedRenderedString*)> LineList;
70 };
71 
73 template <> CEGUIEXPORT
75  const Sizef& area_size);
76 
77 //----------------------------------------------------------------------------//
78 template <typename T>
80  const RenderedString& string) :
82 {
83 }
84 
85 //----------------------------------------------------------------------------//
86 template <typename T>
88 {
89  deleteFormatters();
90 }
91 
92 //----------------------------------------------------------------------------//
93 template <typename T>
95  const Sizef& area_size)
96 {
97  deleteFormatters();
98 
99  RenderedString rstring, lstring;
100  rstring = *d_renderedString;
101  float rs_width;
102 
103  T* frs;
104 
105  for (size_t line = 0; line < rstring.getLineCount(); ++line)
106  {
107  while ((rs_width = rstring.getPixelSize(ref_wnd, line).d_width) > 0)
108  {
109  // skip line if no wrapping occurs
110  if (rs_width <= area_size.d_width)
111  break;
112 
113  // split rstring at width into lstring and remaining rstring
114  rstring.split(ref_wnd, line, area_size.d_width, lstring);
115  frs = CEGUI_NEW_AO T(*new RenderedString(lstring));
116  frs->format(ref_wnd, area_size);
117  d_lines.push_back(frs);
118  line = 0;
119  }
120  }
121 
122  // last line.
123  frs = CEGUI_NEW_AO T(*new RenderedString(rstring));
124  frs->format(ref_wnd, area_size);
125  d_lines.push_back(frs);
126 }
127 
128 //----------------------------------------------------------------------------//
129 template <typename T>
130 void RenderedStringWordWrapper<T>::draw(const Window* ref_wnd,
131  GeometryBuffer& buffer,
132  const Vector2f& position,
133  const ColourRect* mod_colours,
134  const Rectf* clip_rect) const
135 {
136  Vector2f line_pos(position);
137  typename LineList::const_iterator i = d_lines.begin();
138  for (; i != d_lines.end(); ++i)
139  {
140  (*i)->draw(ref_wnd, buffer, line_pos, mod_colours, clip_rect);
141  line_pos.d_y += (*i)->getVerticalExtent(ref_wnd);
142  }
143 }
144 
145 //----------------------------------------------------------------------------//
146 template <typename T>
147 size_t RenderedStringWordWrapper<T>::getFormattedLineCount() const
148 {
149  return d_lines.size();
150 }
151 
152 //----------------------------------------------------------------------------//
153 template <typename T>
154 float RenderedStringWordWrapper<T>::getHorizontalExtent(const Window* ref_wnd) const
155 {
156  // TODO: Cache at format time.
157 
158  float w = 0;
159  typename LineList::const_iterator i = d_lines.begin();
160  for (; i != d_lines.end(); ++i)
161  {
162  const float cur_width = (*i)->getHorizontalExtent(ref_wnd);
163  if (cur_width > w)
164  w = cur_width;
165  }
166 
167  return w;
168 }
169 
170 //----------------------------------------------------------------------------//
171 template <typename T>
172 float RenderedStringWordWrapper<T>::getVerticalExtent(const Window* ref_wnd) const
173 {
174  // TODO: Cache at format time.
175 
176  float h = 0;
177  typename LineList::const_iterator i = d_lines.begin();
178  for (; i != d_lines.end(); ++i)
179  h += (*i)->getVerticalExtent(ref_wnd);
180 
181  return h;
182 }
183 
184 //----------------------------------------------------------------------------//
185 template <typename T>
187 {
188  for (size_t i = 0; i < d_lines.size(); ++i)
189  {
190  // get the rendered string back from rthe formatter
191  const RenderedString* rs = &d_lines[i]->getRenderedString();
192  // delete the formatter
193  CEGUI_DELETE_AO d_lines[i];
194  // delete the rendered string.
195  CEGUI_DELETE_AO rs;
196  }
197 
198  d_lines.clear();
199 }
200 
201 //----------------------------------------------------------------------------//
202 
203 } // End of CEGUI namespace section
204 
205 #endif // end of guard _CEGUIRenderedStringWordWrapper_h_