ParaDocs/src/gameske_funkce.cpp
2025-04-14 12:21:16 +02:00

136 lines
3.0 KiB
C++

#include "gameske_funkce.h"
#include <curses.h>
#include <menu.h>
#include <unistd.h>
#include <clocale>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string>
size_t spawn_menu(uint16_t begin_y, uint16_t begin_x, const char** choices,
size_t n_choices) {
ITEM** sp_items;
int c;
MENU* sp_menu;
WINDOW* sp_menu_win;
int i;
sp_items = new ITEM*[n_choices];
/* Create items */
for (i = 0; i < n_choices; ++i) {
sp_items[i] = new_item(choices[i], choices[i]);
}
/* Crate menu */
sp_menu = new_menu(sp_items);
/* Create the window to be associated with the menu */
sp_menu_win = newwin(10, 40, begin_y, begin_x);
keypad(sp_menu_win, TRUE);
/* Set main window and sub window */
set_menu_win(sp_menu, sp_menu_win);
set_menu_sub(sp_menu, derwin(sp_menu_win, 6, 38, begin_y - 1, begin_x - 3));
/* Set menu mark to the string " * " */
set_menu_mark(sp_menu, " * ");
/* Print a border around the main window and print a title */
box(sp_menu_win, 0, 0);
print_in_middle(sp_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
mvwaddch(sp_menu_win, 2, 0, ACS_LTEE);
mvwhline(sp_menu_win, 2, 1, ACS_HLINE, 38);
mvwaddch(sp_menu_win, 2, 39, ACS_RTEE);
mvprintw(LINES - 2, 0, "F1 to exit");
refresh();
/* Post the menu */
post_menu(sp_menu);
wrefresh(sp_menu_win);
while ((c = wgetch(sp_menu_win)) != 10) {
switch (c) {
case KEY_DOWN:
menu_driver(sp_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(sp_menu, REQ_UP_ITEM);
break;
}
wrefresh(sp_menu_win);
}
size_t selected = item_index(current_item(sp_menu));
/* Unpost and free all the memory taken up */
unpost_menu(sp_menu);
free_menu(sp_menu);
for (i = 0; i < n_choices; ++i)
free_item(sp_items[i]);
delete[] sp_items;
return selected;
}
void print_in_middle(WINDOW* win, int starty, int startx, int width,
char* string, chtype color) {
int length, x, y;
float temp;
if (win == NULL)
win = stdscr;
getyx(win, y, x);
if (startx != 0)
x = startx;
if (starty != 0)
y = starty;
if (width == 0)
width = 80;
length = strlen(string);
temp = (width - length) / 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}
std::string spawncmd() {
char cmd[100] = {0};
// Create command window
WINDOW* cmd_win = newwin(3, 40, LINES - 3, 0);
if (cmd_win == NULL) {
return "";
}
// Setup window
keypad(cmd_win, TRUE);
box(cmd_win, 0, 0);
mvwprintw(cmd_win, 1, 1, "Command: ");
// Explicitly enable echo
echo();
// Make cursor visible during input
curs_set(1);
wrefresh(cmd_win);
// Get input with echo enabled
wgetnstr(cmd_win, cmd, 99);
// Display what was entered
mvprintw(LINES - 2, 0, "You Entered: %s", cmd);
refresh();
// Disable echo and hide cursor when done
noecho();
curs_set(0);
napms(5000);
delwin(cmd_win);
return std::string(cmd);
}