Root/eeshow/gui/aoi.c

1/*
2 * gui/aoi.c - GUI: areas of interest
3 *
4 * Written 2016 by Werner Almesberger
5 * Copyright 2016 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 * Resources:
15 *
16 * http://zetcode.com/gfx/cairo/cairobackends/
17 * https://developer.gnome.org/gtk3/stable/gtk-migrating-2-to-3.html
18 */
19
20#include <stddef.h>
21#include <math.h>
22#include <assert.h>
23
24#include "misc/util.h"
25#include "gui/aoi.h"
26
27
28static const struct aoi *hovering = NULL;
29
30
31struct aoi *aoi_add(struct aoi **aois, const struct aoi *cfg)
32{
33    struct aoi *new;
34
35    new = alloc_type(struct aoi);
36    *new = *cfg;
37    new->next = *aois;
38    *aois = new;
39
40    return new;
41}
42
43
44void aoi_update(struct aoi *aoi, const struct aoi *cfg)
45{
46    struct aoi *next = aoi->next;
47
48    *aoi = *cfg;
49    aoi->next = next;
50}
51
52
53static bool in_aoi(const struct aoi *aoi, int x, int y)
54{
55    return x >= aoi->x && x < aoi->x + aoi->w &&
56        y >= aoi->y && y < aoi->y + aoi->h;
57}
58
59
60static bool hover_d(const struct aoi *aoi, bool on,int x, int y)
61{
62    return aoi->hover(aoi->user, on,
63        x < aoi->x ? -1 : x >= aoi->x + aoi->w ? 1 : 0,
64        y < aoi->y ? -1 : y >= aoi->y + aoi->h ? 1 : 0);
65}
66
67
68/*
69 * We need a pointer to the anchor of the AoI list here because dehovering may
70 * delete the AoI *aois points to.
71 *
72 * We could just check if hovering == *aois, but that seems risky, because
73 * hover(..., 0) may destroy more than just the AoI being dehovered.
74 */
75
76bool aoi_hover(struct aoi *const *aois, int x, int y)
77{
78    static int last_x = 0;
79    static int last_y = 0;
80    const struct aoi *aoi;
81
82    if (hovering) {
83        if (in_aoi(hovering, x, y))
84            return 1;
85        hover_d(hovering, 0, x, y);
86        hovering = NULL;
87    }
88
89    for (aoi = *aois; aoi; aoi = aoi->next)
90        if (aoi->hover && in_aoi(aoi, x, y) &&
91            hover_d(aoi, 1, last_x, last_y)) {
92            hovering = aoi;
93            break;
94        }
95    last_x = x;
96    last_y = y;
97    return aoi;
98}
99
100
101static bool need_dehover(const struct aoi *aois, int x, int y)
102{
103    const struct aoi *aoi;
104
105    if (!hovering)
106        return 0;
107    if (hovering->click)
108        return 0;
109    for (aoi = aois; aoi; aoi = aoi->next)
110        if (aoi->related == hovering && aoi->click && in_aoi(aoi, x, y))
111            return 0;
112    return 1;
113}
114
115
116/* Pointer to the anchor needed for the same reason as in aoi_hover. */
117
118bool aoi_click(struct aoi *const *aois, int x, int y)
119{
120    const struct aoi *aoi;
121
122    if (need_dehover(*aois, x, y))
123        aoi_dehover();
124
125    for (aoi = *aois; aoi; aoi = aoi->next)
126        if (aoi->click && in_aoi(aoi, x, y)) {
127            aoi->click(aoi->user);
128            return 1;
129        }
130    return 0;
131}
132
133
134void aoi_set_related(struct aoi *aoi, const struct aoi *related)
135{
136    assert(!aoi->related);
137    aoi->related = related;
138}
139
140
141void aoi_remove(struct aoi **aois, const struct aoi *aoi)
142{
143    assert(aoi);
144    if (hovering == aoi) {
145        aoi->hover(aoi->user, 0, 0, 0);
146        hovering = NULL;
147    }
148    while (*aois && *aois != aoi)
149        aois = &(*aois)->next;
150    assert(*aois);
151    *aois = aoi->next;
152    free((void *) aoi);
153}
154
155
156void aoi_dehover(void)
157{
158    if (hovering)
159        hovering->hover(hovering->user, 0, 0, 0);
160    hovering = NULL;
161}
162

Archive Download this file

Branches:
master



interactive