mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-07-11 08:47:52 -06:00
Refactoring of GLSelectionRectangle
This commit is contained in:
parent
d79a2b8d2d
commit
9d070410c2
3 changed files with 111 additions and 110 deletions
|
@ -1,116 +1,119 @@
|
|||
#include "GLSelectionRectangle.hpp"
|
||||
#include "Camera.hpp"
|
||||
#include "3DScene.hpp"
|
||||
#include "GLCanvas3D.hpp"
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
namespace Slic3r {
|
||||
namespace GUI {
|
||||
|
||||
void GLSelectionRectangle::start_dragging(const Vec2d& mouse_position, float width, float height, EState status)
|
||||
{
|
||||
if (is_dragging() || status == Off)
|
||||
return;
|
||||
void GLSelectionRectangle::start_dragging(const Vec2d& mouse_position, EState state)
|
||||
{
|
||||
if (is_dragging() || (state == Off))
|
||||
return;
|
||||
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
m_status = status;
|
||||
m_start_corner = mouse_position;
|
||||
m_end_corner = mouse_position;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void GLSelectionRectangle::dragging(const Vec2d& mouse_position)
|
||||
{
|
||||
if (is_dragging())
|
||||
m_state = state;
|
||||
m_start_corner = mouse_position;
|
||||
m_end_corner = mouse_position;
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::vector<unsigned int> GLSelectionRectangle::end_dragging(const Camera& camera, const std::vector<Vec3d>& points)
|
||||
{
|
||||
if (!is_dragging())
|
||||
return std::vector<unsigned int>();
|
||||
|
||||
m_status = Off;
|
||||
std::vector<unsigned int> out;
|
||||
|
||||
const std::array<int, 4>& viewport = camera.get_viewport();
|
||||
const Transform3d& modelview_matrix = camera.get_view_matrix();
|
||||
const Transform3d& projection_matrix = camera.get_projection_matrix();
|
||||
|
||||
// bounding box created from the rectangle corners - will take care of order of the corners
|
||||
BoundingBox rectangle(Points{Point(m_start_corner.cast<int>()), Point(m_end_corner.cast<int>())});
|
||||
|
||||
// Iterate over all points and determine whether they're in the rectangle.
|
||||
for (unsigned int i=0; i<points.size(); ++i) {
|
||||
const Vec3d& point = points[i];
|
||||
GLdouble out_x, out_y, out_z;
|
||||
::gluProject((GLdouble)point(0), (GLdouble)point(1), (GLdouble)point(2), (GLdouble*)modelview_matrix.data(), (GLdouble*)projection_matrix.data(), (GLint*)viewport.data(), &out_x, &out_y, &out_z);
|
||||
out_y = m_height - out_y;
|
||||
|
||||
if (rectangle.contains(Point(out_x, out_y)))
|
||||
out.push_back(i);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
void GLSelectionRectangle::dragging(const Vec2d& mouse_position)
|
||||
{
|
||||
if (!is_dragging())
|
||||
return;
|
||||
|
||||
|
||||
|
||||
void GLSelectionRectangle::render() const
|
||||
{
|
||||
float render_color[3];
|
||||
|
||||
switch (m_status) {
|
||||
case Off : return;
|
||||
case SlaSelect : render_color[0] = 0.f;
|
||||
render_color[1] = 1.f;
|
||||
render_color[2] = 0.f;
|
||||
break;
|
||||
case SlaDeselect : render_color[0] = 1.f;
|
||||
render_color[1] = 0.3f;
|
||||
render_color[2] = 0.3f;
|
||||
break;
|
||||
m_end_corner = mouse_position;
|
||||
}
|
||||
|
||||
glsafe(::glColor3fv(render_color));
|
||||
glsafe(::glLineWidth(1.5f));
|
||||
std::vector<unsigned int> GLSelectionRectangle::stop_dragging(const GLCanvas3D& canvas, const std::vector<Vec3d>& points)
|
||||
{
|
||||
std::vector<unsigned int> out;
|
||||
|
||||
glsafe(::glPushAttrib(GL_TRANSFORM_BIT)); // remember current MatrixMode
|
||||
if (!is_dragging())
|
||||
return out;
|
||||
|
||||
glsafe(::glMatrixMode(GL_MODELVIEW)); // cache modelview matrix and set to identity
|
||||
glsafe(::glPushMatrix());
|
||||
glsafe(::glLoadIdentity());
|
||||
m_state = Off;
|
||||
|
||||
glsafe(::glMatrixMode(GL_PROJECTION)); // cache projection matrix and set to identity
|
||||
glsafe(::glPushMatrix());
|
||||
glsafe(::glLoadIdentity());
|
||||
const Camera& camera = canvas.get_camera();
|
||||
const std::array<int, 4>& viewport = camera.get_viewport();
|
||||
const Transform3d& modelview_matrix = camera.get_view_matrix();
|
||||
const Transform3d& projection_matrix = camera.get_projection_matrix();
|
||||
|
||||
glsafe(::glOrtho(0.f, m_width, m_height, 0.f, -1.f, 1.f)); // set projection matrix so that world coords = window coords
|
||||
// bounding box created from the rectangle corners - will take care of order of the corners
|
||||
BoundingBox rectangle(Points{ Point(m_start_corner.cast<int>()), Point(m_end_corner.cast<int>()) });
|
||||
|
||||
// render the selection rectangle (window coordinates):
|
||||
glsafe(::glPushAttrib(GL_ENABLE_BIT));
|
||||
glsafe(::glLineStipple(4, 0xAAAA));
|
||||
glsafe(::glEnable(GL_LINE_STIPPLE));
|
||||
// Iterate over all points and determine whether they're in the rectangle.
|
||||
for (unsigned int i = 0; i<points.size(); ++i) {
|
||||
const Vec3d& point = points[i];
|
||||
GLdouble out_x, out_y, out_z;
|
||||
::gluProject((GLdouble)point(0), (GLdouble)point(1), (GLdouble)point(2), (GLdouble*)modelview_matrix.data(), (GLdouble*)projection_matrix.data(), (GLint*)viewport.data(), &out_x, &out_y, &out_z);
|
||||
out_y = canvas.get_canvas_size().get_height() - out_y;
|
||||
|
||||
::glBegin(GL_LINE_LOOP);
|
||||
::glVertex3f((GLfloat)m_start_corner(0), (GLfloat)m_start_corner(1), (GLfloat)0.5f);
|
||||
::glVertex3f((GLfloat)m_end_corner(0), (GLfloat)m_start_corner(1), (GLfloat)0.5f);
|
||||
::glVertex3f((GLfloat)m_end_corner(0), (GLfloat)m_end_corner(1), (GLfloat)0.5f);
|
||||
::glVertex3f((GLfloat)m_start_corner(0), (GLfloat)m_end_corner(1), (GLfloat)0.5f);
|
||||
glsafe(::glEnd());
|
||||
glsafe(::glPopAttrib());
|
||||
if (rectangle.contains(Point(out_x, out_y)))
|
||||
out.push_back(i);
|
||||
}
|
||||
|
||||
glsafe(::glPopMatrix()); // restore former projection matrix
|
||||
glsafe(::glMatrixMode(GL_MODELVIEW));
|
||||
glsafe(::glPopMatrix()); // restore former modelview matrix
|
||||
glsafe(::glPopAttrib()); // restore former MatrixMode
|
||||
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void GLSelectionRectangle::stop_dragging()
|
||||
{
|
||||
if (!is_dragging())
|
||||
return;
|
||||
|
||||
m_state = Off;
|
||||
}
|
||||
|
||||
void GLSelectionRectangle::render(const GLCanvas3D& canvas) const
|
||||
{
|
||||
if (!is_dragging())
|
||||
return;
|
||||
|
||||
float zoom = canvas.get_camera().zoom;
|
||||
float inv_zoom = (zoom != 0.0f) ? 1.0f / zoom : 0.0f;
|
||||
|
||||
Size cnv_size = canvas.get_canvas_size();
|
||||
float cnv_half_width = 0.5f * (float)cnv_size.get_width();
|
||||
float cnv_half_height = 0.5f * (float)cnv_size.get_height();
|
||||
if ((cnv_half_width == 0.0f) || (cnv_half_height == 0.0f))
|
||||
return;
|
||||
|
||||
Vec2d start(m_start_corner(0) - cnv_half_width, cnv_half_height - m_start_corner(1));
|
||||
Vec2d end(m_end_corner(0) - cnv_half_width, cnv_half_height - m_end_corner(1));
|
||||
|
||||
float left = (float)std::min(start(0), end(0)) * inv_zoom;
|
||||
float top = (float)std::max(start(1), end(1)) * inv_zoom;
|
||||
float right = (float)std::max(start(0), end(0)) * inv_zoom;
|
||||
float bottom = (float)std::min(start(1), end(1)) * inv_zoom;
|
||||
|
||||
glsafe(::glLineWidth(1.5f));
|
||||
float color[3];
|
||||
color[0] = (m_state == Select) ? 0.3f : 1.0f;
|
||||
color[1] = (m_state == Select) ? 1.0f : 0.3f;
|
||||
color[2] = 0.3f;
|
||||
glsafe(::glColor3fv(color));
|
||||
|
||||
glsafe(::glDisable(GL_DEPTH_TEST));
|
||||
|
||||
glsafe(::glPushMatrix());
|
||||
glsafe(::glLoadIdentity());
|
||||
|
||||
glsafe(::glPushAttrib(GL_ENABLE_BIT));
|
||||
glsafe(::glLineStipple(4, 0xAAAA));
|
||||
glsafe(::glEnable(GL_LINE_STIPPLE));
|
||||
|
||||
::glBegin(GL_LINE_LOOP);
|
||||
::glVertex2f((GLfloat)left, (GLfloat)bottom);
|
||||
::glVertex2f((GLfloat)right, (GLfloat)bottom);
|
||||
::glVertex2f((GLfloat)right, (GLfloat)top);
|
||||
::glVertex2f((GLfloat)left, (GLfloat)top);
|
||||
glsafe(::glEnd());
|
||||
|
||||
glsafe(::glPopAttrib());
|
||||
|
||||
glsafe(::glPopMatrix());
|
||||
}
|
||||
|
||||
} // namespace GUI
|
||||
} // namespace Slic3r
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue