dl
dl_tool_ruler.h
浏览该文件的文档.
1
12#pragma once
13
14#ifndef DL_DISABLE_RENDER
15#include "render/dl_factory.h"
16
17namespace dl::Tool
18{
20class Ruler
21{
22public:
27 static Ruler* Create()
28 {
29 Ruler* ret = new Ruler;
30 return ret;
31 }
32
33 void Update()
34 {
35 _bRender = false;
37 {
38 Point xy_mouse = g_input->GetCursorPosition();
39 _xy1 = toPoint(_coor->WorldToThis(toPosition(xy_mouse)));
40 if (_xy0[0] < 0)
41 {
42 _xy0 = _xy1;
43 }
44 else
45 {
46 _bRender = true;
47
48 _txt[0]->SetString(fmt::format("({})", to_string(_xy0)));
49 _txt[1]->SetString(fmt::format("({})", to_string(_xy1)));
50
51 Point xy_dt = _xy1 - _xy0;
52 _txt[2]->SetString(fmt::format("dx: {} dy: {}", xy_dt[0], xy_dt[1]));
53 _txt[3]->SetString(fmt::format("dis: {}", (unsigned)sqrt(Math::DistancePow2(xy_dt))));
54
55 Position size0 = toPosition(_txt[0]->GetSize());
56 Position size1 = toPosition(_txt[1]->GetSize());
57 Position size3 = toPosition(_txt[3]->GetSize());
58
59 Point xy_center = (_xy0 + _xy1) / 2;
60 _txt[0]->SetPositionAlign(toPosition(_xy0), Align::RB);
61 _txt[1]->SetPositionAlign(toPosition(_xy1), Align::LT);
62 _txt[2]->SetPositionAlign(toPosition(xy_center), Align::LB);
63 _txt[3]->SetPositionAlign(toPosition(xy_center), Align::RT);
64 }
65 }
66 else
67 {
68 _xy0 = { -1, -1 };
69 }
70 }
71
72 void Render()
73 {
74 if (_bRender)
75 {
76 _coor->SetScale(g_ui->GetScale());
77 g_gfx->SetCoor(_coor);
78 g_gfx->Line2D(toPosition(_xy0), toPosition(_xy1));
79 for (auto& iter : _txt)
80 {
81 iter->Render();
82 }
83 }
84 }
85
87 {
88 delete _coor;
89 }
90private:
91 Ruler() :
92 _xy0{ -1, -1 },
93 _bRender{ false }
94 {
95 _coor = Coor::Create();
96 _coor->SetScale(g_ui->GetScale());
97
98 for (auto& iter : _txt)
99 {
100 iter = g_factory->CreateText(0, 16, ColorDef::GREEN);
101 iter->GetCoor()->SetParent(_coor);
102 iter->SetBackground(true);
103 }
104 }
105
106
107 Coor* _coor;
108 std::array<gcText, 4> _txt;//2为dx,dy 3为dis
109 Point _xy0;
110 Point _xy1;
111 bool _bRender;
112};
113
114}
115#endif
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 Line2D(const Position2 &p0, const Position2 &p1)
void SetCoor(Coor *coor)
设置变换
Definition: dl_graphics.h:85
bool KeyState(KeyCode key_code)
检测一个按键是否按下。
Definition: dl_input.h:267
Point GetCursorPosition()
返回鼠标位置。限制到窗口范围
Coor * GetCoor()
返回坐标系
Definition: dl_text.h:250
尺子工具,用于测量像素长度
Definition: dl_tool_ruler.h:21
static Ruler * Create()
构造
Definition: dl_tool_ruler.h:27
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 DistancePow2(const std::array< T, 2 > &a)
返回 向量 距离的平方
Definition: dl_math.h:242
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,已知长度
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