完成登录界面

This commit is contained in:
xianya 2024-12-29 15:54:09 +08:00
parent 13bbb29c90
commit 31c2725eaa

115
ui.c
View File

@ -16,10 +16,13 @@
struct bottom{
int x,y;
int width,height;
char *test[10];
char *test[30];
int private;
};
typedef void (*ButtonCallback)();
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));
@ -28,12 +31,14 @@ struct bottom *creat_bottom(WINDOW *win,int x,int y,int width,int height,char *t
bot->width=width;
bot->height=height;
strcpy(bot->test,test);
mvwprintw(win,x,y,"%s",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;
@ -53,8 +58,11 @@ int Welcome_ui() {
// 标题
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);
@ -63,11 +71,11 @@ int Welcome_ui() {
//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;
return 2;
}
if(click_bottom(event,*signin,start_x,start_y)){
free(login);free(signin);free(exit);endwin();
return 2;
return 3;
}
if(click_bottom(event,*exit,start_x,start_y)){
free(login);free(signin);free(exit);endwin();
@ -78,8 +86,103 @@ int Welcome_ui() {
}
}
int Login_ui(){
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<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;
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(){