YSTest: label.cpp 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
label.cpp
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2011 - 2013.
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/label.h"
29 #include "YSLib/Service/TextLayout.h"
30 #include "YSLib/UI/ywgtevt.h"
31
32 YSL_BEGIN
33
34 YSL_BEGIN_NAMESPACE(UI)
35
36 MLabel::MLabel(const Drawing::Font& fnt, TextAlignment a)
37 : Font(fnt), Margin(Drawing::DefaultMargin),
38 HorizontalAlignment(a), VerticalAlignment(TextAlignment::Center),
39 AutoWrapLine(false), /*AutoSize(false), AutoEllipsis(false),*/ Text()
40 {}
41
42 void
43 MLabel::DrawText(const Size& s, Color c, const PaintContext& e)
44 {
45 Drawing::TextState ts;
46 const Rect bounds(e.Location, s);
47
48 yunseq(ts.Font = Font, ts.Color = c,
49 ts.Margin = FetchMargin(bounds + Margin, e.Target.GetSize())),
50 ts.ResetPen(e.Location, Margin);
51
52 if(!AutoWrapLine)
53 {
54 switch(HorizontalAlignment)
55 {
56 case TextAlignment::Center:
57 case TextAlignment::Right:
58 {
59 SPos horizontal_offset(bounds.Width - GetHorizontalOf(Margin)
60 - FetchStringWidth(ts.Font, Text));
61
62 if(horizontal_offset > 0)
63 {
64 if(HorizontalAlignment == TextAlignment::Center)
65 horizontal_offset /= 2;
66 ts.Pen.X += horizontal_offset;
67 }
68 }
69 case TextAlignment::Left:
70 default:
71 break;
72 }
73 switch(VerticalAlignment)
74 {
75 case TextAlignment::Center:
76 case TextAlignment::Down:
77 {
78 SPos vertical_offset(bounds.Height - GetVerticalOf(Margin)
79 - GetTextLineHeightOf(ts));
80
81 if(vertical_offset > 0)
82 {
83 if(VerticalAlignment == TextAlignment::Center)
84 vertical_offset /= 2;
85 ts.Pen.Y += vertical_offset;
86 }
87 }
88 case TextAlignment::Up:
89 default:
90 break;
91 }
92 }
93 DrawClippedText(e.Target, e.ClipArea & (bounds + Margin), ts, Text,
94 AutoWrapLine);
95 }
96
97
98 void
99 Label::Refresh(PaintEventArgs&& e)
100 {
101 DrawText(GetSizeOf(*this), ForeColor, e);
102 e.ClipArea = Rect(e.Location, GetSizeOf(*this));
103 }
104
105
106 MTextList::MTextList(const shared_ptr<ListType>& h, const Drawing::Font& fnt)
107 : MLabel(fnt),
108 hList(h), tsList(Font)
109 {
110 if(!hList)
111 hList = make_shared<ListType>();
112 }
113
114 MTextList::ItemType*
115 MTextList::GetItemPtr(const IndexType& idx)
116 {
117 auto& lst(GetListRef());
118
119 return IsInInterval<IndexType>(idx, lst.size()) ? &lst[idx] : nullptr;
120 }
121 const MTextList::ItemType*
122 MTextList::GetItemPtr(const IndexType& idx) const
123 {
124 const auto& lst(GetList());
125
126 return IsInInterval<IndexType>(idx, lst.size()) ? &lst[idx] : nullptr;
127 }
128
129 SDst
130 MTextList::GetMaxTextWidth() const
131 {
132 return FetchMaxTextWidth(Font, GetList().cbegin(), GetList().cend());
133 }
134
135 MTextList::IndexType
136 MTextList::Find(const ItemType& text) const
137 {
138 const auto& lst(GetList());
139 const auto i(std::find(lst.begin(), lst.end(), text));
140
141 return i != lst.end() ? i - lst.begin() : IndexType(-1);
142 }
143
144 void
145 MTextList::RefreshTextState()
146 {
147 yunseq(tsList.LineGap = GetVerticalOf(Margin), tsList.Font = Font);
148 }
149
150 YSL_END_NAMESPACE(UI)
151
152 YSL_END
153