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

231 lines
5.1 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.
2019-12-23 18:05:08 +08:00
#include <kiwano/2d/TextActor.h>
2019-11-13 14:33:15 +08:00
#include <kiwano/core/Logger.h>
2019-10-11 21:55:29 +08:00
#include <kiwano/renderer/Renderer.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
namespace
{
Font text_default_font;
TextStyle text_default_style;
}
2019-12-23 18:05:08 +08:00
void kiwano::TextActor::SetDefaultFont(Font const & font)
{
text_default_font = font;
}
2019-12-23 18:05:08 +08:00
void kiwano::TextActor::SetDefaultStyle(TextStyle const & style)
{
text_default_style = style;
}
2019-12-23 18:05:08 +08:00
TextActor::TextActor()
: font_(text_default_font)
, layout_dirty_(false)
2019-08-15 11:22:51 +08:00
, format_dirty_(false)
{
2019-10-31 20:34:38 +08:00
text_layout_.SetTextStyle(text_default_style);
}
2019-12-23 18:05:08 +08:00
TextActor::TextActor(String const& text)
: TextActor(text, text_default_font, text_default_style)
{
}
2019-12-23 18:05:08 +08:00
TextActor::TextActor(String const& text, const Font & font)
: TextActor(text, font, text_default_style)
{
}
2019-12-23 18:05:08 +08:00
TextActor::TextActor(String const& text, const TextStyle & style)
: TextActor(text, text_default_font, style)
{
}
2019-12-23 18:05:08 +08:00
TextActor::TextActor(String const& text, const Font & font, const TextStyle & style)
: font_(font)
, text_(text)
, layout_dirty_(true)
2019-08-18 22:49:44 +08:00
, format_dirty_(true)
{
2019-10-31 20:34:38 +08:00
text_layout_.SetTextStyle(style);
}
2019-12-23 18:05:08 +08:00
TextActor::~TextActor()
{
}
2019-12-23 18:05:08 +08:00
void TextActor::SetText(String const& text)
{
text_ = text;
layout_dirty_ = true;
}
2019-12-23 18:05:08 +08:00
void TextActor::SetStyle(const TextStyle& style)
{
2019-10-31 20:34:38 +08:00
text_layout_.SetTextStyle(style);
layout_dirty_ = true;
}
2019-12-23 18:05:08 +08:00
void TextActor::SetFont(const Font & font)
{
font_ = font;
2019-08-15 11:22:51 +08:00
format_dirty_ = true;
}
2019-12-23 18:05:08 +08:00
void TextActor::SetFontFamily(String const& family)
{
if (font_.family != family)
{
font_.family = family;
2019-08-15 11:22:51 +08:00
format_dirty_ = true;
}
}
2019-12-23 18:05:08 +08:00
void TextActor::SetFontSize(float size)
{
if (font_.size != size)
{
font_.size = size;
2019-08-15 11:22:51 +08:00
format_dirty_ = true;
}
}
2019-12-23 18:05:08 +08:00
void TextActor::SetFontWeight(uint32_t weight)
{
if (font_.weight != weight)
{
font_.weight = weight;
2019-08-15 11:22:51 +08:00
format_dirty_ = true;
}
}
2019-12-23 18:05:08 +08:00
void TextActor::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-12-23 18:05:08 +08:00
void TextActor::SetWrapWidth(float wrap_width)
{
2019-10-31 20:34:38 +08:00
if (text_layout_.GetTextStyle().wrap_width != wrap_width)
{
2019-10-31 20:34:38 +08:00
text_layout_.GetTextStyle().wrap_width = wrap_width;
layout_dirty_ = true;
}
}
2019-12-23 18:05:08 +08:00
void TextActor::SetLineSpacing(float line_spacing)
{
2019-10-31 20:34:38 +08:00
if (text_layout_.GetTextStyle().line_spacing != line_spacing)
{
2019-10-31 20:34:38 +08:00
text_layout_.GetTextStyle().line_spacing = line_spacing;
layout_dirty_ = true;
}
}
2019-12-23 18:05:08 +08:00
void TextActor::SetAlignment(TextAlign align)
{
2019-10-31 20:34:38 +08:00
if (text_layout_.GetTextStyle().alignment != align)
{
2019-10-31 20:34:38 +08:00
text_layout_.GetTextStyle().alignment = align;
layout_dirty_ = true;
}
}
2019-12-23 18:05:08 +08:00
void TextActor::SetUnderline(bool underline)
{
2019-10-31 20:34:38 +08:00
if (text_layout_.GetTextStyle().underline != underline)
{
2019-10-31 20:34:38 +08:00
text_layout_.GetTextStyle().underline = underline;
layout_dirty_ = true;
}
}
2019-12-23 18:05:08 +08:00
void TextActor::SetStrikethrough(bool strikethrough)
{
2019-10-31 20:34:38 +08:00
if (text_layout_.GetTextStyle().strikethrough != strikethrough)
{
2019-10-31 20:34:38 +08:00
text_layout_.GetTextStyle().strikethrough = strikethrough;
layout_dirty_ = true;
}
}
2019-12-23 18:05:08 +08:00
void TextActor::SetColor(Color const& color)
2019-10-31 20:34:38 +08:00
{
text_layout_.GetTextStyle().color = color;
text_layout_.GetTextStyle().color = color;
}
2019-12-23 18:05:08 +08:00
void TextActor::SetOutline(bool outline)
{
2019-10-31 20:34:38 +08:00
text_layout_.GetTextStyle().outline = outline;
}
2019-12-23 18:05:08 +08:00
void TextActor::SetOutlineColor(Color const&outline_color)
{
2019-10-31 20:34:38 +08:00
text_layout_.GetTextStyle().outline_color = outline_color;
}
2019-12-23 18:05:08 +08:00
void TextActor::SetOutlineWidth(float outline_width)
{
2019-10-31 20:34:38 +08:00
text_layout_.GetTextStyle().outline_width = outline_width;
}
2019-12-23 18:05:08 +08:00
void TextActor::SetOutlineStroke(StrokeStyle outline_stroke)
{
2019-10-31 20:34:38 +08:00
text_layout_.GetTextStyle().outline_stroke = outline_stroke;
}
2019-12-23 18:05:08 +08:00
void TextActor::OnRender(RenderTarget* rt)
{
UpdateLayout();
2019-08-27 15:29:32 +08:00
if (text_layout_ && CheckVisibilty(rt))
{
2019-08-20 19:32:36 +08:00
PrepareRender(rt);
rt->DrawTextLayout(text_layout_);
}
}
2019-12-23 18:05:08 +08:00
void TextActor::UpdateLayout()
{
2019-08-15 11:22:51 +08:00
if (format_dirty_)
{
format_dirty_ = false;
2019-10-31 20:34:38 +08:00
text_layout_.UpdateFont(font_);
2019-08-15 11:22:51 +08:00
}
2019-08-15 11:22:51 +08:00
if (layout_dirty_)
{
layout_dirty_ = false;
2019-10-31 20:34:38 +08:00
text_layout_.UpdateLayout(text_);
2019-08-15 11:22:51 +08:00
SetSize(text_layout_.GetLayoutSize());
}
}
}