92 lines
2.1 KiB
C++
92 lines
2.1 KiB
C++
/**
|
|
* @file dl_tool_ctrl_2d.h
|
|
* @brief 2d地图控制器
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 25-07-16
|
|
*
|
|
* @note 通用的2d地图操作控制器
|
|
*/
|
|
#pragma once
|
|
|
|
#include <dl_type.h>
|
|
|
|
namespace dl::Tool
|
|
{
|
|
//! 2d地图控制器,例如平移、缩放等操作
|
|
class Controller2d
|
|
{
|
|
public:
|
|
bool _bMoveWasd = true;
|
|
float _moveSpeed = 400.0f;
|
|
float _moveSpeedShift = 2.0f;
|
|
|
|
bool _bScaleWheel = true;
|
|
float _scaleSpeed = 5.0f;
|
|
Position _scaleRange = { 0.25f, 2.0f };
|
|
bool _bScaleWithMouse = true; // 随鼠标位置缩放,否则以窗口中心缩放
|
|
std::vector<float> _scaleLevel; // 不为空时使用
|
|
size_t _scaleLevelCur = 0;
|
|
MouseCode _keyDrag = MouseCode::MIDDLE;
|
|
|
|
void Update(Coor* coor)
|
|
{
|
|
if (_bMoveWasd)
|
|
{
|
|
float dt = _moveSpeed * g_time->GetDelta();
|
|
if (g_input->KeyState(KeyCode::LEFT_SHIFT))
|
|
dt *= _moveSpeedShift;
|
|
if (g_input->KeyState(KeyCode::W))
|
|
coor->DtPosition({ 0, dt });
|
|
if (g_input->KeyState(KeyCode::S))
|
|
coor->DtPosition({ 0, -dt });
|
|
if (g_input->KeyState(KeyCode::A))
|
|
coor->DtPosition({ dt, 0 });
|
|
if (g_input->KeyState(KeyCode::D))
|
|
coor->DtPosition({ -dt, 0 });
|
|
}
|
|
if (_bScaleWheel)
|
|
{
|
|
Float y = g_input->GetScrollDelta()[1];
|
|
if (y)
|
|
{
|
|
Position pos0;
|
|
if (_bScaleWithMouse)
|
|
pos0 = toPosition(g_input->GetCursorPosition());
|
|
else
|
|
pos0 = toPosition(g_system->GetWindowSize()) * 0.5f;
|
|
|
|
Position pos_in_map = coor->WorldToThis(pos0);
|
|
|
|
if (_scaleLevel.empty())
|
|
{
|
|
coor->DtScale(-y * _scaleSpeed * g_time->GetDelta());
|
|
float s = std::clamp(coor->GetScale()[0], _scaleRange[0], _scaleRange[1]);
|
|
coor->SetScale(s);
|
|
}
|
|
else
|
|
{
|
|
if (y > 0 && _scaleLevelCur > 0)
|
|
--_scaleLevelCur;
|
|
else if (y < 0 && _scaleLevelCur + 1 < _scaleLevel.size())
|
|
++_scaleLevelCur;
|
|
coor->SetScale(_scaleLevel[_scaleLevelCur]);
|
|
}
|
|
|
|
Position pos1 = coor->ThisToWorld(pos_in_map);
|
|
coor->DtPosition(pos0 - pos1);
|
|
}
|
|
}
|
|
if (_keyDrag != MouseCode::UNKNOWN)
|
|
{
|
|
_drag._key = _keyDrag;
|
|
_drag.Update(coor, true);
|
|
}
|
|
}
|
|
private:
|
|
Cpt::Drag _drag;
|
|
};
|
|
|
|
} |