Skip to content

Commit

Permalink
lining things up (in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
r3w0p committed May 22, 2024
1 parent 689c610 commit 8f10bbb
Showing 1 changed file with 222 additions and 22 deletions.
244 changes: 222 additions & 22 deletions src/caravan/view/view_tui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,240 @@
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/dom/elements.hpp"

std::shared_ptr<ftxui::Node> gen_game(
const uint16_t MIN_X = 150;
const uint16_t MIN_Y = 70;

const uint16_t WIDTH_CARAVAN = 16;
const uint16_t HEIGHT_CARAVAN = 34;

const uint16_t WIDTH_DECK = 15;

const uint16_t HEIGHT_CARAVAN_CARD = 4;

const uint16_t WIDTH_POSITION = 4;
const uint16_t HEIGHT_POSITION = 2;

const uint16_t WIDTH_CARD = 5;
const uint16_t HEIGHT_CARD = 2;

const uint16_t WIDTH_FACES = 5;
const uint16_t HEIGHT_FACES = 2;

std::shared_ptr<ftxui::Node> gen_position(std::string position) {
using namespace ftxui;
return text(position) | borderEmpty | size(WIDTH, EQUAL, WIDTH_POSITION) | size(HEIGHT, EQUAL, HEIGHT_POSITION);
}

std::shared_ptr<ftxui::Node> gen_card(std::wstring card) {
using namespace ftxui;
return text(card) | borderDouble | size(WIDTH, EQUAL, WIDTH_CARD) | size(HEIGHT, EQUAL, HEIGHT_CARD);
}

std::shared_ptr<ftxui::Node> gen_faces() {
using namespace ftxui;
return vbox({
text(L"QQK"),
text(L"♠♠♠")
}) | borderEmpty | size(WIDTH, EQUAL, WIDTH_FACES) | size(HEIGHT, EQUAL, HEIGHT_FACES);
}

std::shared_ptr<ftxui::Node> gen_caravan_card(std::string position, std::wstring card) {
using namespace ftxui;
return hbox({
gen_position(position),
gen_card(card),
gen_faces(),
}) | size(HEIGHT, EQUAL, HEIGHT_CARAVAN_CARD);
}

std::shared_ptr<ftxui::Node> gen_caravan(std::string title, bool top) {
using namespace ftxui;
std::shared_ptr<Node> content;

if(top) {
content = vbox({
gen_caravan_card("8", L" K♠"),
gen_caravan_card("7", L" K♠"),
gen_caravan_card("6", L" K♠"),
gen_caravan_card("5", L" K♠"),
gen_caravan_card("4", L" K♠"),
gen_caravan_card("3", L" K♠"),
gen_caravan_card("2", L" K♠"),
gen_caravan_card("1", L" K♠"),
});
} else {
content = vbox({
gen_caravan_card("1", L" K♠"),
gen_caravan_card("2", L" K♠"),
gen_caravan_card("3", L" K♠"),
gen_caravan_card("4", L" K♠"),
gen_caravan_card("5", L" K♠"),
gen_caravan_card("6", L" K♠"),
gen_caravan_card("7", L" K♠"),
gen_caravan_card("8", L" K♠"),
});
}

return window(
text(title) | hcenter | bold,
content
) | size(WIDTH, EQUAL, WIDTH_CARAVAN) | size(HEIGHT, EQUAL, HEIGHT_CARAVAN);
}

std::shared_ptr<ftxui::Node> gen_deck_card(std::string position, std::wstring card) {
using namespace ftxui;
return hbox({
gen_position(position),
gen_card(card),
}) | size(HEIGHT, EQUAL, HEIGHT_CARAVAN_CARD);
}

std::shared_ptr<ftxui::Node> gen_deck(std::string title, bool top, bool hide) {
// TODO bool hide
using namespace ftxui;
std::shared_ptr<Node> content;

if(top) {
content = vbox({
gen_deck_card(" ", L"###"),
gen_deck_card(" ", L"###"),
gen_deck_card(" ", L"###"),
gen_deck_card(" ", L"###"),
gen_deck_card(" ", L"###"),
gen_deck_card(" ", L"###"),
gen_deck_card(" ", L"###"),
gen_deck_card(" ", L"###"),
});
} else {
content = vbox({
gen_deck_card("1", L" K♠"),
gen_deck_card("2", L" K♠"),
gen_deck_card("3", L" K♠"),
gen_deck_card("4", L" K♠"),
gen_deck_card("5", L" K♠"),
gen_deck_card("6", L" K♠"),
gen_deck_card("7", L" K♠"),
gen_deck_card("8", L" K♠"),
});
}

return window(
text(title) | hcenter | bold,
content
) | size(WIDTH, EQUAL, WIDTH_DECK);
}

std::shared_ptr<ftxui::Node> gen_input(
std::shared_ptr<ftxui::ComponentBase> comp_user_input,
std::string user_input,
std::string command) {
std::string command,
std::string message) {
using namespace ftxui;
return vbox({
hbox(text(" YOU > "), comp_user_input->Render()),
separatorEmpty(),
hbox(separatorEmpty(), text("YOU > "), comp_user_input->Render(), separatorEmpty()),
separatorEmpty(),

separator(),

separatorEmpty(),
hbox(separatorEmpty(), paragraph("YOU has yet to move."), separatorEmpty()),
separatorEmpty(),
hbox(separatorEmpty(), paragraph("OPP has yet to move."), separatorEmpty()),
separatorEmpty(),

separator(),
text("Current: " + user_input),
text("Command: " + command),
}) | border;

separatorEmpty(),
hbox(separatorEmpty(), paragraph("YOU's turn to move."), separatorEmpty()),
separatorEmpty(),
}) | border | size(WIDTH, EQUAL, 50);
}

std::shared_ptr<ftxui::Node> gen_game(
std::shared_ptr<ftxui::ComponentBase> comp_user_input,
std::string user_input,
std::string command,
std::string message) {
using namespace ftxui;
return hbox({ // OUTERMOST AREA

vbox({ // GAME AREA

hbox({ // TOP GAME AREA
gen_caravan(" D ", true),
separatorEmpty(),
gen_caravan(" E ", true),
separatorEmpty(),
gen_caravan(" F (100, ASC) ", true),
}), // top game area

separatorEmpty(),

hbox({ // BOTTOM GAME AREA
gen_caravan(" A ", false),
separatorEmpty(),
gen_caravan(" B (100, ASC) ", false),
separatorEmpty(),
gen_caravan(" C ", false),
}), // bottom game area

}), // game area

separatorEmpty(),
separatorEmpty(),
separatorEmpty(),
separatorEmpty(),
separatorEmpty(),

vbox({ // DECK AREA
gen_deck(" BOT (100) ", true, true),
separatorEmpty(),
gen_deck(" YOU ", false, false),
}), // deck area

separatorEmpty(),
separatorEmpty(),
separatorEmpty(),
separatorEmpty(),
separatorEmpty(),

vbox({ // INPUT AREA
hbox({}) | borderEmpty | size(HEIGHT, EQUAL, HEIGHT_CARAVAN),
separatorEmpty(),
gen_input(comp_user_input, user_input, command, message)
}), // input area

}); // outermost area
}

std::shared_ptr<ftxui::Node> gen_terminal_too_small(
ftxui::Dimensions terminal, uint16_t min_x, uint16_t min_y) {
ftxui::Dimensions terminal_size) {
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)),
text("Width: " + std::to_string(terminal_size.dimx) + " < " + std::to_string(MIN_X)),
text("Height: " + std::to_string(terminal_size.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

using namespace ftxui;

if(closed) return;

// Input data
std::string user_input;
std::string command;
std::string message;
Dimensions terminal_size {};

// Create screen
ScreenInteractive screen = ScreenInteractive::Fullscreen();

// User input component
Component comp_user_input = Input(&user_input, "");
Expand All @@ -69,28 +267,30 @@ void ViewTUI::run() {

// Tweak how the component tree is rendered:
auto renderer = Renderer(component, [&] {
auto terminal_size = Terminal::Size();
auto min_terminal_x = 150;
auto min_terminal_y = 50;
// Closes on exit command from user input
if(closed) screen.Exit();

// Ensure minimum terminal dimensions
// TODO prevent user input change during this display
if (terminal_size.dimx < min_terminal_x || terminal_size.dimy < min_terminal_y) {
user_input = "";
return gen_terminal_too_small(terminal_size, min_terminal_x, min_terminal_y);
terminal_size = Terminal::Size();

// Error screen if less than minimum terminal dimensions
if (terminal_size.dimx < MIN_X || terminal_size.dimy < MIN_Y) {
user_input = ""; // Prevent user input change during error
return gen_terminal_too_small(terminal_size);
}

// Handle user input
if(user_input.ends_with('\n')) {
command = user_input;
command.pop_back(); // removes '\n'
user_input = "";
message += command + " ";
}

// TODO immediately wipe command once passed to controller
if(command == "EXIT") screen.Exit();

// Generate game as normal
return gen_game(comp_user_input, user_input, command);
// TODO immediately wipe command once passed to controller
return gen_game(comp_user_input, user_input, command, message);
});

auto screen = ScreenInteractive::Fullscreen();
screen.Loop(renderer);
}

0 comments on commit 8f10bbb

Please sign in to comment.