]> git.draconx.ca Git - rrace.git/blobdiff - src/motif_ui.c
Implement some basic gameplay.
[rrace.git] / src / motif_ui.c
index 0708b4029ba41152c6d3740f9ab3028b5707b904..fbef33b73334fad576410ed2911f3fff56871948 100644 (file)
@@ -123,31 +123,52 @@ construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
 
 static void game_resize(Widget w, void *data, void *cb_data)
 {
-       Dimension width, height;
-
-       XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
-
-       printf("game %ux%u\n", width, height);
+       if (XtIsRealized(w))
+               x11_redraw_game(data);
 }
 
 static void goal_resize(Widget w, void *data, void *cb_data)
 {
+       if (XtIsRealized(w))
+               x11_redraw_goal(data);
+}
+
+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 / (width/5);
+       }
 
-       XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
+       if (x > 4 || y > 4)
+               return;
 
-       printf("goal %ux%u\n", width, height);
+       if (game_do_move(&state->board, x, y) == 0) {
+               x11_redraw_game(state);
+       }
 }
 
-void ui_initialize(Widget shell)
+void ui_initialize(struct app_state *state, Widget shell)
 {
-       Widget game, goal;
-
        construct_widgets(mainwin, shell, 0);
 
-       game = XtNameToWidget(shell, "*gameCanvas");
-       goal = XtNameToWidget(shell, "*goalCanvas");
+       state->game = XtNameToWidget(shell, "*gameCanvas");
+       state->goal = XtNameToWidget(shell, "*goalCanvas");
+
+       XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
+       XtAddCallback(state->game, XmNexposeCallback, game_resize, state);
+       XtAddCallback(state->game, XmNinputCallback,  game_input,  state);
 
-       XtAddCallback(game, XmNresizeCallback, game_resize, NULL);
-       XtAddCallback(goal, XmNresizeCallback, goal_resize, NULL);
+       XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
+       XtAddCallback(state->goal, XmNexposeCallback, goal_resize, state);
 }