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

218 lines
5.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
* @brief <EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD>ɫ
2020-01-21 10:09:55 +08:00
*/
class KGE_API TextActor : public Actor
{
public:
2020-07-22 21:08:48 +08:00
TextActor();
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>ɫ
/// @param text <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2020-07-22 21:08:48 +08:00
TextActor(const String& text);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>ɫ
/// @param text <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param style <20>ı<EFBFBD><C4B1><EFBFBD>ʽ
2020-07-22 21:08:48 +08:00
TextActor(const String& text, const TextStyle& style);
2020-01-21 10:09:55 +08:00
virtual ~TextActor();
/// \~chinese
/// @brief <20><>ȡ<EFBFBD>ı<EFBFBD>
String GetText() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD>ʽ
TextStyle GetStyle() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
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
/// @brief <20><>ȡ<EFBFBD><C8A1>С
2020-02-17 12:06:29 +08:00
Size GetSize() const override;
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD>仭ˢ
2020-01-21 10:09:55 +08:00
BrushPtr GetFillBrush() const;
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD>߻<EFBFBD>ˢ
2020-01-21 10:09:55 +08:00
BrushPtr GetOutlineBrush() const;
2020-02-16 12:53:18 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
2020-02-16 12:53:18 +08:00
StrokeStylePtr GetOutlineStrokeStyle() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
2020-01-21 10:09:55 +08:00
FontPtr GetFont() const;
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
2020-02-19 12:09:50 +08:00
void SetText(const String& text);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>ʽ
2020-01-21 10:09:55 +08:00
void SetStyle(const TextStyle& style);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2020-01-21 10:09:55 +08:00
void SetFont(FontPtr font);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>仭ˢ
2020-01-21 10:09:55 +08:00
void SetFillBrush(BrushPtr brush);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB>Ĭ<EFBFBD><C4AC>ֵΪ Color::White<74><65>
2020-02-19 12:09:50 +08:00
void SetFillColor(const Color& color);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>еĿ<D0B5><C4BF>ȣ<EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ 0<><30>
2020-01-21 10:09:55 +08:00
void SetWrapWidth(float wrap_width);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC>ࣨĬ<E0A3A8><C4AC>Ϊ 0<><30>
2020-01-21 10:09:55 +08:00
void SetLineSpacing(float line_spacing);
/// \~chinese
/// @brief <20><><EFBFBD>ö<EFBFBD><C3B6>ʽ<EBB7BD><CABD>Ĭ<EFBFBD><C4AC>Ϊ TextAlign::Left<66><74>
2020-01-21 10:09:55 +08:00
void SetAlignment(TextAlign align);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߻<EFBFBD>ˢ
2020-01-21 10:09:55 +08:00
void SetOutlineBrush(BrushPtr brush);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
2020-02-19 12:09:50 +08:00
void SetOutlineColor(const Color& outline_color);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
2020-02-16 12:53:18 +08:00
void SetOutlineStrokeStyle(StrokeStylePtr stroke);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ʾ<EFBFBD>»<EFBFBD><C2BB>ߣ<EFBFBD>Ĭ<EFBFBD><C4AC>ֵΪ false<73><65>
2020-01-21 10:09:55 +08:00
void SetUnderline(bool enable);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ʾɾ<CABE><C9BE><EFBFBD>ߣ<EFBFBD>Ĭ<EFBFBD><C4AC>ֵΪ false<73><65>
2020-01-21 10:09:55 +08:00
void SetStrikethrough(bool enable);
2020-02-16 12:53:18 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
2020-02-16 12:53:18 +08:00
void SetTextLayout(TextLayoutPtr layout);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD>
/// @details <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
2020-02-17 12:06:29 +08:00
void UpdateDirtyLayout();
/// \~chinese
/// @brief ǿ<>Ƹ<EFBFBD><C6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD>
/// @details <20><><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µģ<C2B5><C4A3>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD>Ը<EFBFBD><D4B8>½ڵ<C2BD>״̬
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;
void UpdateCachedTexture();
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4>Ⱦģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>»<EFBFBD><C2BB>и<EFBFBD><D0B8>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD>
void SetPreRenderEnabled(bool enable);
2020-01-21 10:09:55 +08:00
private:
bool is_cache_dirty_;
String content_;
TextStyle style_;
TextLayoutPtr layout_;
BrushPtr fill_brush_;
BrushPtr outline_brush_;
StrokeStylePtr outline_stroke_;
TexturePtr texture_cached_;
RenderContextPtr render_ctx_;
2020-01-21 10:09:55 +08:00
};
/** @} */
inline String TextActor::GetText() const
2020-01-21 10:09:55 +08:00
{
2020-08-04 16:31:20 +08:00
return content_;
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 TextStyle TextActor::GetStyle() const
2020-01-21 10:09:55 +08:00
{
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
{
2022-10-12 14:19:33 +08:00
return 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
{
2022-10-12 14:19:33 +08:00
return 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
{
2022-10-12 14:19:33 +08:00
return outline_stroke_;
2020-01-21 10:09:55 +08:00
}
} // namespace kiwano