ParaDocs/src/gameske_funkce.cpp
PoliEcho 4cf408dfa1
Some checks failed
build_test / build (push) Failing after 3m32s
fix templates
2025-05-13 14:05:03 +02:00

217 lines
5.3 KiB
C++

#include "gameske_funkce.h"
#include "memory.h"
#include <chrono>
#include <clocale>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <curses.h>
#include <format>
#include <iostream>
#include <menu.h>
#include <ncurses.h>
#include <string>
#include <thread>
#include <unistd.h>
/*
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,
const 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};
int pos = 0;
int ch;
WINDOW *cmd_win = newwin(3, 40, LINES - 3, 0);
if (cmd_win == NULL)
return "";
keypad(cmd_win, TRUE);
box(cmd_win, 0, 0);
mvwprintw(cmd_win, 1, 1, "Command: ");
curs_set(1);
wrefresh(cmd_win);
wattron(cmd_win, COLOR_PAIR(COLOR_CYAN));
// Get input character by character
while (pos < 99) {
ch = wgetch(cmd_win);
if (ch == '\n' || ch == KEY_ENTER) {
// Enter key pressed, end input
break;
} else if (ch == KEY_BACKSPACE || ch == 127) {
// Backspace key
if (pos > 0) {
pos--;
// Move cursor back and erase the character
wmove(cmd_win, 1, 10 + pos);
waddch(cmd_win, ' ');
wmove(cmd_win, 1, 10 + pos);
}
} else if (ch == KEY_DC) {
// Delete key - not implemented in this simple example
} else if (isprint(ch)) {
// Printable character
cmd[pos] = ch;
mvwaddch(cmd_win, 1, 10 + pos, ch); // Echo the character
pos++;
}
wrefresh(cmd_win);
}
wattroff(cmd_win, COLOR_PAIR(COLOR_CYAN));
cmd[pos] = '\0'; // Ensure null termination
// Restore echo state as needed
curs_set(0);
wclear(cmd_win);
wrefresh(cmd_win);
delwin(cmd_win);
return std::string(cmd);
}
void async_clock(WINDOW *win, WINDOW *text_win) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
bool prev_echo_state;
uint16_t prev_y, prev_x;
while (true) {
prev_echo_state = is_echo();
getyx(stdscr, prev_y, prev_x);
noecho();
wborder(text_win, 0, ' ', 0, 0, 0, ACS_HLINE, 0, ACS_HLINE);
mvwprintw(text_win, 1, 1, "správný čas na podání trestního oznámení je");
wrefresh(text_win);
auto now =
std::chrono::current_zone()->to_local(std::chrono::system_clock::now());
std::string time_str;
try {
// Format just the time (HH:MM:SS)
time_str = std::format("{:%T}", now);
// Remove fractional seconds
size_t dot_pos = time_str.find('.');
if (dot_pos != std::string::npos) {
time_str.erase(dot_pos);
}
} catch (const std::exception &e) {
std::clog << "Format error: " << e.what() << std::endl;
time_str = "Error";
}
werase(win);
wborder(win, 0, 0, 0, 0, ACS_TTEE, 0, ACS_BTEE, 0);
mvwprintw(win, 1, 1, "%s", time_str.c_str());
wrefresh(win);
if (prev_echo_state) {
echo();
}
move(prev_y, prev_x);
refresh();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void async_clock_init() {
// memory leak
WINDOW *win = newwin(3, 10, LINES - 3, COLS - 10);
current_allocated->push_back({WINDOW_TYPE, win, 1});
// memory leak
WINDOW *text_win = newwin(3, 52, LINES - 3, COLS - 10 - 44);
current_allocated->push_back({WINDOW_TYPE, text_win, 1});
std::thread clock_thread(async_clock, win, text_win);
clock_thread.detach();
}