diff options
Diffstat (limited to 'src/game/interface.cpp')
-rw-r--r-- | src/game/interface.cpp | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/src/game/interface.cpp b/src/game/interface.cpp new file mode 100644 index 0000000..c89cdad --- /dev/null +++ b/src/game/interface.cpp @@ -0,0 +1,134 @@ +#include "game.hpp" + +namespace interface { + +state_t::state_t(sf::RenderWindow *window_, game::state_t *game_) +{ + window = window_; + game = game_; +} + +static sf::Vector2f compute_pan(sf::RenderWindow *window, sf::Vector2f pan_ref) +{ + sf::Vector2i mouse = sf::Mouse::getPosition(*window); + sf::Vector2f vmouse = window->mapPixelToCoords(mouse); + return -(vmouse - pan_ref); +} + +void state_t::tick(double dt) +{ + sf::Vector2u size; + sf::Event event; + sf::Vector2f view_size; + v2f_t wmouse; // Mouse position in world space; + + size = window->getSize(); + + camera.zoom = expfade(camera.zoom, camera.target_zoom, 15, dt); + + { + 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)) { + // FIXME: refactor this nested switch clusterfuck + switch (event.type) { + case sf::Event::Closed: + window->close(); + return; + + case sf::Event::MouseButtonPressed: + wmouse = window->mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y)); + + switch (event.mouseButton.button) { + case sf::Mouse::Button::Left: + select.selecting = true; + select.rect[0] = wmouse; + select.rect[1] = wmouse; + break; + + case sf::Mouse::Button::Right: + game->command(wmouse); + break; + + case sf::Mouse::Button::Middle: + camera.panning = true; + camera.pan_ref = wmouse; + break; + + default:; + } + break; + + case sf::Event::MouseButtonReleased: + switch (event.mouseButton.button) { + case sf::Mouse::Button::Left: + if (select.selecting) + game->select(select.rect); + select.selecting = false; + break; + + case sf::Mouse::Button::Middle: + if (camera.panning) + camera.center += compute_pan(window, camera.pan_ref); + camera.panning = false; + break; + + default:; + } + break; + + case sf::Event::MouseWheelScrolled: + camera.target_zoom -= event.mouseWheelScroll.delta; + if (camera.target_zoom < 0) + camera.target_zoom = 0; + if (camera.target_zoom > 11) + camera.target_zoom = 11; + break; + + case sf::Event::KeyPressed: + switch (event.key.code) { + case sf::Keyboard::Key::F1: + debug_draw_cmodels ^= 1; + break; + + case sf::Keyboard::Key::F2: + debug_draw_paths ^= 1; + break; + + case sf::Keyboard::Key::F3: + debug_draw_tile_coords ^= 1; + break; + + default:; + } + default:; + } + } + + if (camera.panning) { + sf::Vector2f delta = compute_pan(window, camera.pan_ref); + window->setView(sf::View(camera.center + delta, view_size)); + } else + window->setView(sf::View(camera.center, view_size)); + + // Compute this _after_ the setView above. + wmouse = window->mapPixelToCoords(sf::Mouse::getPosition(*window)); + + if (select.selecting) + select.rect[1] = wmouse; +} + +} // namespace interface |