From 4c5781d0c99c4387dbe3966ef81e7e1a86e11870 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Thu, 14 Mar 2024 18:14:14 -0500 Subject: [PATCH] Dragging/Translating snapping improvements. --- TiledCollisionEditor/main.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/TiledCollisionEditor/main.cpp b/TiledCollisionEditor/main.cpp index 9f85cd3..a93385c 100644 --- a/TiledCollisionEditor/main.cpp +++ b/TiledCollisionEditor/main.cpp @@ -19,6 +19,8 @@ class TiledCollisionEditor : public olc::PixelGameEngine std::string selectedObj=""; int editingPoint=4; //0-3 for the index we are editing within editingQuad. bool dragging=false; + bool dragTranslate=false; + vf2d upperLeftDragOffset{}; Renderable circle; @@ -131,6 +133,14 @@ public: (*editingQuad)[3]=vf2d{initialPoint.x,cursorPos.y}; } + if(dragTranslate){ + vf2d translateAmt=GetSnapPoint()-originalQuad[0]-upperLeftDragOffset; + for(size_t pointInd=0;vf2d&point:*editingQuad){ + (*editingQuad)[pointInd]=originalQuad[pointInd]+translateAmt; + pointInd++; + } + } + if(GetMouse(Mouse::LEFT).bPressed){ #pragma region Select a point on a collision quad. for(const Quadrilateral&quad:obj.collisionTiles){ @@ -145,8 +155,23 @@ public: pointInd++; } } - exitCollisionCheck: #pragma endregion + for(const Quadrilateral&quad:obj.collisionTiles){ + std::array,2>collisionTris{ + geom2d::triangle{quad[0],quad[1],quad[2]}, + geom2d::triangle{quad[0],quad[2],quad[3]}, + }; + for(geom2d::triangle&tri:collisionTris){ + if(geom2d::contains(tri,view.ScreenToWorld(GetMousePos()))){ + dragTranslate=true; + editingQuad=const_cast(&quad); + upperLeftDragOffset=GetSnapPoint()-quad[0]; + originalQuad=*editingQuad; + goto exitCollisionCheck; + } + } + } + exitCollisionCheck: if(EditingQuad&&!dragging){ (*editingQuad)[editingPoint]=GetSnapPoint(); editingPoint++; @@ -174,6 +199,7 @@ public: (*editingQuad)[editingPoint]=GetSnapPoint(); editingPoint=4; } + dragTranslate=false; } if(GetMouse(Mouse::RIGHT).bPressed||GetKey(ESCAPE).bPressed){