diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | Command.h | 3 | ||||
-rw-r--r-- | LoadFilesCommand.cpp | 6 | ||||
-rw-r--r-- | LoadFilesCommand.h | 7 | ||||
-rw-r--r-- | Museum.h | 1 | ||||
-rw-r--r-- | MuseumPauseCommand.cpp | 13 | ||||
-rw-r--r-- | MuseumPauseCommand.h | 12 | ||||
-rw-r--r-- | OpenFileGUICommand.cpp | 15 | ||||
-rw-r--r-- | OpenFileGUICommand.h | 3 | ||||
-rw-r--r-- | ViewController.cpp | 76 | ||||
-rw-r--r-- | ViewController.h | 6 | ||||
-rw-r--r-- | docs/class-diag.puml | 3 |
12 files changed, 98 insertions, 48 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index bedbdea..ac6511d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ add_executable(main Command.cpp OpenFileGUICommand.cpp LoadFilesCommand.cpp + MuseumPauseCommand.cpp ) target_link_libraries(main @@ -6,9 +6,6 @@ class ViewController; class Command { public: - virtual void execute() = 0; - -public: Command(const Command * c); Command(Museum & m, View & v, ViewController & c); Command(Museum & m, View & v); diff --git a/LoadFilesCommand.cpp b/LoadFilesCommand.cpp index 05b1311..6bcf7da 100644 --- a/LoadFilesCommand.cpp +++ b/LoadFilesCommand.cpp @@ -6,7 +6,7 @@ #include "Exception.h" #include "FileReader.h" #include "FileStrategy.h" -#include "Museum.h" +#include "MuseumPauseCommand.h" #include "Parser.h" using namespace std; @@ -28,7 +28,7 @@ void LoadFilesCommand::execute(vector<string> files) { } void LoadFilesCommand::load_files(vector<string> files) { - this->get_museum().set_pause(true); + MuseumPauseCommand(this).set(true); Deserializer deserializer { this->get_museum() }; for (string url : files) { @@ -40,6 +40,6 @@ void LoadFilesCommand::load_files(vector<string> files) { } } - this->get_museum().set_pause(false); + MuseumPauseCommand(this).set(false); } diff --git a/LoadFilesCommand.h b/LoadFilesCommand.h index 468bb33..e13d873 100644 --- a/LoadFilesCommand.h +++ b/LoadFilesCommand.h @@ -6,16 +6,13 @@ #include "Command.h" class LoadFilesCommand : public Command { + using Command::Command; + public: virtual void execute(std::vector<std::string> files); virtual void execute(int argc, char ** argv); private: void load_files(std::vector<std::string> files); - -private: - virtual void execute() {}; // unused - - using Command::Command; }; @@ -19,6 +19,7 @@ public: public: void set_pause(bool paused); + bool get_pause() { return this->paused; } void update(); private: diff --git a/MuseumPauseCommand.cpp b/MuseumPauseCommand.cpp new file mode 100644 index 0000000..e3d8556 --- /dev/null +++ b/MuseumPauseCommand.cpp @@ -0,0 +1,13 @@ +#include "MuseumPauseCommand.h" +#include "Museum.h" + +void MuseumPauseCommand::set(bool paused) { + Museum & museum = this->get_museum(); + museum.set_pause(paused); +} + +void MuseumPauseCommand::toggle() { + Museum & museum = this->get_museum(); + museum.set_pause(!museum.get_pause()); +} + diff --git a/MuseumPauseCommand.h b/MuseumPauseCommand.h new file mode 100644 index 0000000..442a22a --- /dev/null +++ b/MuseumPauseCommand.h @@ -0,0 +1,12 @@ +#pragma once + +#include "Command.h" + +class MuseumPauseCommand : public Command { + using Command::Command; + +public: + virtual void toggle(); + virtual void set(bool paused); +}; + diff --git a/OpenFileGUICommand.cpp b/OpenFileGUICommand.cpp index 4568f61..1ea2c2f 100644 --- a/OpenFileGUICommand.cpp +++ b/OpenFileGUICommand.cpp @@ -2,15 +2,24 @@ #include <string> #include "OpenFileGUICommand.h" +#include "Exception.h" #include "LoadFilesCommand.h" +#include "MuseumPauseCommand.h" #include "View.h" using namespace std; void OpenFileGUICommand::execute() { + Museum * museum = &this->get_museum(); + + MuseumPauseCommand(*museum).set(true); this->get_view().dialog_file([](vector<string> files, void * data) -> void { - auto self = static_cast<OpenFileGUICommand *>(data); - LoadFilesCommand(self).execute(files); - }, this); + Museum * museum = static_cast<Museum *>(data); + try { + LoadFilesCommand(*museum).execute(files); + } catch (Exception & e) { + printf("%s\n", e.what()); + } + }, museum); } diff --git a/OpenFileGUICommand.h b/OpenFileGUICommand.h index dfaace3..9b504a9 100644 --- a/OpenFileGUICommand.h +++ b/OpenFileGUICommand.h @@ -3,8 +3,9 @@ #include "Command.h" class OpenFileGUICommand : public Command { -public: using Command::Command; + +public: virtual void execute(); }; diff --git a/ViewController.cpp b/ViewController.cpp index fc08c68..6422bbb 100644 --- a/ViewController.cpp +++ b/ViewController.cpp @@ -2,11 +2,18 @@ #include "Exception.h" #include "KeyboardCode.h" #include "MouseCode.h" +#include "MuseumPauseCommand.h" #include "OpenFileGUICommand.h" #include "View.h" #include "Museum.h" -ViewController::ViewController(Museum & m, View & v) : museum(m), view(v) {}; +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(); @@ -51,47 +58,52 @@ void ViewController::update_artists() { } void ViewController::ev_keydown(KeyboardCode key) { - switch (key) { - case KEY_SPACE: { - printf("TODO: toggle museum.pause\n"); - // MuseumPauseCommand().toggle(); - break; - } - case KEY_ENTER: { - break; - } - case KEY_O: { - try { - OpenFileGUICommand(this->museum, this->view, *this).execute(); - } catch (Exception & e) { - printf("OpenFileGUICommand error: %s\n", e.what()); + try { + switch (key) { + case KEY_SPACE: { + MuseumPauseCommand(this->cmd_base).toggle(); + break; } - break; - } - case KEY_A: { - break; - } - case KEY_LEFT: { - break; - } - case KEY_RIGHT: { - break; + case KEY_ENTER: { + break; + } + case KEY_O: { + OpenFileGUICommand(this->cmd_base).execute(); + break; + } + case KEY_A: { + break; + } + case KEY_LEFT: { + break; + } + case KEY_RIGHT: { + break; + } + default: break; } - default: break; + } catch (Exception & e) { + printf("%s\n", e.what()); } } void ViewController::ev_mousedown(MouseCode button) { - printf("mouse %d\n", button); - switch (button) { - case MOUSE_LEFT: { - // MuseumPauseCommand().toggle(); - break; + try { + switch (button) { + case MOUSE_LEFT: { + break; + } + default: 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), + }; } diff --git a/ViewController.h b/ViewController.h index 0f6b3e0..1e41a2c 100644 --- a/ViewController.h +++ b/ViewController.h @@ -1,5 +1,8 @@ #pragma once +#include <tuple> + +#include "Command.h" #include "KeyboardCode.h" #include "MouseCode.h" @@ -9,6 +12,7 @@ class Museum; class ViewController { public: ViewController(Museum & m, View & v); + virtual ~ViewController(); public: void update(); @@ -24,10 +28,12 @@ private: private: Museum & museum; View & view; + const Command * cmd_base = nullptr; 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 }; }; diff --git a/docs/class-diag.puml b/docs/class-diag.puml index a686394..8526346 100644 --- a/docs/class-diag.puml +++ b/docs/class-diag.puml @@ -205,11 +205,12 @@ rectangle Group_Visualization as "Visualization" <<group>> { } rectangle Group_Commands as "Commands" <<group>> { - class Command <<abstract>> { + class Command { #museum : Museum& #view : View& #controller : ViewController& +Command(museum, view, controller) + +Command(command : const Command *) } class MuseumPauseCommand { |