2 * X11 GUI for slide puzzle game
3 * Copyright © 2022 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
28 #define SPLIT_NUMERATOR 75
29 #define SPLIT_DENOMINATOR 100
31 #define MIN(a, b) ((a) < (b) ? (a) : (b))
33 /* XXX generate this list? */
44 widgetMenuBar = widgetMax
47 static WidgetClass * const widgets[widgetMax] = {
48 &xmMainWindowWidgetClass,
51 &xmDrawingAreaWidgetClass,
52 &xmCascadeButtonWidgetClass,
53 &xmPushButtonWidgetClass
56 static const struct ui_widget {
58 uint_least8_t subtree;
59 uint_least8_t widget_type;
60 } mainwin[] = { MAINWIN_INITIALIZER };
62 static const struct ui_menuitem {
65 } mainmenu[] = { MAINMENU_INITIALIZER };
68 ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args)
70 Widget game = XtNameToWidget(form, &tree_strtab[gameArea]);
71 Widget goal = XtNameToWidget(form, &tree_strtab[goalArea]);
72 Dimension w, h, gamesz, gameborder, goalsz, goalborder;
75 XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
76 XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
77 XtVaGetValues(goal, XmNshadowThickness, &goalborder,
81 gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
82 gamesz = 5*( (gamesz - 2*gameborder)/5 ) + 2*gameborder;
84 goalsz = MIN(gamesz*3/5, w - gamesz - gap);
85 goalsz = 3*( (goalsz - 2*goalborder)/3 ) + 2*goalborder;
87 x = (w - gamesz - goalsz - gap) / 2;
93 XtVaSetValues(game, XmNleftOffset, x, XmNtopOffset, y, (char *)NULL);
94 XtVaSetValues(game, XmNwidth, gamesz, XmNheight, gamesz, (char *)NULL);
95 XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL);
98 static void game_configure(Widget form)
100 Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
101 Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
102 XtActionsRec resize_rec;
104 assert(gamearea && goalarea);
105 XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL);
107 XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
108 XmNtopAttachment, XmATTACH_FORM,
111 XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET,
112 XmNleftWidget, gamearea,
113 XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
114 XmNtopWidget, gamearea,
117 resize_rec.string = "ResizeGameArea";
118 resize_rec.proc = ResizeGameArea;
119 XtAppAddActions(XtWidgetToApplicationContext(form), &resize_rec, 1);
120 XtOverrideTranslations(form, XtParseTranslationTable(
121 "<Configure>: ResizeGameArea()\n"
122 "<Map>: ResizeGameArea()\n"
126 static Widget create_widget(const struct ui_widget *item, Widget parent,
127 ArgList args, Cardinal num_args)
129 String name = (void *)&tree_strtab[item->name];
132 if (item->widget_type == widgetMenuBar)
133 return XmCreateMenuBar(parent, name, args, num_args);
135 assert(item->widget_type < widgetMax);
136 class = *widgets[item->widget_type];
137 return XtCreateWidget(name, class, parent, args, num_args);
141 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
143 const struct ui_widget *item;
145 for (item = &root[i]; item->name; item++) {
146 Widget w = create_widget(item, parent, NULL, 0);
149 construct_widgets(root, w, item->subtree);
155 static void menu_cb(Widget w, void *data, void *cb_data)
157 XmRowColumnCallbackStruct *cbs = cb_data;
158 XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
161 static Widget create_pulldown(Widget parent)
165 w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
166 XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
167 XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
173 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
175 const struct ui_menuitem *item;
177 for (item = &root[i]; item->w.name; item++) {
178 const char *label = &strtab[item->label];
184 if (XtClass(parent) == *widgets[widgetCascadeButton])
185 parent = create_pulldown(parent);
187 if (label[0] && label[1] == '|') {
188 XtSetArg(args[n], XmNmnemonic, label[0]); n++;
192 s = XmStringCreateLocalized((void *)label);
193 XtSetArg(args[n], XmNlabelString, s); n++;
195 w = create_widget(&item->w, parent, args, n);
200 construct_menu(root, w, item->w.subtree);
206 static void game_resize(Widget w, void *data, void *cb_data)
209 x11_redraw_game(data, -1);
212 static void goal_resize(Widget w, void *data, void *cb_data)
215 x11_redraw_goal(data, -1);
218 static void do_input_move(struct app_state *state, int x, int y)
220 uint_least32_t *gp = state->board.game, prev[4];
222 memcpy(prev, gp, sizeof prev);
223 if (game_do_move(&state->board, x, y) == 0) {
226 if (game_check_goal(&state->board) == 0) {
227 int_fast32_t ms = game_finish(&state->board);
230 /* Negative time just means clock jumps and
231 * display headaches. */
235 sec = ms / 1000, ms %= 1000;
236 min = sec / 60, sec %= 60;
237 printf("You won! Time was %u:%.2u:%.3u\n",
238 min, sec, (unsigned)ms);
241 /* Figure out which tiles changed */
245 mask = prev[0] | prev[1] | prev[2];
247 x11_redraw_game(state, mask);
251 static void set_view_goal(struct app_state *state, int view_goal)
253 state->view_goal_on_game = view_goal;
254 x11_redraw_game(state, game_check_goal(&state->board));
257 static void game_input(Widget w, void *data, void *cb_data)
259 XmDrawingAreaCallbackStruct *cbs = cb_data;
260 XButtonEvent *b = &cbs->event->xbutton;
261 struct app_state *state = data;
262 Dimension width, height;
264 switch (cbs->event->type) {
268 if (b->state & Button3Mask)
271 XtVaGetValues(w, XmNwidth, &width,
275 do_input_move(state, b->x / (width / 5),
276 b->y / (height / 5));
279 set_view_goal(state, 1);
286 set_view_goal(state, 0);
292 /* Figure out which tiles intersect a rectangle. */
294 expose_mask(int rect_x, int rect_y, int rect_w, int rect_h,
295 int tile_w, int tile_h)
297 return board_right(rect_x / tile_w)
298 & board_below(rect_y / tile_h)
299 & board_above((rect_y + rect_h - 1) / tile_h)
300 & board_left((rect_x + rect_w - 1) / tile_w);
303 static void game_expose(Widget w, void *data, void *cb_data)
305 XmDrawingAreaCallbackStruct *cbs = cb_data;
306 XExposeEvent *e = &cbs->event->xexpose;
307 Dimension width, height;
310 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
311 if (!(width /= 5) || !(height /= 5))
314 mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
315 x11_redraw_game(data, mask);
318 static void goal_expose(Widget w, void *data, void *cb_data)
320 XmDrawingAreaCallbackStruct *cbs = cb_data;
321 XExposeEvent *e = &cbs->event->xexpose;
322 Dimension width, height;
325 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
326 if (!(width /= 3) || !(height /= 3))
329 mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
330 x11_redraw_goal(data, mask);
333 void ui_initialize(struct app_state *state, Widget shell)
335 Widget menubar, help;
337 construct_widgets(mainwin, shell, 0);
339 menubar = XtNameToWidget(shell, "*menuBar");
340 construct_menu(mainmenu, menubar, 0);
342 help = XtNameToWidget(menubar, "helpMenu");
343 XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
345 game_configure(XtNameToWidget(shell, "*game"));
347 state->game = XtNameToWidget(shell, "*gameCanvas");
348 XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
349 XtAddCallback(state->game, XmNexposeCallback, game_expose, state);
350 XtAddCallback(state->game, XmNinputCallback, game_input, state);
352 state->goal = XtNameToWidget(shell, "*goalCanvas");
353 XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
354 XtAddCallback(state->goal, XmNexposeCallback, goal_expose, state);