// // 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[10]; }; typedef void (*ButtonCallback)(); 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"); struct bottom *signin= creat_bottom(welcome_win,8,1,6,1,"Signin"); struct bottom *exit= creat_bottom(welcome_win,12,1,4,1,"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 1; } if(click_bottom(event,*signin,start_x,start_y)){ free(login);free(signin);free(exit);endwin(); return 2; } if(click_bottom(event,*exit,start_x,start_y)){ free(login);free(signin);free(exit);endwin(); return 0; } } } } } int Login_ui(){ } 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; //} // 用于绘制按钮的函数