aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--Command.cpp54
-rw-r--r--Command.h24
-rw-r--r--LoadFilesCommand.cpp26
-rw-r--r--LoadFilesCommand.h15
-rw-r--r--OpenFileGUICommand.cpp12
-rw-r--r--OpenFileGUICommand.h10
-rw-r--r--StepTileCommand.cpp19
-rw-r--r--StepTileCommand.h14
-rw-r--r--TimeTravelCommand.cpp11
-rw-r--r--TimeTravelCommand.h12
-rw-r--r--ToggleArtistVisibilityCommand.cpp10
-rw-r--r--ToggleArtistVisibilityCommand.h11
-rw-r--r--ToggleMuseumPauseCommand.cpp16
-rw-r--r--ToggleMuseumPauseCommand.h14
-rw-r--r--ViewController.cpp14
-rw-r--r--ViewController.h4
-rw-r--r--docs/class-diag.puml55
-rw-r--r--main.cpp7
-rw-r--r--readme.md2
20 files changed, 162 insertions, 169 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c946384..b1b39b8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -42,7 +42,6 @@ add_executable(main
Museum.cpp
People.cpp
Artist.cpp
- Command.cpp
OpenFileGUICommand.cpp
LoadFilesCommand.cpp
ToggleMuseumPauseCommand.cpp
diff --git a/Command.cpp b/Command.cpp
deleted file mode 100644
index f497f0f..0000000
--- a/Command.cpp
+++ /dev/null
@@ -1,54 +0,0 @@
-#include "Command.h"
-#include "Exception.h"
-
-Command::Command(const Command * c) {
- this->set_museum(*c->museum);
- this->set_view(*c->view);
- this->set_controller(*c->controller);
-}
-
-Command::Command(Museum & m, View & v, ViewController & c) {
- this->set_museum(m);
- this->set_view(v);
- this->set_controller(c);
-}
-
-Command::Command(Museum & m, View & v) {
- this->set_museum(m);
- this->set_view(v);
-}
-
-Command::Command(Museum & m) {
- this->set_museum(m);
-}
-
-Museum & Command::get_museum() {
- if (this->museum == nullptr)
- throw Exception("Command error: command needs museum");
- return *this->museum;
-}
-
-View & Command::get_view() {
- if (this->view == nullptr)
- throw Exception("Command error: command needs view");
- return *this->view;
-}
-
-ViewController & Command::get_controller() {
- if (this->controller == nullptr)
- throw Exception("Command error: command needs controller");
- return *this->controller;
-}
-
-void Command::set_museum(Museum & museum) {
- this->museum = &museum;
-}
-
-void Command::set_view(View & view) {
- this->view = &view;
-}
-
-void Command::set_controller(ViewController & controller) {
- this->controller = &controller;
-}
-
diff --git a/Command.h b/Command.h
index b974784..0ad57f9 100644
--- a/Command.h
+++ b/Command.h
@@ -1,29 +1,7 @@
#pragma once
-class Museum;
-class View;
-class ViewController;
-
class Command {
public:
- Command(const Command * c);
- Command(Museum & m, View & v, ViewController & c);
- Command(Museum & m, View & v);
- Command(Museum & m);
-
-protected:
- Museum & get_museum();
- View & get_view();
- ViewController & get_controller();
-
-protected:
- void set_museum(Museum &);
- void set_view(View &);
- void set_controller(ViewController &);
-
-private:
- Museum * museum = nullptr;
- View * view = nullptr;
- ViewController * controller = nullptr;
+ virtual void execute() = 0;
};
diff --git a/LoadFilesCommand.cpp b/LoadFilesCommand.cpp
index ae9f30a..82ede60 100644
--- a/LoadFilesCommand.cpp
+++ b/LoadFilesCommand.cpp
@@ -11,26 +11,22 @@
using namespace std;
-void LoadFilesCommand::execute(int argc, char ** argv) {
+LoadFilesCommand::LoadFilesCommand(Museum & m, int argc, char ** argv) : museum(m) {
vector<string> files = {};
for (size_t i = 0; i < argc; i++) {
files.push_back(string(argv[i]));
}
- return this->execute(files);
+ this->files = files;
}
-void LoadFilesCommand::execute(vector<string> files) {
- try {
- this->load_files(files);
- } catch (Exception & e) {
- throw Exception("LoadFilesCommand error: %s", e.what());
- }
+LoadFilesCommand::LoadFilesCommand(Museum & m, vector<string> files) : museum(m) {
+ this->files = files;
}
-void LoadFilesCommand::load_files(vector<string> files) {
- ToggleMuseumPauseCommand(this).set(true);
+void LoadFilesCommand::load_files() {
+ ToggleMuseumPauseCommand(this->museum, true).execute();
- MuseumDeserializer deserializer { this->get_museum() };
+ MuseumDeserializer deserializer { this->museum };
for (string url : files) {
unique_ptr<FileReader> file = FileReaderFactory::open(url);
try {
@@ -41,3 +37,11 @@ void LoadFilesCommand::load_files(vector<string> files) {
}
}
+void LoadFilesCommand::execute() {
+ try {
+ this->load_files();
+ } catch (Exception & e) {
+ throw Exception("LoadFilesCommand error: %s", e.what());
+ }
+}
+
diff --git a/LoadFilesCommand.h b/LoadFilesCommand.h
index e13d873..19913d8 100644
--- a/LoadFilesCommand.h
+++ b/LoadFilesCommand.h
@@ -5,14 +5,21 @@
#include "Command.h"
+class Museum;
+
class LoadFilesCommand : public Command {
- using Command::Command;
+public:
+ LoadFilesCommand(Museum & m, std::vector<std::string> files);
+ LoadFilesCommand(Museum & m, int argc, char ** argv);
public:
- virtual void execute(std::vector<std::string> files);
- virtual void execute(int argc, char ** argv);
+ virtual void execute();
+
+private:
+ void load_files();
private:
- void load_files(std::vector<std::string> files);
+ Museum & museum;
+ std::vector<std::string> files;
};
diff --git a/OpenFileGUICommand.cpp b/OpenFileGUICommand.cpp
index 13751b2..c23a3cf 100644
--- a/OpenFileGUICommand.cpp
+++ b/OpenFileGUICommand.cpp
@@ -9,17 +9,17 @@
using namespace std;
-void OpenFileGUICommand::execute() {
- Museum * museum = &this->get_museum();
+OpenFileGUICommand::OpenFileGUICommand(Museum & m, View & v) : museum(m), view(v) {}
- ToggleMuseumPauseCommand(*museum).set(true);
- this->get_view().dialog_file([](vector<string> files, void * data) -> void {
+void OpenFileGUICommand::execute() {
+ ToggleMuseumPauseCommand(this->museum, true);
+ this->view.dialog_file([](vector<string> files, void * data) -> void {
Museum * museum = static_cast<Museum *>(data);
try {
- LoadFilesCommand(*museum).execute(files);
+ LoadFilesCommand(*museum, files).execute();
} catch (Exception & e) {
printf("%s\n", e.what());
}
- }, museum);
+ }, &this->museum);
}
diff --git a/OpenFileGUICommand.h b/OpenFileGUICommand.h
index 9b504a9..1990ce7 100644
--- a/OpenFileGUICommand.h
+++ b/OpenFileGUICommand.h
@@ -2,10 +2,18 @@
#include "Command.h"
+class Museum;
+class View;
+
class OpenFileGUICommand : public Command {
- using Command::Command;
+public:
+ OpenFileGUICommand(Museum & m, View & v);
public:
virtual void execute();
+
+private:
+ Museum & museum;
+ View & view;
};
diff --git a/StepTileCommand.cpp b/StepTileCommand.cpp
index ff5c6b2..cc7d62d 100644
--- a/StepTileCommand.cpp
+++ b/StepTileCommand.cpp
@@ -1,11 +1,18 @@
#include "StepTileCommand.h"
-#include "Museum.h"
+#include "Canvas.h"
-void StepTileCommand::execute(unsigned int x, unsigned int y) {
- Museum & museum = this->get_museum();
- if (x >= museum.canvas.data.columns) return;
- if (y >= museum.canvas.data.rows) return;
- Tile & tile = museum.canvas.get_tile(x, y);
+using namespace std;
+
+StepTileCommand::StepTileCommand(Canvas & c, pair<unsigned int, unsigned int> tile) : canvas(c) {
+ this->x = tile.first;
+ this->y = tile.second;
+}
+
+void StepTileCommand::execute() {
+ Canvas & canvas = this->canvas;
+ if (this->x >= canvas.data.columns) return;
+ if (this->y >= canvas.data.rows) return;
+ Tile & tile = canvas.get_tile(this->x, this->y);
tile.step(nullptr);
tile.update();
}
diff --git a/StepTileCommand.h b/StepTileCommand.h
index ce9dccd..24d0b36 100644
--- a/StepTileCommand.h
+++ b/StepTileCommand.h
@@ -1,11 +1,21 @@
#pragma once
+#include <utility>
+
#include "Command.h"
+class Canvas;
+
class StepTileCommand : public Command {
- using Command::Command;
+public:
+ StepTileCommand(Canvas & c, std::pair<unsigned int, unsigned int> tile);
public:
- virtual void execute(unsigned int x, unsigned int y);
+ virtual void execute();
+
+private:
+ Canvas & canvas;
+ unsigned int x;
+ unsigned int y;
};
diff --git a/TimeTravelCommand.cpp b/TimeTravelCommand.cpp
index 34468c0..44320c9 100644
--- a/TimeTravelCommand.cpp
+++ b/TimeTravelCommand.cpp
@@ -1,11 +1,14 @@
#include "TimeTravelCommand.h"
#include "Museum.h"
-void TimeTravelCommand::forwards() {
- this->get_museum().skip_forward();
+TimeTravelCommand::TimeTravelCommand(Museum & m, bool forwards) : museum(m) {
+ this->forwards = forwards;
}
-void TimeTravelCommand::backwards() {
- this->get_museum().skip_backward();
+void TimeTravelCommand::execute() {
+ if (this->forwards)
+ this->museum.skip_forward();
+ else
+ this->museum.skip_backward();
}
diff --git a/TimeTravelCommand.h b/TimeTravelCommand.h
index 7803134..359e63e 100644
--- a/TimeTravelCommand.h
+++ b/TimeTravelCommand.h
@@ -2,11 +2,17 @@
#include "Command.h"
+class Museum;
+
class TimeTravelCommand : public Command {
- using Command::Command;
+public:
+ TimeTravelCommand(Museum & m, bool forwards = false);
public:
- virtual void forwards();
- virtual void backwards();
+ virtual void execute();
+
+private:
+ Museum & museum;
+ bool forwards;
};
diff --git a/ToggleArtistVisibilityCommand.cpp b/ToggleArtistVisibilityCommand.cpp
index 6c22dbc..17583de 100644
--- a/ToggleArtistVisibilityCommand.cpp
+++ b/ToggleArtistVisibilityCommand.cpp
@@ -1,13 +1,11 @@
#include "ToggleArtistVisibilityCommand.h"
#include "ViewController.h"
-void ToggleArtistVisibilityCommand::set(bool visible) {
- ViewController & controller = this->get_controller();
- controller.set_artists_visible(visible);
+ToggleArtistVisibilityCommand::ToggleArtistVisibilityCommand(ViewController & c) : controller(c) {
}
-void ToggleArtistVisibilityCommand::toggle() {
- ViewController & controller = this->get_controller();
- controller.set_artists_visible(!controller.get_artists_visible());
+void ToggleArtistVisibilityCommand::execute() {
+ bool value = !this->controller.get_artists_visible();
+ this->controller.set_artists_visible(value);
}
diff --git a/ToggleArtistVisibilityCommand.h b/ToggleArtistVisibilityCommand.h
index 47e2158..1653d08 100644
--- a/ToggleArtistVisibilityCommand.h
+++ b/ToggleArtistVisibilityCommand.h
@@ -2,11 +2,16 @@
#include "Command.h"
+class ViewController;
+
class ToggleArtistVisibilityCommand : public Command {
- using Command::Command;
+public:
+ ToggleArtistVisibilityCommand(ViewController & c);
public:
- virtual void toggle();
- virtual void set(bool visible);
+ virtual void execute();
+
+private:
+ ViewController & controller;
};
diff --git a/ToggleMuseumPauseCommand.cpp b/ToggleMuseumPauseCommand.cpp
index 57176af..bdd2ef6 100644
--- a/ToggleMuseumPauseCommand.cpp
+++ b/ToggleMuseumPauseCommand.cpp
@@ -1,13 +1,17 @@
#include "ToggleMuseumPauseCommand.h"
#include "Museum.h"
-void ToggleMuseumPauseCommand::set(bool paused) {
- Museum & museum = this->get_museum();
- museum.set_pause(paused);
+ToggleMuseumPauseCommand::ToggleMuseumPauseCommand(Museum & m) : museum(m) {
+ this->toggle = true;
}
-void ToggleMuseumPauseCommand::toggle() {
- Museum & museum = this->get_museum();
- museum.set_pause(!museum.get_pause());
+ToggleMuseumPauseCommand::ToggleMuseumPauseCommand(Museum & m, bool set) : museum(m) {
+ this->toggle = false;
+ this->value = set;
+}
+
+void ToggleMuseumPauseCommand::execute() {
+ if (this->toggle) this->value = !this->museum.get_pause();
+ this->museum.set_pause(this->value);
}
diff --git a/ToggleMuseumPauseCommand.h b/ToggleMuseumPauseCommand.h
index 15d1388..40e0b24 100644
--- a/ToggleMuseumPauseCommand.h
+++ b/ToggleMuseumPauseCommand.h
@@ -2,11 +2,19 @@
#include "Command.h"
+class Museum;
+
class ToggleMuseumPauseCommand : public Command {
- using Command::Command;
+public:
+ ToggleMuseumPauseCommand(Museum & m);
+ ToggleMuseumPauseCommand(Museum & m, bool set);
public:
- virtual void toggle();
- virtual void set(bool paused);
+ virtual void execute();
+
+private:
+ Museum & museum;
+ bool toggle = true;
+ bool value;
};
diff --git a/ViewController.cpp b/ViewController.cpp
index a37c49a..d64ec35 100644
--- a/ViewController.cpp
+++ b/ViewController.cpp
@@ -11,11 +11,9 @@
#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() {
@@ -71,27 +69,27 @@ void ViewController::ev_keydown(KeyboardCode key) {
try {
switch (key) {
case KEY_SPACE: {
- ToggleMuseumPauseCommand(this->cmd_base).toggle();
+ ToggleMuseumPauseCommand(this->museum).execute();
break;
}
case KEY_ENTER: {
- StepTileCommand(this->cmd_base).execute(get<0>(this->mouse_pos), get<1>(this->mouse_pos));
+ StepTileCommand(this->museum.canvas, this->mouse_pos).execute();
break;
}
case KEY_O: {
- OpenFileGUICommand(this->cmd_base).execute();
+ OpenFileGUICommand(this->museum, this->view).execute();
break;
}
case KEY_A: {
- ToggleArtistVisibilityCommand(this->cmd_base).toggle();
+ ToggleArtistVisibilityCommand(*this).execute();
break;
}
case KEY_LEFT: {
- TimeTravelCommand(this->cmd_base).backwards();
+ TimeTravelCommand(this->museum, false).execute();
break;
}
case KEY_RIGHT: {
- TimeTravelCommand(this->cmd_base).forwards();
+ TimeTravelCommand(this->museum, true).execute();
break;
}
default: break;
diff --git a/ViewController.h b/ViewController.h
index 9066d05..bccacd0 100644
--- a/ViewController.h
+++ b/ViewController.h
@@ -1,6 +1,6 @@
#pragma once
-#include <tuple>
+#include <utility>
#include "Command.h"
#include "KeyboardCode.h"
@@ -44,6 +44,6 @@ private:
unsigned int scale = 16;
unsigned int line_width = 0;
unsigned int artist_size = (scale - line_width) / 2;
- std::tuple<float, float> mouse_pos = { 0, 0 };
+ std::pair<float, float> mouse_pos = { 0, 0 };
};
diff --git a/docs/class-diag.puml b/docs/class-diag.puml
index a6fc1cf..56a334f 100644
--- a/docs/class-diag.puml
+++ b/docs/class-diag.puml
@@ -294,7 +294,6 @@ rectangle Group_Visualization as "Visualization" <<group>> {
+ ev_mousemove(x, y);
--
- draw_artists : bool <<+get>> <<+set>>
- - cmd_base : const Command *
}
ViewController ..> View
@@ -306,45 +305,59 @@ rectangle Group_Visualization as "Visualization" <<group>> {
ViewController .l> MouseCode
}
rectangle Group_Commands as "Commands" <<group>> {
- class Command {
- # museum : Museum * <<+get&>> <<-set>>
- # view : View * <<+get&>> <<-set>>
- # controller : ViewController * <<+get&>> <<-set>>
- --
- + Command(command : const Command *)
- + Command(museum, view, controller)
+ interface Command {
+ + execute()
}
class ToggleMuseumPauseCommand {
- + toggle()
- + set(paused : bool)
+ + constructor(Museum &)
+ + constructor(Museum &, set : bool)
+ --
+ toggle : bool
+ value : bool
}
class OpenFileGUICommand {
- + execute()
+ + constructor(Museum &, View &)
+ --
+ - museum : Museum &
+ - view : View &
}
class ToggleArtistVisibilityCommand {
- + toggle()
- + set(paused : bool)
+ + constructor(ViewController &)
+ --
+ - controller : ViewController &
}
class LoadFilesCommand {
- + execute(files)
+ + constructor(Museum &, files : vec<string>)
+ + constructor(Museum &, argc, argv)
+ --
+ - load_files()
+ - museum : Museum &
+ - files : vec<string>
}
class StepTileCommand {
- + execute(x, y)
+ + constructor(Canvas &, pair<x, y>)
+ --
+ - canvas : Canvas &
+ - x : unsigned int
+ - y : unsigned int
+ }
+ class TimeTravelCommand {
+ + constructor(Museum &, forwards : bool)
+ --
+ - museum : Museum &
+ - forwards : bool
}
- Command <|-u- ToggleMuseumPauseCommand
+ Command <|-d- ToggleMuseumPauseCommand
Command <|-u- OpenFileGUICommand
Command <|-u- ToggleArtistVisibilityCommand
- Command <|-u- StepTileCommand
+ Command <|-d- StepTileCommand
Command <|-d- LoadFilesCommand
+ Command <|-d- TimeTravelCommand
}
} /' LAYOUT '/
-Command .[norank]> Museum
-Command .[norank]> View
-Command .[norank]> ViewController
-
Parser .l> FileReader
MuseumDeserializer .l> Museum
diff --git a/main.cpp b/main.cpp
index 9cc8ee8..d601165 100644
--- a/main.cpp
+++ b/main.cpp
@@ -12,7 +12,7 @@ int main(int argc, char** argv) {
View view { museum };
try {
- LoadFilesCommand(museum).execute(argc - 1, &argv[1]);
+ LoadFilesCommand(museum, argc - 1, &argv[1]).execute();
} catch (Exception & e) {
printf("%s\n", e.what());
return EXIT_FAILURE; // invalid files on start cause exit
@@ -20,8 +20,9 @@ int main(int argc, char** argv) {
// Museum and View both create their own worker threads, so the main thread
// should remain to keep the view active.
- while (view.open)
- ;
+ while (view.open) {
+ continue;
+ }
return EXIT_SUCCESS;
}
diff --git a/readme.md b/readme.md
index ab0ca1a..11574a0 100644
--- a/readme.md
+++ b/readme.md
@@ -14,9 +14,7 @@ ALGA:
TODO:
-- command pattern is fout
- low-binding factories naar high-binding factories
- prototype met geen factory maar map ofzo fixen
- txt file parser
-- naamgeving