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-02-16 12:53:18 +08:00
|
|
|
|
TextLayoutPtr GetLayout() const;
|
2020-02-11 12:09:59 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
/// \~chinese
|
2020-02-17 12:06:29 +08:00
|
|
|
|
/// @brief 获取大小
|
|
|
|
|
|
Size GetSize() const override;
|
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
|
|
|
|
BrushPtr GetFillBrush() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 获取描边画刷
|
2020-01-21 10:09:55 +08:00
|
|
|
|
BrushPtr GetOutlineBrush() const;
|
|
|
|
|
|
|
2020-02-16 12:53:18 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief 获取描边线条样式
|
|
|
|
|
|
StrokeStylePtr GetOutlineStrokeStyle() const;
|
|
|
|
|
|
|
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
|
|
|
|
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-16 12:53:18 +08:00
|
|
|
|
/// @brief 设置描边线条样式
|
|
|
|
|
|
void SetOutlineStrokeStyle(StrokeStylePtr 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);
|
|
|
|
|
|
|
2020-02-16 12:53:18 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief 设置文本布局
|
|
|
|
|
|
void SetTextLayout(TextLayoutPtr layout);
|
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
/// \~chinese
|
2020-02-17 12:06:29 +08:00
|
|
|
|
/// @brief 更新脏文字布局
|
|
|
|
|
|
/// @details 仅当文字布局脏时更新
|
|
|
|
|
|
void UpdateDirtyLayout();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief 强制更新文字布局
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @details 文字布局是懒更新的,手动更新文字布局以更新节点状态
|
2020-02-17 12:06:29 +08:00
|
|
|
|
void ForceUpdateLayout();
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
void OnRender(RenderContext& ctx) override;
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
2020-02-17 12:06:29 +08:00
|
|
|
|
void Update(Duration dt) override;
|
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
bool CheckVisibility(RenderContext& ctx) const override;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2020-02-16 12:53:18 +08:00
|
|
|
|
TextStyle style_;
|
|
|
|
|
|
TextLayoutPtr layout_;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
|
|
|
|
inline const String& TextActor::GetText() const
|
|
|
|
|
|
{
|
2020-02-17 17:01:12 +08:00
|
|
|
|
KGE_ASSERT(layout_);
|
|
|
|
|
|
return layout_->GetContent();
|
2020-01-21 10:09:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline FontPtr TextActor::GetFont() const
|
|
|
|
|
|
{
|
2020-02-16 12:53:18 +08:00
|
|
|
|
return style_.font;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline const TextStyle& TextActor::GetStyle() const
|
|
|
|
|
|
{
|
2020-02-16 12:53:18 +08:00
|
|
|
|
return style_;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-16 12:53:18 +08:00
|
|
|
|
inline TextLayoutPtr TextActor::GetLayout() const
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
2020-02-16 12:53:18 +08:00
|
|
|
|
return layout_;
|
2020-02-11 12:09:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
inline BrushPtr TextActor::GetFillBrush() const
|
|
|
|
|
|
{
|
2020-02-16 12:53:18 +08:00
|
|
|
|
return style_.fill_brush;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
}
|
2019-12-23 18:05:08 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
inline BrushPtr TextActor::GetOutlineBrush() const
|
|
|
|
|
|
{
|
2020-02-16 12:53:18 +08:00
|
|
|
|
return style_.outline_brush;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-16 12:53:18 +08:00
|
|
|
|
inline StrokeStylePtr TextActor::GetOutlineStrokeStyle() const
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
2020-02-16 12:53:18 +08:00
|
|
|
|
return style_.outline_stroke;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace kiwano
|