// // 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 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 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; 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; while(1){ 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(click_bottom(event,*login,start_x,start_y)){ free(login);free(signin);free(exit);free(users);free(psword);endwin(); } 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 1; } if(click_bottom(event,*users,start_x,start_y)){ get_input(login_win, 4, 6, username, 15,users,start_x,start_y); continue; } 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(){ } //void draw_button(WINDOW *win, int x, int y, const char *label, bool is_active) { // if (is_active) { // wattron(win, A_REVERSE); // 激活按钮时反显 // } // mvwprintw(win, y, x + (BUTTON_WIDTH - strlen(label)) / 2, "%s", label); // if (is_active) { // wattroff(win, A_REVERSE); // 反显结束 // } // box(win, 0, 0); // 画按钮的边框 // wrefresh(win); //} //void draw_login_window() { // // 绘制登录窗口 // clear(); // box(stdscr, 0, 0); // // // 标题 // mvprintw(2, 20, "Login Window"); // // // 用户名 // mvprintw(USERNAME_Y, USERNAME_X - 10, "Username: "); // mvprintw(PASSWORD_Y, PASSWORD_X - 10, "Password: "); // // // 输入框 // mvprintw(USERNAME_Y, USERNAME_X, "____________________________"); // mvprintw(PASSWORD_Y, PASSWORD_X, "____________________________"); // // // 按钮 // mvprintw(LOGIN_BTN_Y, LOGIN_BTN_X, "[ Login ]"); // mvprintw(REGISTER_BTN_Y, REGISTER_BTN_X, "[ Register ]"); // // refresh(); //} //void handle_mouse_click(MEVENT event) { // // 处理鼠标点击事件 // if (event.bstate & BUTTON1_CLICKED) { // // 判断是否点击了登录按钮 // if (event.x >= LOGIN_BTN_X && event.x <= LOGIN_BTN_X + 9 && // event.y == LOGIN_BTN_Y) { // printw("\nLogin button clicked!\n"); // refresh(); // } // // 判断是否点击了注册按钮 // else if (event.x >= REGISTER_BTN_X && event.x <= REGISTER_BTN_X + 11 && // event.y == REGISTER_BTN_Y) { // printw("\nRegister button clicked!\n"); // refresh(); // } // } //} void clear_input_area(int x, int y, int len) { // 清除输入框区域 for (int i = 0; i < len; i++) { mvaddch(y, x + i, ' '); } } //void Login_ui() { // initscr(); // 初始化屏幕 // cbreak(); // 禁用行缓冲 // noecho(); // 不显示输入的字符 // keypad(stdscr, TRUE); // 启用键盘功能(例如箭头键) // mousemask(ALL_MOUSE_EVENTS, NULL); // 启用所有鼠标事件 // curs_set(0); // 隐藏光标 // // draw_login_window(); // 绘制登录窗口 // // char username[MAX_INPUT_LEN + 1] = ""; // char password[MAX_INPUT_LEN + 1] = ""; // int ch; // MEVENT event; // // while (1) { // ch = getch(); // 获取输入 // // if (ch == KEY_MOUSE) { // 检查是否是鼠标事件 // if (nc_getmouse(&event) == OK) { // handle_mouse_click(event); // 处理鼠标点击事件 // } // } // // // 处理键盘输入(用户输入用户名和密码) // if (ch == 10) { // 回车键 // printw("\n登录成功!\n"); // break; // } // // // 如果是在用户名或密码框中输入 // if (ch == KEY_BACKSPACE || ch == 127) { // 退格键 // // 删除输入框中的最后一个字符 // if (strlen(username) > 0) { // username[strlen(username) - 1] = '\0'; // clear_input_area(USERNAME_X, USERNAME_Y, MAX_INPUT_LEN); // mvprintw(USERNAME_Y, USERNAME_X, "%s", username); // } // } else if (isprint(ch)) { // 只接受可打印的字符 // if (strlen(username) < MAX_INPUT_LEN) { // username[strlen(username)] = ch; // username[strlen(username) + 1] = '\0'; // mvprintw(USERNAME_Y, USERNAME_X, "%s", username); // } // } // // refresh(); // } // // endwin(); // 结束 ncurses 模式 // return; //} // 用于绘制按钮的函数