view UI code/UIElement.h @ 28:953db6518738

leap version more or less there, needs btter results feedback but thats detail. "no movement" bit is stupid cos peopel can move their hand. light flash not work.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Thu, 30 Oct 2014 18:35:00 +0000
parents 27cdf475aa4b
children fea11c3d1d94
line wrap: on
line source
//
//  UIElement.h
//  emptyExample
//
//  Created by Robert Tubb on 22/05/2013.
//
// literally just an area of the screen that is a ui element

#ifndef __emptyExample__UIElement__
#define __emptyExample__UIElement__
#include "ofMain.h"
#include "globalVariables.h"
#include <iostream>
#include <string>
#include "boost/function.hpp"
#include "UIProperties.h"

typedef boost::function<void(int, int)> UICallbackFunction;

class MessageOrganiser;

class UIElement{
public:

    
    UICallbackFunction callback;
    
    UIElement();
    virtual ~UIElement(){};
    
    UIElement(float ax,
              float ay,
              float awidth,
              float aheight);
    
    // recommended
    UIElement(float ax,
              float ay,
              float awidth,
              float aheight,
              const UIProps& props);
              
    UIElement(float ax,
              float ay,
              float awidth,
              float aheight,
              ofColor bg);

    virtual void setLabel(string label){
        labelName = label;
    }
    virtual void draw();
    void hide(){
        hidden = true;
        on = false;
        
    };
    void show(){
        hidden = false;
        on = true;
    
    };
    bool isShowing(){
        return !hidden;
    }
    bool touch(int x, int y, touchType ttype, int touchID);
    
    virtual void addHandler(UICallbackFunction handlerFunction, int paramID) // virtual?
    {
        //cout << "handler added to UIElement " << endl;
        callback = handlerFunction;
        myParamID = paramID;
    };
    
    virtual void setValue(float value){
        cout << "not valid to set value on this?" << endl;
    };
    virtual void setHintValue(float value){
        cout << "not valid to set value on this?" << endl;
    };
    virtual void showHint(bool value){
        cout << "not valid to set value on this?" << endl;
    };
    virtual void setHintColor(ofColor c){
        cout << "not valid to set value on this?" << endl;
    };
    controllerType getType() const {
        return myType;
    }
    float getWidth() const {return width;};
    virtual void setHighlight(bool hion){
        // ?
        on = hion;
    };
    void setActive(bool isActive){
        inactive = !isActive; // thats logic right?
    }
    void setX(float ax){
        x = ax;
    };
    void setY(float ay){
        y = ay;
    };
    void setColor(ofColor c){
        background = c;
    }
    int getZLayer(){
        return zLayer;
        
    }
    void setZLayer(int z){
        zLayer = z;
    }
    void bringToFrontOf(UIElement* e){
        
        setZLayer(e->getZLayer() + 1);
    }
    
    virtual void update(){
        
    }
protected:

    void init();
    
    bool isExistingTouchID(int touchID){
        // is id in list
        std::list<int>::iterator findIter = std::find(myTouchIDs.begin(), myTouchIDs.end(), touchID);
        if (findIter == myTouchIDs.end()){
            return false;
        }else{
            return true;
        }
    };

    void addTouchID(int touchID){
        //list<int>::iterator findIter = std::find(myTouchIDs.begin(), myTouchIDs.end(), touchID);
        //if(findIter == myTouchIDs.end()){ // checks for duplicates
            myTouchIDs.insert(myTouchIDs.begin(), touchID);
        //}
    };
    void removeTouchID(int touchID){
        list<int>::iterator findIter = std::find(myTouchIDs.begin(), myTouchIDs.end(), touchID);
        if(findIter != myTouchIDs.end()){
            myTouchIDs.erase(findIter);
        }
    };
    static float sumWidth(float total, UIElement* element){
        return total + element->getWidth();
    }
    bool isMyTouch(int x, int y, touchType ttype, int touchID);
    bool touchIsInMyArea(int tx, int ty);
    virtual bool handleMyTouch(int x, int y, touchType ttype, int touchID) = 0; // subclass handles it

    bool isCoordInMyRegion(double x, double y); // not used
    
    int zLayer;
    
// protected members:
    
    float x;
    float y;
    float width;
    float height;
    bool on;
    int myParamID;
    list<int> myTouchIDs;
    
    ofColor background;
    
    string labelName;
    ofTrueTypeFont verdana16;
    ofTrueTypeFont bigFont;
    ofTrueTypeFont smallFont;
    controllerType myType;
    
    bool hidden; // don't draw dont touch
    bool inactive;  // dont touch, draw dimmed

    
};
#endif /* defined(__emptyExample__UIElement__) */