]> git.webhop.me Git - lcd4linux.git/commitdiff
test intersection of (displayable) widgets
authorvolker <volker@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Mon, 23 Mar 2009 17:22:24 +0000 (17:22 +0000)
committervolker <volker@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Mon, 23 Mar 2009 17:22:24 +0000 (17:22 +0000)
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@996 3ae390bd-cb1e-0410-b409-cd5a39f66f1f

widget.c
widget.h
widget_bar.c
widget_gpo.c
widget_icon.c
widget_image.c
widget_keypad.c
widget_text.c
widget_timer.c

index bc3fe31b36a91a44e08e39861e8d895a35c96961..165fbc2d36d6165ebac0e75f1a13b080718e6e4d 100644 (file)
--- a/widget.c
+++ b/widget.c
@@ -22,7 +22,7 @@
  *
  */
 
-/* 
+/*
  * exported functions:
  *
  * int widget_junk(void)
@@ -128,6 +128,32 @@ int widget_color(const char *section, const char *name, const char *key, RGBA *
     return 1;
 }
 
+int intersect(WIDGET * w1, WIDGET * w2)
+{
+    int x1w1, y1w1, x2w1, y2w1;        /* 1st rectangle */
+    int x1w2, y1w2, x2w2, y2w2;        /* 2nd rectangle */
+
+    if (w1->x2 == NOCOORD || w1->y2 == NOCOORD || w2->x2 == NOCOORD || w2->y2 == NOCOORD) {
+       /* w1 or w2 is no display widget: no intersection */
+       return 0;
+    }
+    x1w1 = MIN(w1->col, w1->x2);
+    x2w1 = MAX(w1->col, w1->x2);
+    y1w1 = MIN(w1->row, w1->y2);
+    y2w1 = MAX(w1->row, w1->y2);
+    x1w2 = MIN(w2->col, w2->x2);
+    x2w2 = MAX(w2->col, w2->x2);
+    y1w2 = MIN(w2->row, w2->y2);
+    y2w2 = MAX(w2->row, w2->y2);
+
+    if (x1w2 < x2w1 && x2w2 > x1w1 && y1w2 < y2w1 && y2w2 > y1w1) {
+       /* true: Intersection */
+       return 1;
+    } else {
+       return 0;
+    }
+}
+
 int widget_add(const char *name, const int type, const int layer, const int row, const int col)
 {
     int i;
@@ -240,15 +266,26 @@ int widget_add(const char *name, const int type, const int layer, const int row,
     Widget->row = row;
     Widget->col = col;
 
-    info(" widget '%s': Class '%s', Parent '%s', Layer %d, %s %d, %s %d",
-        name, (NULL == Class) ? "<none>" : Class->name,
-        (NULL == Parent) ? "<root>" : Parent->name,
-        layer, (WIDGET_TYPE_XY == Class->type) ? "Y" : "Row", row, (WIDGET_TYPE_XY == Class->type) ? "X" : "Col", col);
-
     if (Class->init != NULL) {
        Class->init(Widget);
     }
 
+    info(" widget '%s': Class '%s', Parent '%s', Layer %d, %s %d, %s %d (to %d,%d)",
+        name, (NULL == Class) ? "<none>" : Class->name,
+        (NULL == Parent) ? "<root>" : Parent->name,
+        layer, (WIDGET_TYPE_XY == Class->type) ? "Y" : "Row", row, (WIDGET_TYPE_XY == Class->type) ? "X" : "Col", col,
+        Widget->y2, Widget->x2);
+
+    /* sanity check: look for overlapping widgets */
+    for (i = 0; i < nWidgets - 1; i++) {
+       if (Widgets[i].layer == layer) {
+           if (intersect(&(Widgets[i]), Widget)) {
+               info("WARNING widget %s(%i,%i) intersects with %s(%i,%i) on layer %d",
+                    Widgets[i].name, Widgets[i].row, Widgets[i].col, name, row, col, layer);
+           }
+       }
+    }
+
     return 0;
 }
 
index 4f943b298c73284dc5439d759a9eeb596c29a1d0..2fe70880943c0acb3bd456406386eac4ad43f981 100644 (file)
--- a/widget.h
+++ b/widget.h
@@ -56,6 +56,8 @@ typedef struct WIDGET {
     int row;
     int col;
     void *data;
+    int x2;                    /* x of opposite corner, -1 for no display widget */
+    int y2;                    /* y of opposite corner, -1 for no display widget */
 } WIDGET;
 
 
@@ -68,8 +70,15 @@ typedef struct WIDGET {
 
 int widget_register(WIDGET_CLASS * widget);
 void widget_unregister(void);
+int intersect(WIDGET * w1, WIDGET * w2);
 int widget_add(const char *name, const int type, const int layer, const int row, const int col);
 WIDGET *widget_find(int type, void *needle);
 int widget_color(const char *section, const char *name, const char *key, RGBA * C);
 
+#undef MIN
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+#undef MAX
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#define NOCOORD (-1)
+
 #endif
index 7e453aa0cca5076022c41e0bcd93f425d8367f89..755855027530e904a92febefdbd0f1daf0dc8bf2 100644 (file)
@@ -22,7 +22,7 @@
  *
  */
 
-/* 
+/*
  * exported functions:
  *
  * WIDGET_CLASS Widget_Bar
@@ -154,15 +154,23 @@ int widget_bar_init(WIDGET * Self)
     switch (toupper(*c)) {
     case 'E':
        Bar->direction = DIR_EAST;
+       Self->x2 = Self->col + Bar->length - 1;
+       Self->y2 = Self->row;
        break;
     case 'W':
        Bar->direction = DIR_WEST;
+       Self->x2 = Self->col + Bar->length - 1;
+       Self->y2 = Self->row;
        break;
     case 'N':
        Bar->direction = DIR_NORTH;
+       Self->x2 = Self->col;
+       Self->y2 = Self->row + Bar->length - 1;
        break;
     case 'S':
        Bar->direction = DIR_SOUTH;
+       Self->x2 = Self->col;
+       Self->y2 = Self->row + Bar->length - 1;
        break;
     default:
        error("widget %s has unknown direction '%s'; known directions: 'E', 'W', 'N', 'S'; using 'E(ast)'", Self->name,
index ebae5fa024870741686419dcb98708fb1248b589..1a0d52d42042865f08810615c397f47acd9df634 100644 (file)
@@ -22,7 +22,7 @@
  *
  */
 
-/* 
+/*
  * exported functions:
  *
  * WIDGET_CLASS Widget_GPO
@@ -96,6 +96,9 @@ int widget_gpo_init(WIDGET * Self)
 
     free(section);
     Self->data = GPO;
+    /* no display dimension */
+    Self->x2 = NOCOORD;
+    Self->y2 = NOCOORD;
 
     /* fire it the first time */
     widget_gpo_update(Self);
index 31299c9905deaedf5c488905523eb23281dd1825..83afbaced7d7f7b1004f1dc560b4e7d059d8d20e 100644 (file)
@@ -22,7 +22,7 @@
  *
  */
 
-/* 
+/*
  * exported functions:
  *
  * WIDGET_CLASS Widget_Icon
@@ -146,6 +146,8 @@ int widget_icon_init(WIDGET * Self)
 
        free(section);
        Self->data = Icon;
+       Self->x2 = Self->col + 1;
+       Self->y2 = Self->row + 1;
 
        /* as the speed is evaluatod on every call, we use 'one-shot'-timers.  */
        /* The timer will be reactivated on every call to widget_icon_update().  */
index 2a8f0adf1212fbfd645ef55acc89048180039df0..688f6c825c15a5f6cb0febb0c6061914db977d05 100644 (file)
@@ -22,7 +22,7 @@
  *
  */
 
-/* 
+/*
  * exported functions:
  *
  * WIDGET_CLASS Widget_Image
@@ -237,6 +237,8 @@ int widget_image_init(WIDGET * Self)
 
        free(section);
        Self->data = Image;
+       Self->x2 = Self->col + Image->width;
+       Self->y2 = Self->row + Image->height;
 
     } else {
 
index 8d571a4bc64e9f8670a34a7d8b250dc64f7b48d8..cf38d3406361d9e07a6e95633f64329f8f8b0ff0 100644 (file)
@@ -22,7 +22,7 @@
  *
  */
 
-/* 
+/*
  * exported functions:
  *
  * WIDGET_CLASS Widget_Keypad
@@ -103,6 +103,8 @@ int widget_keypad_init(WIDGET * Self)
 
     free(section);
     Self->data = keypad;
+    Self->x2 = NOCOORD;
+    Self->y2 = NOCOORD;
 
     return 0;
 }
index 948540ba176d1a6ac015aaf432b5ca06fdaa0e91..1678812b39bb1d20fccb7f77d6f8ecaf2de82855 100644 (file)
@@ -5,7 +5,7 @@
  *
  * Copyright (C) 2003, 2004 Michael Reinelt <michael@reinelt.co.at>
  * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
- * Copyright (C) 2008 Michael Vogt <michu@neophob.com> 
+ * Copyright (C) 2008 Michael Vogt <michu@neophob.com>
  *
  * 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
  *
  */
 
-/* 
+/*
  * exported functions:
  *
  * WIDGET_CLASS Widget_Text
- *   a simple text widget which 
+ *   a simple text widget which
  *   must be supported by all displays
  *
  */
@@ -352,6 +352,8 @@ int widget_text_init(WIDGET * Self)
 
     free(section);
     Self->data = Text;
+    Self->x2 = Self->col + Text->width;
+    Self->y2 = Self->row;
 
     /* add update timer, use one-shot if 'update' is zero */
     timer_add(widget_text_update, Self, Text->update, Text->update == 0);
index 948dee01812e80a8cabb3331ea134b2d152251de..10ba86d8d24e65157256b95765a6b7f9ffa97709 100644 (file)
@@ -22,7 +22,7 @@
  *
  */
 
-/* 
+/*
  * exported functions:
  *
  * WIDGET_CLASS Widget_Timer
@@ -98,6 +98,8 @@ int widget_timer_init(WIDGET * Self)
 
     free(section);
     Self->data = Timer;
+    Self->x2 = NOCOORD;
+    Self->y2 = NOCOORD;
 
     /* just do it! */
     widget_timer_update(Self);