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 _coor->SetScale(g_ui->GetScale());
41 _xy1 = toPoint(_coor->WorldToThis(toPosition(xy_mouse)));
42 if (_xy0[0] < 0)
43 {
44 _xy0 = _xy1;
45 }
46 else
47 {
48 _bRender = true;
49
50 _txt[0]->SetString(fmt::format("({})", to_string(_xy0)));
51 _txt[1]->SetString(fmt::format("({})", to_string(_xy1)));
52
53 Point xy_dt = _xy1 - _xy0;
54 _txt[2]->SetString(fmt::format("dx: {} dy: {}", xy_dt[0], xy_dt[1]));
55 _txt[3]->SetString(fmt::format("dis: {}", (unsigned)sqrt(Math::DistancePow2(xy_dt))));
56
57 Position size0 = toPosition(_txt[0]->GetSize());
58 Position size1 = toPosition(_txt[1]->GetSize());
59 Position size3 = toPosition(_txt[3]->GetSize());
60
61 Point xy_center = (_xy0 + _xy1) / 2;
62 _txt[0]->SetPositionAlign(toPosition(_xy0), Align::RB);
63 _txt[1]->SetPositionAlign(toPosition(_xy1), Align::LT);
64 _txt[2]->SetPositionAlign(toPosition(xy_center), Align::LB);
65 _txt[3]->SetPositionAlign(toPosition(xy_center), Align::RT);
66 }
67 }
68 else
69 {
70 _xy0 = { -1, -1 };
71 }
72 }
73
74 void Render()
75 {
76 if (_bRender)
77 {
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 Array< T, 2 > &a)
返回 向量 距离的平方
Position2 Position
constexpr Array< Position::value_type, N > toPosition(const Array< T, N > &a)
转换到Position,已知长度
Input * g_input
Factory * g_factory
Graphics * g_gfx
std::string to_string(T v)
to_string包装
constexpr Array< Point::value_type, N > toPoint(const Array< T, N > &a)
转换到Point,已知长度
UI::Manager * g_ui
Point2 Point