Trying to add history to the Fill tool
This commit is contained in:
@@ -254,7 +254,8 @@ void CanvasModeGrid::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
case kEventType::MouseMove:
|
||||
{
|
||||
glm::vec3 ro, rd, hit_o, hit_d;
|
||||
if (m_dragging && canvas->point_trace_plane(loc, ro, rd, hit_o, hit_d, m_plane_id))
|
||||
glm::vec2 hit_fb;
|
||||
if (m_dragging && canvas->point_trace_plane(loc, ro, rd, hit_o, hit_d, hit_fb, m_plane_id))
|
||||
{
|
||||
m_lines.back() = { hit_o, hit_d };
|
||||
origin = hit_o;
|
||||
@@ -326,8 +327,34 @@ void CanvasModeFill::init()
|
||||
|
||||
void CanvasModeFill::leave()
|
||||
{
|
||||
canvas->draw_objects(std::bind(&CanvasModeFill::on_Draw, this, glm::mat4(), std::placeholders::_1, std::placeholders::_2));
|
||||
m_points.clear();
|
||||
// canvas->draw_objects(std::bind(&CanvasModeFill::on_Draw, this, glm::mat4(), std::placeholders::_1, std::placeholders::_2));
|
||||
// m_points.clear();
|
||||
}
|
||||
|
||||
// Returns 1 if the lines intersect, otherwise 0. In addition, if the lines
|
||||
// intersect the intersection point may be stored in the floats i_x and i_y.
|
||||
char get_line_intersection(float p0_x, float p0_y, float p1_x, float p1_y,
|
||||
float p2_x, float p2_y, float p3_x, float p3_y, float *i_x, float *i_y)
|
||||
{
|
||||
float s1_x, s1_y, s2_x, s2_y;
|
||||
s1_x = p1_x - p0_x; s1_y = p1_y - p0_y;
|
||||
s2_x = p3_x - p2_x; s2_y = p3_y - p2_y;
|
||||
|
||||
float s, t;
|
||||
s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
|
||||
t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);
|
||||
|
||||
if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
|
||||
{
|
||||
// Collision detected
|
||||
if (i_x != NULL)
|
||||
*i_x = p0_x + (t * s1_x);
|
||||
if (i_y != NULL)
|
||||
*i_y = p0_y + (t * s1_y);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0; // No collision
|
||||
}
|
||||
|
||||
void CanvasModeFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
@@ -336,14 +363,26 @@ void CanvasModeFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
{
|
||||
case kEventType::MouseDownR:
|
||||
{
|
||||
auto drawer = [this](const glm::mat4& camera, const glm::mat4& proj) {
|
||||
ui::ShaderManager::use(ui::kShader::Color);
|
||||
ui::ShaderManager::u_mat4(ui::kShaderUniform::MVP, proj * camera);
|
||||
ui::ShaderManager::u_vec4(ui::kShaderUniform::Col, { node->m_brush.m_tip_color.rgb(), node->m_brush.m_tip_opacity });
|
||||
m_shape.draw_fill();
|
||||
};
|
||||
canvas->draw_objects(std::bind(drawer, std::placeholders::_1, std::placeholders::_2));
|
||||
m_points.clear();
|
||||
if (!m_points.empty())
|
||||
{
|
||||
auto drawer = [this](const glm::mat4& camera, const glm::mat4& proj) {
|
||||
ui::ShaderManager::use(ui::kShader::Color);
|
||||
ui::ShaderManager::u_mat4(ui::kShaderUniform::MVP, proj * camera);
|
||||
ui::ShaderManager::u_vec4(ui::kShaderUniform::Col, { node->m_brush.m_tip_color.rgb(), node->m_brush.m_tip_opacity });
|
||||
m_shape.draw_fill();
|
||||
};
|
||||
std::vector<int> planes;
|
||||
for (auto p : m_dirty_planes)
|
||||
{
|
||||
planes.push_back(p.first);
|
||||
canvas->m_dirty_face[p.first] = true;
|
||||
canvas->m_dirty_box[p.first] = { 0, 0, canvas->m_width, canvas->m_height };
|
||||
}
|
||||
canvas->snap_history(planes);
|
||||
canvas->draw_objects(std::bind(drawer, std::placeholders::_1, std::placeholders::_2));
|
||||
m_points.clear();
|
||||
m_dirty_planes.clear();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case kEventType::MouseDownL:
|
||||
@@ -351,9 +390,11 @@ void CanvasModeFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
node->mouse_capture();
|
||||
m_dragging = true;
|
||||
glm::vec3 ro, rd, hit_o, hit_d;
|
||||
glm::vec2 hit_fb;
|
||||
int plane_id;
|
||||
if (canvas->point_trace(loc, ro, rd, hit_o, hit_d, plane_id))
|
||||
if (canvas->point_trace_plane(loc, ro, rd, hit_o, hit_d, hit_fb, 0))
|
||||
{
|
||||
m_dirty_planes[plane_id]++;
|
||||
ui::Shape::vertex_t v;
|
||||
v.pos = glm::vec4(hit_o, 1);
|
||||
v.uvs = glm::vec2(0);
|
||||
@@ -367,6 +408,11 @@ void CanvasModeFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
m_points.push_back(m_points[0]);
|
||||
m_points.push_back(last);
|
||||
m_points.push_back(v);
|
||||
float isx, isy;
|
||||
if (get_line_intersection(m_points[0].pos.x, m_points[0].pos.y, v.pos.x, v.pos.y, 1, -1, 1, 1, &isx, &isy))
|
||||
{
|
||||
LOG("intersection in %f %f", isx, isy);
|
||||
}
|
||||
}
|
||||
m_shape.update_vertices(m_points.data(), m_points.size());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user