Skip to content

Instantly share code, notes, and snippets.

@clooth
Created September 23, 2013 15:38
Show Gist options
  • Save clooth/6672393 to your computer and use it in GitHub Desktop.
Save clooth/6672393 to your computer and use it in GitHub Desktop.
View Edge Scrolling
// Map scrolling functions
auto panMap = [&mapView, &minimapView] (float speed, int dir) {
float timeValue = TimePerFrame.asSeconds();
float moveValue = speed * timeValue;
if (dir == 0) { mapView.move(-moveValue, 0.f); minimapView.move(-moveValue, 0.f); }
if (dir == 1) { mapView.move(moveValue, 0.f); minimapView.move(moveValue, 0.f); }
if (dir == 2) { mapView.move(0.f, -moveValue); minimapView.move(0.f, -moveValue); }
if (dir == 3) { mapView.move(0.f, moveValue); minimapView.move(0.f, moveValue); }
};
auto panMapLeft = [panMap] (float speed) { panMap(speed, 0); };
auto panMapRight = [panMap] (float speed) { panMap(speed, 1); };
auto panMapUp = [panMap] (float speed) { panMap(speed, 2); };
auto panMapDown = [panMap] (float speed) { panMap(speed, 3); };
sf::Vector2i mousePosition = sf::Mouse::getPosition(window);
// Map scrolling speed
float mapScrollSpeed = 100.f;
// Map scroll trigger edge inset size in pixels
float mapScrollTreshold = 50.f;
// Map edge treshold
float tresholdY = mapScrollTreshold;
float tresholdX = mapScrollTreshold;
float tresholdXEnd = window.getSize().x - mapScrollTreshold;
float tresholdYEnd = window.getSize().y - mapScrollTreshold;
float windowWidth = window.getSize().x;
float windowHeight = window.getSize().y;
float mouseX = mousePosition.x;
float mouseY = mousePosition.y;
if (mouseX < mapScrollTreshold && mouseX >= 0) {
mapScrollSpeed += (mapScrollTreshold - mouseX) * 2.5f;
panMapLeft(mapScrollSpeed);
}
if (mouseX >= windowWidth - mapScrollTreshold && mouseX <= windowWidth) {
mapScrollSpeed += (mouseX - tresholdXEnd) * 2.5f;
panMapRight(mapScrollSpeed);
}
if (mouseY < mapScrollTreshold && mouseY >= 0) {
mapScrollSpeed += (mapScrollTreshold - mouseY) * 2.5f;
panMapUp(mapScrollSpeed);
}
if (mouseY >= windowHeight - mapScrollTreshold && mouseY <= windowHeight) {
mapScrollSpeed += (mouseY - tresholdYEnd) * 2.5f;
panMapDown(mapScrollSpeed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment