ボタン押すフィードバックの修正
諏訪子 <suwako@076.moe>、2026-01-19 22:23
コミット 70facb3bc92339c477c1be8997cc6aa9f4d51d07
・親 0abfbe1
・ファイルを見る
4個のファイル、+64 -55
変更
control.c
| @@ -53,6 +53,8 @@ | ||
| 53 | 53 | |
| 54 | 54 | char curinput[64] = {0}; |
| 55 | 55 | int input_pos = 0; |
| 56 | +int initialized = 0; | |
| 57 | +SuwaButton buttons[NUM_ROWS][NUM_COLS]; | |
| 56 | 58 | |
| 57 | 59 | static const char *btn_labels[][10] = { |
| 58 | 60 | { NULL }, // 数字表示 |
| @@ -102,7 +104,8 @@ | ||
| 102 | 104 | found.width = btn_w; |
| 103 | 105 | found.height = btn_h; |
| 104 | 106 | found.text = btn_labels[row][col]; |
| 105 | - found.pressed = 1; | |
| 107 | + found.row = row; | |
| 108 | + found.col = col; | |
| 106 | 109 | |
| 107 | 110 | return &found; |
| 108 | 111 | } |
| @@ -169,7 +172,35 @@ | ||
| 169 | 172 | return res; |
| 170 | 173 | } |
| 171 | 174 | |
| 172 | -void control_expose(SuwaWindow *window, CtrlLabels *labels) { | |
| 175 | +void initialize_basic_buttons(SuwaWindow *window, XftDraw *backdraw, float scale, | |
| 176 | + SuwaButton *but) { | |
| 177 | + int bw = (int)(DESIGN_BUTTON_WIDTH * scale + .5f); | |
| 178 | + int bh = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f); | |
| 179 | + int bp = (int)(DESIGN_PADDING * scale + .5f); | |
| 180 | + int by = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f); | |
| 181 | + | |
| 182 | + XftColor buttonColor; | |
| 183 | + buttonColor = suwaui_set_button_fgcolor(window, COLOR_BS); | |
| 184 | + | |
| 185 | + for (int row = 1; row < NUM_ROWS; ++row) { | |
| 186 | + for (int col = 0; col < NUM_COLS; ++col) { | |
| 187 | + SuwaButton btn = suwaui_add_button(bp + col * (bw + bp), | |
| 188 | + by + (row - 1) * (bh + bp), | |
| 189 | + bw, bh, btn_labels[row][col], window->font, COLOR_ND, buttonColor); | |
| 190 | + | |
| 191 | + if (but != NULL && but->text != NULL) { | |
| 192 | + if (strcmp(but->text, btn.text) == 0) { | |
| 193 | + if (but->pressed == 1) btn.pressed = 1; | |
| 194 | + else if (but->hovered == 1) btn.hovered = 1; | |
| 195 | + } | |
| 196 | + } | |
| 197 | + suwaui_draw_button(window, &btn, backdraw); | |
| 198 | + buttons[row][col] = btn; | |
| 199 | + } | |
| 200 | + } | |
| 201 | +} | |
| 202 | + | |
| 203 | +void control_expose(SuwaWindow *window, CtrlLabels *labels, SuwaButton *button) { | |
| 173 | 204 | if (window->event.type != Expose && window->event.type != ConfigureNotify) return; |
| 174 | 205 | |
| 175 | 206 | XWindowAttributes attr; |
| @@ -194,7 +225,7 @@ | ||
| 194 | 225 | return; |
| 195 | 226 | } |
| 196 | 227 | |
| 197 | - XSetForeground(window->display, window->gc, BGCOL); | |
| 228 | + XSetForeground(window->display, window->gc, COLOR_BD); | |
| 198 | 229 | XFillRectangle(window->display, window->backbuf, window->gc, 0, 0, w, h); |
| 199 | 230 | |
| 200 | 231 | float scale = get_scale(window); |
| @@ -231,35 +262,15 @@ | ||
| 231 | 262 | tx, ty, (const FcChar8 *)labels->problem->text, 64); |
| 232 | 263 | } |
| 233 | 264 | |
| 234 | - int bw = (int)(DESIGN_BUTTON_WIDTH * scale + .5f); | |
| 235 | - int bh = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f); | |
| 236 | - int bp = (int)(DESIGN_PADDING * scale + .5f); | |
| 237 | - int by = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f); | |
| 265 | + initialize_basic_buttons(window, backdraw, scale, button); | |
| 238 | 266 | |
| 239 | - XftColor buttonColor; | |
| 240 | -#if defined(__OpenBSD__) | |
| 241 | - buttonColor = suwaui_set_button_fgcolor(window, "#232320"); | |
| 242 | -#elif defined(__FreeBSD__) | |
| 243 | - buttonColor = suwaui_set_button_fgcolor(window, "#232020"); | |
| 244 | -#endif | |
| 245 | - | |
| 246 | - for (int row = 1; row < NUM_ROWS; ++row) { | |
| 247 | - for (int col = 0; col < NUM_COLS; ++col) { | |
| 248 | - SuwaButton btn = suwaui_add_button(bp + col * (bw + bp), | |
| 249 | - by + (row - 1) * (bh + bp), | |
| 250 | - bw, bh, btn_labels[row][col], window->font, BTCOL, buttonColor); | |
| 251 | - | |
| 252 | - suwaui_draw_button(window, &btn, backdraw); | |
| 253 | - } | |
| 254 | - } | |
| 255 | - | |
| 256 | 267 | XCopyArea(window->display, window->backbuf, window->xwindow, window->gc, |
| 257 | 268 | 0, 0, w, h, 0, 0); |
| 258 | 269 | XftDrawDestroy(backdraw); |
| 259 | 270 | XFlush(window->display); |
| 260 | 271 | } |
| 261 | 272 | |
| 262 | -void handle_button_press(SuwaWindow *window, int mx, int my) { | |
| 273 | +void handle_button_press(SuwaWindow *window, CtrlLabels *labels, int mx, int my) { | |
| 263 | 274 | SuwaButton *btn = find_button_at(window, mx, my); |
| 264 | 275 | if (!btn) return; |
| 265 | 276 | |
| @@ -274,8 +285,8 @@ | ||
| 274 | 285 | return; |
| 275 | 286 | } |
| 276 | 287 | |
| 277 | - suwaui_draw_button(window, btn, backdraw); | |
| 278 | - XFlush(window->display); | |
| 288 | + window->event = (XEvent){.type = Expose}; | |
| 289 | + control_expose(window, labels, btn); | |
| 279 | 290 | } |
| 280 | 291 | |
| 281 | 292 | void handle_button_release(SuwaWindow *window, CtrlLabels *labels, int mx, int my) { |
| @@ -293,11 +304,6 @@ | ||
| 293 | 304 | return; |
| 294 | 305 | } |
| 295 | 306 | |
| 296 | - window->target = window->backbuf; | |
| 297 | - suwaui_draw_button(window, btn, backdraw); | |
| 298 | - XftDrawDestroy(backdraw); | |
| 299 | - XFlush(window->display); | |
| 300 | - | |
| 301 | 307 | const char *label = btn->text; |
| 302 | 308 | if (strcmp(label, "C") == 0) { |
| 303 | 309 | clear_calculator(labels); |
| @@ -317,7 +323,7 @@ | ||
| 317 | 323 | } |
| 318 | 324 | |
| 319 | 325 | window->event = (XEvent){.type = Expose}; |
| 320 | - control_expose(window, labels); | |
| 326 | + control_expose(window, labels, btn); | |
| 321 | 327 | } |
| 322 | 328 | |
| 323 | 329 | void handle_key_press(SuwaWindow *window, CtrlLabels *labels) { |
| @@ -396,23 +402,26 @@ | ||
| 396 | 402 | |
| 397 | 403 | redraw: |
| 398 | 404 | window->event = (XEvent){.type = Expose}; |
| 399 | - control_expose(window, labels); | |
| 405 | + control_expose(window, labels, &(SuwaButton){0}); | |
| 400 | 406 | } |
| 401 | 407 | |
| 402 | -// void handle_mouse_hover(SuwaWindow *window) { | |
| 403 | -// int x = window->event,xmotion.x; | |
| 404 | -// int y = window->event,xmotion.y; | |
| 405 | -// SuwaButton *hover = find_button_at(x, y); | |
| 408 | +void handle_mouse_hover(SuwaWindow *window, CtrlLabels *labels) { | |
| 409 | + int x = window->event.xmotion.x; | |
| 410 | + int y = window->event.xmotion.y; | |
| 411 | + SuwaButton *btn = find_button_at(window, x, y); | |
| 412 | + if (!btn) return; | |
| 406 | 413 | |
| 407 | -// if (hover != hovered_btn) { | |
| 408 | -// if (hovered_btn) { | |
| 409 | -// hovered_btn->pressed = 0; | |
| 410 | -// redraw_single_button(&window, hovered_btn); | |
| 411 | -// } | |
| 412 | -// hovered_btn = hover; | |
| 414 | + btn->hovered = 1; | |
| 415 | + XftDraw *backdraw = XftDrawCreate(window->display, window->backbuf, | |
| 416 | + DefaultVisual(window->display, DefaultScreen(window->display)), | |
| 417 | + DefaultColormap(window->display, DefaultScreen(window->display))); | |
| 418 | + if (!backdraw) { | |
| 419 | + fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n"); | |
| 420 | + XFreePixmap(window->display, window->backbuf); | |
| 421 | + window->backbuf = None; | |
| 422 | + return; | |
| 423 | + } | |
| 413 | 424 | |
| 414 | -// if (hovered_btn) { | |
| 415 | -// redraw_single_button(&window, hovered_btn); | |
| 416 | -// } | |
| 417 | -// } | |
| 418 | -// } | |
| 425 | + window->event = (XEvent){.type = Expose}; | |
| 426 | + control_expose(window, labels, btn); | |
| 427 | +} |
変更
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); |
| @@ -332,8 +332,8 @@ | ||
| 332 | 332 | | ButtonPressMask |
| 333 | 333 | | ButtonReleaseMask |
| 334 | 334 | | KeyPressMask |
| 335 | - | PointerMotionMask | |
| 336 | - | ButtonMotionMask | |
| 335 | + // | PointerMotionMask | |
| 336 | + // | ButtonMotionMask | |
| 337 | 337 | // | StructureNotifyMask |
| 338 | 338 | ); |
| 339 | 339 |
変更
main.c
| @@ -108,9 +108,9 @@ | ||
| 108 | 108 | case KeyPress: |
| 109 | 109 | handle_key_press(&window, &lbl); |
| 110 | 110 | break; |
| 111 | - // case MotionNotify: | |
| 112 | - // handle_mouse_hover(&window, &lbl); | |
| 113 | - // break; | |
| 111 | + case MotionNotify: | |
| 112 | + handle_mouse_hover(&window, &lbl); | |
| 113 | + break; | |
| 114 | 114 | case ClientMessage: |
| 115 | 115 | // WM_DELETE_WINDOW |
| 116 | 116 | break; |