Files
dl_old/dl/tool/dl_tool_ruler.h
T
2023-12-03 20:36:43 +08:00

114 lines
2.1 KiB
C++

/**
* @file dl_tool_ruler.h
* @brief 尺子,单位像素
*
*
* @version 1.0
* @author lveyou
* @date 22-03-04
*
* @note 按下ctrl开始
*/
#pragma once
#include "render/dl_factory.h"
namespace dl::Tool
{
//! 尺子工具,用于测量像素长度
class Ruler
{
public:
/**
* @brief 构造
* @note 使用0号字体
*/
static Ruler* Create()
{
Ruler* ret = new Ruler;
return ret;
}
void Update()
{
_bRender = false;
if (g_input->KeyState(KeyCode::LEFT_CONTROL))
{
Point xy_mouse = g_input->GetCursorPosition();
_xy1 = toPoint(_coor->WorldToThis(toPosition(xy_mouse)));
if (_xy0[0] < 0)
{
_xy0 = _xy1;
}
else
{
_bRender = true;
_txt[0]->SetString(fmt::format("({})", to_string(_xy0)));
_txt[1]->SetString(fmt::format("({})", to_string(_xy1)));
Point xy_dt = _xy1 - _xy0;
_txt[2]->SetString(fmt::format("dx: {} dy: {}", xy_dt[0], xy_dt[1]));
_txt[3]->SetString(fmt::format("dis: {}", (unsigned)sqrt(Math::DisPow2(xy_dt))));
Position size0 = toPosition(_txt[0]->GetSize());
Position size1 = toPosition(_txt[1]->GetSize());
Position size3 = toPosition(_txt[3]->GetSize());
Point xy_center = (_xy0 + _xy1) / 2;
_txt[0]->SetPositionAlign(toPosition(_xy0), Align::RB);
_txt[1]->SetPositionAlign(toPosition(_xy1), Align::LT);
_txt[2]->SetPositionAlign(toPosition(xy_center), Align::LB);
_txt[3]->SetPositionAlign(toPosition(xy_center), Align::RT);
}
}
else
{
_xy0 = { -1, -1 };
}
}
void Render()
{
if (_bRender)
{
_coor->SetScale(g_ui->GetScale());
g_gfx->SetCoor(_coor);
g_gfx->Line(toLonger(toPosition(_xy0)), toLonger(toPosition(_xy1)));
for (auto& iter : _txt)
{
iter->Render();
}
}
}
~Ruler()
{
delete _coor;
}
private:
Ruler() :
_xy0{ -1, -1 },
_bRender{ false }
{
_coor = Coor::Create();
_coor->SetScale(g_ui->GetScale());
for (auto& iter : _txt)
{
iter = g_factory->CreateText(0, 16, ColorDef::GREEN);
iter->GetCoor()->SetParent(_coor);
iter->SetBackground(true);
}
}
Coor* _coor;
std::array<gcText, 4> _txt;//2为dx,dy 3为dis
Point _xy0;
Point _xy1;
bool _bRender;
};
}