From 689c610634dba0d96b9d505e7993194f8ae22c65 Mon Sep 17 00:00:00 2001 From: r3w0p Date: Tue, 21 May 2024 17:46:18 +0100 Subject: [PATCH] dfsafd --- CMakeLists.txt | 4 +++ include/caravan/model/model.h | 30 +++++++++++++++++++++ include/caravan/model/model_tui.h | 18 +++++++++++++ src/caravan/model/model.cpp | 9 +++++++ src/caravan/model/model_tui.cpp | 5 ++++ src/caravan/view/view_tui.cpp | 45 +++++++++++++++++++------------ 6 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 include/caravan/model/model.h create mode 100644 include/caravan/model/model_tui.h create mode 100644 src/caravan/model/model.cpp create mode 100644 src/caravan/model/model_tui.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5549e28..9e07497 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,12 +59,16 @@ add_library(model "include/caravan/model/caravan.h" "include/caravan/model/deck.h" "include/caravan/model/game.h" + "include/caravan/model/model.h" + "include/caravan/model/model_tui.h" "include/caravan/model/player.h" "include/caravan/model/table.h" "src/caravan/model/caravan.cpp" "src/caravan/model/deck.cpp" "src/caravan/model/game.cpp" + "src/caravan/model/model.cpp" + "src/caravan/model/model_tui.cpp" "src/caravan/model/player.cpp" "src/caravan/model/table.cpp" ) diff --git a/include/caravan/model/model.h b/include/caravan/model/model.h new file mode 100644 index 0000000..0d14ed1 --- /dev/null +++ b/include/caravan/model/model.h @@ -0,0 +1,30 @@ +// Copyright (c) 2022-2024 r3w0p +// The following code can be redistributed and/or +// modified under the terms of the GPL-3.0 License. + +#ifndef CARAVAN_MODEL_H +#define CARAVAN_MODEL_H + +#include +#include "caravan/core/common.h" + +class ModelSubscriber { +public: + virtual void on_model_change_me() = 0; +}; + +class Model : public Publisher { +protected: + bool closed; +public: + virtual ~Model() = default; + explicit Model() : closed(false) {}; + + virtual void run() = 0; + void subscribe(ModelSubscriber *sub) override; + void close() { closed = true; }; +}; + + + +#endif //CARAVAN_MODEL_H diff --git a/include/caravan/model/model_tui.h b/include/caravan/model/model_tui.h new file mode 100644 index 0000000..e4937f2 --- /dev/null +++ b/include/caravan/model/model_tui.h @@ -0,0 +1,18 @@ +// Copyright (c) 2022-2024 r3w0p +// The following code can be redistributed and/or +// modified under the terms of the GPL-3.0 License. + +#ifndef CARAVAN_MODEL_TUI_H +#define CARAVAN_MODEL_TUI_H + +#include "caravan/model/model.h" +#include "caravan/core/common.h" + +class ModelTUI : public Model { +public: + explicit ModelTUI() : Model() {}; + + void run() override; +}; + +#endif //CARAVAN_MODEL_TUI_H diff --git a/src/caravan/model/model.cpp b/src/caravan/model/model.cpp new file mode 100644 index 0000000..edba425 --- /dev/null +++ b/src/caravan/model/model.cpp @@ -0,0 +1,9 @@ +// Copyright (c) 2022-2024 r3w0p +// The following code can be redistributed and/or +// modified under the terms of the GPL-3.0 License. + +#include "caravan/model/model.h" + +void Model::subscribe(ModelSubscriber *sub) { + subscribers.push_back(sub); +} diff --git a/src/caravan/model/model_tui.cpp b/src/caravan/model/model_tui.cpp new file mode 100644 index 0000000..223cb82 --- /dev/null +++ b/src/caravan/model/model_tui.cpp @@ -0,0 +1,5 @@ +// Copyright (c) 2022-2024 r3w0p +// The following code can be redistributed and/or +// modified under the terms of the GPL-3.0 License. + +#include "caravan/model/model_tui.h" diff --git a/src/caravan/view/view_tui.cpp b/src/caravan/view/view_tui.cpp index 3398b44..014f619 100644 --- a/src/caravan/view/view_tui.cpp +++ b/src/caravan/view/view_tui.cpp @@ -14,6 +14,32 @@ #include "ftxui/component/screen_interactive.hpp" #include "ftxui/dom/elements.hpp" +std::shared_ptr gen_game( + std::shared_ptr comp_user_input, + std::string user_input, + std::string command) { + using namespace ftxui; + return vbox({ + hbox(text(" YOU > "), comp_user_input->Render()), + separator(), + text("Current: " + user_input), + text("Command: " + command), + }) | border; +} + +std::shared_ptr gen_terminal_too_small( + ftxui::Dimensions terminal, uint16_t min_x, uint16_t min_y) { + using namespace ftxui; + return vbox({ + text("Terminal too small"), + separatorEmpty(), + text("Width: " + std::to_string(terminal.dimx) + " < " + std::to_string(min_x)), + text("Height: " + std::to_string(terminal.dimy) + " < " + std::to_string(min_y)), + separatorEmpty(), + text("Resize terminal or press Ctrl+C"), + }) | center; +} + void ViewTUI::run() { // TODO on_model_exit, close the game @@ -51,16 +77,7 @@ void ViewTUI::run() { // TODO prevent user input change during this display if (terminal_size.dimx < min_terminal_x || terminal_size.dimy < min_terminal_y) { user_input = ""; - return vbox({ - text("Terminal too small"), - separatorEmpty(), - text("Width: " + std::to_string(terminal_size.dimx) + " < " + - std::to_string(min_terminal_x)), - text("Height: " + std::to_string(terminal_size.dimy) + " < " + - std::to_string(min_terminal_y)), - separatorEmpty(), - text("Resize terminal or press Ctrl+C"), - }) | center; + return gen_terminal_too_small(terminal_size, min_terminal_x, min_terminal_y); } if(user_input.ends_with('\n')) { @@ -71,13 +88,7 @@ void ViewTUI::run() { // TODO immediately wipe command once passed to controller // Generate game as normal - return vbox({ - hbox(text(" YOU > "), comp_user_input->Render()), - separator(), - text("Current: " + user_input), - text("Command: " + command), - text("Terminal: x=" + std::to_string(terminal_size.dimx) + " y=" + std::to_string(terminal_size.dimy)), - }) | border; + return gen_game(comp_user_input, user_input, command); }); auto screen = ScreenInteractive::Fullscreen();