マウスオーバー効果
諏訪子 <suwako@076.moe>、2026-01-21 23:50
コミット dd3be3a555c5aae19acf95c528046b620fb54363
・親 01666a7
・ファイルを見る
4個のファイル、+24 -21
変更
control.c
| @@ -54,7 +54,7 @@ | ||
| 54 | 54 | char curinput[64] = {0}; |
| 55 | 55 | int input_pos = 0; |
| 56 | 56 | int initialized = 0; |
| 57 | -SuwaButton buttons[NUM_ROWS][NUM_COLS]; | |
| 57 | +SuwaButton *lasthover = NULL; | |
| 58 | 58 | |
| 59 | 59 | static const char *btn_labels[][10] = { |
| 60 | 60 | { NULL }, // 数字表示 |
| @@ -195,7 +195,6 @@ | ||
| 195 | 195 | } |
| 196 | 196 | } |
| 197 | 197 | suwaui_draw_button(window, &btn, backdraw); |
| 198 | - buttons[row][col] = btn; | |
| 199 | 198 | } |
| 200 | 199 | } |
| 201 | 200 | } |
| @@ -385,23 +384,14 @@ | ||
| 385 | 384 | control_expose(window, labels, &(SuwaButton){0}); |
| 386 | 385 | } |
| 387 | 386 | |
| 388 | -void handle_mouse_hover(SuwaWindow *window, CtrlLabels *labels) { | |
| 389 | - int x = window->event.xmotion.x; | |
| 390 | - int y = window->event.xmotion.y; | |
| 391 | - SuwaButton *btn = find_button_at(window, x, y); | |
| 387 | +void handle_mouse_hover(SuwaWindow *window, CtrlLabels *labels, int mx, int my) { | |
| 388 | + SuwaButton *btn = find_button_at(window, mx, my); | |
| 392 | 389 | if (!btn) return; |
| 393 | 390 | |
| 394 | - btn->hovered = 1; | |
| 395 | - XftDraw *backdraw = XftDrawCreate(window->display, window->backbuf, | |
| 396 | - DefaultVisual(window->display, DefaultScreen(window->display)), | |
| 397 | - DefaultColormap(window->display, DefaultScreen(window->display))); | |
| 398 | - if (!backdraw) { | |
| 399 | - fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n"); | |
| 400 | - XFreePixmap(window->display, window->backbuf); | |
| 401 | - window->backbuf = None; | |
| 402 | - return; | |
| 403 | - } | |
| 391 | + if (lasthover && lasthover != btn) lasthover->hovered = 0; | |
| 392 | + if (btn) btn->hovered = 1; | |
| 393 | + lasthover = btn; | |
| 404 | 394 | |
| 405 | 395 | window->event = (XEvent){.type = Expose}; |
| 406 | - control_expose(window, labels, btn); | |
| 396 | + control_expose(window, labels, btn ? btn : &(SuwaButton){0}); | |
| 407 | 397 | } |
変更
control.h
| @@ -47,4 +47,4 @@ | ||
| 47 | 47 | void handle_button_press(SuwaWindow *window, CtrlLabels *labels, int mx, int my); |
| 48 | 48 | void handle_button_release(SuwaWindow *window, CtrlLabels *labels, int mx, int my); |
| 49 | 49 | void handle_key_press(SuwaWindow *window, CtrlLabels *labels); |
| 50 | -void handle_mouse_hover(SuwaWindow *window, CtrlLabels *labels); | |
| 50 | +void handle_mouse_hover(SuwaWindow *window, CtrlLabels *labels, int mx, int my); |
| @@ -332,8 +332,8 @@ | ||
| 332 | 332 | | ButtonPressMask |
| 333 | 333 | | ButtonReleaseMask |
| 334 | 334 | | KeyPressMask |
| 335 | - // | PointerMotionMask | |
| 336 | - // | ButtonMotionMask | |
| 335 | + | PointerMotionMask | |
| 336 | + | Button1MotionMask | |
| 337 | 337 | // | StructureNotifyMask |
| 338 | 338 | ); |
| 339 | 339 |
変更
main.c
| @@ -43,6 +43,8 @@ | ||
| 43 | 43 | const char *version = "0.0.0"; |
| 44 | 44 | const char *disname = "Unix Calc"; |
| 45 | 45 | |
| 46 | +static Time lasthovertime = 0; | |
| 47 | + | |
| 46 | 48 | int main() { |
| 47 | 49 | #if defined(__OpenBSD__) |
| 48 | 50 | SuwaWindow window = suwaui_create_window( |
| @@ -109,7 +111,18 @@ | ||
| 109 | 111 | handle_key_press(&window, &lbl); |
| 110 | 112 | break; |
| 111 | 113 | case MotionNotify: |
| 112 | - handle_mouse_hover(&window, &lbl); | |
| 114 | + Time now = window.event.xmotion.time; | |
| 115 | + if (now - lasthovertime < 40) break; | |
| 116 | + lasthovertime = now; | |
| 117 | + Window root, child; | |
| 118 | + int root_x, root_y, | |
| 119 | + win_x, win_y; | |
| 120 | + unsigned int mask; | |
| 121 | + | |
| 122 | + XQueryPointer(window.display, window.xwindow, &root, &child, | |
| 123 | + &root_x, &root_y, &win_x, &win_y, &mask); | |
| 124 | + if (window.event.xmotion.state == 0) break; | |
| 125 | + handle_mouse_hover(&window, &lbl, win_x, win_y); | |
| 113 | 126 | break; |
| 114 | 127 | case ClientMessage: |
| 115 | 128 | // WM_DELETE_WINDOW |