1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/***********************************************************************
created: 25/05/2009
author: Paul Turner
*************************************************************************/
/***************************************************************************
* Copyright (C) 2004 - 2009 Paul D Turner & The CEGUI Development Team
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
***************************************************************************/
#include "CEGUI/RenderedStringTextComponent.h"
#include "CEGUI/FontManager.h"
#include "CEGUI/Font.h"
#include "CEGUI/System.h"
#include "CEGUI/Exceptions.h"
#include "CEGUI/TextUtils.h"
#include "CEGUI/GUIContext.h"
#include "CEGUI/Window.h"
// Start of CEGUI namespace section
namespace CEGUI
{
//----------------------------------------------------------------------------//
RenderedStringTextComponent::RenderedStringTextComponent() :
d_font(0),
d_colours(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF),
d_selectionStart(0),
d_selectionLength(0)
{
}
//----------------------------------------------------------------------------//
RenderedStringTextComponent::RenderedStringTextComponent(const String& text) :
d_text(text),
d_font(0),
d_colours(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF),
d_selectionStart(0),
d_selectionLength(0)
{
}
//----------------------------------------------------------------------------//
RenderedStringTextComponent::RenderedStringTextComponent(
const String& text, const String& font_name) :
d_text(text),
d_font(font_name.empty() ? 0 : &FontManager::getSingleton().get(font_name)),
d_colours(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF),
d_selectionStart(0),
d_selectionLength(0)
{
}
//----------------------------------------------------------------------------//
RenderedStringTextComponent::RenderedStringTextComponent(const String& text,
const Font* font) :
d_text(text),
d_font(font),
d_colours(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF),
d_selectionStart(0),
d_selectionLength(0)
{
}
//----------------------------------------------------------------------------//
void RenderedStringTextComponent::setText(const String& text)
{
d_text = text;
}
//----------------------------------------------------------------------------//
const String& RenderedStringTextComponent::getText() const
{
return d_text;
}
//----------------------------------------------------------------------------//
void RenderedStringTextComponent::setFont(const Font* font)
{
d_font = font;
}
//----------------------------------------------------------------------------//
void RenderedStringTextComponent::setFont(const String& font_name)
{
d_font =
font_name.empty() ? 0 : &FontManager::getSingleton().get(font_name);
}
//----------------------------------------------------------------------------//
const Font* RenderedStringTextComponent::getFont() const
{
return d_font;
}
//----------------------------------------------------------------------------//
void RenderedStringTextComponent::setColours(const ColourRect& cr)
{
d_colours = cr;
}
//----------------------------------------------------------------------------//
void RenderedStringTextComponent::setColours(const Colour& c)
{
d_colours.setColours(c);
}
//----------------------------------------------------------------------------//
const ColourRect& RenderedStringTextComponent::getColours() const
{
return d_colours;
}
//----------------------------------------------------------------------------//
void RenderedStringTextComponent::setSelection(const Window* ref_wnd,
const float start, const float end)
{
if (start >= end)
{
d_selectionStart = d_selectionLength = 0;
return;
}
const Font* fnt = getEffectiveFont(ref_wnd);
d_selectionStart = fnt->getCharAtPixel(d_text, start);
d_selectionLength = fnt->getCharAtPixel(d_text, end) - d_selectionStart;
}
//----------------------------------------------------------------------------//
const Font* RenderedStringTextComponent::getEffectiveFont(
const Window* window) const
{
if (d_font)
return d_font;
if (window)
return window->getFont();
return 0;
}
//----------------------------------------------------------------------------//
void RenderedStringTextComponent::draw(const Window* ref_wnd,
GeometryBuffer& buffer,
const Vector2f& position,
const ColourRect* mod_colours,
const Rectf* clip_rect,
const float vertical_space,
const float space_extra) const
{
const Font* fnt = getEffectiveFont(ref_wnd);
if (!fnt)
return;
Vector2f final_pos(position);
float y_scale = 1.0f;
// handle formatting options
switch (d_verticalFormatting)
{
case VF_BOTTOM_ALIGNED:
final_pos.d_y += vertical_space - getPixelSize(ref_wnd).d_height;
break;
case VF_CENTRE_ALIGNED:
final_pos.d_y += (vertical_space - getPixelSize(ref_wnd).d_height) / 2 ;
break;
case VF_STRETCHED:
y_scale = vertical_space / getPixelSize(ref_wnd).d_height;
break;
case VF_TOP_ALIGNED:
// nothing additional to do for this formatting option.
break;
default:
CEGUI_THROW(InvalidRequestException(
"unknown VerticalFormatting option specified."));
}
// apply padding to position:
final_pos += d_padding.getPosition();
// apply modulative colours if needed.
ColourRect final_cols(d_colours);
if (mod_colours)
final_cols *= *mod_colours;
// render selection
if (d_selectionImage && d_selectionLength)
{
float sel_start_extent = 0, sel_end_extent = 0;
if (d_selectionStart > 0)
sel_start_extent = fnt->getTextExtent(d_text.substr(0, d_selectionStart));
sel_end_extent = fnt->getTextExtent(d_text.substr(0, d_selectionStart + d_selectionLength));
Rectf sel_rect(position.d_x + sel_start_extent,
position.d_y,
position.d_x + sel_end_extent,
position.d_y + vertical_space);
d_selectionImage->render(buffer, sel_rect, clip_rect, ColourRect(0xFF002FFF));
}
// draw the text string.
fnt->drawText(buffer, d_text, final_pos, clip_rect, final_cols,
space_extra, 1.0f, y_scale);
}
//----------------------------------------------------------------------------//
Sizef RenderedStringTextComponent::getPixelSize(const Window* ref_wnd) const
{
const Font* fnt = getEffectiveFont(ref_wnd);
Sizef psz(d_padding.d_min.d_x + d_padding.d_max.d_x,
d_padding.d_min.d_y + d_padding.d_max.d_y);
if (fnt)
{
psz.d_width += fnt->getTextExtent(d_text);
psz.d_height += fnt->getFontHeight();
}
return psz;
}
//----------------------------------------------------------------------------//
bool RenderedStringTextComponent::canSplit() const
{
return d_text.length() > 1;
}
//----------------------------------------------------------------------------//
RenderedStringTextComponent* RenderedStringTextComponent::split(
const Window* ref_wnd,
float split_point,
bool first_component)
{
const Font* fnt = getEffectiveFont(ref_wnd);
// This is checked, but should never fail, since if we had no font our
// extent would be 0 and we would never cause a split to be needed here.
if (!fnt)
CEGUI_THROW(InvalidRequestException(
"unable to split with no font set."));
// create 'left' side of split and clone our basic configuration
RenderedStringTextComponent* lhs = CEGUI_NEW_AO RenderedStringTextComponent();
lhs->d_padding = d_padding;
lhs->d_verticalFormatting = d_verticalFormatting;
lhs->d_font = d_font;
lhs->d_colours = d_colours;
// calculate the 'best' place to split the text
size_t left_len = 0;
float left_extent = 0.0f;
while (left_len < d_text.length())
{
size_t token_len = getNextTokenLength(d_text, left_len);
// exit loop if no more valid tokens.
if (token_len == 0)
break;
const float token_extent =
fnt->getTextExtent(d_text.substr(left_len, token_len));
// does the next token extend past the split point?
if (left_extent + token_extent > split_point)
{
// if it was the first token, split the token itself
if (first_component && left_len == 0)
left_len =
ceguimax(static_cast<size_t>(1),
fnt->getCharAtPixel(
d_text.substr(0, token_len), split_point));
// left_len is now the character index at which to split the line
break;
}
// add this token to the left side
left_len += token_len;
left_extent += token_extent;
}
// perform the split.
lhs->d_text = d_text.substr(0, left_len);
// here we're trimming leading delimiters from the substring range
size_t rhs_start =
d_text.find_first_not_of(TextUtils::DefaultWrapDelimiters, left_len);
if (rhs_start == String::npos)
rhs_start = left_len;
// split the selection
if (d_selectionLength)
{
const size_t sel_end = d_selectionStart + d_selectionLength - 1;
lhs->d_selectionStart = d_selectionStart;
lhs->d_selectionLength = sel_end < left_len ? d_selectionLength : left_len - d_selectionStart;
if (sel_end >= left_len)
{
d_selectionStart = 0;
d_selectionLength -= rhs_start;
}
else
setSelection(ref_wnd, 0, 0);
}
d_text = d_text.substr(rhs_start);
return lhs;
}
//----------------------------------------------------------------------------//
size_t RenderedStringTextComponent::getNextTokenLength(const String& text,
size_t start_idx)
{
String::size_type word_start =
text.find_first_not_of(TextUtils::DefaultWrapDelimiters, start_idx);
if (word_start == String::npos)
word_start = start_idx;
String::size_type word_end =
text.find_first_of(TextUtils::DefaultWrapDelimiters, word_start);
if (word_end == String::npos)
word_end = text.length();
return word_end - start_idx;
}
//----------------------------------------------------------------------------//
RenderedStringTextComponent* RenderedStringTextComponent::clone() const
{
RenderedStringTextComponent* c = CEGUI_NEW_AO RenderedStringTextComponent(*this);
return c;
}
//----------------------------------------------------------------------------//
size_t RenderedStringTextComponent::getSpaceCount() const
{
// TODO: The value calculated here is a good candidate for caching.
size_t space_count = 0;
// Count the number of spaces in this component.
// NB: here I'm not countng tabs since those are really intended to be
// something other than just a bigger space.
const size_t char_count = d_text.length();
for (size_t c = 0; c < char_count; ++c)
if (d_text[c] == ' ') // TODO: There are other space characters!
++space_count;
return space_count;
}
//----------------------------------------------------------------------------//
} // End of CEGUI namespace section
|