YSTest: Label.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

Label.cpp
浏览该文件的文档.
1 /*
2 © 2011-2015 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "YSLib/UI/YModules.h"
29 #include YFM_YSLib_UI_Label
30 #include YFM_YSLib_Service_TextLayout
31 #include YFM_YSLib_UI_YWidgetEvent
32
33 namespace YSLib
34 {
35
36 using namespace Drawing;
37
38 namespace UI
39 {
40
41 MLabel::MLabel(const Drawing::Font& fnt, Color c, TextAlignment a)
42 : ForeColor(c), Font(fnt), HorizontalAlignment(a)
43 {}
44
45 void
46 MLabel::operator()(PaintEventArgs&& e) const
47 {
48 DrawText(GetSizeOf(e.GetSender()), e);
49 }
50
51 Point
52 MLabel::GetAlignedPenOffset(const Size& s) const
53 {
54 Point pt;
55
56 if(!AutoWrapLine)
57 {
58 switch(HorizontalAlignment)
59 {
60 case TextAlignment::Center:
61 case TextAlignment::Right:
62 {
63 // XXX: Conversion to 'SPos' might be implementation-defined.
64 SPos horizontal_offset(SPos(s.Width - GetHorizontalOf(Margin)
65 - FetchStringWidth(Font, Text)));
66
67 if(horizontal_offset > 0)
68 {
69 if(HorizontalAlignment == TextAlignment::Center)
70 horizontal_offset /= 2;
71 pt.X += horizontal_offset;
72 }
73 }
74 case TextAlignment::Left:
75 default:
76 break;
77 }
78 switch(VerticalAlignment)
79 {
80 case TextAlignment::Center:
81 case TextAlignment::Down:
82 {
83 // XXX: Conversion to 'SPos' might be implementation-defined.
84 SPos vertical_offset(SPos(s.Height - GetVerticalOf(Margin)
85 - Font.GetHeight()));
86
87 if(vertical_offset > 0)
88 {
89 if(VerticalAlignment == TextAlignment::Center)
90 vertical_offset /= 2;
91 pt.Y += vertical_offset;
92 }
93 }
94 case TextAlignment::Up:
95 default:
96 break;
97 }
98 }
99 return pt;
100 }
101
102 void
103 MLabel::DrawText(const Size& s, const PaintContext& pc) const
104 {
105 const auto r(Rect(pc.Location, s) + Margin);
106 TextState ts(Font, FetchMargin(r, pc.Target.GetSize()));
107
108 ts.Color = ForeColor,
109 ts.ResetPen(pc.Location, Margin);
110 ts.Pen += GetAlignedPenOffset(s);
111 UpdateClippedText({pc.Target, pc.Location, pc.ClipArea & r}, ts, Text,
112 AutoWrapLine);
113 }
114
115 void
116 MLabel::DefaultUpdateClippedText(const PaintContext& pc, TextState& ts,
117 const String& text, bool auto_wrap_line)
118 {
119 DrawClippedText(pc.Target, pc.ClipArea, ts, text, auto_wrap_line);
120 }
121
122
123 Rect
124 Label::CalculateBounds(const String& text, Rect r, const Drawing::Font& fnt,
125 const Padding& m)
126 {
127 if(r.GetSize() == Size::Invalid)
128 {
129 r.GetSizeRef() = {FetchStringWidth(fnt, text), fnt.GetHeight()};
130 // TODO: Add %operator-=?
131 r = r - m;
132 }
133 return r;
134 }
135
136 void
137 Label::Refresh(PaintEventArgs&& e)
138 {
139 (*this)(std::move(e));
140 }
141
142 unique_ptr<Label>
143 MakeLabel(const String& text, const Rect& r, const Font& fnt,
144 const Padding& m)
145 {
146 auto p(make_unique<Label>(Label::CalculateBounds(text, r, fnt, m), fnt));
147
148 yunseq(p->Margin = m, p->Text = text);
149 return p;
150 }
151
152 void
153 SetupContentsOf(Label& lbl, const String& text, const Rect& r, const Font& fnt,
154 const Padding& m)
155 {
156 SetBoundsOf(lbl, Label::CalculateBounds(text, r, fnt, m));
157 yunseq(lbl.Font = fnt, lbl.Margin = m, lbl.Text = text);
158 }
159
160 } // namespace UI;
161
162 } // namespace YSLib;
163
YSLib::UI::TextAlignment::Down
@ Down
YSLib::Drawing::TextState
文本状态。
Definition: TextBase.h:87
YSLib::Drawing::Color
The boundary is out of the const Rect SDst SPos SPos SPos Color
Definition: YDraw.h:128
YSLib::UI::MLabel::HorizontalAlignment
TextAlignment HorizontalAlignment
文本水平和竖直对齐属性。
Definition: Label.h:85
YSLib::UI::SPos
const IWidget &wgt SPos
Definition: YWidget.h:109
YSLib::Drawing::Size
屏幕区域大小。
Definition: YGDIBase.h:536
YSLib::Drawing::c
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect Color c
Definition: YDraw.h:174
YSLib::Drawing::r
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect & r
Definition: YDraw.h:174
YSLib::Drawing::FetchStringWidth
YB_ATTR_nodiscard YB_PURE SDst FetchStringWidth(const Font &fnt, _tIter s)
取迭代器指定的单行字符串在字体指定、无边界限制时的显示宽度。
Definition: TextLayout.h:198
YSLib::UI::MLabel::VerticalAlignment
TextAlignment VerticalAlignment
Definition: Label.h:86
YSLib::UI::Label::Text
String Text
标签文本。
Definition: Label.h:94
YSLib::UI::SetBoundsOf
void SetBoundsOf(IWidget &wgt, const Rect &r)
Definition: YWidget.cpp:56
YSLib::Drawing::ts
const TextState &ts const TextState &ts const TextState &ts TextState & ts
Definition: TextBase.h:190
YSLib::UI::GetSizeOf
YB_ATTR_nodiscard YB_PURE pt pt Y YB_ATTR_nodiscard YB_PURE const IWidget &wgt const IWidget &wgt GetSizeOf
无效化：使相对部件的子部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: YWidget.h:165
YSLib::Drawing::TextState::Pen
Point Pen
笔坐标。
Definition: TextBase.h:98
YSLib::UI::Label
标签。
Definition: Label.h:143
YSLib::UI::MLabel::UpdateClippedText
Drawing::GBrushUpdater< Drawing::TextState &, const String &, bool > UpdateClippedText
当前使用的剪切文本更新器。
Definition: Label.h:71
YSLib::Drawing::TextState::ResetPen
PDefHOp(TextState &,=, const PenStyle &ps) ImplRet(PenStyle void ResetPen()
赋值：笔样式。
Definition: TextBase.h:145
YSLib::Drawing::Size::Invalid
static const Size Invalid
无效对象。
Definition: YGDIBase.h:542
YSLib::UI::TextAlignment::Up
@ Up
YSLib::UI::MLabel::AutoWrapLine
bool AutoWrapLine
启用自动换行。
Definition: Label.h:91
YSLib::Drawing::TextState::Color
Drawing::Color Color
Definition: TextBase.h:58
YSLib::Text::String
YSLib 标准字符串（使用 UCS-2 作为内部编码）。
Definition: YString.h:47
YSLib::UI::TextAlignment::Center
@ Center
YSLib::Drawing::Padding
空白样式。
Definition: YGDI.h:47
YSLib::UI::MakeLabel
unique_ptr< Label > MakeLabel(const String &text, const Rect &r, const Font &fnt, const Padding &m)
Definition: Label.cpp:143
YSLib::Drawing::GBinaryGroup< SPos >
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:1535
YSLib::UI::Label::Refresh
void Refresh(PaintEventArgs &&) override
刷新：按指定参数绘制界面并更新状态。
Definition: Label.cpp:137
YSLib::UI::Label::CalculateBounds
static Rect CalculateBounds(const String &, Rect, const Drawing::Font &, const Drawing::Padding &={})
计算指定字符串、初始边界、字体和边距的单行文本需要的标签边界。
Definition: Label.cpp:124
YSLib::UI::MLabel::ForeColor
Color ForeColor
默认前景色。
Definition: Label.h:77
YSLib::Drawing::GBinaryGroup::X
_type X
分量。
Definition: YGDIBase.h:305
YSLib::UI::e
void e
Definition: YWidget.h:356
YSLib::Drawing::Rect
屏幕标准矩形：表示屏幕矩形区域。
Definition: YGDIBase.h:788
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:143
YModules.h
YSLib 用户界面模块配置文件。
YSLib::UI::MLabel::Margin
Drawing::Padding Margin
文本和容器的间距。
Definition: Label.h:79
YSLib::UI::MLabel::operator()
void operator()(PaintEventArgs &&) const
描画：使用发送者的大小并调用 DrawText 绘制文本。
Definition: Label.cpp:46
YSLib::UI::SetupContentsOf
YF_API void SetupContentsOf(Label &, const String &, const Rect &=Rect::Invalid, const Drawing::Font &={}, const Drawing::Padding &=Drawing::DefaultMargin *2)
设置指定字符串、初始边界、字体和边距的单行文本需要的标签。
Definition: Label.cpp:153
YSLib::Drawing::DrawText
YF_API void DrawText(const Graphics &g, TextState &ts, const String &str, bool line_wrap)
绘制文本。
Definition: TextRenderer.cpp:246
YSLib::UI::TextAlignment::Left
@ Left
YSLib::UI::PaintEventArgs
部件绘制参数。
Definition: YWidgetEvent.h:330
YSLib::UI::TextAlignment::Right
@ Right
YSLib::UI::MLabel::Text
String Text
标签文本。
Definition: Label.h:94
YSLib::Drawing::GetVerticalOf
YB_ATTR_nodiscard YB_PURE const Padding &m ynothrowv GetVerticalOf
Definition: YGDI.h:151
YSLib::UI::MLabel::MLabel
MLabel(const Drawing::Font &={}, Color=Drawing::ColorSpace::Black, TextAlignment=TextAlignment::Left)
构造：使用指定字体、文本颜色和文本对齐样式。
Definition: Label.cpp:41
YSLib::Drawing::GBinaryGroup::Y
_type Y
Definition: YGDIBase.h:305
YSLib::Drawing::Font
字体：字模，包含字型、样式和大小。
Definition: Font.h:589
YSLib::Drawing::FetchMargin
Padding FetchMargin(const Rect &r, const Size &s) ynothrow
Definition: YGDI.cpp:51
YSLib::Drawing::GetHorizontalOf
YB_ATTR_nodiscard YB_PURE GetHorizontalOf
Definition: YGDI.h:146
platform::p
return p p
Definition: YCLib/FileSystem.h:209
YSLib::Drawing::Font::GetHeight
YB_ATTR_nodiscard YB_PURE FontSize GetHeight() const ynothrow
取字体对应的字符高度。
Definition: Font.cpp:541
platform::SPos
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:43
YSLib::UI::Label::Margin
Drawing::Padding Margin
文本和容器的间距。
Definition: Label.h:79
YSLib::Drawing::DrawClippedText
YF_API void DrawClippedText(const Graphics &g, const Rect &bounds, TextState &ts, const String &str, bool line_wrap)
绘制剪切区域的文本。
Definition: TextRenderer.cpp:224
YSLib::UI::Label::Font
Drawing::Font Font
字体。
Definition: Label.h:78
YSLib::UI::MLabel::DefaultUpdateClippedText
static void DefaultUpdateClippedText(const PaintContext &, Drawing::TextState &, const String &, bool)
绘制剪切文本：使用指定的绘制上下文和文本状态。
Definition: Label.cpp:116
YSLib::UI::TextAlignment
TextAlignment
Definition: Label.h:46
YSLib::UI::MLabel::GetAlignedPenOffset
YB_ATTR_nodiscard YB_PURE Point GetAlignedPenOffset(const Size &) const
按参数指定的边界大小和当前状态计算笔的偏移位置。
Definition: Label.cpp:52
YSLib::a
YB_PURE const _type * a
Definition: YCoreUtilities.h:168