]> git.draconx.ca Git - rrace.git/blob - src/motif_ui.c
Reduce the amount of redundant drawing in the goal area.
[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_least32_t *gp = state->board.game, prev[4];
221
222         memcpy(prev, gp, sizeof prev);
223         if (game_do_move(&state->board, x, y) == 0) {
224                 uint_least32_t mask;
225
226                 if (game_check_goal(&state->board)) {
227                         printf("You win!\n");
228                         game_finish(&state->board);
229                 }
230
231                 /* Figure out which tiles changed */
232                 prev[0] ^= gp[0];
233                 prev[1] ^= gp[1];
234                 prev[2] ^= gp[2];
235                 mask = prev[0] | prev[1] | prev[2];
236
237                 x11_redraw_game(state, mask);
238         }
239 }
240
241 static void game_input(Widget w, void *data, void *cb_data)
242 {
243         XmDrawingAreaCallbackStruct *cbs = cb_data;
244         XButtonEvent *click = &cbs->event->xbutton;
245         struct app_state *state = data;
246         Dimension width, height;
247         unsigned x = -1, y = -1;
248
249         switch (cbs->event->type) {
250         case ButtonPress:
251                 XtVaGetValues(w, XmNwidth, &width,
252                                  XmNheight, &height,
253                                  (char *)NULL);
254                 x = click->x / (width/5);
255                 y = click->y / (height/5);
256         }
257
258         if (x > 4 || y > 4)
259                 return;
260
261         do_input_move(state, x, y);
262 }
263
264 /* Figure out which tiles intersect a rectangle. */
265 static uint_fast32_t
266 expose_mask(int rect_x, int rect_y, int rect_w, int rect_h,
267                                     int tile_w, int tile_h)
268 {
269         return board_right(rect_x / tile_w)
270              & board_below(rect_y / tile_h)
271              & board_above((rect_y + rect_h - 1) / tile_h)
272              & board_left((rect_x + rect_w - 1) / tile_w);
273 }
274
275 static void game_expose(Widget w, void *data, void *cb_data)
276 {
277         XmDrawingAreaCallbackStruct *cbs = cb_data;
278         XExposeEvent *e = &cbs->event->xexpose;
279         Dimension width, height;
280         uint_fast32_t mask;
281
282         XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
283         if (!(width /= 5) || !(height /= 5))
284                 return;
285
286         mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
287         x11_redraw_game(data, mask);
288 }
289
290 static void goal_expose(Widget w, void *data, void *cb_data)
291 {
292         XmDrawingAreaCallbackStruct *cbs = cb_data;
293         XExposeEvent *e = &cbs->event->xexpose;
294         Dimension width, height;
295         uint_fast32_t mask;
296
297         XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
298         if (!(width /= 3) || !(height /= 3))
299                 return;
300
301         mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
302         x11_redraw_goal(data, mask);
303 }
304
305 void ui_initialize(struct app_state *state, Widget shell)
306 {
307         Widget menubar, help;
308
309         construct_widgets(mainwin, shell, 0);
310
311         menubar = XtNameToWidget(shell, "*menuBar");
312         construct_menu(mainmenu, menubar, 0);
313
314         help = XtNameToWidget(menubar, "helpMenu");
315         XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
316
317         game_configure(XtNameToWidget(shell, "*game"));
318
319         state->game = XtNameToWidget(shell, "*gameCanvas");
320         XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
321         XtAddCallback(state->game, XmNexposeCallback, game_expose, state);
322         XtAddCallback(state->game, XmNinputCallback,  game_input,  state);
323
324         state->goal = XtNameToWidget(shell, "*goalCanvas");
325         XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
326         XtAddCallback(state->goal, XmNexposeCallback, goal_expose, state);
327 }