aboutsummaryrefslogtreecommitdiff
path: root/ViewController.cpp
blob: 303d5b977fa39aa69e7f9109cccfc99c9f30e1d6 (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
#include "ViewController.h"
#include "ArtistVisibilityCommand.h"
#include "Exception.h"
#include "KeyboardCode.h"
#include "MouseCode.h"
#include "MuseumPauseCommand.h"
#include "OpenFileGUICommand.h"
#include "TileDecayCommand.h"
#include "View.h"
#include "Museum.h"

ViewController::ViewController(Museum & m, View & v) : museum(m), view(v) {
	this->cmd_base = new Command(this->museum, this->view, *this);
}

ViewController::~ViewController() {
	delete this->cmd_base;
}

void ViewController::update() {
	this->update_size();
	this->update_tiles();
	if (this->draw_artists) this->update_artists();
}

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 = x * scale,
				.y = y * scale,
				.width = scale - line_width,
				.height = scale - line_width,
			};
			this->view.draw_rect(rect, tile.color);
		}
	}
}

void ViewController::update_artists() {
	People & people = this->museum.people;
	for (Artist * artist : people.get_artists()) {
		if (artist == nullptr) continue;
		Rectangle rect = {
			.x = static_cast<unsigned int>(artist->data.x * scale),
			.y = static_cast<unsigned int>(artist->data.y * scale),
			.width = artist_size,
			.height = artist_size,
		};
		this->view.draw_rect(rect, artist->color);
	}
}

void ViewController::ev_keydown(KeyboardCode key) {
	try {
		switch (key) {
			case KEY_SPACE: {
				MuseumPauseCommand(this->cmd_base).toggle();
				break;
			}
			case KEY_ENTER: {
				TileDecayCommand(this->cmd_base).execute(get<0>(this->mouse_pos), get<1>(this->mouse_pos));
				break;
			}
			case KEY_O: {
				OpenFileGUICommand(this->cmd_base).execute();
				break;
			}
			case KEY_A: {
				ArtistVisibilityCommand(this->cmd_base).toggle();
				break;
			}
			case KEY_LEFT: {
				break;
			}
			case KEY_RIGHT: {
				break;
			}
			default: break;
		}
	} catch (Exception & e) {
		printf("%s\n", e.what());
	}
}

void ViewController::ev_mousedown(MouseCode button) {
	try {
		switch (button) {
			case MOUSE_LEFT: {
				TileDecayCommand(this->cmd_base).execute(get<0>(this->mouse_pos), get<1>(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 = {
		static_cast<float>(x) / static_cast<float>(this->scale),
		static_cast<float>(y) / static_cast<float>(this->scale),
	};
}