Skip to content

Commit

Permalink
dfsafd
Browse files Browse the repository at this point in the history
  • Loading branch information
r3w0p committed May 21, 2024
1 parent 3f3074a commit 689c610
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
30 changes: 30 additions & 0 deletions include/caravan/model/model.h
Original file line number Diff line number Diff line change
@@ -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 <string>
#include "caravan/core/common.h"

class ModelSubscriber {
public:
virtual void on_model_change_me() = 0;
};

class Model : public Publisher<ModelSubscriber> {
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
18 changes: 18 additions & 0 deletions include/caravan/model/model_tui.h
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions src/caravan/model/model.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 5 additions & 0 deletions src/caravan/model/model_tui.cpp
Original file line number Diff line number Diff line change
@@ -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"
45 changes: 28 additions & 17 deletions src/caravan/view/view_tui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/dom/elements.hpp"

std::shared_ptr<ftxui::Node> gen_game(
std::shared_ptr<ftxui::ComponentBase> 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<ftxui::Node> 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

Expand Down Expand Up @@ -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')) {
Expand All @@ -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();
Expand Down

0 comments on commit 689c610

Please sign in to comment.