/* * X11 GUI for slide puzzle game * Copyright © 2022 Nick Bowler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include "motif.h" #include "motifstr.h" #include "motifgui.h" #define SPLIT_NUMERATOR 75 #define SPLIT_DENOMINATOR 100 #define MIN(a, b) ((a) < (b) ? (a) : (b)) /* XXX generate this list? */ enum { widgetMainWindow, widgetForm, widgetFrame, widgetDrawingArea, widgetCascadeButton, widgetPushButton, widgetMax, /* Pseudo-widgets */ widgetMenuBar = widgetMax }; static WidgetClass * const widgets[widgetMax] = { &xmMainWindowWidgetClass, &xmFormWidgetClass, &xmFrameWidgetClass, &xmDrawingAreaWidgetClass, &xmCascadeButtonWidgetClass, &xmPushButtonWidgetClass }; static const struct ui_widget { uint_least16_t name; uint_least8_t subtree; uint_least8_t widget_type; } mainwin[] = { MAINWIN_INITIALIZER }; static const struct ui_menuitem { struct ui_widget w; uint_least16_t label; } mainmenu[] = { MAINMENU_INITIALIZER }; static void ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args) { Widget game = XtNameToWidget(form, &tree_strtab[gameArea]); Widget goal = XtNameToWidget(form, &tree_strtab[goalArea]); Dimension w, h, gamesz, gameborder, goalsz, goalborder; int x, y, gap; XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL); XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL); XtVaGetValues(goal, XmNshadowThickness, &goalborder, XmNleftOffset, &gap, (char *)NULL); gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR); gamesz = 5*( (gamesz - 2*gameborder)/5 ) + 2*gameborder; goalsz = MIN(gamesz*3/5, w - gamesz - gap); goalsz = 3*( (goalsz - 2*goalborder)/3 ) + 2*goalborder; x = (w - gamesz - goalsz - gap) / 2; if (x < 2) x = 0; y = (h - gamesz) / 2; if (y < 3) y = 0; XtVaSetValues(game, XmNleftOffset, x, XmNtopOffset, y, (char *)NULL); XtVaSetValues(game, XmNwidth, gamesz, XmNheight, gamesz, (char *)NULL); XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL); } static void game_configure(Widget form) { Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]); Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]); XtActionsRec resize_rec; assert(gamearea && goalarea); XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL); XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM, XmNtopAttachment, XmATTACH_FORM, (char *)NULL); XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, gamearea, XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET, XmNtopWidget, gamearea, (char *)NULL); resize_rec.string = "ResizeGameArea"; resize_rec.proc = ResizeGameArea; XtAppAddActions(XtWidgetToApplicationContext(form), &resize_rec, 1); XtOverrideTranslations(form, XtParseTranslationTable( ": ResizeGameArea()\n" ": ResizeGameArea()\n" )); } static Widget create_widget(const struct ui_widget *item, Widget parent, ArgList args, Cardinal num_args) { String name = (void *)&tree_strtab[item->name]; WidgetClass class; if (item->widget_type == widgetMenuBar) return XmCreateMenuBar(parent, name, args, num_args); assert(item->widget_type < widgetMax); class = *widgets[item->widget_type]; return XtCreateWidget(name, class, parent, args, num_args); } static void construct_widgets(const struct ui_widget *root, Widget parent, unsigned i) { const struct ui_widget *item; for (item = &root[i]; item->name; item++) { Widget w = create_widget(item, parent, NULL, 0); if (item->subtree) construct_widgets(root, w, item->subtree); XtManageChild(w); } } static void menu_cb(Widget w, void *data, void *cb_data) { XmRowColumnCallbackStruct *cbs = cb_data; XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0); } static Widget create_pulldown(Widget parent) { Widget w; w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0); XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL); XtAddCallback(w, XmNentryCallback, menu_cb, NULL); return w; } static void construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i) { const struct ui_menuitem *item; for (item = &root[i]; item->w.name; item++) { const char *label = &strtab[item->label]; unsigned n = 0; Arg args[2]; XmString s; Widget w; if (XtClass(parent) == *widgets[widgetCascadeButton]) parent = create_pulldown(parent); if (label[0] && label[1] == '|') { XtSetArg(args[n], XmNmnemonic, label[0]); n++; label += 2; } s = XmStringCreateLocalized((void *)label); XtSetArg(args[n], XmNlabelString, s); n++; w = create_widget(&item->w, parent, args, n); XmStringFree(s); if (item->w.subtree) construct_menu(root, w, item->w.subtree); XtManageChild(w); } } static void game_resize(Widget w, void *data, void *cb_data) { if (XtIsRealized(w)) x11_redraw_game(data, -1); } static void goal_resize(Widget w, void *data, void *cb_data) { if (XtIsRealized(w)) x11_redraw_goal(data, -1); } static void do_input_move(struct app_state *state, int x, int y) { uint_least32_t *gp = state->board.game, prev[4]; memcpy(prev, gp, sizeof prev); if (game_do_move(&state->board, x, y) == 0) { uint_least32_t mask; if (game_check_goal(&state->board)) { printf("You win!\n"); game_finish(&state->board); } /* Figure out which tiles changed */ prev[0] ^= gp[0]; prev[1] ^= gp[1]; prev[2] ^= gp[2]; mask = prev[0] | prev[1] | prev[2]; x11_redraw_game(state, mask); } } static void game_input(Widget w, void *data, void *cb_data) { XmDrawingAreaCallbackStruct *cbs = cb_data; XButtonEvent *click = &cbs->event->xbutton; struct app_state *state = data; Dimension width, height; unsigned x = -1, y = -1; switch (cbs->event->type) { case ButtonPress: XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL); x = click->x / (width/5); y = click->y / (height/5); } if (x > 4 || y > 4) return; do_input_move(state, x, y); } /* 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, int tile_w, int tile_h) { return board_right(rect_x / tile_w) & board_below(rect_y / tile_h) & board_above((rect_y + rect_h - 1) / tile_h) & board_left((rect_x + rect_w - 1) / tile_w); } static void game_expose(Widget w, void *data, void *cb_data) { XmDrawingAreaCallbackStruct *cbs = cb_data; XExposeEvent *e = &cbs->event->xexpose; Dimension width, height; uint_fast32_t mask; XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL); if (!(width /= 5) || !(height /= 5)) return; mask = expose_mask(e->x, e->y, e->width, e->height, width, height); x11_redraw_game(data, mask); } static void goal_expose(Widget w, void *data, void *cb_data) { XmDrawingAreaCallbackStruct *cbs = cb_data; XExposeEvent *e = &cbs->event->xexpose; Dimension width, height; uint_fast32_t mask; XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL); if (!(width /= 3) || !(height /= 3)) return; mask = expose_mask(e->x, e->y, e->width, e->height, width, height); x11_redraw_goal(data, mask); } void ui_initialize(struct app_state *state, Widget shell) { Widget menubar, help; construct_widgets(mainwin, shell, 0); menubar = XtNameToWidget(shell, "*menuBar"); construct_menu(mainmenu, menubar, 0); help = XtNameToWidget(menubar, "helpMenu"); XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL); game_configure(XtNameToWidget(shell, "*game")); state->game = XtNameToWidget(shell, "*gameCanvas"); 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); }