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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#include <memory>
#include "ViewController.h"
#include "QuadTree.h"
#include "ToggleArtistVisibilityCommand.h"
#include "Exception.h"
#include "KeyboardCode.h"
#include "MouseCode.h"
#include "ToggleMuseumPauseCommand.h"
#include "OpenFileGUICommand.h"
#include "StepTileCommand.h"
#include "TimeTravelCommand.h"
#include "View.h"
#include "Museum.h"
using namespace std;
ViewController::ViewController(Museum & m, View & v) : museum(m), view(v) {
}
ViewController::~ViewController() {
}
void ViewController::update() {
this->update_size();
this->update_tiles();
if (this->draw_artists) this->update_artists();
if (this->draw_pathfinding) this->update_pathfinding();
if (this->draw_quadtree) this->update_quadtree();
}
void ViewController::update_size() {
this->view.window_size(
this->scale * this->museum.canvas.data.columns,
this->scale * this->museum.canvas.data.rows
);
}
void ViewController::update_tiles() {
for (unsigned y = 0; y < this->museum.canvas.data.rows; y++) {
for (unsigned x = 0; x < this->museum.canvas.data.columns; x++) {
Tile & tile = this->museum.canvas.get_tile(x, y);
Rectangle rect = {
.x = static_cast<float>(x * scale),
.y = static_cast<float>(y * scale),
.width = static_cast<float>(scale - line_width),
.height = static_cast<float>(scale - line_width),
};
this->view.fill_rect(rect, tile.color);
}
}
}
void ViewController::update_artists() {
People & people = this->museum.people;
for (Artist * artist : people.get_artists()) {
Rectangle rect = {
.x = static_cast<float>(artist->data.x * scale),
.y = static_cast<float>(artist->data.y * scale),
.width = static_cast<float>(artist_size),
.height = static_cast<float>(artist_size),
};
this->view.fill_rect(rect, artist->color);
}
}
void ViewController::update_pathfinding() {
}
void ViewController::update_quadtree_recursive(QuadTree * tree) {
if (tree == nullptr) return;
Rectangle rect = {
.x = tree->boundary.x * scale,
.y = tree->boundary.y * scale,
.width = tree->boundary.width * scale,
.height = tree->boundary.height * scale,
};
this->view.draw_rect(rect, { .red = 0xff, .green = 0x00, .blue = 0xff, }, 2);
this->update_quadtree_recursive(tree->subtree[0].get());
this->update_quadtree_recursive(tree->subtree[1].get());
this->update_quadtree_recursive(tree->subtree[2].get());
this->update_quadtree_recursive(tree->subtree[3].get());
}
void ViewController::update_quadtree() {
shared_ptr<QuadTree> tree = this->museum.collision.get_quadtree();
this->update_quadtree_recursive(tree.get());
}
void ViewController::ev_keydown(KeyboardCode key) {
try {
switch (key) {
case KEY_SPACE: {
ToggleMuseumPauseCommand(this->museum).execute();
break;
}
case KEY_ENTER: {
StepTileCommand(this->museum.canvas, this->mouse_pos).execute();
break;
}
case KEY_O: {
OpenFileGUICommand(this->museum, this->view).execute();
break;
}
case KEY_A: {
ToggleArtistVisibilityCommand(*this).execute();
break;
}
case KEY_LEFT: {
TimeTravelCommand(this->museum, false).execute();
break;
}
case KEY_RIGHT: {
TimeTravelCommand(this->museum, true).execute();
break;
}
default: break;
}
} catch (Exception & e) {
printf("%s\n", e.what());
}
}
void ViewController::ev_mousedown(MouseCode button) {
try {
switch (button) {
case MOUSE_LEFT: {
// TODO: pathfinding start point
break;
}
case MOUSE_RIGHT: {
// TODO: pathfinding end point
break;
}
default: break;
}
} catch (Exception & e) {
printf("%s\n", e.what());
}
}
void ViewController::ev_mousemove(unsigned x, unsigned y) {
this->mouse_pos = {
static_cast<float>(x) / static_cast<float>(this->scale),
static_cast<float>(y) / static_cast<float>(this->scale),
};
}
|