Skip to content

Commit

Permalink
Removed line of code in NetworkMap.cpp that re-centers view every tim…
Browse files Browse the repository at this point in the history
…e the user makes use of the panning functionality. Improved logic for panning and drawing the visualizer nodes.
  • Loading branch information
fedoraman737 committed Aug 10, 2024
1 parent 27d3640 commit c50b26f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/NetworkMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,39 @@ void NetworkMap::calculatePanLimits() {
}

void NetworkMap::draw(sf::RenderWindow& window) {
window.setView(view);
// Set view initially if it hasn't been centered yet
if (!isViewCenteredInitially && !hosts.empty()) {
// Determine the center of the window (this will be the initial position for the root node)
float centerX = static_cast<float>(window.getSize().x) / 2.0f;
float centerY = static_cast<float>(window.getSize().y) / 2.0f;

if (hosts.empty()) {
return;
}
// Assume the first host is the gateway/modem and initially position it at the center
hostPositions[hosts[0].ip] = sf::Vector2f(centerX, centerY);

// Position the other hosts using the force-directed algorithm
positionHostsUsingForceDirected(centerX, centerY);

// Determine the center of the window (this will be the initial position for the root node)
float centerX = static_cast<float>(window.getSize().x) / 2.0f;
float centerY = static_cast<float>(window.getSize().y) / 2.0f;
// Set the view to be centered on the root node
sf::Vector2f rootPosition = hostPositions[hosts[0].ip];
view.setCenter(rootPosition);

// Assume the first host is the gateway/modem and initially position it at the center
hostPositions[hosts[0].ip] = sf::Vector2f(centerX, centerY);
isViewCenteredInitially = true; // Mark that the view has been centered
}

// Position the other hosts using the force-directed algorithm
positionHostsUsingForceDirected(centerX, centerY);
window.setView(view);

// Now adjust the view to center it on the root node's actual position
sf::Vector2f rootPosition = hostPositions[hosts[0].ip];
view.setCenter(rootPosition);
if (hosts.empty()) {
return;
}

// Draw the connections and nodes
drawConnections(window);
drawNodes(window);
drawHostDetails(window);
}



void NetworkMap::positionHostsUsingForceDirected(float centerX, float centerY) {
const float repulsiveForceStrength = 2000.0f;
const float damping = 0.85f;
Expand Down
1 change: 1 addition & 0 deletions src/NetworkMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class NetworkMap {
sf::FloatRect panLimits;
std::unordered_map<std::string, sf::Vector2f> hostPositions;
std::unordered_map<std::string, sf::Vector2f> forces; // declare forces
bool isViewCenteredInitially = false;

void loadFont(const std::string& fontPath);
void calculatePanLimits();
Expand Down

0 comments on commit c50b26f

Please sign in to comment.