el h / SimpleGUI

Fork of SimpleGUI by Duncan McIntyre

Revision:
8:a460cabc85ac
Parent:
7:303850a4b30c
Child:
12:63db16fea709
--- a/Widgets/Widget.h	Sun Apr 10 16:48:44 2016 +0000
+++ b/Widgets/Widget.h	Mon Apr 11 16:54:02 2016 +0000
@@ -1,8 +1,7 @@
 #ifndef SIMPLEGUI_WIDGET_H
 #define SIMPLEGUI_WIDGET_H
 
-#include "EventListener.h"
-#include "GraphicsDisplay.h"
+#include "GUI.h"
 
 /**
 * A basic widget draws itself in a rectangular area
@@ -12,10 +11,10 @@
     
     public:
     
-        Widget(GraphicsDisplay& display) : _display(display), _fg(White), _bg(Black), _x(0), _y(0), _width(0), _height(0) {}
+        Widget(GUI* gui) : _gui(gui), _fg(White), _bg(Black), _x(0), _y(0), _width(0), _height(0), _hidden(false) {}
      
         virtual bool isEventTarget(Event e) {
-            return e.screenX >= _x && e.screenX <= (_x+_width) && e.screenY >= _y && e.screenY <= (_y+_height);
+            return !_hidden && e.screenX >= _x && e.screenX <= (_x+_width) && e.screenY >= _y && e.screenY <= (_y+_height);
         }
        
         virtual void setLocation(int x, int y) {
@@ -52,19 +51,53 @@
             _bg = color;
         }
         
-        virtual void draw() = 0;
+        virtual void draw() {
+            if(!_hidden) _draw();
+        }
+        
+        virtual void clear() {
+            if(!_hidden) _clear();
+        }
         
-        void setHidden(bool hidden) {
-            _hidden = hidden;
+        void show() {
+            _hidden = false;
+            draw();
+        }
+        
+        void hide() {
+            clear();
+            _hidden = true;
         }
         
         bool isHidden() {
             return _hidden;
         }
+
+        void setEventHandler(uint8_t type, EventHandler handler) {
+            EventListener::setEventHandler(type, handler);
+            _gui->eventDispatcher()->detachListener(this);
+            _gui->eventDispatcher()->attachListener(this);
+        }
         
+        int unsetEventHandler(uint8_t type) {
+            
+            int remaining = EventListener::unsetEventHandler(type);
+            
+            if(remaining == 0) {
+                _gui->eventDispatcher()->detachListener(this);
+            }
+            
+            return remaining;
+        }
+
+       
     protected:
+
+        virtual void _draw() = 0;
+        virtual void _clear() = 0;
+        
     
-        GraphicsDisplay& _display;
+        GUI* _gui;
         uint16_t _fg, _bg;
         int _x,_y,_width,_height;
         bool _hidden;