dl
dl_tool_ruler.h
浏览该文件的文档.
1
12#pragma once
13
14#ifndef DL_DISABLE_RENDER
15#include "render/dl_factory.h"
16#include "render/dl_graphics.h"
17
18namespace dl::Tool
19{
21class Ruler
22{
23public:
28 static Ruler* Create()
29 {
30 Ruler* ret = new Ruler;
31 return ret;
32 }
33
34 void Update()
35 {
36 _bRender = false;
37 if (g_input->KeyState(KeyCode::LEFT_CONTROL))
38 {
39 Point xy_mouse = g_input->GetCursorPosition();
40 _xy1 = toPoint(_coor->WorldToThis(toPosition(xy_mouse)));
41 if (_xy0[0] < 0)
42 {
43 _xy0 = _xy1;
44 }
45 else
46 {
47 _bRender = true;
48
49 _txt[0]->SetString(fmt::format("({})", to_string(_xy0)));
50 _txt[1]->SetString(fmt::format("({})", to_string(_xy1)));
51
52 Point xy_dt = _xy1 - _xy0;
53 _txt[2]->SetString(fmt::format("dx: {} dy: {}", xy_dt[0], xy_dt[1]));
54 _txt[3]->SetString(fmt::format("dis: {}", (unsigned)sqrt(Math::DistancePow2(xy_dt))));
55
56 Position size0 = toPosition(_txt[0]->GetSize());
57 Position size1 = toPosition(_txt[1]->GetSize());
58 Position size3 = toPosition(_txt[3]->GetSize());
59
60 Point xy_center = (_xy0 + _xy1) / 2;
61 _txt[0]->SetPositionAlign(toPosition(_xy0), Align::RB);
62 _txt[1]->SetPositionAlign(toPosition(_xy1), Align::LT);
63 _txt[2]->SetPositionAlign(toPosition(xy_center), Align::LB);
64 _txt[3]->SetPositionAlign(toPosition(xy_center), Align::RT);
65 }
66 }
67 else
68 {
69 _xy0 = { -1, -1 };
70 }
71 }
72
73 void Render()
74 {
75 if (_bRender)
76 {
77 _coor->SetScale(g_ui->GetScale());
78 g_gfx->SetCoor(_coor);
79 g_gfx->Line2D(toPosition(_xy0), toPosition(_xy1));
80 for (auto& iter : _txt)
81 {
82 iter->Render();
83 }
84 }
85 }
86
88 {
89 delete _coor;
90 }
91private:
92 Ruler() :
93 _xy0{ -1, -1 },
94 _bRender{ false }
95 {
96 _coor = Coor::Create();
97 _coor->SetScale(g_ui->GetScale());
98
99 for (auto& iter : _txt)
100 {
101 iter = g_factory->CreateText(0, 16, ColorDef::GREEN);
102 iter->GetCoor()->SetParent(_coor);
103 iter->SetBackground(true);
104 }
105 }
106
107
108 Coor* _coor;
109 std::array<gcText, 4> _txt;//2为dx,dy 3为dis
110 Point _xy0;
111 Point _xy1;
112 bool _bRender;
113};
114
115}
116#endif
static Coor * Create(Coor *parent=nullptr)
void SetScale(const Position &scale)
尺子工具,用于测量像素长度
static Ruler * Create()
构造
资源工厂
图像绘制
constexpr Direction2 LB
constexpr Direction2 RT
constexpr Direction2 LT
constexpr Direction2 RB
constexpr Color GREEN
T DistancePow2(const std::array< T, 2 > &a)
返回 向量 距离的平方
Position2 Position
Input * g_input
Factory * g_factory
Graphics * g_gfx
std::string to_string(T v)
to_string包装
constexpr std::array< Point::value_type, N > toPoint(const std::array< T, N > &a)
转换到Point,已知长度
constexpr std::array< Position::value_type, N > toPosition(const std::array< T, N > &a)
转换到Position,已知长度
UI::Manager * g_ui
Point2 Point