blob: 17c172715b3a81ae453d7289ac314112a94c4341 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#include "common.hpp"
namespace interface {
state_t::state_t(sf::RenderWindow *window_, game::state_t *game_)
{
window = window_;
game = game_;
}
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 == 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 > 11)
camera.target_zoom = 11;
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;
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
|