From 52555029453c35013dcf092b4b7b739c6679e1f2 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Fri, 15 Mar 2024 18:10:33 -0500 Subject: [PATCH] Added ability to create new objects, delete objects, and delete/move collision ranges without changing targets until mouse leaves a previous collisoin target. --- TiledCollisionEditor/main.cpp | 146 ++++++++++++++---------- TiledCollisionEditor/olcPGEX_QuickGUI.h | 2 + 2 files changed, 88 insertions(+), 60 deletions(-) diff --git a/TiledCollisionEditor/main.cpp b/TiledCollisionEditor/main.cpp index 02e3d12..5780932 100644 --- a/TiledCollisionEditor/main.cpp +++ b/TiledCollisionEditor/main.cpp @@ -22,9 +22,11 @@ class TiledCollisionEditor : public olc::PixelGameEngine bool dragging=false; bool dragTranslate=false; vf2d upperLeftDragOffset{}; + Quadrilateral*highlightedQuad=nullptr; bool dragNewObj=false; vi2d upperLeftObjTile{}; + vi2d lowerRightObjTile{}; Renderable circle; Renderable createNewButtonImg; @@ -70,6 +72,7 @@ public: createNewButton->hotkey=Q; editButton=new ImageCheckBox{gui,editButtonImg,true,vf2d{40.f,ScreenHeight()-36.f},{32.f,32.f},{4,4},{32,32}}; editButton->hotkey=E; + return true; } @@ -77,6 +80,13 @@ public: const Tileset&tileset=tilesets[activeTileset]; if(selectedObj.length()>0){ + + if(GetKey(DEL).bPressed){ + tilesets[activeTileset].objects.erase(selectedObj); + selectedObj=""; + return; + } + for(int y=0;ybChecked){ if(GetMouse(Mouse::LEFT).bPressed){ - #pragma region Select a point on a collision quad. - for(const Quadrilateral&quad:obj.collisionTiles){ - for(size_t pointInd=0;const vf2d&point:quad){ + if(highlightedQuad!=nullptr){ + #pragma region Select a point on a collision quad. + for(size_t pointInd=0;const vf2d&point:*highlightedQuad){ if(geom2d::line(point,view.ScreenToWorld(GetMousePos())).length()<4){ - if(GetMouse(Mouse::LEFT).bPressed){ - editingPoint=pointInd; - editingQuad=const_cast(&quad); - originalQuad=quad; - } + editingPoint=pointInd; + editingQuad=highlightedQuad; + originalQuad=*highlightedQuad; goto exitCollisionCheck; } pointInd++; } - } - #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::overlaps(tri,geom2d::circle(view.ScreenToWorld(GetMousePos()),3.f))){ - dragTranslate=true; - editingQuad=const_cast(&quad); - upperLeftDragOffset=GetSnapPoint()-quad[0]; - originalQuad=*editingQuad; - goto exitCollisionCheck; - } - } + #pragma endregion + + dragTranslate=true; + editingQuad=highlightedQuad; + upperLeftDragOffset=GetSnapPoint()-(*highlightedQuad)[0]; + originalQuad=*editingQuad; + goto exitCollisionCheck; } exitCollisionCheck: if(EditingQuad&&!dragging){ @@ -174,23 +173,8 @@ public: } if(GetMouse(Mouse::RIGHT).bPressed&&!EditingQuad&&!dragTranslate){ - std::vectorquadsToBeRemoved; - 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::overlaps(tri,geom2d::circle(view.ScreenToWorld(GetMousePos()),3.f))){ - //Delete this quad, the mouse is overlapping it! - quadsToBeRemoved.push_back(const_cast(&quad)); - break; - } - } - } - - for(Quadrilateral*quad:quadsToBeRemoved){ - std::erase_if(tilesets[activeTileset].objects[selectedObj].collisionTiles,[&](Quadrilateral&q){return &q==quad;}); + if(highlightedQuad!=nullptr){ + std::erase_if(tilesets[activeTileset].objects[selectedObj].collisionTiles,[&](Quadrilateral&q){return &q==highlightedQuad;}); } } @@ -215,18 +199,26 @@ public: void NewObjectUpdate(){ const Tileset&tileset=tilesets[activeTileset]; - if(GetMouse(Mouse::LEFT).bPressed){ - vf2d worldCoords=view.ScreenToWorld(GetMousePos()); - upperLeftObjTile=vi2d{int(round(worldCoords.x/tileset.tilewidth)*tileset.tilewidth),int(round(worldCoords.y/tileset.tileheight)*tileset.tileheight)}; - dragNewObj=true; - } if(GetMouse(Mouse::LEFT).bReleased){ dragNewObj=false; vf2d worldCoords=view.ScreenToWorld(GetMousePos()); - vi2d lowerRightObjTile=vi2d{int(round(worldCoords.x/tileset.tilewidth)*tileset.tilewidth),int(round(worldCoords.y/tileset.tileheight)*tileset.tileheight)}; + vi2d newUpperLeftTile=upperLeftObjTile; + vi2d newLowerRightTile=lowerRightObjTile; + + if(worldCoords.xnewObjRect{upperLeftObjTile,lowerRightObjTile-upperLeftObjTile}; + geom2d::rectnewObjRect{newUpperLeftTile,newLowerRightTile-newUpperLeftTile}; const Tileset&tileset=tilesets[activeTileset]; //Check for intersection with other objects, if found then we deny creating this object this way. @@ -244,10 +236,10 @@ public: TilesetObject&newObj=tilesets[activeTileset].objects[objName]; newObj.name=objName; - for(int y=0;ybPressed)createNewButton->bChecked=false; if(GetMouseY()72){ + if(selectedObj.length()==0){ + if(GetMouse(Mouse::LEFT).bPressed){ + vf2d worldCoords=view.ScreenToWorld(GetMousePos()); + upperLeftObjTile=vi2d{int(floor(worldCoords.x/tileset.tilewidth)*tileset.tilewidth),int(floor(worldCoords.y/tileset.tileheight)*tileset.tileheight)}; + dragNewObj=true; + } + } if(!dragNewObj){ Update(); }else{ @@ -308,18 +307,30 @@ public: if(selectedObj.length()>0&&!dragNewObj){ const TilesetObject&obj=tileset.objects.at(selectedObj); - Quadrilateral*highlightedQuad=nullptr; - for(const Quadrilateral&quad:obj.collisionTiles){ + if(highlightedQuad!=nullptr){ std::array,2>collisionTris{ - geom2d::triangle{quad[0],quad[1],quad[2]}, - geom2d::triangle{quad[0],quad[2],quad[3]}, + geom2d::triangle{(*highlightedQuad)[0],(*highlightedQuad)[1],(*highlightedQuad)[2]}, + geom2d::triangle{(*highlightedQuad)[0],(*highlightedQuad)[2],(*highlightedQuad)[3]}, }; for(geom2d::triangle&tri:collisionTris){ if(geom2d::overlaps(tri,geom2d::circle(view.ScreenToWorld(GetMousePos()),3.f))){ - highlightedQuad=const_cast(&quad); goto renderQuads; } } + highlightedQuad=nullptr; + }else{ + 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::overlaps(tri,geom2d::circle(view.ScreenToWorld(GetMousePos()),3.f))){ + highlightedQuad=const_cast(&quad); + goto renderQuads; + } + } + } } renderQuads: for(const Quadrilateral&quad:obj.collisionTiles){ @@ -347,11 +358,26 @@ public: }else if(dragNewObj){ vf2d worldCoords=view.ScreenToWorld(GetMousePos()); - vi2d lowerRightObjTile=vi2d{int(round(worldCoords.x/tileset.tilewidth)*tileset.tilewidth),int(round(worldCoords.y/tileset.tileheight)*tileset.tileheight)}; + lowerRightObjTile=vi2d{int(ceil(worldCoords.x/tileset.tilewidth)*tileset.tilewidth),int(ceil(worldCoords.y/tileset.tileheight)*tileset.tileheight)}; + + vi2d newUpperLeftTile=upperLeftObjTile; + vi2d newLowerRightTile=lowerRightObjTile; + + if(worldCoords.xnewObjRect{upperLeftObjTile,lowerRightObjTile-upperLeftObjTile}; + geom2d::rectnewObjRect{newUpperLeftTile,newLowerRightTile-newUpperLeftTile}; for(auto&[name,obj]:tileset.objects){ geom2d::rectoffsetBounds{obj.bounds.pos+vf2d{0.5f,0.5f},obj.bounds.size-vf2d{1.f,1.f}}; @@ -361,7 +387,7 @@ public: } } - view.FillRectDecal(upperLeftObjTile,lowerRightObjTile-upperLeftObjTile,overlayCol); + view.FillRectDecal(newObjRect.pos,newObjRect.size,overlayCol); } for(auto&[objName,obj]:tileset.objects){ diff --git a/TiledCollisionEditor/olcPGEX_QuickGUI.h b/TiledCollisionEditor/olcPGEX_QuickGUI.h index d70c3bb..3513f9f 100644 --- a/TiledCollisionEditor/olcPGEX_QuickGUI.h +++ b/TiledCollisionEditor/olcPGEX_QuickGUI.h @@ -1029,6 +1029,8 @@ namespace olc::QuickGUI bHeld = false; } + if(fMax