Skip to content

Commit

Permalink
Improved movement of vertices by user.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarkham committed May 1, 2023
1 parent 4efbe24 commit d863144
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 50 deletions.
89 changes: 49 additions & 40 deletions ImNodeSoup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,12 +592,13 @@ nodesoup::adj_list_t read_from_dot(const char* aDotData)




constexpr std::size_t kInvadidVertex=static_cast<std::size_t>(-1);
constexpr float kMinUIDist=9.0f; // TODO: Calculate this with DPI?
const float kWindowInitWidth = 800.0f;
const float kWindowInitHeight= 600.0f;
static ImVec2 gDisp{0.0f,0.0f};
static float gScale=1.0f;

static nodesoup::vertex_id_t gSelectedVertex=kInvadidVertex;



Expand Down Expand Up @@ -649,68 +650,76 @@ static nodesoup::vertex_id_t GetPosAt(const ImVec2& aPos,const std::vector<NsPos






static MoveRes MovePos(const std::vector<NsPosition>& aPositions)
{
MoveRes res;

ImGuiIO& io = ImGui::GetIO();

if(!io.MouseDown[1] && !io.MouseReleased[1])
{
res.m_Moved=false;
res.m_Recalculate=false;

return res;
}

ImVec2 mouse_pos=io.MousePos;
nodesoup::vertex_id_t v_id=GetPosAt(mouse_pos,aPositions);
if(v_id==-1) // Not over a node
if(gSelectedVertex==kInvadidVertex) // No hay ninguno seleccionado
{
res.m_Moved=false;
res.m_Recalculate=false;
return res;
}
if(io.MouseDown[1])
{
ImVec2 mouse_pos=io.MousePos;
gSelectedVertex=GetPosAt(mouse_pos,aPositions);

if(io.MouseDown[1])
{
res.m_Index=v_id;
res.m_Disp=io.MouseDelta/gScale;
res.m_Moved=true;
res.m_Recalculate=false;
return res;
}

if(io.MouseReleased[1])
{
res.m_Index=v_id;
res.m_Recalculate=true;
res.m_Index=gSelectedVertex;
res.m_Disp={0.0f,0.0f};
res.m_Moved=(gSelectedVertex==kInvadidVertex?false:true);
res.m_Recalculate=false;

if(io.MouseDragMaxDistanceSqr[1]<10)
{
res.m_Disp={kInvalidPos,kInvalidPos};
return res;
}
else
{
res.m_Disp=io.MouseDelta/gScale;
}
res.m_Moved=false;
res.m_Recalculate=false;

return res;
return res;
}
}
else // Hay un vertice seleccionado
{
if(io.MouseDown[1]) // Lo esta moviendo
{
res.m_Index=gSelectedVertex;
res.m_Disp=io.MouseDelta/gScale;
res.m_Moved=true;
res.m_Recalculate=false;
return res;
}
else // Ha dejado de pulsar
{
res.m_Index=gSelectedVertex;
res.m_Moved=true;
res.m_Recalculate=true;

assert(0);

res.m_Moved=false;
res.m_Recalculate=false;
if(io.MouseDragMaxDistanceSqr[1]<kMinUIDist)
{
res.m_Disp={kInvalidPos,kInvalidPos};
}
else
{
res.m_Disp=io.MouseDelta/gScale;
}

return res;
gSelectedVertex=kInvadidVertex;

return res;
}
}

assert(0);
}





static void DrawData(const nodesoup::adj_list_t& aAdjList,const std::vector<NsPosition>& aPositions,bool aAllowMove
,bool aDrawDebug)
{
Expand Down
10 changes: 5 additions & 5 deletions fruchterman_reingold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,7 @@ void FruchtermanReingold::MovePos(vertex_id_t aVertexId,const ImVec2& aDisp,bool
{
if(aDisp.x==kInvalidPos && aDisp.y==kInvalidPos)
{
if(m_Positions[aVertexId].m_Fixed)
{
m_Positions[aVertexId].m_Fixed=false;
}
m_Positions[aVertexId].m_Fixed=!m_Positions[aVertexId].m_Fixed;
}

m_CurrIter=1;
Expand All @@ -195,7 +192,10 @@ void FruchtermanReingold::MovePos(vertex_id_t aVertexId,const ImVec2& aDisp,bool
}

m_Positions[aVertexId].m_Pos+=aDisp;
m_Positions[aVertexId].m_Fixed=true;
if(sq_norm(aDisp)>0.0f)
{
m_Positions[aVertexId].m_Fixed=true;
}
}


Expand Down
10 changes: 5 additions & 5 deletions kamada_kawai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,7 @@ void KamadaKawai::MovePos(vertex_id_t aVertexId,const ImVec2& aDisp,bool aRecalc
{
if(aDisp.x==kInvalidPos && aDisp.y==kInvalidPos)
{
if(m_Positions[aVertexId].m_Fixed)
{
m_Positions[aVertexId].m_Fixed=false;
}
m_Positions[aVertexId].m_Fixed=!m_Positions[aVertexId].m_Fixed;
}

double energy=ComputeVertexEnergy(aVertexId);
Expand All @@ -356,7 +353,10 @@ void KamadaKawai::MovePos(vertex_id_t aVertexId,const ImVec2& aDisp,bool aRecalc

ImVec2 disp=aDisp/m_Scale;
m_Positions[aVertexId].m_Pos+=disp;
m_Positions[aVertexId].m_Fixed=true;
if(sq_norm(aDisp)>0.0f)
{
m_Positions[aVertexId].m_Fixed=true;
}
}


Expand Down
9 changes: 9 additions & 0 deletions nodesoup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ struct NsPosition

constexpr float kInvalidPos=-1000000.0f;


inline double norm(const ImVec2& aImVec2) noexcept
{
return sqrt(aImVec2.x * aImVec2.x + aImVec2.y * aImVec2.y);
}

inline float sq_norm(const ImVec2& aImVec2) noexcept
{
return aImVec2.x*aImVec2.x + aImVec2.y*aImVec2.y;
}




namespace nodesoup
{

Expand Down

0 comments on commit d863144

Please sign in to comment.