math/ui.c
xianya e787cac39a 增加pdcurses,提供图形化
增加头文件ui.h,pdcurses依赖pdcurses.a,curses.h,分离login模块和ui模块。
2024-12-28 17:24:07 +08:00

210 lines
6.4 KiB
C

//
// Created by zhang on 2024/12/28.
//
#include <string.h>
#include <ctype.h>
#include "./include/function.h"
#include "./include/curses.h"
#include "./include/ui.h"
#include <stdlib.h>
#define BUTTON_WIDTH 12
#define BUTTON_HEIGHT 3
#define LOGIN_BTN_X 10
#define LOGIN_BTN_Y 10
#define REGISTER_BTN_X 20
#define REGISTER_BTN_Y 10
#define USERNAME_X 15
#define USERNAME_Y 5
#define PASSWORD_X 15
#define PASSWORD_Y 7
#define MAX_INPUT_LEN 30
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;
}
// 用于绘制按钮的函数
void Welcome_ui() {
MEVENT event;
int startx, starty;
int button_x, button_y;
// 初始化ncurses环境
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
curs_set(0);
// 启用鼠标事件
mousemask(ALL_MOUSE_EVENTS, NULL);
// 获取屏幕的尺寸
int rows, cols;
getmaxyx(stdscr, rows, cols);
// 计算按钮的显示位置
startx = cols / 2 - BUTTON_WIDTH / 2;
starty = rows / 2 - 2; // 使文本和按钮垂直居中
// 打印欢迎文本
mvprintw(starty - 3, startx, "欢迎来到我们的程序!");
refresh();
// 创建两个按钮的窗口
WINDOW *login_button_win = newwin(BUTTON_HEIGHT, BUTTON_WIDTH, starty, startx);
WINDOW *register_button_win = newwin(BUTTON_HEIGHT, BUTTON_WIDTH, starty + BUTTON_HEIGHT + 1, startx);
// 绘制按钮
draw_button(login_button_win, 0, 0, "Login", false);
draw_button(register_button_win, 0, 0, "Signin", false);
refresh();
bool login_active = false;
bool register_active = false;
while (1) {
int ch = getch();
if (ch == KEY_MOUSE) {
if (nc_getmouse(&event) == OK) {
// 检查点击的区域是否是登录按钮
if (event.x >= startx && event.x < startx + BUTTON_WIDTH && event.y >= starty && event.y < starty + BUTTON_HEIGHT) {
login_active = true;
register_active = false;
draw_button(login_button_win, 0, 0, "Login", true); // 激活登录按钮
draw_button(register_button_win, 0, 0, "Signin", false); // 取消激活注册按钮
printw("您点击了登录按钮!\n");
}
// 检查点击的区域是否是注册按钮
else if (event.x >= startx && event.x < startx + BUTTON_WIDTH && event.y >= starty + BUTTON_HEIGHT + 1 && event.y < starty + BUTTON_HEIGHT + 1 + BUTTON_HEIGHT) {
login_active = false;
register_active = true;
draw_button(login_button_win, 0, 0, "登录", false); // 取消激活登录按钮
draw_button(register_button_win, 0, 0, "注册", true); // 激活注册按钮
printw("您点击了注册按钮!\n");
}
refresh();
}
}
// 按 'q' 键退出
if (ch == 'q') {
break;
}
}
// 清理退出
endwin();
return;
}