view UI code/UIElement.mm @ 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 8d7ae43b2edd
children ba426cc4e6e1
line wrap: on
line source
//
//  UIElement.cpp
//  emptyExample
//
//  Created by Robert Tubb on 22/05/2013.
//
//

#include "UIElement.h"

//----------------------------------------------------------------------
UIElement::UIElement(){
    //
    //cout << " UIElement default constructur BAD !!!\n";
    init();
}
//----------------------------------------------------------------------


//----------------------------------------------------------------------
UIElement::UIElement(float ax,
                     float ay,
                     float awidth,
                     float aheight,
                     const UIProps& props) :
x(ax),
y(ay),
width(awidth),
height(aheight),
background(props.generalBackground)
{
    //cout << " UIElement constructur with defs \n";
    init();
    verdana16 = props.verdana16;
    bigFont = props.bigFont;
    
}
//----------------------------------------------------------------------
UIElement::UIElement(float ax,
                     float ay,
                     float awidth,
                     float aheight,
                     ofColor bg) :
x(ax),
y(ay),
width(awidth),
height(aheight),
background(bg)
{
    init();

}
//----------------------------------------------------------------------
void UIElement::init(){

    
    hidden = false;
    inactive = false;
    zLayer = 0;
}
//----------------------------------------------------------------------
void UIElement::draw(){
    if(hidden) return;
    //cout<<"element draw\n";
    ofSetColor(background);
    ofRect(x,y,width,height);
    
    
};
//----------------------------------------------------------------------
bool UIElement::touch(int tx, int ty, touchType ttype, int touchID){
    if(isMyTouch(tx,ty,ttype,touchID)){
        handleMyTouch(tx, ty, ttype,touchID);
        return true;
    }else{
        return false;
    }
}
//----------------------------------------------------------------------
// called first from all subclasses
bool UIElement::isMyTouch(int tx, int ty, touchType ttype, int touchID){
    if(hidden || inactive) return false;

    if(ttype == TOUCH_DOWN){
        if (touchIsInMyArea(tx, ty)){
            if (!isExistingTouchID(touchID)){
                //cout << "Touchdown in area, grabbing focus " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
                addTouchID(touchID);
                return true;

            }else{
                //shouldn't happen?
                return true;
            }

        }else{
            //cout << "Touchdown outside area, ignoring " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
            return false;

        }
    }
    if(ttype == TOUCH_UP){
        if (isExistingTouchID(touchID)){
            //cout << "Touch Up for my ID, handling " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
            //myTouchID = -1;
            removeTouchID(touchID);
            return true;
        }else{
            //cout << "Touch Up NOT my  ID, ignoring " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
            return false;
        }
    }
    
    if(ttype == TOUCH_MOVED){
        if(isExistingTouchID(touchID)){
            //cout << "Touch moved for my ID, handling " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
            return true;
        }else{
            //cout << "Touch moved NOT my ID, ignore " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
            return false;
        }
    }
    
    cout << "UNHANDLED SITUATION!" << labelName << endl;
    return false;

}

//----------------------------------------------------------------------
bool UIElement::touchIsInMyArea(int tx, int ty){
    
    // work out relative coords
    double relx = tx - x;
    double rely = ty - y;
    return !(relx < 0 || relx > width || rely < 0 || rely > height);
}
//----------------------------------------------------------------------
bool UIElement::isCoordInMyRegion(double ax, double ay){
    if(hidden) return false;
    
    if( (ax > x && ax < x+width) && (ay > y && ay < y+height)){
        return true;
    }else{
        return false;
    }
  
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------