Skip to content

Commit

Permalink
Remove brawler menu
Browse files Browse the repository at this point in the history
  • Loading branch information
natesdev committed Jan 5, 2025
1 parent 7dfea7b commit e2ac6e5
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 21 deletions.
10 changes: 5 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ std::string tolower(std::string text)

int main(int argc, char **argv)
{
if (argc == 1)
if (argc >= 2)
{
TUI tui;
if (argc == 2)
{
tui.assetsFolder = argv[1];
}
tui.mainMenu();
}
else if (argc <= 2)
{
help();
}
else if (!strcmp("list", argv[2]))
{
BrawlerMaker bm;
Expand Down
110 changes: 98 additions & 12 deletions tui.cpp
Original file line number Diff line number Diff line change
@@ -1,67 +1,153 @@
#include <curses.h>
#include <vector>
#include <string>
#include <stdexcept>

#include "brawler.h"
#include "brawlermaker.h"
#include "tui.h"

int TUI::menu(std::string tittle, const char *choices[])
int TUI::menu(std::string title, const char *choices[])
{
initscr();
clear();
noecho();
cbreak();
keypad(stdscr, TRUE);

int ch;
int highlight = 0;
int choice = 0;
int start = 0;
int visible = LINES - 3;

int numChoices = 0;
while (choices[numChoices] != nullptr)
{
numChoices++;
}

mvprintw(0, 0, tittle.c_str());
mvprintw(0, 0, title.c_str());

while (true)
{
for (int i = 0; i < numChoices; i++)
clear();
mvprintw(0, 0, title.c_str());

for (int i = start; i < std::min(start + visible, numChoices); i++)
{
if (i == highlight)
attron(A_REVERSE);
mvprintw(i + 2, 0, choices[i]);
mvprintw(i - start + 2, 0, choices[i]);
attroff(A_REVERSE);
}

ch = getch();
switch (ch)
{
case KEY_UP:
highlight--;
if (highlight == -1)
highlight = 0;
if (highlight > 0)
highlight--;
else if (start > 0)
start--;
break;
case KEY_DOWN:
highlight++;
if (highlight == 5)
highlight = 4;
if (highlight < numChoices - 1)
highlight++;
if (highlight >= start + visible && start + visible < numChoices)
start++;
break;
default:
break;
}

if (ch == 10)
{
choice = highlight;
break;
}

if (highlight < start)
start = highlight;
if (highlight >= start + visible)
start = highlight - visible + 1;
}

endwin();
return choice;
}

Brawler TUI::brawlerSelectionMenu()
{
BrawlerMaker bm;
std::vector<Brawler> brawlers = bm.getBrawlers(
assetsFolder + "/csv_logic/characters.csv",
assetsFolder + "/csv_logic/cards.csv",
assetsFolder + "/csv_logic/skills.csv",
assetsFolder + "/localization/texts.csv",
true);

if (brawlers.empty())
{
throw std::runtime_error("No brawlers found");
}

std::vector<const char*> choices;
for (const auto &brawler : brawlers)
{
choices.push_back(brawler.name.c_str());
}
choices.push_back(nullptr);

int choice = menu("Select a brawler", choices.data());

if (choice >= 0 && choice < static_cast<int>(brawlers.size()))
{
return brawlers[choice];
}

throw std::runtime_error("Invalid choice");
}


void TUI::mainMenu()
{
initscr();
noecho();
cbreak();
const char *choices[] = {"Add Brawler", "Edit Brawler", "Remove Brawler", "Options", "Exit"};
menu("Brawlermaker v2.0", choices);
int choice = menu("Brawlermaker v2.0", choices);

switch (choice)
{
case 0:
//addBrawlerMenu();
break;
case 1:
//editBrawlerMenu();
break;
case 2:
removeBrawlerMenu();
break;
case 3:
//optionsMenu();
break;
case 4:
endwin();
break;
}
}

void TUI::removeBrawlerMenu()
{
Brawler brawler = brawlerSelectionMenu();
initscr();
noecho();
cbreak();
const char *choices[] = {"Yes", "No"};
int choice = menu("Are you sure you want to remove " + brawler.name + "?", choices);
if (choice == 0)
{
BrawlerMaker bm;
bm.removeBrawler(brawler.tid, assetsFolder + "/csv_logic/characters.csv", assetsFolder + "/csv_logic/cards.csv", assetsFolder + "/csv_logic/skills.csv", assetsFolder + "/localization/texts.csv");
}
}
8 changes: 4 additions & 4 deletions tui.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class TUI
void mainMenu();
void assetsFolderSelectionMenu();
Brawler brawlerSelectionMenu();
void addBrawlerMenu(Brawler &brawler);
void editBrawlerMenu(Brawler &brawler);
void removeBrawlerMenu(Brawler &brawler);
void addBrawlerMenu();
void editBrawlerMenu();
void removeBrawlerMenu();
void optionsMenu();
std::string assetsFolder;
private:
int menu(std::string tittle, const char *choices[]);
std::string assetsFolder;
};

0 comments on commit e2ac6e5

Please sign in to comment.