From 568ff47f18d09158e4760ff793be9004e4d61a96 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sat, 12 Mar 2022 17:36:07 -0500 Subject: [PATCH] Restructure motif code a bit. Move the game input callback into motif.c, which essentially brings all the user input parts together in one file and all the display parts in the other file. --- src/motif.c | 68 ++++++++++++++++++++++++++++++++++++++++++++ src/motif_ui.c | 77 +++----------------------------------------------- 2 files changed, 72 insertions(+), 73 deletions(-) diff --git a/src/motif.c b/src/motif.c index 12959f7..94e959b 100644 --- a/src/motif.c +++ b/src/motif.c @@ -125,6 +125,72 @@ static Widget early_setup(XtAppContext *app, int argc, char **argv) return shell; } +static void do_input_move(struct app_state *state, int x, int y) +{ + uint_fast32_t mask; + + if ((mask = game_do_move(&state->board, x, y)) != 0) { + if (game_check_goal(&state->board) == 0) { + int_fast32_t ms = game_finish(&state->board); + unsigned min, sec; + + /* Negative time just means clock jumps and + * display headaches. */ + if (ms < 0) + ms = 0; + + sec = ms / 1000, ms %= 1000; + min = sec / 60, sec %= 60; + printf("You won! Time was %u:%.2u:%.3u\n", + min, sec, (unsigned)ms); + mask |= ~GOAL_MASK; + } + + x11_redraw_game(state, mask); + } +} + +static void set_view_goal(struct app_state *state, int view_goal) +{ + state->view_goal_on_game = view_goal; + x11_redraw_game(state, game_check_goal(&state->board)); +} + +static void game_input(Widget w, void *data, void *cb_data) +{ + XmDrawingAreaCallbackStruct *cbs = cb_data; + XButtonEvent *b = &cbs->event->xbutton; + struct app_state *state = data; + Dimension width, height; + + switch (cbs->event->type) { + case ButtonPress: + switch (b->button) { + case Button1: + if (b->state & Button3Mask) + break; + + XtVaGetValues(w, XmNwidth, &width, + XmNheight, &height, + (char *)NULL); + + do_input_move(state, b->x / (width / 5), + b->y / (height / 5)); + break; + case Button3: + set_view_goal(state, 1); + break; + } + break; + case ButtonRelease: + switch (b->button) { + case Button3: + set_view_goal(state, 0); + break; + } + } +} + static struct app_state state; static void proc_exit(Widget w, XEvent *e, String *argv, Cardinal *argc) @@ -167,6 +233,8 @@ static XtAppContext app_initialize(int argc, char **argv) ui_initialize(&state, shell); x11_initialize(&state, shell); + XtAddCallback(state.game, XmNinputCallback, game_input, &state); + /* Begin with the game in winning state */ game_reset(&state.board); for (i = 0; i < 3; i++) diff --git a/src/motif_ui.c b/src/motif_ui.c index 85241d8..726e24f 100644 --- a/src/motif_ui.c +++ b/src/motif_ui.c @@ -95,7 +95,7 @@ ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args) XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL); } -static void game_configure(Widget form) +static void configure_mainwin(struct app_state *state, Widget form) { Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]); Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]); @@ -104,10 +104,12 @@ static void game_configure(Widget form) assert(gamearea && goalarea); XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL); + state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]); XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM, XmNtopAttachment, XmATTACH_FORM, (char *)NULL); + state->goal = XtNameToWidget(goalarea, &tree_strtab[goalCanvas]); XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, gamearea, XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET, @@ -215,72 +217,6 @@ static void goal_resize(Widget w, void *data, void *cb_data) x11_redraw_goal(data, -1); } -static void do_input_move(struct app_state *state, int x, int y) -{ - uint_fast32_t mask; - - if ((mask = game_do_move(&state->board, x, y)) != 0) { - if (game_check_goal(&state->board) == 0) { - int_fast32_t ms = game_finish(&state->board); - unsigned min, sec; - - /* Negative time just means clock jumps and - * display headaches. */ - if (ms < 0) - ms = 0; - - sec = ms / 1000, ms %= 1000; - min = sec / 60, sec %= 60; - printf("You won! Time was %u:%.2u:%.3u\n", - min, sec, (unsigned)ms); - mask |= ~GOAL_MASK; - } - - x11_redraw_game(state, mask); - } -} - -static void set_view_goal(struct app_state *state, int view_goal) -{ - state->view_goal_on_game = view_goal; - x11_redraw_game(state, game_check_goal(&state->board)); -} - -static void game_input(Widget w, void *data, void *cb_data) -{ - XmDrawingAreaCallbackStruct *cbs = cb_data; - XButtonEvent *b = &cbs->event->xbutton; - struct app_state *state = data; - Dimension width, height; - - switch (cbs->event->type) { - case ButtonPress: - switch (b->button) { - case Button1: - if (b->state & Button3Mask) - break; - - XtVaGetValues(w, XmNwidth, &width, - XmNheight, &height, - (char *)NULL); - - do_input_move(state, b->x / (width / 5), - b->y / (height / 5)); - break; - case Button3: - set_view_goal(state, 1); - break; - } - break; - case ButtonRelease: - switch (b->button) { - case Button3: - set_view_goal(state, 0); - break; - } - } -} - /* Figure out which tiles intersect a rectangle. */ static uint_fast32_t expose_mask(int rect_x, int rect_y, int rect_w, int rect_h, @@ -334,14 +270,9 @@ void ui_initialize(struct app_state *state, Widget shell) help = XtNameToWidget(menubar, "helpMenu"); XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL); - game_configure(XtNameToWidget(shell, "*game")); - - state->game = XtNameToWidget(shell, "*gameCanvas"); + configure_mainwin(state, XtNameToWidget(shell, "*game")); XtAddCallback(state->game, XmNresizeCallback, game_resize, state); XtAddCallback(state->game, XmNexposeCallback, game_expose, state); - XtAddCallback(state->game, XmNinputCallback, game_input, state); - - state->goal = XtNameToWidget(shell, "*goalCanvas"); XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state); XtAddCallback(state->goal, XmNexposeCallback, goal_expose, state); } -- 2.43.2