]> git.draconx.ca Git - rrace.git/blob - src/motif_ui.c
Report changed positions from game_do_move.
[rrace.git] / src / motif_ui.c
1 /*
2  * X11 GUI for slide puzzle game
3  * Copyright © 2022 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20 #include <inttypes.h>
21 #include <assert.h>
22 #include <Xm/XmAll.h>
23
24 #include "motif.h"
25 #include "motifstr.h"
26 #include "motifgui.h"
27
28 #define SPLIT_NUMERATOR    75
29 #define SPLIT_DENOMINATOR 100
30
31 #define MIN(a, b) ((a) < (b) ? (a) : (b))
32
33 /* XXX generate this list? */
34 enum {
35         widgetMainWindow,
36         widgetForm,
37         widgetFrame,
38         widgetDrawingArea,
39         widgetCascadeButton,
40         widgetPushButton,
41         widgetMax,
42
43         /* Pseudo-widgets */
44         widgetMenuBar = widgetMax
45 };
46
47 static WidgetClass * const widgets[widgetMax] = {
48         &xmMainWindowWidgetClass,
49         &xmFormWidgetClass,
50         &xmFrameWidgetClass,
51         &xmDrawingAreaWidgetClass,
52         &xmCascadeButtonWidgetClass,
53         &xmPushButtonWidgetClass
54 };
55
56 static const struct ui_widget {
57         uint_least16_t name;
58         uint_least8_t subtree;
59         uint_least8_t widget_type;
60 } mainwin[] = { MAINWIN_INITIALIZER };
61
62 static const struct ui_menuitem {
63         struct ui_widget w;
64         uint_least16_t label;
65 } mainmenu[] = { MAINMENU_INITIALIZER };
66
67 static void
68 ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args)
69 {
70         Widget game = XtNameToWidget(form, &tree_strtab[gameArea]);
71         Widget goal = XtNameToWidget(form, &tree_strtab[goalArea]);
72         Dimension w, h, gamesz, gameborder, goalsz, goalborder;
73         int x, y, gap;
74
75         XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
76         XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
77         XtVaGetValues(goal, XmNshadowThickness, &goalborder,
78                             XmNleftOffset, &gap,
79                             (char *)NULL);
80
81         gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
82         gamesz = 5*( (gamesz - 2*gameborder)/5 ) + 2*gameborder;
83
84         goalsz = MIN(gamesz*3/5, w - gamesz - gap);
85         goalsz = 3*( (goalsz - 2*goalborder)/3 ) + 2*goalborder;
86
87         x = (w - gamesz - goalsz - gap) / 2;
88         if (x < 2) x = 0;
89
90         y = (h - gamesz) / 2;
91         if (y < 3) y = 0;
92
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);
96 }
97
98 static void game_configure(Widget form)
99 {
100         Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
101         Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
102         XtActionsRec resize_rec;
103
104         assert(gamearea && goalarea);
105         XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL);
106
107         XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
108                                 XmNtopAttachment, XmATTACH_FORM,
109                                 (char *)NULL);
110
111         XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET,
112                                 XmNleftWidget, gamearea,
113                                 XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
114                                 XmNtopWidget, gamearea,
115                                 (char *)NULL);
116
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"
123         ));
124 }
125
126 static Widget create_widget(const struct ui_widget *item, Widget parent,
127                             ArgList args, Cardinal num_args)
128 {
129         String name = (void *)&tree_strtab[item->name];
130         WidgetClass class;
131
132         if (item->widget_type == widgetMenuBar)
133                 return XmCreateMenuBar(parent, name, args, num_args);
134
135         assert(item->widget_type < widgetMax);
136         class = *widgets[item->widget_type];
137         return XtCreateWidget(name, class, parent, args, num_args);
138 }
139
140 static void
141 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
142 {
143         const struct ui_widget *item;
144
145         for (item = &root[i]; item->name; item++) {
146                 Widget w = create_widget(item, parent, NULL, 0);
147
148                 if (item->subtree)
149                         construct_widgets(root, w, item->subtree);
150
151                 XtManageChild(w);
152         }
153 }
154
155 static void menu_cb(Widget w, void *data, void *cb_data)
156 {
157         XmRowColumnCallbackStruct *cbs = cb_data;
158         XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
159 }
160
161 static Widget create_pulldown(Widget parent)
162 {
163         Widget w;
164
165         w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
166         XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
167         XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
168
169         return w;
170 }
171
172 static void
173 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
174 {
175         const struct ui_menuitem *item;
176
177         for (item = &root[i]; item->w.name; item++) {
178                 const char *label = &strtab[item->label];
179                 unsigned n = 0;
180                 Arg args[2];
181                 XmString s;
182                 Widget w;
183
184                 if (XtClass(parent) == *widgets[widgetCascadeButton])
185                         parent = create_pulldown(parent);
186
187                 if (label[0] && label[1] == '|') {
188                         XtSetArg(args[n], XmNmnemonic, label[0]); n++;
189                         label += 2;
190                 }
191
192                 s = XmStringCreateLocalized((void *)label);
193                 XtSetArg(args[n], XmNlabelString, s); n++;
194
195                 w = create_widget(&item->w, parent, args, n);
196
197                 XmStringFree(s);
198
199                 if (item->w.subtree)
200                         construct_menu(root, w, item->w.subtree);
201
202                 XtManageChild(w);
203         }
204 }
205
206 static void game_resize(Widget w, void *data, void *cb_data)
207 {
208         if (XtIsRealized(w))
209                 x11_redraw_game(data, -1);
210 }
211
212 static void goal_resize(Widget w, void *data, void *cb_data)
213 {
214         if (XtIsRealized(w))
215                 x11_redraw_goal(data, -1);
216 }
217
218 static void do_input_move(struct app_state *state, int x, int y)
219 {
220         uint_fast32_t mask;
221
222         if ((mask = game_do_move(&state->board, x, y)) != 0) {
223                 if (game_check_goal(&state->board) == 0) {
224                         int_fast32_t ms = game_finish(&state->board);
225                         unsigned min, sec;
226
227                         /* Negative time just means clock jumps and
228                          * display headaches. */
229                         if (ms < 0)
230                                 ms = 0;
231
232                         sec = ms / 1000, ms %= 1000;
233                         min = sec / 60, sec %= 60;
234                         printf("You won!  Time was %u:%.2u:%.3u\n",
235                                min, sec, (unsigned)ms);
236                         mask |= ~GOAL_MASK;
237                 }
238
239                 x11_redraw_game(state, mask);
240         }
241 }
242
243 static void set_view_goal(struct app_state *state, int view_goal)
244 {
245         state->view_goal_on_game = view_goal;
246         x11_redraw_game(state, game_check_goal(&state->board));
247 }
248
249 static void game_input(Widget w, void *data, void *cb_data)
250 {
251         XmDrawingAreaCallbackStruct *cbs = cb_data;
252         XButtonEvent *b = &cbs->event->xbutton;
253         struct app_state *state = data;
254         Dimension width, height;
255
256         switch (cbs->event->type) {
257         case ButtonPress:
258                 switch (b->button) {
259                 case Button1:
260                         if (b->state & Button3Mask)
261                                 break;
262
263                         XtVaGetValues(w, XmNwidth, &width,
264                                          XmNheight, &height,
265                                          (char *)NULL);
266
267                         do_input_move(state, b->x / (width / 5),
268                                              b->y / (height / 5));
269                         break;
270                 case Button3:
271                         set_view_goal(state, 1);
272                         break;
273                 }
274                 break;
275         case ButtonRelease:
276                 switch (b->button) {
277                 case Button3:
278                         set_view_goal(state, 0);
279                         break;
280                 }
281         }
282 }
283
284 /* Figure out which tiles intersect a rectangle. */
285 static uint_fast32_t
286 expose_mask(int rect_x, int rect_y, int rect_w, int rect_h,
287                                     int tile_w, int tile_h)
288 {
289         return board_right(rect_x / tile_w)
290              & board_below(rect_y / tile_h)
291              & board_above((rect_y + rect_h - 1) / tile_h)
292              & board_left((rect_x + rect_w - 1) / tile_w);
293 }
294
295 static void game_expose(Widget w, void *data, void *cb_data)
296 {
297         XmDrawingAreaCallbackStruct *cbs = cb_data;
298         XExposeEvent *e = &cbs->event->xexpose;
299         Dimension width, height;
300         uint_fast32_t mask;
301
302         XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
303         if (!(width /= 5) || !(height /= 5))
304                 return;
305
306         mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
307         x11_redraw_game(data, mask);
308 }
309
310 static void goal_expose(Widget w, void *data, void *cb_data)
311 {
312         XmDrawingAreaCallbackStruct *cbs = cb_data;
313         XExposeEvent *e = &cbs->event->xexpose;
314         Dimension width, height;
315         uint_fast32_t mask;
316
317         XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
318         if (!(width /= 3) || !(height /= 3))
319                 return;
320
321         mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
322         x11_redraw_goal(data, mask);
323 }
324
325 void ui_initialize(struct app_state *state, Widget shell)
326 {
327         Widget menubar, help;
328
329         construct_widgets(mainwin, shell, 0);
330
331         menubar = XtNameToWidget(shell, "*menuBar");
332         construct_menu(mainmenu, menubar, 0);
333
334         help = XtNameToWidget(menubar, "helpMenu");
335         XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
336
337         game_configure(XtNameToWidget(shell, "*game"));
338
339         state->game = XtNameToWidget(shell, "*gameCanvas");
340         XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
341         XtAddCallback(state->game, XmNexposeCallback, game_expose, state);
342         XtAddCallback(state->game, XmNinputCallback,  game_input,  state);
343
344         state->goal = XtNameToWidget(shell, "*goalCanvas");
345         XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
346         XtAddCallback(state->goal, XmNexposeCallback, goal_expose, state);
347 }