aboutsummaryrefslogtreecommitdiff
path: root/ViewController.cpp
blob: 3238f0fd24bcc5cc83152c0e48a32b91806f6cf0 (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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <memory>

#include "ViewController.h"
#include "CollisionChecker.h"
#include "ControlBooleanCommand.h"
#include "CycleCollisionMethodCommand.h"
#include "CyclePathfindingMethodCommand.h"
#include "QuadTreeCollisionChecker.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"
#include "Pathfinder.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();
	this->update_artists();
	this->update_pathfinding();
	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 (int y = 0; y < this->museum.canvas.data.rows; y++) {
		for (int x = 0; x < this->museum.canvas.data.columns; x++) {
			Tile & tile = this->museum.canvas.get_tile({ x, y });
			Rectangle rect = {
				.x = x * scale,
				.y = y * scale,
				.width = scale,
				.height = scale,
			};
			this->view.fill_rect(rect, tile.color);
		}
	}
}

void ViewController::update_artists() {
	if (!this->draw_artists) return;

	People & people = this->museum.people;
	for (Artist * artist : people.get_artists()) {
		Rectangle rect = {
			.x = artist->data.x * scale,
			.y = artist->data.y * scale,
			.width = artist_size,
			.height = artist_size,
		};
		this->view.fill_rect(rect, artist->color);
	}
}

void ViewController::draw_pathfinding_dot(const XY & point, const Color & color) {
	this->view.fill_rect(center({
		.x = point.x * scale,
		.y = point.y * scale,
		.width = pathfinding_size,
		.height = pathfinding_size,
	}), color);
}

void ViewController::update_pathfinding() {
  PathfindingContext & ctx = this->museum.pathfinding;

	Pathfinder & solver = ctx.get_solver();
	if (this->draw_visited) {
		for (int y = 0; y < this->museum.canvas.data.rows; y++) {
			for (int x = 0; x < this->museum.canvas.data.columns; x++) {
				if (!solver.is_visited({ x, y })) continue;
				Rectangle rect = {
					.x = x * scale,
					.y = y * scale,
					.width = scale,
					.height = scale,
				};
				this->view.draw_rect(rect, { 0, 0, 0 }, 2);
			}
		}
	}

	if (this->draw_path) {
		const Pathfinder::Path & solution = solver.get_path();
		for (const XY & point : solution) {
			this->draw_pathfinding_dot(point, { 0, 0, 0 });
		}
		this->draw_pathfinding_dot(ctx.get_start(), { 0xff, 0xff, 0xff });
	}
}

void ViewController::update_quadtree_recursive(QuadTreeCollisionChecker * tree) {
	if (tree == nullptr) return;

	const Rectangle & tree_boundary = tree->get_boundary();
	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, }, 1);

	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() {
	if (!this->draw_quadtree) return;
	shared_ptr<CollisionChecker> checker = this->museum.collision.get_checker();
	auto tree = dynamic_cast<QuadTreeCollisionChecker*>(checker.get());
	if (tree == nullptr) return;
	this->update_quadtree_recursive(tree);
}

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: {
				ControlBooleanCommand(this->draw_artists).execute();
				break;
			}
			case KEY_LEFT: {
				TimeTravelCommand(this->museum, false).execute();
				break;
			}
			case KEY_RIGHT: {
				TimeTravelCommand(this->museum, true).execute();
				break;
			}
			case KEY_C: {
				CycleCollisionMethodCommand(this->museum).execute();
				break;
			}
			case KEY_Q: {
				ControlBooleanCommand(this->draw_quadtree).execute();
				break;
			}
			case KEY_P: {
				ControlBooleanCommand(this->draw_path).execute();
				break;
			}
			case KEY_V: {
				ControlBooleanCommand(this->draw_visited).execute();
				break;
			}
			case KEY_D: {
				CyclePathfindingMethodCommand(this->museum).execute();
				break;
			}
			case KEY_W: {
				// TODO: toggle collision calculation between artist and path
				break;
			}
			default: break;
		}
	} catch (Exception & e) {
		printf("%s\n", e.what());
	}
}

void ViewController::ev_mousedown(MouseCode button) {
	try {
		switch (button) {
			case MOUSE_LEFT: {
				// TODO: call through command
        this->museum.pathfinding.set_start(this->mouse_pos);
				break;
			}
			case MOUSE_RIGHT: {
        this->museum.pathfinding.set_end(this->mouse_pos);
				break;
			}
			default: break;
		}
	} catch (Exception & e) {
		printf("%s\n", e.what());
	}
}

void ViewController::ev_mousemove(unsigned x, unsigned y) {
	this->mouse_pos = {
		.x = static_cast<int>(x / this->scale),
		.y = static_cast<int>(y / this->scale),
	};
}

Rectangle ViewController::center(Rectangle rect) {
  rect.x += (scale - rect.width) / 2;
  rect.y += (scale - rect.height) / 2;
  return rect;
}