Crazy Eddie's GUI System  0.8.7
RenderedStringWordWrapper.h
1 /***********************************************************************
2  created: 25/05/2009
3  author: Paul Turner
4  *************************************************************************/
5 /***************************************************************************
6  * Copyright (C) 2004 - 2009 Paul D Turner & The CEGUI Development Team
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  ***************************************************************************/
27 #ifndef _CEGUIRenderedStringWordWrapper_h_
28 #define _CEGUIRenderedStringWordWrapper_h_
29 
30 #include "CEGUI/FormattedRenderedString.h"
31 #include "CEGUI/JustifiedRenderedString.h"
32 #include "CEGUI/Vector.h"
33 #include <vector>
34 
35 // Start of CEGUI namespace section
36 namespace CEGUI
37 {
43 template <typename T>
45 {
46 public:
51 
52  // implementation of base interface
53  void format(const Window* ref_wnd, const Sizef& area_size);
54  void draw(const Window* ref_wnd, GeometryBuffer& buffer,
55  const Vector2f& position, const ColourRect* mod_colours,
56  const Rectf* clip_rect) const;
57  size_t getFormattedLineCount() const;
58  float getHorizontalExtent(const Window* ref_wnd) const;
59  float getVerticalExtent(const Window* ref_wnd) const;
60 
61 protected:
63  void deleteFormatters();
65  typedef std::vector<FormattedRenderedString*
66  CEGUI_VECTOR_ALLOC(FormattedRenderedString*)> LineList;
69 };
70 
72 template <> CEGUIEXPORT
74  const Sizef& area_size);
75 
76 //----------------------------------------------------------------------------//
77 template <typename T>
79  const RenderedString& string) :
81 {
82 }
83 
84 //----------------------------------------------------------------------------//
85 template <typename T>
87 {
88  deleteFormatters();
89 }
90 
91 //----------------------------------------------------------------------------//
92 template <typename T>
94  const Sizef& area_size)
95 {
96  deleteFormatters();
97 
98  RenderedString rstring, lstring;
99  rstring = *d_renderedString;
100  float rs_width;
101 
102  T* frs;
103 
104  for (size_t line = 0; line < rstring.getLineCount(); ++line)
105  {
106  while ((rs_width = rstring.getPixelSize(ref_wnd, line).d_width) > 0)
107  {
108  // skip line if no wrapping occurs
109  if (rs_width <= area_size.d_width)
110  break;
111 
112  // split rstring at width into lstring and remaining rstring
113  rstring.split(ref_wnd, line, area_size.d_width, lstring);
114  frs = CEGUI_NEW_AO T(*new RenderedString(lstring));
115  frs->format(ref_wnd, area_size);
116  d_lines.push_back(frs);
117  line = 0;
118  }
119  }
120 
121  // last line.
122  frs = CEGUI_NEW_AO T(*new RenderedString(rstring));
123  frs->format(ref_wnd, area_size);
124  d_lines.push_back(frs);
125 }
126 
127 //----------------------------------------------------------------------------//
128 template <typename T>
129 void RenderedStringWordWrapper<T>::draw(const Window* ref_wnd,
130  GeometryBuffer& buffer,
131  const Vector2f& position,
132  const ColourRect* mod_colours,
133  const Rectf* clip_rect) const
134 {
135  Vector2f line_pos(position);
136  typename LineList::const_iterator i = d_lines.begin();
137  for (; i != d_lines.end(); ++i)
138  {
139  (*i)->draw(ref_wnd, buffer, line_pos, mod_colours, clip_rect);
140  line_pos.d_y += (*i)->getVerticalExtent(ref_wnd);
141  }
142 }
143 
144 //----------------------------------------------------------------------------//
145 template <typename T>
146 size_t RenderedStringWordWrapper<T>::getFormattedLineCount() const
147 {
148  return d_lines.size();
149 }
150 
151 //----------------------------------------------------------------------------//
152 template <typename T>
153 float RenderedStringWordWrapper<T>::getHorizontalExtent(const Window* ref_wnd) const
154 {
155  // TODO: Cache at format time.
156 
157  float w = 0;
158  typename LineList::const_iterator i = d_lines.begin();
159  for (; i != d_lines.end(); ++i)
160  {
161  const float cur_width = (*i)->getHorizontalExtent(ref_wnd);
162  if (cur_width > w)
163  w = cur_width;
164  }
165 
166  return w;
167 }
168 
169 //----------------------------------------------------------------------------//
170 template <typename T>
171 float RenderedStringWordWrapper<T>::getVerticalExtent(const Window* ref_wnd) const
172 {
173  // TODO: Cache at format time.
174 
175  float h = 0;
176  typename LineList::const_iterator i = d_lines.begin();
177  for (; i != d_lines.end(); ++i)
178  h += (*i)->getVerticalExtent(ref_wnd);
179 
180  return h;
181 }
182 
183 //----------------------------------------------------------------------------//
184 template <typename T>
186 {
187  for (size_t i = 0; i < d_lines.size(); ++i)
188  {
189  // get the rendered string back from rthe formatter
190  const RenderedString* rs = &d_lines[i]->getRenderedString();
191  // delete the formatter
192  CEGUI_DELETE_AO d_lines[i];
193  // delete the rendered string.
194  CEGUI_DELETE_AO rs;
195  }
196 
197  d_lines.clear();
198 }
199 
200 //----------------------------------------------------------------------------//
201 
202 } // End of CEGUI namespace section
203 
204 #endif // end of guard _CEGUIRenderedStringWordWrapper_h_
std::vector< FormattedRenderedString *CEGUI_VECTOR_ALLOC(FormattedRenderedString *)> LineList
type of collection used to track the formatted lines.
Definition: RenderedStringWordWrapper.h:66
Main namespace for Crazy Eddie's GUI Library.
Definition: arch_overview.dox:1
Class that handles wrapping of a rendered string into sub-strings. Each sub-string is rendered using ...
Definition: RenderedStringWordWrapper.h:44
Abstract class defining the interface for objects that buffer geometry for later rendering.
Definition: GeometryBuffer.h:42
Class that holds details of colours for the four corners of a rectangle.
Definition: ColourRect.h:43
RenderedStringWordWrapper(const RenderedString &string)
Constructor.
Definition: RenderedStringWordWrapper.h:78
Root of a class hierarchy that wrap RenderedString objects and render them with additional formatting...
Definition: FormattedRenderedString.h:40
Class representing a rendered string of entities.
Definition: RenderedString.h:50
size_t getLineCount() const
return number of lines in this string.
void deleteFormatters()
Delete the current formatters and associated RenderedStrings.
Definition: RenderedStringWordWrapper.h:185
LineList d_lines
collection of lines.
Definition: RenderedStringWordWrapper.h:68
An abstract base class providing common functionality and specifying the required interface for deriv...
Definition: Window.h:149
~RenderedStringWordWrapper()
Destructor.
Definition: RenderedStringWordWrapper.h:86
void split(const Window *ref_wnd, const size_t line, float split_point, RenderedString &left)
split the string in line line as close to split_point as possible.
Sizef getPixelSize(const Window *ref_wnd, const size_t line) const
Return the pixel size of a specified line for the RenderedString.