// // Created by zhang on 2024/12/28. // #include #include #include "./include/function.h" #include "./include/curses.h" #include "./include/ui.h" #include // 定义窗口宽度和高度 #define WIN_HEIGHT 15 #define WIN_WIDTH 40 struct bottom { int x, y; int width, height; char *test[30]; int private; }; void draw_bottom(WINDOW *win, struct bottom *bot) { mvwprintw(win, bot->y, bot->x, "%s", bot->test); } struct bottom *creat_bottom(WINDOW *win, int x, int y, int width, int height, char *test[]) { struct bottom *bot = malloc(sizeof(struct bottom)); bot->y = x; bot->x = y; bot->width = width; bot->height = height; strcpy(bot->test, test); //mvwprintw(win,x,y,"%s",test); return bot; } bool click_bottom(MEVENT event, struct bottom bottom, int sx, int sy) { return event.x - sx >= bottom.x && event.x - sx < bottom.x + bottom.width && event.y - sy >= bottom.y && event.y - sy < bottom.y + bottom.height; } int Welcome_ui() { MEVENT event; int startx, starty; initscr(); // 初始化curses cbreak(); // 禁用行缓冲 noecho(); // 不显示输入字符 curs_set(0); // 隐藏光标 keypad(stdscr, TRUE); // 启用键盘按键的特殊处理 mousemask(ALL_MOUSE_EVENTS, NULL); // 启用鼠标事件 // 获取屏幕的尺寸 // 计算按钮的显示位置 int start_y = (LINES - WIN_HEIGHT) / 2; int start_x = (COLS - WIN_WIDTH) / 2; WINDOW *welcome_win = newwin(WIN_HEIGHT, WIN_WIDTH, start_y, start_x); keypad(welcome_win, TRUE); wborder(welcome_win, '|', '|', '-', '-', '+', '+', '+', '+'); // 标题 mvwprintw(welcome_win, 1, 1, "Welcome to math calculate system!"); struct bottom *login = creat_bottom(welcome_win, 4, 1, 5, 1, "Login"); draw_bottom(welcome_win, login); struct bottom *signin = creat_bottom(welcome_win, 8, 1, 6, 1, "Signin"); draw_bottom(welcome_win, signin); struct bottom *exit = creat_bottom(welcome_win, 12, 1, 4, 1, "Exit"); draw_bottom(welcome_win, exit); wrefresh(welcome_win); while (1) { int rc = wgetch(welcome_win); if (rc == KEY_MOUSE) { if (nc_getmouse(&event) == OK) { //mvwprintw(welcome_win, 3, 1, "Mouse at (%d, %d), button %d", event.x-start_x, event.y-start_y, event.bstate); if (click_bottom(event, *login, start_x, start_y)) { free(login); free(signin); free(exit); endwin(); return 2; } if (click_bottom(event, *signin, start_x, start_y)) { free(login); free(signin); free(exit); endwin(); return 3; } if (click_bottom(event, *exit, start_x, start_y)) { free(login); free(signin); free(exit); endwin(); return 0; } } } } } void draw_login_window(WINDOW *win, struct bottom *login, struct bottom *signin, struct bottom *user, struct bottom *psword, struct bottom *exit) { // 清空窗口并绘制边框 wclear(win); box(win, 0, 0); // 标题 mvwprintw(win, 1, (WIN_WIDTH - 10) / 2, "Login Menu"); // 用户名输入框 mvwprintw(win, 3, 4, "Username:"); // 密码输入框 mvwprintw(win, 6, 4, "Password:"); //绘制剩余内容 draw_bottom(win, login); draw_bottom(win, signin); draw_bottom(win, exit); draw_bottom(win, user); draw_bottom(win, psword); wrefresh(win); // 刷新窗口 } void draw_signin_window(WINDOW *win, struct bottom *signin, struct bottom *user, struct bottom *psword, struct bottom *exit) { // 清空窗口并绘制边框 wclear(win); box(win, 0, 0); // 标题 mvwprintw(win, 1, (WIN_WIDTH - 10) / 2, "Signin"); // 用户名输入框 mvwprintw(win, 3, 4, "Username:"); // 密码输入框 mvwprintw(win, 6, 4, "Password:"); //绘制剩余内容 draw_bottom(win, signin); draw_bottom(win, exit); draw_bottom(win, user); draw_bottom(win, psword); wrefresh(win); // 刷新窗口 } void get_input(WINDOW *win, int y, int x, char *buffer, int max_len, struct bottom *bot, int sx, int sy) { MEVENT event; int ch, pos = 0; char *test = bot->test; while (pos < max_len && buffer[pos] != '\0')pos++; while (1) { ch = wgetch(win); if (ch == KEY_BACKSPACE || ch == 8) { // 处理退格键 if (pos > 0) { pos--; buffer[pos] = '\0'; test[pos + 1] = '_'; draw_bottom(win, bot); wrefresh(win); } } else if (pos < max_len - 1 && ch >= 32 && ch <= 126) { // 可打印字符 buffer[pos++] = ch; if (bot->private == 0)test[pos] = ch; else test[pos] = '*'; draw_bottom(win, bot); wrefresh(win); } else if (ch == KEY_MOUSE) { if (nc_getmouse(&event) == OK) { if (!click_bottom(event, *bot, sx, sy)) { return; } } } } } int Login_ui() { MEVENT event; char *errmsg[30]; memset(errmsg, 0, 30); int start_y = (LINES - WIN_HEIGHT) / 2; int start_x = (COLS - WIN_WIDTH) / 2; WINDOW *login_win = newwin(WIN_HEIGHT, WIN_WIDTH, start_y, start_x); keypad(login_win, TRUE); char *username[15]; //储存用户名 memset(username, 0, 15); // 清空缓冲区 char *password[15]; //储存密码 memset(password, 0, 15); // 清空缓冲区 struct bottom *login = creat_bottom(login_win, 10, 4, 5, 1, "Login"); struct bottom *signin = creat_bottom(login_win, 10, 14, 5, 1, "Signin"); struct bottom *exit = creat_bottom(login_win, 10, 25, 5, 1, "Exit"); struct bottom *users = creat_bottom(login_win, 4, 4, 16, 1, "[______________]"); struct bottom *psword = creat_bottom(login_win, 7, 4, 16, 1, "[______________]"); psword->private = 1; users->private = 0; int willdraw = 1; while (1) { if (willdraw)draw_login_window(login_win, login, signin, users, psword, exit); int rc = wgetch(login_win); if (rc == KEY_MOUSE) { if (nc_getmouse(&event) == OK) { //mvwprintw(welcome_win, 3, 1, "Mouse at (%d, %d), button %d", event.x-start_x, event.y-start_y, event.bstate); if (willdraw) if (click_bottom(event, *login, start_x, start_y)) { int recall; Login(username, password, &recall, errmsg); if (recall != 0) { wclear(login_win); mvwprintw(login_win, 5, 5, "%s", errmsg); exit->x=5;exit->y=8; draw_bottom(login_win, exit); wrefresh(login_win); willdraw=0; continue; } } if (willdraw) if (click_bottom(event, *signin, start_x, start_y)) { free(login); free(signin); free(exit); free(users); free(psword); endwin(); return 3; } if (click_bottom(event, *exit, start_x, start_y)) { free(login); free(signin); free(exit); free(users); free(psword); endwin(); return willdraw==1?1:2; } if (willdraw) if (click_bottom(event, *users, start_x, start_y)) { get_input(login_win, 4, 6, username, 15, users, start_x, start_y); continue; } if (willdraw) if (click_bottom(event, *psword, start_x, start_y)) { get_input(login_win, 7, 6, password, 15, psword, start_x, start_y); continue; } } } } } int Signin_ui() { MEVENT event; char *errmsg[100]; memset(errmsg, 0, 30); int start_y = (LINES - WIN_HEIGHT) / 2; int start_x = (COLS - WIN_WIDTH) / 2; WINDOW *signin_win = newwin(WIN_HEIGHT, WIN_WIDTH, start_y, start_x); keypad(signin_win, TRUE); char *username[15]; //储存用户名 memset(username, 0, 15); // 清空缓冲区 char *password[15]; //储存密码 memset(password, 0, 15); // 清空缓冲区 struct bottom *signin = creat_bottom(signin_win, 10, 4, 5, 1, "Signin"); struct bottom *exit = creat_bottom(signin_win, 10, 25, 5, 1, "Exit"); struct bottom *users = creat_bottom(signin_win, 4, 4, 16, 1, "[______________]"); struct bottom *psword = creat_bottom(signin_win, 7, 4, 16, 1, "[______________]"); psword->private = 0; users->private = 0; int willdraw = 1; while (1) { if (willdraw)draw_signin_window(signin_win, signin, users, psword, exit); int rc = wgetch(signin_win); if (rc == KEY_MOUSE) { if (nc_getmouse(&event) == OK) { //mvwprintw(welcome_win, 3, 1, "Mouse at (%d, %d), button %d", event.x-start_x, event.y-start_y, event.bstate); if (willdraw) if (click_bottom(event, *signin, start_x, start_y)) { int recall; Signin(username,password,&recall,errmsg); if (recall != 0) { wclear(signin_win); mvwprintw(signin_win, 5, 5, "%s", errmsg); exit->x=5;exit->y=8; draw_bottom(signin_win, exit); wrefresh(signin_win); willdraw=0; continue; } else{ wclear(signin_win); mvwprintw(signin_win, 5, 5, "Accepted"); exit->x=5;exit->y=8; draw_bottom(signin_win, exit); wrefresh(signin_win); willdraw=0; continue; } } if (click_bottom(event, *exit, start_x, start_y)) { free(signin); free(exit); free(users); free(psword); endwin(); return willdraw==1?1:3; } if (willdraw) if (click_bottom(event, *users, start_x, start_y)) { get_input(signin_win, 4, 6, username, 15, users, start_x, start_y); continue; } if (willdraw) if (click_bottom(event, *psword, start_x, start_y)) { get_input(signin_win, 7, 6, password, 15, psword, start_x, start_y); continue; } } } } }