1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-12-23 13:38:37 +02:00

eeshow/gui-aoi.c: prepare API for future dragging support

This commit is contained in:
Werner Almesberger 2016-08-14 12:30:24 -03:00
parent c00f065993
commit 5ccd805b43
3 changed files with 92 additions and 20 deletions

View File

@ -18,12 +18,19 @@
*/ */
#include <stddef.h> #include <stddef.h>
#include <math.h>
#include <assert.h>
#include "util.h" #include "util.h"
#include "gui-aoi.h" #include "gui-aoi.h"
#define DRAG_RADIUS 5
static const struct aoi *hovering = NULL; static const struct aoi *hovering = NULL;
static const struct aoi *clicked = NULL;
static int clicked_x, clicked_y;
struct aoi *aoi_add(struct aoi **aois, const struct aoi *cfg) struct aoi *aoi_add(struct aoi **aois, const struct aoi *cfg)
@ -48,6 +55,18 @@ void aoi_update(struct aoi *aoi, const struct aoi *cfg)
} }
static const struct aoi *find_aoi(const struct aoi *aois, int x, int y)
{
const struct aoi *aoi;
for (aoi = aois; aoi; aoi = aoi->next)
if (x >= aoi->x && x < aoi->x + aoi->w &&
y >= aoi->y && y < aoi->y + aoi->h)
break;
return aoi;
}
bool aoi_hover(const struct aoi *aois, int x, int y) bool aoi_hover(const struct aoi *aois, int x, int y)
{ {
const struct aoi *aoi; const struct aoi *aoi;
@ -60,10 +79,7 @@ bool aoi_hover(const struct aoi *aois, int x, int y)
hovering = NULL; hovering = NULL;
} }
for (aoi = aois; aoi; aoi = aoi->next) aoi = find_aoi(aois, x, y);
if (x >= aoi->x && x < aoi->x + aoi->w &&
y >= aoi->y && y < aoi->y + aoi->h)
break;
if (aoi && aoi->hover && aoi->hover(aoi->user, 1)) { if (aoi && aoi->hover && aoi->hover(aoi->user, 1)) {
hovering = aoi; hovering = aoi;
return 1; return 1;
@ -72,23 +88,66 @@ bool aoi_hover(const struct aoi *aois, int x, int y)
} }
bool aoi_click(const struct aoi *aois, int x, int y) bool aoi_move(const struct aoi *aois, int x, int y)
{ {
const struct aoi *aoi; const struct aoi *aoi;
if (hovering) { if (!clicked)
hovering->hover(hovering->user, 0); return 0;
hovering = NULL;
} /*
* Ensure we're on the right list and are using the same coordinate
* system.
*/
for (aoi = aois; aoi; aoi = aoi->next) for (aoi = aois; aoi; aoi = aoi->next)
if (x >= aoi->x && x < aoi->x + aoi->w && if (aoi == clicked)
y >= aoi->y && y < aoi->y + aoi->h)
break; break;
if (aoi && aoi->click) { if (!aoi)
aoi->click(aoi->user); return 0;
if (hypot(x - clicked_x, y - clicked_y) > DRAG_RADIUS)
clicked = NULL;
return 1; return 1;
} }
bool aoi_down(const struct aoi *aois, int x, int y)
{
assert(!clicked);
aoi_dehover();
clicked = find_aoi(aois, x, y);
if (!clicked)
return 0; return 0;
clicked_x = x;
clicked_y = y;
return 1;
}
bool aoi_up(const struct aoi *aois, int x, int y)
{
const struct aoi *aoi;
if (!aoi_move(aois, x, y))
return 0;
/*
* Ensure we're on the right list and are using the same coordinate
* system.
*/
for (aoi = aois; aoi; aoi = aoi->next)
if (aoi == clicked)
break;
if (!aoi)
return 0;
clicked->click(clicked->user);
clicked = NULL;
return 1;
} }

View File

@ -31,7 +31,11 @@ struct aoi {
struct aoi *aoi_add(struct aoi **aois, const struct aoi *cfg); struct aoi *aoi_add(struct aoi **aois, const struct aoi *cfg);
void aoi_update(struct aoi *aoi, const struct aoi *cfg); void aoi_update(struct aoi *aoi, const struct aoi *cfg);
bool aoi_hover(const struct aoi *aois, int x, int y); bool aoi_hover(const struct aoi *aois, int x, int y);
bool aoi_click(const struct aoi *aois, int x, int y);
bool aoi_move(const struct aoi *aois, int x, int y);
bool aoi_down(const struct aoi *aois, int x, int y);
bool aoi_up(const struct aoi *aois, int x, int y);
void aoi_remove(struct aoi **aois, const struct aoi *aoi); void aoi_remove(struct aoi **aois, const struct aoi *aoi);
void aoi_dehover(void); void aoi_dehover(void);

View File

@ -771,6 +771,9 @@ static gboolean motion_notify_event(GtkWidget *widget, GdkEventMotion *event,
canvas_coord(ctx, event->x, event->y, &x, &y); canvas_coord(ctx, event->x, event->y, &x, &y);
aoi_move(ctx->aois, event->x, event->y) ||
aoi_move(curr_sheet->aois,
x + curr_sheet->xmin, y + curr_sheet->ymin) ||
aoi_hover(ctx->aois, event->x, event->y) || aoi_hover(ctx->aois, event->x, event->y) ||
aoi_hover(curr_sheet->aois, aoi_hover(curr_sheet->aois,
x + curr_sheet->xmin, y + curr_sheet->ymin); x + curr_sheet->xmin, y + curr_sheet->ymin);
@ -791,9 +794,9 @@ static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
switch (event->button) { switch (event->button) {
case 1: case 1:
if (aoi_click(ctx->aois, event->x, event->y)) if (aoi_down(ctx->aois, event->x, event->y))
break; break;
if (aoi_click(curr_sheet->aois, if (aoi_down(curr_sheet->aois,
x + curr_sheet->xmin, y + curr_sheet->ymin)) x + curr_sheet->xmin, y + curr_sheet->ymin))
break; break;
if (ctx->showing_history) if (ctx->showing_history)
@ -815,12 +818,18 @@ static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
gpointer data) gpointer data)
{ {
struct gui_ctx *ctx = data; struct gui_ctx *ctx = data;
const struct gui_sheet *curr_sheet = ctx->curr_sheet;
int x, y; int x, y;
canvas_coord(ctx, event->x, event->y, &x, &y); canvas_coord(ctx, event->x, event->y, &x, &y);
switch (event->button) { switch (event->button) {
case 1: case 1:
if (aoi_up(ctx->aois, event->x, event->y))
break;
if (aoi_up(curr_sheet->aois,
x + curr_sheet->xmin, y + curr_sheet->ymin))
break;
break; break;
case 2: case 2:
pan_end(ctx, event->x, event->y); pan_end(ctx, event->x, event->y);