YSTest: Viewer.cpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

Viewer.cpp
浏览该文件的文档.
1 /*
2 © 2014-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_Viewer
30 #include YFM_YSLib_UI_YWidget // for UI::GetSizeOf;
31
32 namespace YSLib
33 {
34
35 namespace UI
36 {
37
38 bool
39 SequenceViewer::Contains(size_t i) const ynothrow
40 {
41 return Length != 0 && IsInInterval<size_t>(i - GetHeadIndex(), Length);
42 }
43
44 size_t
45 SequenceViewer::GetValid(size_t total) const ynothrow
46 {
47 YAssert(total >= head, "Total number of items is less than head index");
48
49 return min<size_t>(total - head, Length);
50 }
51
52 bool
53 SequenceViewer::SetHeadIndex(size_t t, size_t total)
54 {
55 if(t < total && t != head)
56 {
57 if(t == 0)
58 MoveViewerToBegin();
59 else if(total < Length + t)
60 MoveViewerToEnd(total);
61 else
62 head = t;
63 return true;
64 }
65 return {};
66 }
67 bool
68 SequenceViewer::SetSelectedIndex(size_t t, size_t total)
69 {
70 if(t < total && !(t == selected && is_selected))
71 {
72 selected = t;
73 RestrictView();
74 is_selected = true;
75 return true;
76 }
77 return {};
78 }
79
80 bool
81 SequenceViewer::AdjustForContent(size_t total)
82 {
83 if(total != 0)
84 {
85 if(!(selected < total))
86 selected = total - 1;
87 return RestrictView();
88 }
89 else
90 Reset();
91 return true;
92 }
93
94 SequenceViewer&
95 SequenceViewer::IncreaseHead(ptrdiff_t d, size_t total)
96 {
97 // XXX: Conversion to 'ptrdiff_t' might be implementation-defined.
98 auto t(ptrdiff_t(head) + d);
99
100 RestrictInInterval(t, ptrdiff_t(0), ptrdiff_t(total));
101 SetHeadIndex(size_t(t), total);
102 return *this;
103 }
104
105 SequenceViewer&
106 SequenceViewer::IncreaseSelected(ptrdiff_t d, size_t total)
107 {
108 // XXX: Conversion to 'ptrdiff_t' might be implementation-defined.
109 auto t(ptrdiff_t(selected) + d);
110
111 RestrictInInterval(t, ptrdiff_t(0), ptrdiff_t(total));
112 SetSelectedIndex(size_t(t), total);
113 return *this;
114 }
115
116 bool
117 SequenceViewer::MoveViewerToBegin()
118 {
119 if(head)
120 {
121 head = 0;
122 return true;
123 }
124 return {};
125 }
126
127 bool
128 SequenceViewer::MoveViewerToEnd(size_t total)
129 {
130 if(total < Length)
131 return {};
132 head = total - Length;
133 return true;
134 }
135
136 bool
137 SequenceViewer::RestrictSelected()
138 {
139 if(selected < head)
140 selected = head;
141 else if(selected < head + Length)
142 return {};
143 else
144 selected = head + Length - 1;
145 return true;
146 }
147
148 bool
149 SequenceViewer::RestrictView()
150 {
151 if(selected < head)
152 head = selected;
153 else if(selected < head + Length)
154 return {};
155 else
156 head = selected + 1 - Length;
157 return true;
158 }
159
160
161 SDst
162 AMUnitList::GetFullViewHeight() const
163 {
164 return SDst(GetItemHeight() * GetTotal());
165 }
166 SDst
167 AMUnitList::GetItemHeight() const
168 {
169 const auto item_h(GetItemHeightCore());
170
171 if(item_h != 0)
172 return item_h;
173 throw LoggedEvent("Zero item height found.");
174 }
175 SDst
176 AMUnitList::GetItemHeightCore() const
177 {
178 return GetSizeOf(GetUnitRef()).Height;
179 }
180 size_t
181 AMUnitList::GetLastLabelIndexClipped(SPos v_off, SDst height) const
182 {
183 // XXX: Conversion to 'SPos' might be implementation-defined.
184 return vwList.GetHeadIndex() + min(size_t((SPos(height + uTopOffset)
185 - v_off - 1) / SPos(GetItemHeight()) + 1), vwList.GetValid(GetTotal()));
186 }
187 Point
188 AMUnitList::GetUnitLocation(size_t idx) const
189 {
190 // XXX: Conversion to 'SPos' might be implementation-defined.
191 return Point(0, -SPos(uTopOffset) + SPos(GetItemHeight()) * (SPos(idx)
192 - SPos(vwList.GetHeadIndex())));
193 }
194 SDst
195 AMUnitList::GetViewPosition() const
196 {
197 return SDst(GetItemHeight() * vwList.GetHeadIndex() + uTopOffset);
198 }
199
200 SDst
201 AMUnitList::AdjustBottomForHeight(SDst item_h, SDst h)
202 {
203 const auto d(uTopOffset);
204
205 uTopOffset = 0;
206 AdjustViewLengthForHeight(item_h, h);
207 return d;
208 }
209
210 SDst
211 AMUnitList::AdjustOffsetForHeight(SDst h, bool is_top)
212 {
213 if(GetFullViewHeight() > h)
214 {
215 const SDst item_h(GetItemHeight());
216
217 if(YB_UNLIKELY(item_h == 0))
218 return 0;
219 vwList.RestrictSelected();
220 if(is_top)
221 return AdjustBottomForHeight(item_h, h);
222 else
223 {
224 const auto d((h + uTopOffset) % item_h);
225
226 if(d != 0)
227 {
228 const auto tmp(uTopOffset + item_h - d);
229
230 uTopOffset = tmp % item_h;
231 AdjustViewLengthForHeight(item_h, h);
232 // XXX: Conversion to 'ptrdiff_t' might be implementation-defined.
233 vwList.IncreaseHead(ptrdiff_t(tmp / item_h), GetTotal());
234 }
235 return d;
236 }
237 }
238 return 0;
239 }
240
241 void
242 AMUnitList::AdjustViewForContent(SDst h)
243 {
244 if(vwList.AdjustForContent(GetTotal()) && vwList.IsSelected())
245 {
246 AdjustOffsetForHeight(h,
247 vwList.GetSelectedIndex() == vwList.GetHeadIndex());
248 return;
249 }
250 if(GetFullViewHeight() < GetViewPosition() + h)
251 uTopOffset = 0;
252 AdjustViewLengthForHeight(GetItemHeight(), h);
253 }
254
255 void
256 AMUnitList::AdjustViewLengthForHeight(SDst item_h, SDst h)
257 {
258 if(YB_LIKELY(item_h != 0))
259 vwList.Length = size_t((h + uTopOffset + item_h - 1) / item_h);
260 }
261
262 size_t
263 AMUnitList::CheckPoint(const Size& s, const Point& pt)
264 {
265 return Rect(s).Contains(pt) ? size_t((pt.Y + SPos(uTopOffset))
266 / SPos(GetItemHeight())) + vwList.GetHeadIndex() : size_t(-1);
267 }
268
269 void
270 AMUnitList::ResetView()
271 {
272 bool b(vwList.IsSelected());
273
274 vwList.Reset();
275 if(b)
276 vwList.SetSelectedIndex(0, GetTotal());
277 yunseq(uTopOffset = 0, idxShared = size_t(-1));
278 }
279
280 } // namespace UI;
281
282 } // namespace YSLib;
283
ytest::timing::total
auto total(size_t n, _fNow now, _func &&f, _tParams &&...args) -> decltype(now()-now())
测试指定函数执行若干次的总时间。
Definition: timing.hpp:91
YSLib::UI::ynothrow
AView &y ynothrow
Definition: YWidgetView.cpp:47
YSLib::UI::AMUnitList::GetFullViewHeight
SDst GetFullViewHeight() const
取完整视图高。
Definition: Viewer.cpp:162
YSLib::Drawing::GBinaryGroup::Y
_type Y
分量。
Definition: YGDIBase.h:63
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
YSLib::UI::SequenceViewer::MoveViewerToEnd
bool MoveViewerToEnd(size_t total)
移动视图至序列结尾。
Definition: Viewer.cpp:128
YSLib::UI::AMUnitList::GetViewPosition
SDst GetViewPosition() const
取视图顶端竖直位置。
Definition: Viewer.cpp:195
UI
YSLib::UI::AMUnitList::AdjustViewForContent
void AdjustViewForContent(SDst)
按内容大小依次调整视图中选中和首个项目的索引，然后按需调整竖直偏移量。
Definition: Viewer.cpp:242
platform::size_t
YF_API size_t
Definition: FileIO.h:1353
YSLib::UI::AMUnitList::uTopOffset
SDst uTopOffset
列表视图首项目超出上边界的竖直偏移量。
Definition: Viewer.h:201
YSLib::UI::SequenceViewer::SetSelectedIndex
bool SetSelectedIndex(size_t t, size_t total)
设置选中项目的索引。
Definition: Viewer.cpp:68
YSLib::Drawing::Rect::Contains
bool Contains(SPos px, SPos py) const ynothrow
判断点 (px, py) 是否在矩形内或边上。
Definition: YGDIBase.cpp:92
YB_UNLIKELY
#define YB_UNLIKELY(_expr)
分支预测提示。
Definition: ydef.h:464
YSLib::UI::SequenceViewer::IncreaseSelected
SequenceViewer & IncreaseSelected(ptrdiff_t d, size_t total)
按偏移设置选中项目的索引。
Definition: Viewer.cpp:106
YSLib::UI::AMUnitList::vwList
SequenceViewer vwList
列表视图。
Definition: Viewer.h:196
YSLib::LoggedEvent
记录日志的异常事件类。
Definition: YException.h:50
YSLib::Drawing::Point
GBinaryGroup< SPos > Point
屏幕二维点（直角坐标表示）。
Definition: YGDIBase.h:278
YSLib::YAssert
YAssert(!(v< Deref(a)),"Value less than lower bound found.")
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:1022
YSLib::UI::AMUnitList::GetItemHeight
SDst GetItemHeight() const
取项目行高：调用 GetItemHeightCore 取结果。
Definition: Viewer.cpp:167
platform::SDst
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:44
YSLib::UI::SequenceViewer::MoveViewerToBegin
bool MoveViewerToBegin()
移动视图至序列起始。
Definition: Viewer.cpp:117
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
YSLib::i
return i
Definition: YCoreUtilities.h:176
YSLib::UI::SequenceViewer::is_selected
bool is_selected
选中状态。
Definition: Viewer.h:55
YSLib::UI::AMUnitList::GetItemHeightCore
virtual SDst GetItemHeightCore() const
取默认行高：实现。
Definition: Viewer.cpp:176
ystdex::min
const _type & min(const _type &a, const _type &b, _fComp comp=less< _type >())
取较小的元素。
Definition: algorithm.hpp:527
YSLib::UI::SequenceViewer::RestrictView
bool RestrictView()
约束视图包含被选中的元素。
Definition: Viewer.cpp:149
YSLib::UI::AMUnitList::CheckPoint
size_t CheckPoint(const Size &, const Point &)
检查点（相对左上角）是否在选择范围内，
Definition: Viewer.cpp:263
YSLib::UI::SequenceViewer::AdjustForContent
bool AdjustForContent(size_t total)
按序列内容大小依次调整选中和首个项目的索引。
Definition: Viewer.cpp:81
YSLib::Drawing::GBinaryGroup< SPos >
YSLib::UI::AMUnitList::ResetView
void ResetView()
复位视图。
Definition: Viewer.cpp:270
YModules.h
platform_ex::Reset
unsigned long Reset(COMPtr< _iCOM > &ptr) ynothrow
Definition: COM.h:406
YSLib::Drawing::Rect
屏幕标准矩形：表示屏幕矩形区域。
Definition: YGDIBase.h:540
YSLib::UI::SequenceViewer
序列视图。
Definition: Viewer.h:45
YSLib::UI::AMUnitList::idxShared
size_t idxShared
Definition: Viewer.h:203
YSLib::UI::SequenceViewer::Length
size_t Length
视图长度：最大可视项目数。
Definition: Viewer.h:58
YSLib::UI::AMUnitList::AdjustOffsetForHeight
SDst AdjustOffsetForHeight(SDst, bool)
调整列表视图底项目的竖直偏移量为零。
Definition: Viewer.cpp:211
YSLib::UI::SequenceViewer::selected
size_t selected
Definition: Viewer.h:53
YSLib::UI::SequenceViewer::head
size_t head
视图中首个项目的索引，大于序列大小时无效。
Definition: Viewer.h:52
YSLib::UI::AMUnitList::GetLastLabelIndexClipped
size_t GetLastLabelIndexClipped(SPos, SDst) const
Definition: Viewer.cpp:181
YSLib::Drawing::Size
屏幕区域大小。
Definition: YGDIBase.h:293
YSLib::UI::SPos
const IWidget &wgt SPos
Definition: YWidget.h:109
YSLib::UI::AMUnitList::AdjustBottomForHeight
SDst AdjustBottomForHeight(SDst, SDst)
调整列表视图底项目（可能不完全）超出下边界以上的竖直偏移量为零。
Definition: Viewer.cpp:201
CHRLib::d
_tDst * d
Definition: CharacterProcessing.h:139
YSLib::UI::AMUnitList::SDst
SDst
Definition: Viewer.h:246
YSLib::RestrictInInterval
void RestrictInInterval(_type &i, const _type &a, const _type &b) ynothrow
约束整数 i 在左闭右开区间 [a, b) 中。
Definition: YCoreUtilities.h:220
YB_LIKELY
#define YB_LIKELY(_expr)
Definition: ydef.h:463
YSLib::UI::SequenceViewer::SetHeadIndex
bool SetHeadIndex(size_t t, size_t total)
设置视图中首个项目的索引。
Definition: Viewer.cpp:53
YSLib::UI::GetSizeOf
pt pt Y const IWidget &wgt const IWidget &wgt GetSizeOf
无效化：使相对部件的子部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: YWidget.h:161
YSLib::UI::AMUnitList::AdjustViewLengthForHeight
void AdjustViewLengthForHeight(SDst, SDst)
根据指定的单元部件的高和视图可视区域的高调整视图长度。
Definition: Viewer.cpp:256