Foundation classes for a basic GUI implementing simple widgets and events

Dependents:   TouchScreenGUIDemo

Widgets/Widget.h

Committer:
duncanFrance
Date:
2016-04-11
Revision:
8:a460cabc85ac
Parent:
7:303850a4b30c
Child:
12:63db16fea709

File content as of revision 8:a460cabc85ac:

#ifndef SIMPLEGUI_WIDGET_H
#define SIMPLEGUI_WIDGET_H

#include "GUI.h"

/**
* A basic widget draws itself in a rectangular area
**/

class Widget : public EventListener {
    
    public:
    
        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 !_hidden && e.screenX >= _x && e.screenX <= (_x+_width) && e.screenY >= _y && e.screenY <= (_y+_height);
        }
       
        virtual void setLocation(int x, int y) {
            _x = x;
            _y = y;
        }
        
        virtual void setSize(int width, int height) {
            _width = width;
            _height = height;
        }
        
        virtual int x() {
            return _x;
        }
        
        virtual int y() {
            return _y;
        }
        
        virtual int height() {
            return _height;
        }
        
        virtual int width() {
            return _width;
        }
        
        virtual void setForeground(uint16_t color) {
            _fg = color;
        }
        
        virtual void setBackground(uint16_t color) {
            _bg = color;
        }
        
        virtual void draw() {
            if(!_hidden) _draw();
        }
        
        virtual void clear() {
            if(!_hidden) _clear();
        }
        
        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;
        
    
        GUI* _gui;
        uint16_t _fg, _bg;
        int _x,_y,_width,_height;
        bool _hidden;
        
};

#endif