dl
dl_tool_ruler.h
浏览该文件的文档.
1
12#pragma once
13
14#include "render/dl_factory.h"
15
16namespace dl::Tool
17{
19class Ruler
20{
21public:
26 static Ruler* Create()
27 {
28 Ruler* ret = new Ruler;
29 return ret;
30 }
31
32 void Update()
33 {
34 _bRender = false;
36 {
37 Point xy_mouse = g_input->GetCursorPosition();
38 _xy1 = toPoint(_coor->WorldToThis(toPosition(xy_mouse)));
39 if (_xy0[0] < 0)
40 {
41 _xy0 = _xy1;
42 }
43 else
44 {
45 _bRender = true;
46
47 _txt[0]->SetString(fmt::format("({})", to_string(_xy0)));
48 _txt[1]->SetString(fmt::format("({})", to_string(_xy1)));
49
50 Point xy_dt = _xy1 - _xy0;
51 _txt[2]->SetString(fmt::format("dx: {} dy: {}", xy_dt[0], xy_dt[1]));
52 _txt[3]->SetString(fmt::format("dis: {}", (unsigned)sqrt(Math::DisPow2(xy_dt))));
53
54 Position size0 = toPosition(_txt[0]->GetSize());
55 Position size1 = toPosition(_txt[1]->GetSize());
56 Position size3 = toPosition(_txt[3]->GetSize());
57
58 Point xy_center = (_xy0 + _xy1) / 2;
59 _txt[0]->SetPositionAlign(toPosition(_xy0), Align::RB);
60 _txt[1]->SetPositionAlign(toPosition(_xy1), Align::LT);
61 _txt[2]->SetPositionAlign(toPosition(xy_center), Align::LB);
62 _txt[3]->SetPositionAlign(toPosition(xy_center), Align::RT);
63 }
64 }
65 else
66 {
67 _xy0 = { -1, -1 };
68 }
69 }
70
71 void Render()
72 {
73 if (_bRender)
74 {
75 _coor->SetScale(g_ui->GetScale());
76 g_gfx->SetCoor(_coor);
78 for (auto& iter : _txt)
79 {
80 iter->Render();
81 }
82 }
83 }
84
86 {
87 delete _coor;
88 }
89private:
90 Ruler() :
91 _xy0{ -1, -1 },
92 _bRender{ false }
93 {
94 _coor = Coor::Create();
95 _coor->SetScale(g_ui->GetScale());
96
97 for (auto& iter : _txt)
98 {
99 iter = g_factory->CreateText(0, 16, ColorDef::GREEN);
100 iter->GetCoor()->SetParent(_coor);
101 iter->SetBackground(true);
102 }
103 }
104
105
106 Coor* _coor;
107 std::array<gcText, 4> _txt;//2为dx,dy 3为dis
108 Point _xy0;
109 Point _xy1;
110 bool _bRender;
111};
112
113}
void SetParent(Coor *coor)
Position WorldToThis(Position pos) const
将点 从 世界坐标系 转到 自身坐标系
static Coor * Create(Coor *parent=nullptr)
void SetScale(const Position &scale)
gcText CreateText(size_t font_id=0, unsigned font_size=16, const Color &col=ColorDef::WHITE, unsigned font_outline=0, const Color &col_outline=ColorDef::BLACK, unsigned font_shadow=0, const Color &col_shadow=ColorDef::BLACK)
创建一个文本
void Line(const Position3 &p0, const Position3 &p1)
void SetCoor(Coor *coor)
设置变换
Definition: dl_graphics.h:156
bool KeyState(KeyCode key_code)
检测一个按键是否按下。
Definition: dl_input.h:262
Point GetCursorPosition()
返回鼠标位置。限制到窗口范围
Coor * GetCoor()
返回坐标系
Definition: dl_text.h:250
尺子工具,用于测量像素长度
Definition: dl_tool_ruler.h:20
static Ruler * Create()
构造
Definition: dl_tool_ruler.h:26
float GetScale()
返回ui缩放
资源工厂
constexpr Direction2 LB
Definition: dl_align.h:35
constexpr Direction2 RT
Definition: dl_align.h:41
constexpr Direction2 LT
Definition: dl_align.h:33
constexpr Direction2 RB
Definition: dl_align.h:43
constexpr Color GREEN
Definition: dl_color.h:253
T DisPow2(const std::array< T, 2 > &a)
返回 向量 距离的平方
Definition: dl_math.h:230
std::string to_string(const SpriteID &id)
Definition: dl_sprite.h:50
Input * g_input
Factory * g_factory
Graphics * g_gfx
constexpr std::array< Point::value_type, N > toPoint(const std::array< T, N > &a)
转换到Point,已知长度
constexpr std::array< T, N+1 > toLonger(const std::array< T, N > &a, const T &v={})
长度+1
Point2 Point
Definition: dl_type_def.h:23
constexpr std::array< Position::value_type, N > toPosition(const std::array< T, N > &a)
转换到Position,已知长度
Position2 Position
Definition: dl_type_def.h:43
UI::Manager * g_ui