summaryrefslogtreecommitdiff
path: root/gui/aoi.c
blob: daa65eedcf7339a0ea784ec8b9e17d6188122239 (plain)
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
/*
 * gui/aoi.c - GUI: areas of interest
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 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.
 */

/*
 * Resources:
 *
 * http://zetcode.com/gfx/cairo/cairobackends/
 * https://developer.gnome.org/gtk3/stable/gtk-migrating-2-to-3.html
 */

#include <stddef.h>
#include <math.h>
#include <assert.h>

#include "misc/util.h"
#include "gui/aoi.h"


static const struct aoi *hovering = NULL;


struct aoi *aoi_add(struct aoi **aois, const struct aoi *cfg)
{
	struct aoi *new;

	new = alloc_type(struct aoi);
	*new = *cfg;
	new->next = *aois;
	*aois = new;

	return new;
}


void aoi_update(struct aoi *aoi, const struct aoi *cfg)
{
	struct aoi *next = aoi->next;

	*aoi = *cfg;
	aoi->next = next;
}


static bool in_aoi(const struct aoi *aoi, int x, int y)
{
	return x >= aoi->x && x < aoi->x + aoi->w &&
	    y >= aoi->y && y < aoi->y + aoi->h;
}


static bool hover_d(const struct aoi *aoi, bool on,int x, int y)
{
	return aoi->hover(aoi->user, on,
	    x < aoi->x ? -1 : x >= aoi->x + aoi->w ? 1 : 0,
	    y < aoi->y ? -1 : y >= aoi->y + aoi->h ? 1 : 0);
}


/*
 * We need a pointer to the anchor of the AoI list here because dehovering may
 * delete the AoI *aois points to.
 *
 * We could just check if hovering == *aois, but that seems risky, because
 * hover(..., 0) may destroy more than just the AoI being dehovered.
 */

bool aoi_hover(struct aoi *const *aois, int x, int y)
{
	static int last_x = 0;
	static int last_y = 0;
	const struct aoi *aoi;

	if (hovering) {
		if (in_aoi(hovering, x, y))
			return 1;
		hover_d(hovering, 0, x, y);
		hovering = NULL;
	}

	for (aoi = *aois; aoi; aoi = aoi->next)
		if (aoi->hover && in_aoi(aoi, x, y) &&
		    hover_d(aoi, 1, last_x, last_y)) {
			hovering = aoi;
			break;
		}
	last_x = x;
	last_y = y;
	return aoi;
}


static bool need_dehover(const struct aoi *aois, int x, int y)
{
	const struct aoi *aoi;

	if (!hovering)
		return 0;
	if (hovering->click)
		return 0;
	for (aoi = aois; aoi; aoi = aoi->next)
		if (aoi->related == hovering && aoi->click && in_aoi(aoi, x, y))
			return 0;
	return 1;
}


/* Pointer to the anchor needed for the same reason as in aoi_hover. */

bool aoi_click(struct aoi *const *aois, int x, int y)
{
	const struct aoi *aoi;

	if (need_dehover(*aois, x, y))
		aoi_dehover();

	for (aoi = *aois; aoi; aoi = aoi->next)
		if (aoi->click && in_aoi(aoi, x, y)) {
			aoi->click(aoi->user);
			return 1;
		}
	return 0;
}


void aoi_set_related(struct aoi *aoi, const struct aoi *related)
{
	assert(!aoi->related);
	aoi->related = related;
}


void aoi_remove(struct aoi **aois, const struct aoi *aoi)
{
	assert(aoi);
	if (hovering == aoi) {
		aoi->hover(aoi->user, 0, 0, 0);
		hovering = NULL;
	}
	while (*aois && *aois != aoi)
		aois = &(*aois)->next;
	assert(*aois);
	*aois = aoi->next;
	free((void *) aoi);
}


void aoi_dehover(void)
{
	if (hovering)
		hovering->hover(hovering->user, 0, 0, 0);
	hovering = NULL;
}