diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2017-10-07 08:03:44 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2017-10-07 08:03:44 +0200 |
commit | 9e7d64d052eabcf40d85c3e903aaba44903a380a (patch) | |
tree | 0a3a2c7f53884bd035b54812a77d493be058f43c /src/interface.cpp |
Initial commit.
Diffstat (limited to 'src/interface.cpp')
-rw-r--r-- | src/interface.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/interface.cpp b/src/interface.cpp new file mode 100644 index 0000000..aacc4b0 --- /dev/null +++ b/src/interface.cpp @@ -0,0 +1,92 @@ +#include "common.hpp" + +namespace interface { + +state_t::state_t(sf::RenderWindow *window_, world::world_t *world_) +{ + window = window_; + world = world_; +} + +static sf::Vector2f compute_drag(sf::RenderWindow *window, sf::Vector2f drag_ref) +{ + sf::Vector2i mouse = sf::Mouse::getPosition(*window); + sf::Vector2f vmouse = window->mapPixelToCoords(mouse); + return -(vmouse - drag_ref); +} + +void state_t::tick(void) +{ + sf::Vector2u size; + sf::Event event; + sf::Vector2f view_size; + + size = window->getSize(); + + camera.zoom = (camera.zoom + camera.target_zoom) / 2; + + { + float view_scale; + + view_scale = 3 * exp(camera.zoom * 0.3); + if (size.x > size.y) { + view_size.y = view_scale; + view_size.x = view_scale * size.x / size.y; + } else { + view_size.x = view_scale; + view_size.y = view_scale * size.y / size.x; + } + + window->setView(sf::View(camera.center, view_size)); + } + + + while (window->pollEvent(event)) { + switch (event.type) { + case sf::Event::Closed: + window->close(); + return; + + case sf::Event::MouseButtonPressed: + if (event.mouseButton.button == 0) { + sf::Vector2f vmouse = window->mapPixelToCoords( + sf::Vector2i(event.mouseButton.x, + event.mouseButton.y)); + world->get_tile(vmouse.x, vmouse.y)->type = 0; + } + if (event.mouseButton.button == 1) { + camera.dragging = true; + camera.drag_ref = window->mapPixelToCoords( + sf::Vector2i(event.mouseButton.x, + event.mouseButton.y)); + } + break; + + case sf::Event::MouseButtonReleased: + if (event.mouseButton.button == 1) { + if (camera.dragging) + camera.center += compute_drag(window, camera.drag_ref); + camera.dragging = false; + } + break; + + case sf::Event::MouseWheelScrolled: + camera.target_zoom -= event.mouseWheelScroll.delta; + if (camera.target_zoom < 0) + camera.target_zoom = 0; + if (camera.target_zoom > 10) + camera.target_zoom = 10; + break; + + default:; + } + } + + if (camera.dragging) { + sf::Vector2f delta = compute_drag(window, camera.drag_ref); + window->setView(sf::View(camera.center + delta, view_size)); + } else + window->setView(sf::View(camera.center, view_size)); +} + +} // namespace interface |