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

301 lines
7.1 KiB
C
Raw Normal View History

2019-12-23 18:05:08 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
2019-12-23 18:05:08 +08:00
// 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:
2020-01-21 10:09:55 +08:00
//
2019-12-23 18:05:08 +08:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
2019-12-23 18:05:08 +08:00
// 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.
#pragma once
#include <kiwano/2d/Actor.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/Color.h>
#include <kiwano/render/TextLayout.h>
2019-12-23 18:05:08 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
KGE_DECLARE_SMART_PTR(TextActor);
/**
* \addtogroup Actors
* @{
*/
/**
* \~chinese
2020-02-10 17:32:04 +08:00
* @brief
2020-01-21 10:09:55 +08:00
*/
class KGE_API TextActor : public Actor
{
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建文本角色
/// @param text 文字内容
2020-02-06 16:54:47 +08:00
static TextActorPtr Create(const String& text);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建文本角色
/// @param text 文字内容
/// @param style 文本样式
2020-02-06 16:54:47 +08:00
static TextActorPtr Create(const String& text, const TextStyle& style);
TextActor();
2020-01-21 10:09:55 +08:00
virtual ~TextActor();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取文本
2020-01-21 10:09:55 +08:00
const String& GetText() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取文本样式
2020-01-21 10:09:55 +08:00
const TextStyle& GetStyle() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取文本布局
2020-01-21 10:09:55 +08:00
const TextLayout& GetLayout() const;
2020-02-11 12:09:59 +08:00
/// \~chinese
/// @brief 获取文本布局
TextLayout& GetLayout();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取文本布局大小
2020-01-21 10:09:55 +08:00
Size GetLayoutSize() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取填充画刷
2020-01-21 10:09:55 +08:00
BrushPtr GetFillBrush() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取描边画刷
2020-01-21 10:09:55 +08:00
BrushPtr GetOutlineBrush() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取字体
2020-01-21 10:09:55 +08:00
FontPtr GetFont() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文本
2020-01-21 10:09:55 +08:00
void SetText(String const& text);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文本样式
2020-01-21 10:09:55 +08:00
void SetStyle(const TextStyle& style);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置字体
2020-01-21 10:09:55 +08:00
void SetFont(FontPtr font);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置字体族
2020-01-21 10:09:55 +08:00
void SetFontFamily(String const& family);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置字号(默认值为 18
2020-01-21 10:09:55 +08:00
void SetFontSize(float size);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置字体粗细值(默认值为 FontWeight::Normal
2020-01-21 10:09:55 +08:00
void SetFontWeight(uint32_t weight);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文字填充画刷
2020-01-21 10:09:55 +08:00
void SetFillBrush(BrushPtr brush);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文字填充颜色(默认值为 Color::White
2020-01-21 10:09:55 +08:00
void SetFillColor(Color const& color);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文字斜体(默认值为 false
2020-01-21 10:09:55 +08:00
void SetItalic(bool italic);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文本自动换行的宽度(默认为 0
2020-01-21 10:09:55 +08:00
void SetWrapWidth(float wrap_width);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置行间距(默认为 0
2020-01-21 10:09:55 +08:00
void SetLineSpacing(float line_spacing);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置对齐方式(默认为 TextAlign::Left
2020-01-21 10:09:55 +08:00
void SetAlignment(TextAlign align);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文字描边画刷
2020-01-21 10:09:55 +08:00
void SetOutlineBrush(BrushPtr brush);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文字描边颜色
2020-01-21 10:09:55 +08:00
void SetOutlineColor(Color const& outline_color);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文字描边线宽
2020-01-21 10:09:55 +08:00
void SetOutlineWidth(float outline_width);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文字描边线相交样式
2020-02-06 16:54:47 +08:00
void SetOutlineStroke(StrokeStylePtr outline_stroke);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置是否显示下划线(默认值为 false
2020-01-21 10:09:55 +08:00
void SetUnderline(bool enable);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置是否显示删除线(默认值为 false
2020-01-21 10:09:55 +08:00
void SetStrikethrough(bool enable);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 更新文字布局
/// @details 文字布局是懒更新的,手动更新文字布局以更新节点状态
2020-01-21 10:09:55 +08:00
void UpdateLayout();
void OnRender(RenderContext& ctx) override;
void OnUpdate(Duration dt) override;
protected:
bool CheckVisibility(RenderContext& ctx) const override;
private:
bool show_underline_;
bool show_strikethrough_;
TextLayout text_layout_;
};
/** @} */
inline const String& TextActor::GetText() const
{
return text_layout_.GetText();
}
inline FontPtr TextActor::GetFont() const
{
return text_layout_.GetStyle().font;
}
inline const TextStyle& TextActor::GetStyle() const
{
return text_layout_.GetStyle();
}
inline const TextLayout& TextActor::GetLayout() const
{
return text_layout_;
}
2020-02-11 12:09:59 +08:00
inline TextLayout& TextActor::GetLayout()
{
return text_layout_;
}
2020-01-21 10:09:55 +08:00
inline Size TextActor::GetLayoutSize() const
{
return text_layout_.GetLayoutSize();
}
inline BrushPtr TextActor::GetFillBrush() const
{
return text_layout_.GetFillBrush();
}
2019-12-23 18:05:08 +08:00
2020-01-21 10:09:55 +08:00
inline BrushPtr TextActor::GetOutlineBrush() const
{
return text_layout_.GetOutlineBrush();
}
inline void TextActor::SetText(String const& text)
{
text_layout_.SetText(text);
}
inline void TextActor::SetStyle(const TextStyle& style)
{
text_layout_.SetStyle(style);
}
inline void TextActor::SetFont(FontPtr font)
{
text_layout_.SetFont(font);
}
inline void TextActor::SetFontFamily(String const& family)
{
text_layout_.SetFontFamily(family);
}
inline void TextActor::SetFontSize(float size)
{
text_layout_.SetFontSize(size);
}
inline void TextActor::SetFontWeight(uint32_t weight)
{
text_layout_.SetFontWeight(weight);
}
inline void TextActor::SetItalic(bool italic)
{
text_layout_.SetItalic(italic);
}
inline void TextActor::SetWrapWidth(float wrap_width)
{
text_layout_.SetWrapWidth(wrap_width);
}
inline void TextActor::SetLineSpacing(float line_spacing)
{
text_layout_.SetLineSpacing(line_spacing);
}
inline void TextActor::SetAlignment(TextAlign align)
{
text_layout_.SetAlignment(align);
}
inline void TextActor::SetUnderline(bool enable)
{
show_underline_ = enable;
}
inline void TextActor::SetStrikethrough(bool enable)
{
show_strikethrough_ = enable;
}
inline void TextActor::SetFillBrush(BrushPtr brush)
{
text_layout_.SetFillBrush(brush);
}
inline void TextActor::SetOutlineBrush(BrushPtr brush)
{
text_layout_.SetOutlineBrush(brush);
}
inline void TextActor::SetOutlineWidth(float outline_width)
{
text_layout_.SetOutlineWidth(outline_width);
}
2020-02-06 16:54:47 +08:00
inline void TextActor::SetOutlineStroke(StrokeStylePtr outline_stroke)
2020-01-21 10:09:55 +08:00
{
text_layout_.SetOutlineStroke(outline_stroke);
2019-12-23 18:05:08 +08:00
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano