dl
dl_locator.h
浏览该文件的文档.
1
12#pragma once
13
14#include "io/dl_log.h"
15#include "math/dl_math.h"
16#include "misc/dl_layout.h"
17
18namespace dl
19{
22{
23public:
25 struct SL
26 {
30 size_t _num;
31 SL() :
32 _start{},
33 _dt{ 32, 0 },
34 _layout{ Layout::X },
35 _num{ 0 }
36 {}
37
38 bool Check()
39 {
40 return (_layout == Layout::X && _dt[0])
41 || (_layout == Layout::Y && _dt[1])
42 || (_layout == Layout::XY && _dt[0] && _dt[1]);
43 }
44 };
48 Locator(const SL& sl = SL()) :
49 _sl{sl},
50 _x{0},
51 _y{0}
52 {
53 if (!_sl.Check())
54 {
55 log_err0("定位器无效!");
56 _sl = SL();
57 }
58 }
64 Position Get(const Size& size, const Position& offset = {})
65 {
66 if (_sl._layout == Layout::X)
67 {
68 unsigned dx = (Float)size[0] / abs(_sl._dt[0]);
69 ++dx;
70 if (_sl._num && dx + _x > _sl._num)
71 {//需要换行
72 ++_y;
73 _x = 0;
74 if (dx > _sl._num)
75 {
76 log_warn0("元素过宽!");
77 }
78 }
79 Position ret = _sl._start + (_sl._dt * toPosition(_x, _y)) + offset;
80 _x += dx;
81 return ret;
82 }
83 else if (_sl._layout == Layout::Y)
84 {
85 unsigned dy = (Float)size[1] / abs(_sl._dt[1]);
86 ++dy;
87 if (_sl._num && dy + _y > _sl._num)
88 {//需要换行
89 ++_x;
90 _y = 0;
91 if (dy > _sl._num)
92 {
93 log_warn0("元素过高!");
94 }
95 }
96 Position ret = _sl._start + (_sl._dt * toPosition(_x, _y)) + offset;
97 _y += dy;
98 return ret;
99 }
100 else if (_sl._layout == Layout::XY)
101 {
102 unsigned dx = (Float)size[0] / abs(_sl._dt[0]);
103 unsigned dy = (Float)size[1] / abs(_sl._dt[1]);
104 Position ret = _sl._start + (_sl._dt * toPosition(_x, _y)) + offset;
105 _x += dx;
106 _y += dy;
107 return ret;
108 }
109 else
110 assert(0);
111 }
112
114 {
115 return Math::CastU(toPosition(_x, _y) * _sl._dt);
116 }
117
118private:
119 SL _sl;
120 size_t _x;//当前x
121 size_t _y;//当前y
122};
123
124}
定位器
Definition: dl_locator.h:22
Position Get(const Size &size, const Position &offset={})
添加一个元素,并返回位置
Definition: dl_locator.h:64
Size GetSize()
Definition: dl_locator.h:113
Locator(const SL &sl=SL())
初始化
Definition: dl_locator.h:48
布局定义
日志系统
数学相关的通用算法
std::array< unsigned, N > CastU(const std::array< T, N > &a, bool show_log=true)
有符号转无符号,并且判断负数
Definition: dl_math.h:89
Definition: dl_array2.h:17
PointU Size
Definition: dl_type_def.h:34
float Float
Definition: dl_type_def.h:37
void log_warn0(std::string_view str, const std::source_location location=std::source_location::current())
constexpr std::array< Position::value_type, N > toPosition(const std::array< T, N > &a)
Position2 Position
Definition: dl_type_def.h:43
void log_err0(std::string_view str, const std::source_location location=std::source_location::current())
Layout
Definition: dl_layout.h:19
@ X
x轴移动
@ Y
y轴移动
@ XY
xy同时移动
创建参数
Definition: dl_locator.h:26
Position _dt
每项偏移
Definition: dl_locator.h:28
Position _start
初始位置
Definition: dl_locator.h:27
size_t _num
换行,0为不换行
Definition: dl_locator.h:30
Layout _layout
布局方向
Definition: dl_locator.h:29