Magic_Game/src/kiwano/2d/Text.cpp

230 lines
4.5 KiB
C++
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// 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 OR COPYRIGHT HOLDERS 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 "Text.h"
2019-08-14 21:52:49 +08:00
#include "../base/Logger.h"
#include "../renderer/Renderer.h"
2019-04-11 14:40:54 +08:00
namespace kiwano
{
namespace
{
Font text_default_font;
TextStyle text_default_style;
}
2019-04-11 14:40:54 +08:00
void kiwano::Text::SetDefaultFont(Font const & font)
{
text_default_font = font;
}
2019-04-11 14:40:54 +08:00
void kiwano::Text::SetDefaultStyle(TextStyle const & style)
{
text_default_style = style;
}
Text::Text()
: font_(text_default_font)
, style_(text_default_style)
, layout_dirty_(false)
2019-08-15 11:22:51 +08:00
, format_dirty_(false)
{
}
Text::Text(String const& text)
: Text(text, text_default_font, text_default_style)
{
}
Text::Text(String const& text, const Font & font)
: Text(text, font, text_default_style)
{
}
Text::Text(String const& text, const TextStyle & style)
: Text(text, text_default_font, style)
{
}
Text::Text(String const& text, const Font & font, const TextStyle & style)
: font_(font)
, style_(style)
, text_(text)
, layout_dirty_(true)
2019-08-18 22:49:44 +08:00
, format_dirty_(true)
{
}
Text::~Text()
{
}
void Text::SetText(String const& text)
{
text_ = text;
layout_dirty_ = true;
}
void Text::SetStyle(const TextStyle& style)
{
style_ = style;
layout_dirty_ = true;
}
void Text::SetFont(const Font & font)
{
font_ = font;
2019-08-15 11:22:51 +08:00
format_dirty_ = true;
}
void Text::SetFontFamily(String const& family)
{
if (font_.family != family)
{
font_.family = family;
2019-08-15 11:22:51 +08:00
format_dirty_ = true;
}
}
2019-08-18 22:49:44 +08:00
void Text::SetFontSize(Float32 size)
{
if (font_.size != size)
{
font_.size = size;
2019-08-15 11:22:51 +08:00
format_dirty_ = true;
}
}
2019-08-18 22:49:44 +08:00
void Text::SetFontWeight(UInt32 weight)
{
if (font_.weight != weight)
{
font_.weight = weight;
2019-08-15 11:22:51 +08:00
format_dirty_ = true;
}
}
void Text::SetColor(Color const& color)
{
style_.color = color;
}
2019-08-23 17:26:41 +08:00
void Text::SetItalic(bool italic)
{
2019-08-23 17:26:41 +08:00
if (font_.italic != italic)
{
2019-08-23 17:26:41 +08:00
font_.italic = italic;
2019-08-15 11:22:51 +08:00
format_dirty_ = true;
}
}
2019-08-18 22:49:44 +08:00
void Text::SetWrapWidth(Float32 wrap_width)
{
if (style_.wrap_width != wrap_width)
{
2019-08-15 11:22:51 +08:00
style_.wrap_width = wrap_width;
layout_dirty_ = true;
}
}
2019-08-18 22:49:44 +08:00
void Text::SetLineSpacing(Float32 line_spacing)
{
if (style_.line_spacing != line_spacing)
{
style_.line_spacing = line_spacing;
layout_dirty_ = true;
}
}
void Text::SetAlignment(TextAlign align)
{
if (style_.alignment != align)
{
style_.alignment = align;
layout_dirty_ = true;
}
}
void Text::SetUnderline(bool underline)
{
if (style_.underline != underline)
{
style_.underline = underline;
layout_dirty_ = true;
}
}
void Text::SetStrikethrough(bool strikethrough)
{
if (style_.strikethrough != strikethrough)
{
style_.strikethrough = strikethrough;
layout_dirty_ = true;
}
}
void Text::SetOutline(bool outline)
{
style_.outline = outline;
}
void Text::SetOutlineColor(Color const&outline_color)
{
style_.outline_color = outline_color;
}
2019-08-18 22:49:44 +08:00
void Text::SetOutlineWidth(Float32 outline_width)
{
style_.outline_width = outline_width;
}
void Text::SetOutlineStroke(StrokeStyle outline_stroke)
{
style_.outline_stroke = outline_stroke;
}
2019-08-20 19:32:36 +08:00
void Text::OnRender(RenderTarget* rt)
{
UpdateLayout();
2019-08-20 21:15:15 +08:00
if (text_layout_ && rt->CheckVisibility(GetBounds(), GetTransformMatrix()))
{
2019-08-20 19:32:36 +08:00
PrepareRender(rt);
rt->DrawTextLayout(text_layout_);
}
}
2019-08-15 11:22:51 +08:00
void Text::UpdateLayout()
{
2019-08-15 11:22:51 +08:00
if (format_dirty_)
{
format_dirty_ = false;
text_layout_.Update(font_);
}
2019-08-15 11:22:51 +08:00
if (layout_dirty_)
{
layout_dirty_ = false;
text_layout_.Update(text_, style_);
SetSize(text_layout_.GetLayoutSize());
}
}
}