2016-08-04 13:19:18 +03:00
|
|
|
/*
|
2016-08-18 02:54:25 +03:00
|
|
|
* gui/aoi.c - GUI: areas of interest
|
2016-08-04 13:19:18 +03:00
|
|
|
*
|
|
|
|
* 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>
|
2016-08-14 18:30:24 +03:00
|
|
|
#include <math.h>
|
|
|
|
#include <assert.h>
|
2016-08-04 13:19:18 +03:00
|
|
|
|
2016-08-18 03:37:15 +03:00
|
|
|
#include "misc/util.h"
|
2016-08-18 02:54:25 +03:00
|
|
|
#include "gui/aoi.h"
|
2016-08-04 13:19:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
static const struct aoi *hovering = NULL;
|
|
|
|
|
|
|
|
|
2016-08-07 11:09:34 +03:00
|
|
|
struct aoi *aoi_add(struct aoi **aois, const struct aoi *cfg)
|
2016-08-04 13:19:18 +03:00
|
|
|
{
|
|
|
|
struct aoi *new;
|
|
|
|
|
|
|
|
new = alloc_type(struct aoi);
|
2016-08-07 11:09:34 +03:00
|
|
|
*new = *cfg;
|
2016-08-04 13:19:18 +03:00
|
|
|
new->next = *aois;
|
|
|
|
*aois = new;
|
2016-08-04 13:53:25 +03:00
|
|
|
|
|
|
|
return new;
|
2016-08-04 13:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-07 11:09:34 +03:00
|
|
|
void aoi_update(struct aoi *aoi, const struct aoi *cfg)
|
|
|
|
{
|
|
|
|
struct aoi *next = aoi->next;
|
|
|
|
|
|
|
|
*aoi = *cfg;
|
|
|
|
aoi->next = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-19 09:42:01 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-21 02:39:08 +03:00
|
|
|
/*
|
|
|
|
* 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)
|
2016-08-04 13:19:18 +03:00
|
|
|
{
|
|
|
|
const struct aoi *aoi;
|
|
|
|
|
|
|
|
if (hovering) {
|
2016-08-19 11:30:58 +03:00
|
|
|
if (in_aoi(hovering, x, y))
|
2016-08-04 13:19:18 +03:00
|
|
|
return 1;
|
|
|
|
hovering->hover(hovering->user, 0);
|
|
|
|
hovering = NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-21 02:39:08 +03:00
|
|
|
for (aoi = *aois; aoi; aoi = aoi->next)
|
2016-08-19 11:35:37 +03:00
|
|
|
if (aoi->hover && in_aoi(aoi, x, y) &&
|
|
|
|
aoi->hover(aoi->user, 1)) {
|
|
|
|
hovering = aoi;
|
|
|
|
return 1;
|
|
|
|
}
|
2016-08-04 13:19:18 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-19 09:42:01 +03:00
|
|
|
static bool need_dehover(const struct aoi *aois, int x, int y)
|
2016-08-19 09:25:56 +03:00
|
|
|
{
|
|
|
|
const struct aoi *aoi;
|
|
|
|
|
|
|
|
if (!hovering)
|
|
|
|
return 0;
|
|
|
|
if (hovering->click)
|
|
|
|
return 0;
|
|
|
|
for (aoi = aois; aoi; aoi = aoi->next)
|
2016-08-19 09:42:01 +03:00
|
|
|
if (aoi->related == hovering && aoi->click && in_aoi(aoi, x, y))
|
2016-08-19 09:25:56 +03:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-21 02:42:34 +03:00
|
|
|
/* Pointer to the anchor needed for the same reason as in aoi_hover. */
|
|
|
|
|
|
|
|
bool aoi_click(struct aoi *const *aois, int x, int y)
|
2016-08-14 18:30:24 +03:00
|
|
|
{
|
|
|
|
const struct aoi *aoi;
|
|
|
|
|
2016-08-21 02:42:34 +03:00
|
|
|
if (need_dehover(*aois, x, y))
|
2016-08-19 09:25:56 +03:00
|
|
|
aoi_dehover();
|
2016-08-14 18:30:24 +03:00
|
|
|
|
2016-08-21 02:42:34 +03:00
|
|
|
for (aoi = *aois; aoi; aoi = aoi->next)
|
2016-08-19 11:35:37 +03:00
|
|
|
if (aoi->click && in_aoi(aoi, x, y)) {
|
|
|
|
aoi->click(aoi->user);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2016-08-04 13:19:18 +03:00
|
|
|
}
|
2016-08-04 13:53:25 +03:00
|
|
|
|
|
|
|
|
2016-08-19 09:25:56 +03:00
|
|
|
void aoi_set_related(struct aoi *aoi, const struct aoi *related)
|
|
|
|
{
|
|
|
|
assert(!aoi->related);
|
|
|
|
aoi->related = related;
|
|
|
|
}
|
|
|
|
|
2016-08-19 11:30:58 +03:00
|
|
|
|
2016-08-04 13:53:25 +03:00
|
|
|
void aoi_remove(struct aoi **aois, const struct aoi *aoi)
|
|
|
|
{
|
2016-08-21 02:39:08 +03:00
|
|
|
assert(aoi);
|
2016-08-07 11:09:34 +03:00
|
|
|
if (hovering == aoi) {
|
2016-08-04 13:53:25 +03:00
|
|
|
aoi->hover(aoi->user, 0);
|
2016-08-07 11:09:34 +03:00
|
|
|
hovering = NULL;
|
|
|
|
}
|
2016-08-21 02:39:08 +03:00
|
|
|
while (*aois && *aois != aoi)
|
2016-08-04 13:53:25 +03:00
|
|
|
aois = &(*aois)->next;
|
2016-08-21 02:39:08 +03:00
|
|
|
assert(*aois);
|
2016-08-04 13:53:25 +03:00
|
|
|
*aois = aoi->next;
|
|
|
|
free((void *) aoi);
|
|
|
|
}
|
2016-08-11 03:50:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
void aoi_dehover(void)
|
|
|
|
{
|
|
|
|
if (hovering)
|
|
|
|
hovering->hover(hovering->user, 0);
|
|
|
|
hovering = NULL;
|
|
|
|
}
|