add history to guides

This commit is contained in:
2019-06-19 17:55:35 +02:00
parent 6058f05d3f
commit 9ad3c351ce
7 changed files with 395 additions and 11 deletions

View File

@@ -433,7 +433,33 @@ void CanvasModeGrid::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
glm::vec2 fb_pos;
if (Canvas::I->point_trace(loc, ro, rd, hit_o, fb_pos, hit_d, m_plane_id))
{
m_lines.push_back({ hit_o, hit_d });
m_action = std::make_unique<ActionModeGrid>();
m_action->m_mode = this;
m_action->m_highlight = m_highlight;
m_action->m_selected_index = m_selected_index;
m_action->m_lines = m_lines;
int select = -1;
for (int i = 0; i < m_lines.size(); i++)
{
auto const& l = m_lines[i];
float d = lines_distance(ro, ro+rd*10.f, l.o-l.d*10.f, l.o+l.d*10.f);
if (d < 0.03f)
select = i;
}
if (select == -1)
{
m_lines.push_back({ hit_o, hit_d });
m_selected_index = (int)m_lines.size() - 1;
m_highlight = false;
m_added = true;
}
else
{
m_selected_index = select;
m_highlight = true;
m_added = false;
}
origin = hit_o;
dir = hit_d;
m_dragging = true;
@@ -443,6 +469,7 @@ void CanvasModeGrid::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
case kEventType::MouseUpL:
node->mouse_release();
m_dragging = false;
ActionManager::add(m_action.release());
//commit();
break;
case kEventType::MouseMove:
@@ -451,7 +478,7 @@ void CanvasModeGrid::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
glm::vec2 hit_fb;
if (m_dragging && Canvas::I->point_trace_plane(loc, ro, rd, hit_o, hit_d, hit_fb, m_plane_id))
{
m_lines.back() = { hit_o, hit_d };
m_lines[m_selected_index] = { hit_o, hit_d };
origin = hit_o;
dir = hit_d;
m_dragging = true;
@@ -459,7 +486,7 @@ void CanvasModeGrid::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
break;
}
case kEventType::MouseCancel:
if (m_dragging)
if (m_dragging && m_selected_index == m_lines.size() - 1)
m_lines.pop_back();
m_dragging = false;
node->mouse_release();
@@ -471,14 +498,16 @@ void CanvasModeGrid::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
void CanvasModeGrid::on_Draw(const glm::mat4& ortho, const glm::mat4& proj, const glm::mat4& camera)
{
//if (m_dragging)
for (auto l : m_lines)
const glm::vec4 blue(0, 0, 1, 1);
const glm::vec4 red(1, 0, 0, 1);
for (int i = 0; i < m_lines.size(); i++)
{
auto const& l = m_lines[i];
auto origin = l.o;
auto dir = l.d;
ShaderManager::use(kShader::Color);
ShaderManager::u_mat4(kShaderUniform::MVP, proj * camera);
ShaderManager::u_vec4(kShaderUniform::Col, {1, 0, 0, 1});
ShaderManager::u_vec4(kShaderUniform::Col, m_highlight && i == m_selected_index ? blue : red);
static glm::vec4 AB[2];
AB[0] = {origin - dir * 10.f, 1};
AB[1] = {origin + dir * 10.f, 1 };
@@ -512,6 +541,41 @@ void CanvasModeGrid::clear()
m_lines.clear();
}
void CanvasModeGrid::on_KeyEvent(KeyEvent* ke)
{
if ((ke->m_key == kKey::KeyBackspace || ke->m_key == kKey::KeyDel)
&& ke->m_type == kEventType::KeyUp)
{
if (m_highlight)
{
auto a = new ActionModeGrid;
a->m_mode = this;
a->m_highlight = m_highlight;
a->m_selected_index = m_selected_index;
a->m_lines = m_lines;
ActionManager::add(a);
m_lines.erase(m_lines.begin() + m_selected_index);
m_highlight = false;
}
}
}
Action* ActionModeGrid::get_redo()
{
auto a = new ActionModeGrid;
a->m_mode = m_mode;
a->m_highlight = m_mode->m_highlight;
a->m_selected_index = m_mode->m_selected_index;
a->m_lines = m_mode->m_lines;
return a;
}
void ActionModeGrid::undo()
{
m_mode->m_highlight = m_highlight;
m_mode->m_selected_index = m_selected_index;
m_mode->m_lines = m_lines;
}
////////////////////////////////////////////////////////////////////
void CanvasModeMaskFree::init()