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