Magic_Game/src/core/Text.cpp

340 lines
5.9 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"
2018-11-21 17:18:59 +08:00
#include "Factory.h"
#include "render.h"
#include "include-forwards.h"
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
{
}
2019-01-21 21:24:45 +08:00
Text::Text(String const& text)
2018-11-15 14:35:19 +08:00
: Text(text, text_default_font, text_default_style)
{
}
2019-01-21 21:24:45 +08:00
Text::Text(String const& text, const Font & font)
2018-11-15 14:35:19 +08:00
: Text(text, font, text_default_style)
{
}
2019-01-21 21:24:45 +08:00
Text::Text(String const& text, const TextStyle & style)
2018-11-15 14:35:19 +08:00
: Text(text, text_default_font, style)
{
}
2019-01-21 21:24:45 +08:00
Text::Text(String const& 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
{
2018-11-18 20:26:41 +08:00
UpdateLayout();
2018-11-08 00:21:59 +08:00
}
2018-11-08 00:21:59 +08:00
Text::~Text()
{
}
2019-01-21 21:24:45 +08:00
String const& 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
2019-01-21 21:24:45 +08:00
String const& 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
{
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;
}
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;
}
2019-01-21 21:24:45 +08:00
void Text::SetText(String const& text)
2018-11-08 00:21:59 +08:00
{
text_ = text;
2018-11-18 20:26:41 +08:00
UpdateLayout();
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-18 20:26:41 +08:00
UpdateLayout();
}
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-18 20:26:41 +08:00
UpdateLayout();
2018-11-08 00:21:59 +08:00
}
2019-01-21 21:24:45 +08:00
void Text::SetFontFamily(String const& family)
2018-11-08 00:21:59 +08:00
{
2018-11-15 14:35:19 +08:00
if (font_.family != family)
{
font_.family = family;
2018-11-18 20:26:41 +08:00
UpdateLayout();
2018-11-15 14:35:19 +08:00
}
}
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;
2018-11-18 20:26:41 +08:00
UpdateLayout();
2018-11-15 14:35:19 +08:00
}
}
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;
2018-11-18 20:26:41 +08:00
UpdateLayout();
2018-11-15 14:35:19 +08:00
}
}
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;
2018-11-18 20:26:41 +08:00
UpdateLayout();
2018-11-15 14:35:19 +08:00
}
}
2018-11-08 00:21:59 +08:00
void Text::SetWrapEnabled(bool wrap)
{
if (style_.wrap != wrap)
{
style_.wrap = wrap;
2018-11-18 20:26:41 +08:00
UpdateLayout();
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-18 20:26:41 +08:00
UpdateLayout();
2018-11-08 00:21:59 +08:00
}
}
void Text::SetLineacingPtr(float line_spacing)
2018-11-08 00:21:59 +08:00
{
if (style_.line_spacing != line_spacing)
{
style_.line_spacing = line_spacing;
2018-11-18 20:26:41 +08:00
UpdateLayout();
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-18 20:26:41 +08:00
UpdateLayout();
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-18 20:26:41 +08:00
UpdateLayout();
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-18 20:26:41 +08:00
UpdateLayout();
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;
}
2018-11-21 19:24:18 +08:00
void Text::OnRender()
{
2018-11-08 00:21:59 +08:00
if (text_layout_)
{
auto& rt = RenderSystem::Instance();
rt.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
);
rt.DrawTextLayout(text_layout_);
2018-11-08 00:21:59 +08:00
}
}
void Text::UpdateLayout()
{
text_format_ = nullptr;
text_layout_ = nullptr;
2018-11-15 14:35:19 +08:00
if (text_.empty())
return;
2018-08-28 00:06:10 +08:00
ThrowIfFailed(
Factory::Instance().CreateTextFormat(
text_format_,
2018-11-18 20:26:41 +08:00
font_,
style_
2018-08-28 00:06:10 +08:00
)
);
2018-11-18 20:26:41 +08:00
Size layout_size;
ThrowIfFailed(
Factory::Instance().CreateTextLayout(
2018-11-18 20:26:41 +08:00
text_layout_,
layout_size,
text_,
text_format_,
style_
)
);
2018-11-15 14:35:19 +08:00
2018-11-18 20:26:41 +08:00
this->SetSize(layout_size);
}
}