Advertisement
FatalCatharsis

PlotWidget.cpp

Apr 13th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "PlotWidget.h"
  2.  
  3. const float PI = 3.141592f;
  4.  
  5. PlotWidget::PlotWidget(QWidget *plot, QWidget *parent) :
  6.     QWidget(parent),
  7.     m_Plot(plot)
  8. {
  9.     plot->setParent(this);
  10.     plot->setMouseTracking(false);
  11.     QSizePolicy pol;
  12.     pol.setVerticalPolicy(QSizePolicy::MinimumExpanding);
  13.     pol.setHorizontalPolicy(QSizePolicy::MinimumExpanding);
  14.     setSizePolicy(pol);
  15. }
  16.  
  17. void PlotWidget::resizeEvent(QResizeEvent *)
  18. {
  19.     if(width() > height())
  20.         resize(height(), height());
  21.     else
  22.         resize(width(), width());
  23.  
  24.     m_Plot->resize(width(), height());
  25. }
  26.  
  27.  
  28. PlotWindow::PlotWindow(std::vector<std::pair<Point, vanim::comp> >& vec, QWindow *parent) :
  29.     QWindow(parent),
  30.     m_Points(vec),
  31.     m_Selection(-1),
  32.     m_EditMode(COMPONENT_EDITOR),
  33.     m_KeyframeSelection(-1)
  34. {
  35.     this->setSurfaceType(OpenGLSurface);
  36.  
  37.     QSurfaceFormat format;
  38.     format.setDepthBufferSize(24);
  39.     format.setMajorVersion(3);
  40.     format.setMinorVersion(0);
  41.     format.setSamples(4);
  42.     format.setProfile(QSurfaceFormat::CompatibilityProfile);
  43.     this->setFormat(format);
  44.     this->create();
  45.  
  46.     m_Context = new QOpenGLContext;
  47.     m_Context->setFormat(format);
  48.  
  49.     if(!m_Context->create())
  50.     {
  51.         qDebug() << "Failed to Initialize OpenGL : 1";
  52.  
  53.         exit(1);
  54.     }
  55.  
  56.     qDebug() << "OpenGL Info:";
  57.     format = m_Context->format();
  58.     qDebug() << format.majorVersion() << "  " << format.minorVersion();
  59.  
  60.     m_Context->makeCurrent(this);
  61.  
  62.  
  63.     gl = m_Context->versionFunctions<QOpenGLFunctions_3_0>();
  64.  
  65.     if(!gl)
  66.     {
  67.         exit(1);
  68.     }
  69.  
  70.     gl->initializeOpenGLFunctions();
  71.  
  72.  
  73.     gl->glShadeModel(GL_SMOOTH);
  74.     gl->glClearColor(0.7f, 0.7f, 0.7f, 1.0f);
  75.  
  76.     m_Timer = new QTimer(this);
  77.     connect(m_Timer, SIGNAL(timeout()), this, SLOT(draw()));
  78.     m_Timer->start(16);
  79.  
  80.     QImage img;
  81.     if(!img.load("point.png"))
  82.     {
  83.         std::cout << "ERROR: Could not find \"point.png\"\n";
  84.         exit(1);
  85.     }
  86.  
  87.     QImage gltex = QGLWidget::convertToGLFormat(img);
  88.  
  89.     if(gltex.isNull())
  90.         exit(1);
  91.  
  92.     gl->glGenTextures(1, &m_PointTexture);
  93.     gl->glBindTexture(GL_TEXTURE_2D, m_PointTexture);
  94.  
  95.     gl->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gltex.width(), gltex.height(),
  96.                      0, GL_RGBA, GL_UNSIGNED_BYTE, gltex.bits());
  97.  
  98.     gl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  99.     gl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  100.  
  101. }
  102.  
  103. PlotWindow::~PlotWindow()
  104. {}
  105.  
  106. void PlotWindow::draw()
  107. {
  108.     const int pointWidthP = 3;
  109.     const int selectWidthP = 4;
  110.     float pointWidth = pointWidthP / static_cast<float>(width());
  111.     float selectWidth = selectWidthP/ static_cast<float>(width());
  112.  
  113.     const int handleWidthP = 5;
  114.     float handleWidth = handleWidthP / static_cast<float>(width());
  115.  
  116.     QColor handleColors[4];
  117.  
  118.     handleColors[0].setRgbF(1.0, 0.0, 0.0);
  119.     handleColors[1].setRgbF(0.0, 0.0, 1.0);
  120.     handleColors[2].setRgbF(0.0, 1.0, 0.0);
  121.     handleColors[3].setRgbF(1.0, 1.0, 0.0);
  122.  
  123.     m_Context->makeCurrent(this);
  124.  
  125.     gl->glMatrixMode(GL_PROJECTION);
  126.     gl->glLoadIdentity();
  127.     gl->glOrtho(0.0, 1.0, 1.0, 0.0, 0.0, 1.0);
  128.     gl->glMatrixMode(GL_MODELVIEW);
  129.     gl->glDisable(GL_DEPTH_TEST);
  130.  
  131.     gl->glClear(GL_COLOR_BUFFER_BIT);
  132.  
  133.     glEnable(GL_BLEND);
  134.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  135.     glEnable(GL_TEXTURE_2D);
  136.  
  137.  
  138.     gl->glColor4f(1.0, 1.0f, 1.0f, 1.0f);
  139.     float s;
  140.     float c;
  141.     float px;
  142.     float py;
  143.     float cx;
  144.     float cy;
  145.  
  146.     for(uint i = 0; i < m_Points.size(); i++)
  147.     {
  148.         s = sin(m_Points[i].first.rot);
  149.         c = cos(m_Points[i].first.rot);
  150.         cx = m_Points[i].first.x;
  151.         cy = m_Points[i].first.y;
  152.  
  153.         gl->glBegin(GL_QUADS);
  154.             gl->glTexCoord2f(m_Points[i].first.texSX, m_Points[i].first.texSY);
  155.             px = (m_Points[i].first.x - m_Points[i].first.width) - cx;
  156.             py = (m_Points[i].first.y - m_Points[i].first.length) - cy;
  157.             gl->glVertex2f(((px * c) - (py * s)) + cx, ((px * s) + (py * c)) + cy);
  158.  
  159.             gl->glTexCoord2f(m_Points[i].first.texEX, m_Points[i].first.texSY);
  160.             px = (m_Points[i].first.x + m_Points[i].first.width) - cx;
  161.             py = (m_Points[i].first.y - m_Points[i].first.length) - cy;
  162.             gl->glVertex2f(((px * c) - (py * s)) + cx, ((px * s) + (py * c)) + cy);
  163.  
  164.             gl->glTexCoord2f(m_Points[i].first.texEX, m_Points[i].first.texEY);
  165.             px = (m_Points[i].first.x + m_Points[i].first.width) - cx;
  166.             py = (m_Points[i].first.y + m_Points[i].first.length) - cy;
  167.             gl->glVertex2f(((px * c) - (py * s)) + cx, ((px * s) + (py * c)) + cy);
  168.  
  169.             gl->glTexCoord2f(m_Points[i].first.texSX, m_Points[i].first.texEY);
  170.             px = (m_Points[i].first.x - m_Points[i].first.width) - cx;
  171.             py = (m_Points[i].first.y + m_Points[i].first.length) - cy;
  172.             gl->glVertex2f(((px * c) - (py * s)) + cx, ((px * s) + (py * c)) + cy);
  173.         gl->glEnd();
  174.     }
  175.  
  176.     gl->glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
  177.     if(m_Selection != -1)
  178.     {
  179.         gl->glColor4f(m_Rot.color.redF(), m_Rot.color.greenF(),
  180.                       m_Rot.color.blueF(), m_Rot.color.alphaF());
  181.         gl->glBegin(GL_QUADS);
  182.                 gl->glTexCoord2f(0.0f, 0.0f);
  183.                 gl->glVertex2f(m_Rot.x - handleWidth, m_Rot.y - handleWidth);
  184.                 gl->glTexCoord2f(1.0f, 0.0f);
  185.                 gl->glVertex2f(m_Rot.x + handleWidth, m_Rot.y - handleWidth);
  186.                 gl->glTexCoord2f(1.0f, 1.0f);
  187.                 gl->glVertex2f(m_Rot.x + handleWidth, m_Rot.y + handleWidth);
  188.                 gl->glTexCoord2f(0.0f, 1.0f);
  189.                 gl->glVertex2f(m_Rot.x - handleWidth, m_Rot.y + handleWidth);
  190.         gl->glEnd();
  191.  
  192.         gl->glColor4f(m_ScaleX.color.redF(), m_ScaleX.color.greenF(),
  193.                       m_ScaleX.color.blueF(), m_ScaleX.color.alphaF());
  194.         gl->glBegin(GL_QUADS);
  195.                 gl->glTexCoord2f(0.0f, 0.0f);
  196.                 gl->glVertex2f(m_ScaleX.x - handleWidth, m_ScaleX.y - handleWidth);
  197.                 gl->glTexCoord2f(1.0f, 0.0f);
  198.                 gl->glVertex2f(m_ScaleX.x + handleWidth, m_ScaleX.y - handleWidth);
  199.                 gl->glTexCoord2f(1.0f, 1.0f);
  200.                 gl->glVertex2f(m_ScaleX.x + handleWidth, m_ScaleX.y + handleWidth);
  201.                 gl->glTexCoord2f(0.0f, 1.0f);
  202.                 gl->glVertex2f(m_ScaleX.x - handleWidth, m_ScaleX.y + handleWidth);
  203.         gl->glEnd();
  204.  
  205.         gl->glColor4f(m_ScaleY.color.redF(), m_ScaleY.color.greenF(),
  206.                       m_ScaleY.color.blueF(), m_ScaleY.color.alphaF());
  207.         gl->glBegin(GL_QUADS);
  208.                 gl->glTexCoord2f(0.0f, 0.0f);
  209.                 gl->glVertex2f(m_ScaleY.x - handleWidth, m_ScaleY.y - handleWidth);
  210.                 gl->glTexCoord2f(1.0f, 0.0f);
  211.                 gl->glVertex2f(m_ScaleY.x + handleWidth, m_ScaleY.y - handleWidth);
  212.                 gl->glTexCoord2f(1.0f, 1.0f);
  213.                 gl->glVertex2f(m_ScaleY.x + handleWidth, m_ScaleY.y + handleWidth);
  214.                 gl->glTexCoord2f(0.0f, 1.0f);
  215.                 gl->glVertex2f(m_ScaleY.x - handleWidth, m_ScaleY.y + handleWidth);
  216.         gl->glEnd();
  217.  
  218.         if(m_EditMode == COMPONENT_EDITOR)
  219.         {
  220.             gl->glColor4f(m_Offset.color.redF(), m_Offset.color.greenF(),
  221.                           m_Offset.color.blueF(), m_Offset.color.alphaF());
  222.             gl->glBegin(GL_QUADS);
  223.                     gl->glTexCoord2f(0.0f, 0.0f);
  224.                     gl->glVertex2f(m_Offset.x - handleWidth, m_Offset.y - handleWidth);
  225.                     gl->glTexCoord2f(1.0f, 0.0f);
  226.                     gl->glVertex2f(m_Offset.x + handleWidth, m_Offset.y - handleWidth);
  227.                     gl->glTexCoord2f(1.0f, 1.0f);
  228.                     gl->glVertex2f(m_Offset.x + handleWidth, m_Offset.y + handleWidth);
  229.                     gl->glTexCoord2f(0.0f, 1.0f);
  230.                     gl->glVertex2f(m_Offset.x - handleWidth, m_Offset.y + handleWidth);
  231.             gl->glEnd();
  232.         }
  233.  
  234.         gl->glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
  235.         gl->glBegin(GL_QUADS);
  236.             gl->glTexCoord2f(0.0f, 0.0f);
  237.             gl->glVertex2f(m_Points[m_Selection].first.x - selectWidth, m_Points[m_Selection].first.y - selectWidth);
  238.             gl->glTexCoord2f(1.0f, 0.0f);
  239.             gl->glVertex2f(m_Points[m_Selection].first.x + selectWidth, m_Points[m_Selection].first.y - selectWidth);
  240.             gl->glTexCoord2f(1.0f, 1.0f);
  241.             gl->glVertex2f(m_Points[m_Selection].first.x + selectWidth, m_Points[m_Selection].first.y + selectWidth);
  242.             gl->glTexCoord2f(0.0f, 1.0f);
  243.             gl->glVertex2f(m_Points[m_Selection].first.x - selectWidth, m_Points[m_Selection].first.y + selectWidth);
  244.         gl->glEnd();
  245.     }
  246.  
  247.     gl->glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
  248.     for(uint i = 0; i < m_Points.size(); i++)
  249.     {
  250.         gl->glBegin(GL_QUADS);
  251.             gl->glTexCoord2f(0.0f, 0.0f);
  252.             gl->glVertex2f(m_Points[i].first.x - pointWidth, m_Points[i].first.y - pointWidth);
  253.             gl->glTexCoord2f(1.0f, 0.0f);
  254.             gl->glVertex2f(m_Points[i].first.x + pointWidth, m_Points[i].first.y - pointWidth);
  255.             gl->glTexCoord2f(1.0f, 1.0f);
  256.             gl->glVertex2f(m_Points[i].first.x + pointWidth, m_Points[i].first.y + pointWidth);
  257.             gl->glTexCoord2f(0.0f, 1.0f);
  258.             gl->glVertex2f(m_Points[i].first.x - pointWidth, m_Points[i].first.y + pointWidth);
  259.         gl->glEnd();
  260.     }
  261.  
  262.     glDisable(GL_BLEND);
  263.     glDisable(GL_TEXTURE_2D);
  264.  
  265.     m_Context->swapBuffers(this);
  266. }
  267.  
  268. void PlotWindow::resizeEvent(QResizeEvent* event)
  269. {
  270.     QSize newSize = event->size();
  271.     gl->glViewport(0, 0, newSize.width(), newSize.height());
  272. }
  273.  
  274. void PlotWindow::mouseDoubleClickEvent(QMouseEvent * event)
  275. {
  276.     Point add;
  277.     vanim::comp newComp;
  278.  
  279.     if(m_EditMode == COMPONENT_EDITOR)
  280.     {
  281.         add.x = static_cast<float>(event->x()) / this->width();
  282.         add.y = static_cast<float>(event->y()) / this->height();
  283.         add.texSX = 0;
  284.         add.texSY = 0;
  285.         add.texEX = 0;
  286.         add.texEY = 0;
  287.         add.rot = 0;
  288.         add.length = 0;
  289.         add.width = 0;
  290.         add.offx = 0;
  291.         add.offy = 0;
  292.  
  293.         newComp.x = add.x;
  294.         newComp.y = add.y;
  295.         newComp.texSX = add.texSX;
  296.         newComp.texSY = add.texSY;
  297.         newComp.texEX = add.texEX;
  298.         newComp.texEY = add.texEY;
  299.         newComp.rot = add.rot;
  300.         newComp.length = add.length;
  301.         newComp.width = add.width;
  302.         newComp.offx = add.offx;
  303.         newComp.offy = add.offy;
  304.  
  305.         m_Points.push_back({add, newComp});
  306.     }
  307. }
  308.  
  309. void PlotWindow::mousePressEvent(QMouseEvent * event)
  310. {
  311.     const int pointWidthP = 3;
  312.     float pointWidth = pointWidthP / static_cast<float>(width());
  313.  
  314.     float handleDist = handleDistP / static_cast<float>(width());
  315.     const int handleWidthP = 5;
  316.     float handleWidth = handleWidthP / static_cast<float>(width());
  317.  
  318.     m_PosBefore = event->pos();
  319.  
  320.     for(uint i = 0; i < m_Points.size(); i++)
  321.     {
  322.         float d = sqrt(pow((static_cast<float>(event->x())/ this->width()) - m_Points[i].first.x, 2) +
  323.                        pow((static_cast<float>(event->y())/ this->height()) - m_Points[i].first.y, 2));
  324.  
  325.         float dRotHandle = sqrt(pow((static_cast<float>(event->x())/ this->width()) - m_Rot.x, 2) +
  326.                                 pow((static_cast<float>(event->y())/ this->height()) - m_Rot.y, 2));
  327.  
  328.         float dScaleXHandle = sqrt(pow((static_cast<float>(event->x())/ this->width()) - m_ScaleX.x, 2) +
  329.                                    pow((static_cast<float>(event->y())/ this->height()) - m_ScaleX.y, 2));
  330.  
  331.         float dScaleYHandle = sqrt(pow((static_cast<float>(event->x())/ this->width()) - m_ScaleY.x, 2) +
  332.                                    pow((static_cast<float>(event->y())/ this->height()) - m_ScaleY.y, 2));
  333.  
  334.         float dOffsetHandle = sqrt(pow((static_cast<float>(event->x())/ this->width()) - m_Offset.x, 2) +
  335.                                    pow((static_cast<float>(event->y())/ this->height()) - m_Offset.y, 2));
  336.  
  337.  
  338.         if(dRotHandle < handleWidth)
  339.         {
  340.             m_Rot.isPressed = true;
  341.             m_Rot.color.setRgbF(0.8, 0.0f, 0.0f);
  342.             return;
  343.         }
  344.         else if(dScaleXHandle < handleWidth)
  345.         {
  346.             m_ScaleX.isPressed = true;
  347.             m_ScaleX.color.setRgbF(0.0f, 0.0f, 0.8f);
  348.             return;
  349.         }
  350.         else if(dScaleYHandle < handleWidth)
  351.         {
  352.             m_ScaleY.isPressed = true;
  353.             m_ScaleY.color.setRgbF(0.0f, 0.8f, 0.0f);
  354.             return;
  355.         }
  356.         else if(dOffsetHandle < handleWidth)
  357.         {
  358.             if(m_EditMode == COMPONENT_EDITOR)
  359.             {
  360.                 m_Offset.isPressed = true;
  361.                 m_Offset.color.setRgbF(0.8f, 0.8f, 0.0f);
  362.                 return;
  363.             }
  364.         }
  365.  
  366.         if(d < pointWidth)
  367.         {
  368.             m_Selection = i;
  369.             m_SelectionPressed = true;
  370.  
  371.             m_Rot.isPressed = false;
  372.             m_Rot.color.setRgbF(1.0f, 0.0f, 0.0f);
  373.             m_Rot.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot + 3*PI/2);
  374.             m_Rot.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot + 3*PI/2);
  375.  
  376.             m_ScaleX.isPressed = false;
  377.             m_ScaleX.color.setRgbF(0.0f, 0.0f, 1.0f);
  378.             m_ScaleX.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot +  0);
  379.             m_ScaleX.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot +  0);
  380.  
  381.             m_ScaleY.isPressed = false;
  382.             m_ScaleY.color.setRgbF(0.0f, 1.0f, 0.0f);
  383.             m_ScaleY.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot +  PI/2);
  384.             m_ScaleY.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot +  PI/2);
  385.  
  386.             if(m_EditMode == COMPONENT_EDITOR)
  387.             {
  388.                 m_Offset.isPressed = false;
  389.                 m_Offset.color.setRgbF(1.0f, 1.0f, 0.0f);
  390.                 m_Offset.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot +  PI);
  391.                 m_Offset.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot +  PI);
  392.             }
  393.  
  394.             emit setSelection(m_Selection);
  395.             return;
  396.         }
  397.     }
  398.  
  399.     m_Selection = -1;
  400.     m_SelectionPressed = false;
  401.  
  402.     emit setSelection(-1);
  403. }
  404.  
  405. void PlotWindow::mouseMoveEvent(QMouseEvent * event)
  406. {
  407.     float handleDist = handleDistP / static_cast<float>(width());
  408.  
  409.     if(m_Selection != -1)
  410.     {
  411.         if(event->x() > 0 && event->x() < width() &&
  412.            event->y() > 0 && event->y() < height() && event->buttons() == Qt::LeftButton)
  413.         {
  414.             if(m_SelectionPressed)
  415.             {
  416.                 m_Points[m_Selection].first.x = event->x() /static_cast<float>(width());
  417.                 m_Points[m_Selection].first.y = event->y() /static_cast<float>(height());
  418.  
  419.                 if(m_EditMode == COMPONENT_EDITOR)
  420.                 {
  421.                     m_Points[m_Selection].second.x = m_Points[m_Selection].first.x;
  422.                     m_Points[m_Selection].second.y = m_Points[m_Selection].first.y;
  423.                 }
  424.  
  425.                 if(m_KeyframeMode == TRANS_KEYS &&
  426.                    m_KeyframeSelection != -1)
  427.                 {
  428.                     std::vector<vanim::comp::TranslateDelta>& transKeys = m_Points[m_Selection].second.m_TranslateDeltas;
  429.                     transKeys[m_KeyframeSelection].x = m_Points[m_Selection].first.x;
  430.                     transKeys[m_KeyframeSelection].y = m_Points[m_Selection].first.y;
  431.                 }
  432.  
  433.                 updateHandles();
  434.             }
  435.             else if(m_Rot.isPressed)
  436.             {
  437.                 m_Rot.x = event->x()/static_cast<float>(width());
  438.                 m_Rot.y = event->y()/static_cast<float>(height());
  439.  
  440.                 if(m_Rot.x - m_Points[m_Selection].first.x > 0.0f && m_Rot.y - m_Points[m_Selection].first.y < 0.0f)
  441.                     m_Points[m_Selection].first.rot = -atan((m_Rot.x - m_Points[m_Selection].first.x)/
  442.                                                    (m_Rot.y - m_Points[m_Selection].first.y));
  443.                 else if(m_Rot.x - m_Points[m_Selection].first.x > 0.0f && m_Rot.y - m_Points[m_Selection].first.y > 0.0f)
  444.                     m_Points[m_Selection].first.rot = -atan((m_Rot.x - m_Points[m_Selection].first.x)/
  445.                                                    (m_Rot.y - m_Points[m_Selection].first.y)) + PI;
  446.                 else if(m_Rot.x - m_Points[m_Selection].first.x < 0.0f && m_Rot.y - m_Points[m_Selection].first.y > 0.0f)
  447.                     m_Points[m_Selection].first.rot = -atan((m_Rot.x - m_Points[m_Selection].first.x)/
  448.                                                    (m_Rot.y - m_Points[m_Selection].first.y)) + PI;
  449.                 else if(m_Rot.x - m_Points[m_Selection].first.x < 0.0f && m_Rot.y - m_Points[m_Selection].first.y < 0.0f)
  450.                     m_Points[m_Selection].first.rot = -atan((m_Rot.x - m_Points[m_Selection].first.x)/
  451.                                                    (m_Rot.y - m_Points[m_Selection].first.y)) + 2*PI;
  452.  
  453.                 if(m_EditMode == COMPONENT_EDITOR)
  454.                 {
  455.                     m_Points[m_Selection].second.rot = m_Points[m_Selection].first.rot;
  456.                 }
  457.  
  458.                 updateHandles();
  459.             }
  460.             else if(m_ScaleX.isPressed)
  461.             {
  462.                 float deltaX;
  463.                 float deltaY;
  464.                 float delta;
  465.  
  466.                 if((m_Points[m_Selection].first.rot > 7*PI/4 || m_Points[m_Selection].first.rot < PI/4) ||
  467.                    (m_Points[m_Selection].first.rot > 3*PI/4 && m_Points[m_Selection].first.rot < 5*PI/4))
  468.                 {
  469.                     deltaX = (event->x() - m_PosBefore.x())/ static_cast<float>(width());
  470.                     deltaY = ((event->x() - m_PosBefore.x())/ static_cast<float>(width())) * tan(m_Points[m_Selection].first.rot);
  471.                 }
  472.                 else
  473.                 {
  474.                     deltaX = ((event->y() - m_PosBefore.y())/ static_cast<float>(height())) * 1/tan(m_Points[m_Selection].first.rot);
  475.                     deltaY = (event->y() - m_PosBefore.y())/ static_cast<float>(height());
  476.                 }
  477.  
  478.                 float distBefore = sqrt( pow(m_ScaleX.x - m_Points[m_Selection].first.x, 2) + pow(m_ScaleX.y - m_Points[m_Selection].first.y, 2));
  479.                 float distAfter = sqrt(pow((m_ScaleX.x + deltaX) - m_Points[m_Selection].first.x, 2) +
  480.                                        pow((m_ScaleX.y + deltaY) - m_Points[m_Selection].first.y, 2));
  481.  
  482.                 delta = sqrt(pow(deltaX,2) + pow(deltaY,2));
  483.  
  484.                 if(distBefore < distAfter)
  485.                     m_Points[m_Selection].first.width += delta;
  486.                 else
  487.                     m_Points[m_Selection].first.width -= delta;
  488.  
  489.                 if(m_Points[m_Selection].first.width < 0)
  490.                     m_Points[m_Selection].first.width = 0;
  491.                 else
  492.                 {
  493.                     m_ScaleX.x += deltaX;
  494.                     m_ScaleX.y += deltaY;
  495.                 }
  496.  
  497.                 m_Rot.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot + 3*PI/2);
  498.                 m_Rot.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot + 3*PI/2);
  499.  
  500.                 m_ScaleY.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot +  PI/2);
  501.                 m_ScaleY.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot +  PI/2);
  502.  
  503.                 if(m_EditMode == COMPONENT_EDITOR)
  504.                 {
  505.                     m_Offset.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot +  PI);
  506.                     m_Offset.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot +  PI);
  507.  
  508.  
  509.                     m_Points[m_Selection].second.width = m_Points[m_Selection].first.width;
  510.                 }
  511.             }
  512.             else if(m_ScaleY.isPressed)
  513.             {
  514.                 float deltaX;
  515.                 float deltaY;
  516.                 float delta;
  517.  
  518.                 if((m_Points[m_Selection].first.rot > 7*PI/4 || m_Points[m_Selection].first.rot < PI/4) ||
  519.                    (m_Points[m_Selection].first.rot > 3*PI/4 && m_Points[m_Selection].first.rot < 5*PI/4))
  520.                 {
  521.                     deltaX = ((event->y() - m_PosBefore.y())/ static_cast<float>(height())) * 1/tan(m_Points[m_Selection].first.rot + PI/2);
  522.                     deltaY = (event->y() - m_PosBefore.y())/ static_cast<float>(height());
  523.                 }
  524.                 else
  525.                 {
  526.                     deltaX = (event->x() - m_PosBefore.x()) / static_cast<float>(width());
  527.                     deltaY = ((event->x() - m_PosBefore.x())/ static_cast<float>(width())) * tan(m_Points[m_Selection].first.rot + PI/2);
  528.                 }
  529.  
  530.                 float distBefore = sqrt( pow(m_ScaleY.x - m_Points[m_Selection].first.x, 2) + pow(m_ScaleY.y - m_Points[m_Selection].first.y, 2));
  531.                 float distAfter = sqrt(pow((m_ScaleY.x + deltaX) - m_Points[m_Selection].first.x, 2) +
  532.                                        pow((m_ScaleY.y + deltaY) - m_Points[m_Selection].first.y, 2));
  533.  
  534.                 delta = sqrt(pow(deltaX, 2) + pow(deltaY, 2));
  535.  
  536.                 if(distBefore < distAfter)
  537.                     m_Points[m_Selection].first.length += delta;
  538.                 else
  539.                     m_Points[m_Selection].first.length -= delta;
  540.  
  541.                 if(m_Points[m_Selection].first.length < 0)
  542.                     m_Points[m_Selection].first.length = 0;
  543.                 else
  544.                 {
  545.                     m_ScaleY.x += deltaX;
  546.                     m_ScaleY.y += deltaY;
  547.                 }
  548.  
  549.                 m_Rot.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot + 3*PI/2);
  550.                 m_Rot.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot + 3*PI/2);
  551.  
  552.                 m_ScaleX.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot +  0);
  553.                 m_ScaleX.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot +  0);
  554.  
  555.                 if(m_EditMode == COMPONENT_EDITOR)
  556.                 {
  557.                     m_Offset.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot +  PI);
  558.                     m_Offset.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot +  PI);
  559.  
  560.                     m_Points[m_Selection].second.length = m_Points[m_Selection].first.length;
  561.                 }
  562.             }
  563.             else if(m_Offset.isPressed)
  564.             {
  565.                 // to be implemented
  566.             }
  567.  
  568.             m_PosBefore = event->pos();
  569.  
  570.             emit selectionChanged();
  571.         }
  572.     }
  573. }
  574.  
  575. void PlotWindow::mouseReleaseEvent(QMouseEvent *)
  576. {
  577.     m_Rot.isPressed = false;
  578.     m_Rot.color.setRgbF(1.0f, 0.0f, 0.0f);
  579.     m_ScaleX.isPressed = false;
  580.     m_ScaleX.color.setRgbF(0.0f, 0.0f, 1.0f);
  581.     m_ScaleY.isPressed = false;
  582.     m_ScaleY.color.setRgbF(0.0f, 1.0f, 0.0f);
  583.     m_Offset.isPressed = false;
  584.     m_Offset.color.setRgbF(1.0f, 1.0f, 0.0f);
  585.     m_SelectionPressed = false;
  586.  
  587.     updateHandles();
  588. }
  589.  
  590. void PlotWindow::keyPressEvent(QKeyEvent * event)
  591. {
  592.     if(event->key() == Qt::Key_Delete)
  593.     {
  594.         if(m_Selection != -1 && m_KeyframeSelection == -1)
  595.         {
  596.             auto it = m_Points.begin() + m_Selection;
  597.             m_Points.erase(it);
  598.  
  599.             m_Selection = -1;
  600.             emit setSelection(m_Selection);
  601.         }
  602.  
  603.         updateHandles();
  604.     }
  605.     else if(event->key() == Qt::Key_Return)
  606.     {
  607.         if(m_EditMode == ANIM_EDITOR && m_Selection != -1)
  608.         {
  609.             std::vector<vanim::comp::TranslateDelta> & transDeltas =  m_Points[m_Selection].second.m_TranslateDeltas;
  610.             if(transDeltas.empty())
  611.             {
  612.                 if(m_CurrentTime > 0.0f && (m_Points[m_Selection].first.x != m_Points[m_Selection].second.x ||
  613.                                             m_Points[m_Selection].first.y != m_Points[m_Selection].second.y))
  614.                 {
  615.                     transDeltas.emplace_back(m_CurrentTime, m_Points[m_Selection].first.x, m_Points[m_Selection].first.y);
  616.                 }
  617.             }
  618.             else
  619.             {
  620.                 for(auto it = transDeltas.begin(); it != transDeltas.end(); it++)
  621.                 {
  622.                     auto next = std::next(it, 1);
  623.                     if(m_CurrentTime < (*it).time)
  624.                     {
  625.                         transDeltas.insert(it, {m_CurrentTime, m_Points[m_Selection].first.x, m_Points[m_Selection].first.y});
  626.                         break;
  627.                     }
  628.  
  629.                     if(next == transDeltas.end() && m_CurrentTime > (*it).time)
  630.                     {
  631.                         transDeltas.emplace_back(m_CurrentTime, m_Points[m_Selection].first.x, m_Points[m_Selection].first.y);
  632.                         break;
  633.                     }
  634.                 }
  635.             }
  636.  
  637.             std::vector<vanim::comp::RotateDelta> & rotateDeltas = m_Points[m_Selection].second.m_RotateDeltas;
  638.             if(rotateDeltas.empty())
  639.             {
  640.                 if(m_CurrentTime > 0.0f && (m_Points[m_Selection].first.rot != m_Points[m_Selection].second.rot))
  641.                     rotateDeltas.emplace_back(m_CurrentTime, m_Points[m_Selection].first.rot);
  642.             }
  643.             else
  644.             {
  645.                 for(auto it = rotateDeltas.begin(); it != rotateDeltas.end(); it++)
  646.                 {
  647.                     auto next = std::next(it, 1);
  648.                     if(m_CurrentTime < (*it).time)
  649.                     {
  650.                         if(m_Points[m_Selection].first.rot != (*it).rot)
  651.                             rotateDeltas.insert(it, {m_CurrentTime, m_Points[m_Selection].first.rot});
  652.                         break;
  653.                     }
  654.  
  655.                     if(next == rotateDeltas.end() && m_CurrentTime > (*it).time)
  656.                     {
  657.                         rotateDeltas.emplace_back(m_CurrentTime, m_Points[m_Selection].first.rot);
  658.                         break;
  659.                     }
  660.                 }
  661.             }
  662.  
  663.             std::vector<vanim::comp::ScaleDelta> & scaleDeltas = m_Points[m_Selection].second.m_ScaleDeltas;
  664.             if(scaleDeltas.empty())
  665.             {
  666.                 if(m_CurrentTime > 0.0f && (m_Points[m_Selection].first.length != m_Points[m_Selection].second.length ||
  667.                                             m_Points[m_Selection].first.width != m_Points[m_Selection].second.width))
  668.                     scaleDeltas.emplace_back(m_CurrentTime, m_Points[m_Selection].first.width, m_Points[m_Selection].first.length);
  669.             }
  670.             else
  671.             {
  672.                 for(auto it = scaleDeltas.begin(); it != scaleDeltas.end(); it++)
  673.                 {
  674.                     auto next = std::next(it, 1);
  675.                     if(m_CurrentTime < (*it).time)
  676.                     {
  677.                         if(m_Points[m_Selection].first.length != (*it).length || m_Points[m_Selection].first.width != (*it).width)
  678.                             scaleDeltas.insert(it, {m_CurrentTime, m_Points[m_Selection].first.width, m_Points[m_Selection].first.length});
  679.                         break;
  680.                     }
  681.  
  682.                     if(next == scaleDeltas.end() && m_CurrentTime > (*it).time)
  683.                     {
  684.                         scaleDeltas.emplace_back(m_CurrentTime, m_Points[m_Selection].first.width, m_Points[m_Selection].first.length);
  685.                         break;
  686.                     }
  687.                 }
  688.             }
  689.  
  690.             emit selectionChanged();
  691.         }
  692.     }\
  693.  
  694.     QWindow::keyPressEvent(event);
  695. }
  696.  
  697. void PlotWindow::setTime(float time)
  698. {
  699.     m_CurrentTime = time;
  700.     float handleDist = handleDistP / static_cast<float>(width());
  701.     Q_UNUSED(handleDist);
  702.  
  703.     if(m_EditMode == ANIM_EDITOR)
  704.     {
  705.         for(uint i = 0; i < m_Points.size(); i++)
  706.         {
  707.             float x, y;
  708.             std::vector<vanim::comp::TranslateDelta>& transDeltas = m_Points[i].second.m_TranslateDeltas;
  709.             if(!transDeltas.empty())
  710.             {
  711.                 if(m_CurrentTime < transDeltas[0].time)
  712.                 {
  713.                     x = ((transDeltas[0].x - m_Points[i].first.x) * ((m_CurrentTime - 0.0f) / (transDeltas[0].time - 0.0f))) + m_Points[i].first.x;
  714.                     y = ((transDeltas[0].y - m_Points[i].first.y) * ((m_CurrentTime - 0.0f) / (transDeltas[0].time - 0.0f))) + m_Points[i].first.y;
  715.                 }
  716.                 else
  717.                 {
  718.                     for(auto it = transDeltas.begin(); it != transDeltas.end(); it++)
  719.                     {
  720.                         auto next = std::next(it, 1);
  721.  
  722.                         if(next == transDeltas.end())
  723.                         {
  724.                             x = (*it).x;
  725.                             y = (*it).y;
  726.                             break;
  727.                         }
  728.  
  729.                         if(m_CurrentTime >= (*it).time && m_CurrentTime < (*next).time)
  730.                         {
  731.                             x = (((*next).x - (*it).x) * ((m_CurrentTime - (*it).time)/ ((*next).time - (*it).time))) + (*it).x;
  732.                             y = (((*next).y - (*it).y) * ((m_CurrentTime - (*it).time)/ ((*next).time - (*it).time))) + (*it).y;
  733.                             break;
  734.                         }
  735.                     }
  736.                 }
  737.                 m_Points[i].first.x = x;
  738.                 m_Points[i].first.y = y;
  739.             }
  740.  
  741.         }
  742.  
  743.         updateHandles();
  744.         emit selectionChanged();
  745.     }
  746.  
  747. }
  748.  
  749. void PlotWindow::setEditMode(EDITOR_MODE mode)
  750. {
  751.     m_EditMode = mode;
  752.  
  753.     if(mode == ANIM_EDITOR)
  754.     {
  755.         for(uint i = 0; i < m_Points.size(); i++)
  756.         {
  757.             m_Points[i].first.x = m_Points[i].second.x;
  758.             m_Points[i].first.y = m_Points[i].second.y;
  759.             m_Points[i].first.rot = m_Points[i].second.rot;
  760.             m_Points[i].first.length = m_Points[i].second.length;
  761.             m_Points[i].first.width = m_Points[i].second.width;
  762.             m_Points[i].first.texSX = m_Points[i].second.texSX;
  763.             m_Points[i].first.texSY = m_Points[i].second.texSY;
  764.             m_Points[i].first.texEX = m_Points[i].second.texEX;
  765.             m_Points[i].first.texEY = m_Points[i].second.texEY;
  766.  
  767.  
  768.         }
  769.  
  770.         emit selectionChanged();
  771.         updateHandles();
  772.     }
  773. }
  774.  
  775. void PlotWindow::updateHandles()
  776. {
  777.     float handleDist = handleDistP / static_cast<float>(width());
  778.  
  779.     if(m_Selection != -1)
  780.     {
  781.         m_Rot.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot + 3*PI/2);
  782.         m_Rot.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot + 3*PI/2);
  783.  
  784.         m_ScaleX.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot +  0);
  785.         m_ScaleX.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot +  0);
  786.  
  787.         m_ScaleY.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot +  PI/2);
  788.         m_ScaleY.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot +  PI/2);
  789.  
  790.         if(m_EditMode == COMPONENT_EDITOR)
  791.         {
  792.             m_Offset.x = m_Points[m_Selection].first.x + handleDist * cos(m_Points[m_Selection].first.rot +  PI);
  793.             m_Offset.y = m_Points[m_Selection].first.y + handleDist * sin(m_Points[m_Selection].first.rot +  PI);
  794.         }
  795.     }
  796.     else
  797.     {
  798.         m_Rot.isPressed = false;
  799.         m_Rot.x = 0;
  800.         m_Rot.y = 0;
  801.  
  802.         m_ScaleX.isPressed = false;
  803.         m_ScaleX.x = 0;
  804.         m_ScaleX.y = 0;
  805.  
  806.         m_ScaleY.isPressed = false;
  807.         m_ScaleY.x = 0;
  808.         m_ScaleY.y = 0;
  809.  
  810.         m_Offset.isPressed = false;
  811.         m_Offset.x = 0;
  812.         m_Offset.y = 0;
  813.     }
  814. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement