Magic_Game/core/base/Text.cpp

419 lines
7.7 KiB
C++
Raw Normal View History

2018-10-03 22:02:46 +08:00
// Copyright (c) 2016-2018 Easy2D - 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"
#include "render.h"
2018-11-16 15:53:39 +08:00
#include "base.hpp"
2018-11-15 17:59:18 +08:00
#include "logs.h"
2018-11-08 00:21:59 +08:00
namespace easy2d
{
2018-11-15 14:35:19 +08:00
namespace
{
Font text_default_font;
TextStyle text_default_style;
}
void easy2d::Text::SetDefaultFont(Font const & font)
{
text_default_font = font;
}
void easy2d::Text::SetDefaultStyle(TextStyle const & style)
{
text_default_style = style;
}
2018-11-08 00:21:59 +08:00
Text::Text()
2018-11-15 14:35:19 +08:00
: font_(text_default_font)
, style_(text_default_style)
2018-11-08 00:21:59 +08:00
, text_layout_(nullptr)
, text_format_(nullptr)
2018-11-15 14:35:19 +08:00
, dirty_layout_(false)
2018-11-08 00:21:59 +08:00
{
}
2018-11-15 14:35:19 +08:00
Text::Text(const String & text)
: Text(text, text_default_font, text_default_style)
{
}
Text::Text(const String & text, const Font & font)
: Text(text, font, text_default_style)
{
}
Text::Text(const String & text, const TextStyle & style)
: Text(text, text_default_font, style)
{
}
Text::Text(const String & text, const Font & font, const TextStyle & style)
2018-11-08 00:21:59 +08:00
: font_(font)
, style_(style)
2018-11-15 14:35:19 +08:00
, text_(text)
2018-11-08 00:21:59 +08:00
, text_layout_(nullptr)
, text_format_(nullptr)
2018-11-15 14:35:19 +08:00
, dirty_layout_(true)
2018-11-08 00:21:59 +08:00
{
}
2018-11-08 00:21:59 +08:00
Text::~Text()
{
SafeRelease(text_format_);
SafeRelease(text_layout_);
}
const String& Text::GetText() const
2018-11-08 00:21:59 +08:00
{
return text_;
}
2018-11-08 00:21:59 +08:00
const Font& Text::GetFont() const
{
return font_;
}
2018-11-15 14:35:19 +08:00
const TextStyle& Text::GetStyle() const
2018-04-09 18:41:56 +08:00
{
2018-11-08 00:21:59 +08:00
return style_;
2018-04-09 18:41:56 +08:00
}
2018-11-08 00:21:59 +08:00
const String& Text::GetFontFamily() const
2018-04-09 18:41:56 +08:00
{
2018-11-08 00:21:59 +08:00
return font_.family;
2018-04-09 18:41:56 +08:00
}
2018-11-08 00:21:59 +08:00
float Text::GetFontSize() const
{
return font_.size;
}
2018-11-15 14:35:19 +08:00
unsigned int Text::GetFontWeight() const
2018-11-08 00:21:59 +08:00
{
return font_.weight;
}
2018-04-22 14:08:29 +08:00
2018-11-08 00:21:59 +08:00
const Color& Text::GetColor() const
{
return style_.color;
}
2018-04-22 14:08:29 +08:00
2018-11-08 00:21:59 +08:00
const Color& Text::GetOutlineColor() const
{
return style_.outline_color;
}
2018-11-08 00:21:59 +08:00
float Text::GetOutlineWidth() const
{
return style_.outline_width;
}
StrokeStyle Text::GetOutlineStroke() const
2018-11-08 00:21:59 +08:00
{
return style_.outline_stroke;
}
int Text::GetLineCount()
2018-11-08 00:21:59 +08:00
{
2018-11-15 14:35:19 +08:00
UpdateLayout();
2018-11-08 00:21:59 +08:00
if (text_layout_)
{
DWRITE_TEXT_METRICS metrics;
2018-11-15 14:35:19 +08:00
if (SUCCEEDED(text_layout_->GetMetrics(&metrics)))
{
return static_cast<int>(metrics.lineCount);
}
2018-11-08 00:21:59 +08:00
}
2018-11-15 14:35:19 +08:00
return 0;
}
Rect Text::GetBounds()
2018-11-15 14:35:19 +08:00
{
UpdateLayout();
return Node::GetBounds();
2018-11-08 00:21:59 +08:00
}
2018-11-08 00:21:59 +08:00
bool Text::IsItalic() const
{
return font_.italic;
}
2018-11-15 14:35:19 +08:00
bool Text::HasStrikethrough() const
2018-11-08 00:21:59 +08:00
{
return style_.strikethrough;
}
2018-11-15 14:35:19 +08:00
bool Text::HasUnderline() const
2018-11-08 00:21:59 +08:00
{
return style_.underline;
}
2018-11-15 14:35:19 +08:00
bool Text::HasOutline() const
2018-11-08 00:21:59 +08:00
{
return style_.outline;
}
void Text::SetText(const String& text)
2018-11-08 00:21:59 +08:00
{
text_ = text;
2018-11-15 14:35:19 +08:00
dirty_layout_ = true;
2018-11-08 00:21:59 +08:00
}
2018-11-15 14:35:19 +08:00
void Text::SetStyle(const TextStyle& style)
{
2018-11-08 00:21:59 +08:00
style_ = style;
2018-11-15 14:35:19 +08:00
dirty_layout_ = true;
}
2018-11-08 00:21:59 +08:00
void Text::SetFont(const Font & font)
{
2018-11-08 00:21:59 +08:00
font_ = font;
2018-11-15 14:35:19 +08:00
dirty_layout_ = true;
2018-11-08 00:21:59 +08:00
}
void Text::SetFontFamily(const String& family)
2018-11-08 00:21:59 +08:00
{
2018-11-15 14:35:19 +08:00
if (font_.family != family)
{
font_.family = family;
dirty_layout_ = true;
}
}
2018-11-08 00:21:59 +08:00
void Text::SetFontSize(float size)
{
2018-11-15 14:35:19 +08:00
if (font_.size != size)
{
font_.size = size;
dirty_layout_ = true;
}
}
2018-11-15 14:35:19 +08:00
void Text::SetFontWeight(unsigned int weight)
{
2018-11-15 14:35:19 +08:00
if (font_.weight != weight)
{
font_.weight = weight;
dirty_layout_ = true;
}
}
2018-11-15 14:35:19 +08:00
void Text::SetColor(Color const& color)
{
2018-11-08 00:21:59 +08:00
style_.color = color;
}
void Text::SetItalic(bool val)
{
2018-11-15 14:35:19 +08:00
if (font_.italic != val)
{
font_.italic = val;
dirty_layout_ = true;
}
}
2018-11-08 00:21:59 +08:00
void Text::SetWrapEnabled(bool wrap)
{
if (style_.wrap != wrap)
{
style_.wrap = wrap;
2018-11-15 14:35:19 +08:00
dirty_layout_ = true;
2018-11-08 00:21:59 +08:00
}
}
2018-11-08 00:21:59 +08:00
void Text::SetWrapWidth(float wrap_width)
{
if (style_.wrap_width != wrap_width)
{
style_.wrap_width = std::max(wrap_width, 0.f);
2018-11-15 14:35:19 +08:00
dirty_layout_ = true;
2018-11-08 00:21:59 +08:00
}
}
2018-11-08 00:21:59 +08:00
void Text::SetLineSpacing(float line_spacing)
{
if (style_.line_spacing != line_spacing)
{
style_.line_spacing = line_spacing;
2018-11-15 14:35:19 +08:00
dirty_layout_ = true;
2018-11-08 00:21:59 +08:00
}
}
2018-11-15 14:35:19 +08:00
void Text::SetAlignment(TextAlign align)
2018-11-08 00:21:59 +08:00
{
if (style_.alignment != align)
{
style_.alignment = align;
2018-11-15 14:35:19 +08:00
dirty_layout_ = true;
2018-11-08 00:21:59 +08:00
}
}
2018-11-08 00:21:59 +08:00
void Text::SetUnderline(bool underline)
{
if (style_.underline != underline)
{
style_.underline = underline;
2018-11-15 14:35:19 +08:00
dirty_layout_ = true;
2018-11-08 00:21:59 +08:00
}
}
2018-11-08 00:21:59 +08:00
void Text::SetStrikethrough(bool strikethrough)
{
if (style_.strikethrough != strikethrough)
{
style_.strikethrough = strikethrough;
2018-11-15 14:35:19 +08:00
dirty_layout_ = true;
2018-11-08 00:21:59 +08:00
}
2018-08-28 00:06:10 +08:00
}
2018-11-08 00:21:59 +08:00
void Text::SetOutline(bool outline)
2018-08-28 00:06:10 +08:00
{
2018-11-08 00:21:59 +08:00
style_.outline = outline;
2018-08-28 00:06:10 +08:00
}
2018-11-08 00:21:59 +08:00
2018-11-15 14:35:19 +08:00
void Text::SetOutlineColor(Color const&outline_color)
2018-08-28 00:06:10 +08:00
{
2018-11-08 00:21:59 +08:00
style_.outline_color = outline_color;
}
2018-11-08 00:21:59 +08:00
void Text::SetOutlineWidth(float outline_width)
{
style_.outline_width = outline_width;
}
void Text::SetOutlineStroke(StrokeStyle outline_stroke)
{
2018-11-08 00:21:59 +08:00
style_.outline_stroke = outline_stroke;
}
void Text::OnDraw()
{
2018-11-08 00:21:59 +08:00
if (text_layout_)
{
2018-11-15 14:35:19 +08:00
auto graphics = devices::Graphics::Instance();
graphics->SetBrushOpacity(GetDisplayOpacity());
graphics->SetTextStyle(
2018-11-08 00:21:59 +08:00
style_.color,
style_.outline,
style_.outline_color,
style_.outline_width,
style_.outline_stroke
2018-11-08 00:21:59 +08:00
);
2018-11-15 14:35:19 +08:00
graphics->DrawTextLayout(text_layout_);
2018-11-08 00:21:59 +08:00
}
}
void Text::Update(Duration const & dt)
{
UpdateLayout();
Node::Update(dt);
}
void Text::UpdateLayout()
{
2018-11-15 14:35:19 +08:00
if (!dirty_layout_)
return;
2018-11-08 00:21:59 +08:00
SafeRelease(text_format_);
2018-11-15 14:35:19 +08:00
SafeRelease(text_layout_);
if (text_.empty())
return;
auto graphics = devices::Graphics::Instance();
2018-11-08 00:21:59 +08:00
2018-08-28 00:06:10 +08:00
ThrowIfFailed(
2018-11-15 14:35:19 +08:00
graphics->CreateTextFormat(
&text_format_,
font_
2018-08-28 00:06:10 +08:00
)
);
2018-11-08 00:21:59 +08:00
if (style_.line_spacing == 0.f)
{
text_format_->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_DEFAULT, 0, 0);
}
else
{
text_format_->SetLineSpacing(
DWRITE_LINE_SPACING_METHOD_UNIFORM,
style_.line_spacing,
style_.line_spacing * 0.8f
);
}
2018-11-15 14:35:19 +08:00
text_format_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT(style_.alignment));
text_format_->SetWordWrapping(style_.wrap ? DWRITE_WORD_WRAPPING_WRAP : DWRITE_WORD_WRAPPING_NO_WRAP);
2018-11-08 00:21:59 +08:00
if (style_.wrap)
{
ThrowIfFailed(
2018-11-15 14:35:19 +08:00
graphics->CreateTextLayout(
&text_layout_,
text_,
2018-11-08 00:21:59 +08:00
text_format_,
style_.wrap_width
2018-11-08 00:21:59 +08:00
)
);
DWRITE_TEXT_METRICS metrics;
text_layout_->GetMetrics(&metrics);
this->SetSize(metrics.layoutWidth, metrics.height);
2018-11-08 00:21:59 +08:00
}
else
{
ThrowIfFailed(
2018-11-15 14:35:19 +08:00
graphics->CreateTextLayout(
&text_layout_,
text_,
2018-11-08 00:21:59 +08:00
text_format_,
0
2018-11-08 00:21:59 +08:00
)
);
DWRITE_TEXT_METRICS metrics;
text_layout_->GetMetrics(&metrics);
this->SetSize(metrics.width, metrics.height);
2018-11-08 00:21:59 +08:00
SafeRelease(text_layout_);
ThrowIfFailed(
2018-11-15 14:35:19 +08:00
graphics->CreateTextLayout(
&text_layout_,
text_,
2018-11-08 00:21:59 +08:00
text_format_,
2018-11-15 14:35:19 +08:00
metrics.width
2018-11-08 00:21:59 +08:00
)
);
}
DWRITE_TEXT_RANGE range = { 0, static_cast<UINT32>(text_.length()) };
2018-11-08 00:21:59 +08:00
if (style_.underline)
{
text_layout_->SetUnderline(true, range);
2018-11-08 00:21:59 +08:00
}
if (style_.strikethrough)
{
text_layout_->SetStrikethrough(true, range);
2018-11-08 00:21:59 +08:00
}
2018-11-15 14:35:19 +08:00
dirty_layout_ = false;
}
}