Root/gui_over.c

Source at commit 5fdea0e99a2e8543a59d508cf28e1f2ff3607f18 created 4 years 7 months ago.
By Werner Almesberger, gui_canvas.c (key_press_event): make / rotate through packages as well
1/*
2 * gui_over.c - GUI, canvas overlays
3 *
4 * Written 2009, 2010 by Werner Almesberger
5 * Copyright 2009, 2010 by Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13/*
14 * This file is for the overlay state machine only. Given the heavy use of
15 * global variables, adding other functionality would quickly render it
16 * illegible.
17 */
18
19
20#include <stdlib.h>
21#include <stdio.h>
22
23#include "coord.h"
24#include "gui_util.h"
25#include "gui_over.h"
26
27
28#if 0
29#define DPRINTF(fmt, ...) fprintf(stderr, fmt "\n", ##__VA_ARGS__)
30#define DSAVE(pix_buf) debug_save_pixbuf(pix_buf->buf)
31#else
32#define DPRINTF(fmt, ...)
33#define DSAVE(buf)
34#endif
35
36
37static enum states {
38    NOTHING,
39    HOVER,
40    DRAG,
41    BOTH,
42} state = NOTHING;
43
44
45/*
46 * We cache some externally provided state so that we can redraw without the
47 * outside telling us what to redraw, etc.
48 */
49
50static struct pix_buf *buf_D, *buf_H;
51static struct pix_buf *(*over_D_save_and_draw)(void *user, struct coord to);
52static void *over_D_user;
53static struct pix_buf *(*over_H_save_and_draw)(void *user);
54static void *over_H_user;
55static struct coord over_pos;
56
57
58/* ----- actions ----------------------------------------------------------- */
59
60
61static void draw_D(void)
62{
63    buf_D = over_D_save_and_draw(over_D_user, over_pos);
64    DSAVE(buf_D);
65}
66
67
68static void draw_H(void)
69{
70    buf_H = over_H_save_and_draw(over_H_user);
71    DSAVE(buf_H);
72}
73
74
75#define STATE(s) DPRINTF("%s", #s); state = s; break;
76#define restore(x) DPRINTF(" restore(%s)", #x); restore_pix_buf(buf_##x)
77#define drop(x) DPRINTF(" drop(%s)", #x); free_pix_buf(buf_##x)
78#define save(x) DPRINTF(" save(%s)", #x)
79#define draw(x) DPRINTF(" draw(%s)", #x); draw_##x()
80#define update() DPRINTF(" update"); over_pos = pos
81
82
83/* ----- state machine ----------------------------------------------------- */
84
85
86void over_enter(struct pix_buf *(*save_and_draw)(void *user), void *user)
87{
88    over_H_save_and_draw = save_and_draw;
89    over_H_user = user;
90
91    DPRINTF("enter");
92    switch (state) {
93    case NOTHING:
94        save(H);
95        draw(H);
96        STATE(HOVER);
97    case DRAG:
98        restore(D);
99        save(H);
100        draw(H);
101        save(D);
102        draw(D);
103        STATE(BOTH);
104    default:
105        abort();
106    }
107}
108
109
110void over_leave(void)
111{
112    DPRINTF("leave");
113    switch (state) {
114    case HOVER:
115        restore(H);
116        STATE(NOTHING);
117    case BOTH:
118        restore(D);
119        restore(H);
120        save(D);
121        draw(D);
122        STATE(DRAG);
123    default:
124        abort();
125    }
126}
127
128
129void over_begin(struct pix_buf *(*save_and_draw)(void *user, struct coord to),
130    void *user, struct coord pos)
131{
132    over_pos = pos;
133    over_D_save_and_draw = save_and_draw;
134    over_D_user = user;
135
136    DPRINTF("begin");
137    switch (state) {
138    case NOTHING:
139        save(D);
140        draw(D);
141        STATE(DRAG);
142    case HOVER:
143        save(D);
144        draw(D);
145        STATE(BOTH);
146    default:
147        abort();
148    }
149}
150
151
152void over_move(struct coord pos)
153{
154    over_pos = pos;
155
156    DPRINTF("move");
157    switch (state) {
158    case NOTHING:
159        break;
160    case HOVER:
161        break;
162    case DRAG:
163        restore(D);
164        update();
165        save(D);
166        draw(D);
167        STATE(DRAG);
168    case BOTH:
169        restore(D);
170        update();
171        save(D);
172        draw(D);
173        STATE(BOTH);
174    default:
175        abort();
176    }
177}
178
179
180void over_end(void)
181{
182    DPRINTF("end");
183    switch (state) {
184    case DRAG:
185        restore(D);
186        STATE(NOTHING);
187    case BOTH:
188        restore(D);
189        STATE(HOVER);
190    default:
191        abort();
192    }
193}
194
195
196void over_reset(void)
197{
198    DPRINTF("reset");
199    switch (state) {
200    case NOTHING:
201        break;
202    case HOVER:
203        drop(H);
204        STATE(NOTHING);
205    case DRAG:
206        drop(D);
207        STATE(NOTHING);
208    case BOTH:
209        drop(D);
210        drop(H);
211        STATE(NOTHING);
212    default:
213        abort();
214    }
215}
216

Archive Download this file

Branches:
master



interactive