src/gldraw.cpp

changeset 208
0bb57b81c92a
parent 207
e8c47e51e03d
child 209
d3bdd13bce4d
equal deleted inserted replaced
207:e8c47e51e03d 208:0bb57b81c92a
97 resetAngles (); 97 resetAngles ();
98 m_picking = m_rangepick = false; 98 m_picking = m_rangepick = false;
99 m_camera = (GL::Camera) gl_camera.value; 99 m_camera = (GL::Camera) gl_camera.value;
100 m_drawToolTip = false; 100 m_drawToolTip = false;
101 m_editmode = Select; 101 m_editmode = Select;
102 m_rectdraw = false;
102 103
103 m_toolTipTimer = new QTimer (this); 104 m_toolTipTimer = new QTimer (this);
104 m_toolTipTimer->setSingleShot (true); 105 m_toolTipTimer->setSingleShot (true);
105 connect (m_toolTipTimer, SIGNAL (timeout ()), this, SLOT (slot_toolTipTimer ())); 106 connect (m_toolTipTimer, SIGNAL (timeout ()), this, SLOT (slot_toolTipTimer ()));
106 107
501 paint.drawText (m_width - textSize.width (), m_height - 16, textSize.width (), 502 paint.drawText (m_width - textSize.width (), m_height - 16, textSize.width (),
502 textSize.height (), Qt::AlignCenter, text); 503 textSize.height (), Qt::AlignCenter, text);
503 504
504 // If we're drawing, draw the vertices onto the screen. 505 // If we're drawing, draw the vertices onto the screen.
505 if (m_editmode == Draw) { 506 if (m_editmode == Draw) {
506 ushort numverts = m_drawedVerts.size () + 1; 507 ushort numverts;
508
509 if (!m_rectdraw)
510 numverts = m_drawedVerts.size () + 1;
511 else
512 numverts = (m_drawedVerts.size () > 0) ? 4 : 1;
513
507 const short blipsize = 8; 514 const short blipsize = 8;
508 515
509 if (numverts > 0) { 516 if (numverts > 0) {
510 QPoint* poly = new QPoint[numverts]; 517 QPoint* poly = new QPoint[numverts];
511 518
512 uchar i = 0; 519 if (!m_rectdraw) {
513 for (vertex& vert : m_drawedVerts) { 520 uchar i = 0;
514 poly[i] = coordconv3_2 (vert); 521 for (vertex& vert : m_drawedVerts) {
515 ++i; 522 poly[i] = coordconv3_2 (vert);
523 ++i;
524 }
525
526 // Draw the cursor vertex as the last one in the list.
527 if (numverts < 5)
528 poly[i] = coordconv3_2 (m_hoverpos);
529 else
530 numverts = 4;
531 } else {
532 if (m_drawedVerts.size () > 0) {
533 QPoint v0 = coordconv3_2 (m_drawedVerts[0]),
534 v1 = coordconv3_2 ((m_drawedVerts.size () >= 2) ? m_drawedVerts[1] : m_hoverpos);
535
536 poly[0] = QPoint (v0.x (), v0.y ());
537 poly[1] = QPoint (v0.x (), v1.y ());
538 poly[2] = QPoint (v1.x (), v1.y ());
539 poly[3] = QPoint (v1.x (), v0.y ());
540 } else {
541 poly[0] = coordconv3_2 (m_hoverpos);
542 }
516 } 543 }
517
518 // Draw the cursor vertex as the last one in the list.
519 if (numverts < 5)
520 poly[i] = coordconv3_2 (m_hoverpos);
521 else
522 numverts = 4;
523 544
524 paint.setPen (m_thinBorderPen); 545 paint.setPen (m_thinBorderPen);
525 paint.setBrush (QColor (128, 192, 0)); 546 paint.setBrush (QColor (128, 192, 0));
526 547
527 // Draw vertex blips 548 // Draw vertex blips
539 } 560 }
540 } 561 }
541 } 562 }
542 563
543 // Camera icons 564 // Camera icons
544 if (!m_picking && m_editmode == Select) { 565 if (!m_picking) {
545 // Draw a background for the selected camera 566 // Draw a background for the selected camera
546 paint.setPen (m_thinBorderPen); 567 paint.setPen (m_thinBorderPen);
547 paint.setBrush (QBrush (QColor (0, 128, 160, 128))); 568 paint.setBrush (QBrush (QColor (0, 128, 160, 128)));
548 paint.drawRect (g_CameraIcons[camera ()].selRect); 569 paint.drawRect (g_CameraIcons[camera ()].selRect);
549 570
550 // Draw the actual icons 571 // Draw the actual icons
551 for (CameraIcon& info : g_CameraIcons) 572 for (CameraIcon& info : g_CameraIcons) {
573 // Don't draw the free camera icon when in draw mode
574 if (&info == &g_CameraIcons[GL::Free] && editMode () != Select)
575 continue;
576
552 paint.drawPixmap (info.destRect, *info.img, info.srcRect); 577 paint.drawPixmap (info.destRect, *info.img, info.srcRect);
578 }
553 579
554 // Draw a label for the current camera in the top left corner 580 // Draw a label for the current camera in the top left corner
555 { 581 {
556 const ushort margin = 4; 582 const ushort margin = 4;
557 583
811 void GLRenderer::mouseReleaseEvent (QMouseEvent* ev) { 837 void GLRenderer::mouseReleaseEvent (QMouseEvent* ev) {
812 const bool wasLeft = (m_lastButtons & Qt::LeftButton) && !(ev->buttons() & Qt::LeftButton); 838 const bool wasLeft = (m_lastButtons & Qt::LeftButton) && !(ev->buttons() & Qt::LeftButton);
813 const bool wasRight = (m_lastButtons & Qt::RightButton) && !(ev->buttons() & Qt::RightButton); 839 const bool wasRight = (m_lastButtons & Qt::RightButton) && !(ev->buttons() & Qt::RightButton);
814 840
815 if (wasLeft) { 841 if (wasLeft) {
816 if (m_editmode == Draw) { 842 // Check if we selected a camera icon
817 // If we have 4 verts, stop drawing. 843 if (!m_rangepick) {
818 if (m_drawedVerts.size () >= 4) { 844 for (CameraIcon& info : g_CameraIcons) {
819 endDraw (true); 845 if (info.destRect.contains (ev->pos ())) {
820 return; 846 setCamera (info.cam);
847 update ();
848 return;
849 }
821 } 850 }
822 851 }
823 // If we picked an already-existing vertex, stop drawing 852
824 for (vertex& vert : m_drawedVerts) { 853 switch (editMode ()) {
825 if (vert == m_hoverpos) { 854 case Draw:
855 if (m_rectdraw) {
856 if (m_drawedVerts.size () == 2) {
826 endDraw (true); 857 endDraw (true);
827 return; 858 return;
828 } 859 }
860 } else {
861 // If we have 4 verts, stop drawing.
862 if (m_drawedVerts.size () >= 4) {
863 endDraw (true);
864 return;
865 }
866
867 if (m_drawedVerts.size () == 0 && ev->modifiers () & Qt::ShiftModifier)
868 m_rectdraw = true;
869
870 // If we picked an already-existing vertex, stop drawing
871 for (vertex& vert : m_drawedVerts) {
872 if (vert == m_hoverpos) {
873 endDraw (true);
874 return;
875 }
876 }
829 } 877 }
830 878
831 m_drawedVerts.push_back (m_hoverpos); 879 m_drawedVerts.push_back (m_hoverpos);
832
833 update (); 880 update ();
834 return; 881 break;
835 } else { 882
883 case Select:
836 if (!m_rangepick) 884 if (!m_rangepick)
837 m_addpick = (m_keymods & Qt::ControlModifier); 885 m_addpick = (m_keymods & Qt::ControlModifier);
838 886
839 if (m_totalmove < 10 || m_rangepick) 887 if (m_totalmove < 10 || m_rangepick)
840 pick (ev->x (), ev->y ()); 888 pick (ev->x (), ev->y ());
889 break;
841 } 890 }
842 891
843 m_rangepick = false; 892 m_rangepick = false;
844 m_totalmove = 0; 893 m_totalmove = 0;
845 return; 894 return;
846 } 895 }
847 896
848 if (wasRight && m_editmode == Draw) { 897 if (wasRight && m_drawedVerts.size () > 0) {
849 if (m_drawedVerts.size () > 0) { 898 // Remove the last vertex
850 // Remove the last vertex 899 m_drawedVerts.erase (m_drawedVerts.end () - 1);
851 m_drawedVerts.erase (m_drawedVerts.end () - 1); 900
852 } 901 if (m_drawedVerts.size () == 0)
902 m_rectdraw = false;
853 903
854 update (); 904 update ();
855 } 905 }
856 } 906 }
857 907
944 994
945 // ============================================================================= 995 // =============================================================================
946 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 996 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
947 // ============================================================================= 997 // =============================================================================
948 void GLRenderer::pick (uint mouseX, uint mouseY) { 998 void GLRenderer::pick (uint mouseX, uint mouseY) {
949 // Check if we selected a camera icon
950 if (!m_rangepick) {
951 QPoint pos (mouseX, mouseY);
952
953 for (CameraIcon& info : g_CameraIcons) {
954 if (info.destRect.contains (pos)) {
955 setCamera (info.cam);
956 update ();
957 return;
958 }
959 }
960 }
961
962 GLint viewport[4]; 999 GLint viewport[4];
963 1000
964 // Clear the selection if we do not wish to add to it. 1001 // Clear the selection if we do not wish to add to it.
965 if (!m_addpick) { 1002 if (!m_addpick) {
966 std::vector<LDObject*> oldsel = g_win->sel (); 1003 std::vector<LDObject*> oldsel = g_win->sel ();
1119 update (); 1156 update ();
1120 } 1157 }
1121 1158
1122 // ============================================================================= 1159 // =============================================================================
1123 void GLRenderer::endDraw (bool accept) { 1160 void GLRenderer::endDraw (bool accept) {
1124 // If we accepted, clean the selection and create the object 1161 (void) accept;
1125 if (accept) { 1162
1126 vector<vertex>& verts = m_drawedVerts; 1163 // Clean the selection and create the object
1127 LDObject* obj = null; 1164 vector<vertex>& verts = m_drawedVerts;
1128 1165 LDObject* obj = null;
1166
1167 if (m_rectdraw) {
1168 LDQuad* quad = new LDQuad;
1169 vertex v0 = m_drawedVerts[0],
1170 v1 = m_drawedVerts[1];
1171 const Axis axisX = cameraAxis (false),
1172 axisY = cameraAxis (true);
1173
1174 memset (quad->coords, 0, sizeof quad->coords);
1175 quad->coords[0][axisX] = v0[axisX];
1176 quad->coords[0][axisY] = v0[axisY];
1177 quad->coords[1][axisX] = v1[axisX];
1178 quad->coords[1][axisY] = v0[axisY];
1179 quad->coords[2][axisX] = v1[axisX];
1180 quad->coords[2][axisY] = v1[axisY];
1181 quad->coords[3][axisX] = v0[axisX];
1182 quad->coords[3][axisY] = v1[axisY];
1183 quad->color = maincolor;
1184 obj = quad;
1185 } else {
1129 switch (verts.size ()) { 1186 switch (verts.size ()) {
1130 case 1: 1187 case 1:
1131 { 1188 // 1 vertex - add a vertex object
1132 // 1 vertex - add a vertex object 1189 obj = new LDVertex;
1133 obj = new LDVertex; 1190 static_cast<LDVertex*> (obj)->pos = verts[0];
1134 static_cast<LDVertex*> (obj)->pos = verts[0]; 1191 obj->color = maincolor;
1135 obj->color = maincolor;
1136 }
1137 break; 1192 break;
1138 1193
1139 case 2: 1194 case 2:
1140 { 1195 // 2 verts - make a line
1141 // 2 verts - make a line 1196 obj = new LDLine;
1142 obj = new LDLine; 1197 obj->color = edgecolor;
1143 obj->color = edgecolor; 1198 for (ushort i = 0; i < 2; ++i)
1144 for (ushort i = 0; i < 2; ++i) 1199 obj->coords[i] = verts[i];
1145 obj->coords[i] = verts[i];
1146 }
1147 break; 1200 break;
1148 1201
1149 case 3: 1202 case 3:
1150 case 4: 1203 case 4:
1151 { 1204 obj = (verts.size () == 3) ?
1152 obj = (verts.size () == 3) ? 1205 static_cast<LDObject*> (new LDTriangle) :
1153 static_cast<LDObject*> (new LDTriangle) : 1206 static_cast<LDObject*> (new LDQuad);
1154 static_cast<LDObject*> (new LDQuad); 1207
1155 1208 obj->color = maincolor;
1156 obj->color = maincolor; 1209 for (ushort i = 0; i < obj->vertices (); ++i)
1157 for (ushort i = 0; i < obj->vertices (); ++i) 1210 obj->coords[i] = verts[i];
1158 obj->coords[i] = verts[i];
1159 }
1160 break; 1211 break;
1161 } 1212 }
1162 1213 }
1163 if (obj) { 1214
1164 g_curfile->addObject (obj); 1215 if (obj) {
1165 compileObject (obj); 1216 g_curfile->addObject (obj);
1166 g_win->fullRefresh (); 1217 compileObject (obj);
1167 1218 g_win->fullRefresh ();
1168 History::addEntry (new AddHistory ({(ulong) obj->getIndex (g_curfile)}, {obj->clone ()})); 1219
1169 } 1220 History::addEntry (new AddHistory ({(ulong) obj->getIndex (g_curfile)}, {obj->clone ()}));
1170 } 1221 }
1171 1222
1172 m_drawedVerts.clear (); 1223 m_drawedVerts.clear ();
1224 m_rectdraw = false;
1173 } 1225 }
1174 1226
1175 // ============================================================================= 1227 // =============================================================================
1176 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 1228 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1177 // ============================================================================= 1229 // =============================================================================

mercurial