summaryrefslogtreecommitdiff
path: root/src/interface.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/interface.cpp')
-rw-r--r--src/interface.cpp79
1 files changed, 55 insertions, 24 deletions
diff --git a/src/interface.cpp b/src/interface.cpp
index 0650889..d6137fd 100644
--- a/src/interface.cpp
+++ b/src/interface.cpp
@@ -8,11 +8,11 @@ state_t::state_t(sf::RenderWindow *window_, game::state_t *game_)
game = game_;
}
-static sf::Vector2f compute_drag(sf::RenderWindow *window, sf::Vector2f drag_ref)
+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 - drag_ref);
+ return -(vmouse - pan_ref);
}
void state_t::tick(void)
@@ -20,6 +20,7 @@ void state_t::tick(void)
sf::Vector2u size;
sf::Event event;
sf::Vector2f view_size;
+ v2f_t wmouse; // Mouse position in world space;
size = window->getSize();
@@ -42,30 +43,46 @@ void state_t::tick(void)
while (window->pollEvent(event)) {
+ // FIXME: refactor this nested switch clusterfuck
switch (event.type) {
case sf::Event::Closed:
window->close();
return;
case sf::Event::MouseButtonPressed:
- if (event.mouseButton.button == 0) {
- v2f_t point = window->mapPixelToCoords(
- sf::Vector2i(event.mouseButton.x,
- event.mouseButton.y));
- game->debug_click(point);
- } else if (event.mouseButton.button == 1) {
- camera.dragging = true;
- camera.drag_ref = window->mapPixelToCoords(
- sf::Vector2i(event.mouseButton.x,
- event.mouseButton.y));
+ 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::Middle:
+ camera.panning = true;
+ camera.pan_ref = wmouse;
+ break;
+
+ default:;
}
break;
case sf::Event::MouseButtonReleased:
- if (event.mouseButton.button == 1) {
- if (camera.dragging)
- camera.center += compute_drag(window, camera.drag_ref);
- camera.dragging = false;
+ 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;
@@ -78,22 +95,36 @@ void state_t::tick(void)
break;
case sf::Event::KeyPressed:
- if (event.key.code == sf::Keyboard::Key::I) {
- sf::Vector2i mouse = sf::Mouse::getPosition(*window);
- sf::Vector2f vmouse = window->mapPixelToCoords(mouse);
- game->world.debug_point(vmouse);
- }
- break;
+ 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.dragging) {
- sf::Vector2f delta = compute_drag(window, camera.drag_ref);
+ 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