rt300@0: // rt300@0: // UIElement.cpp rt300@0: // emptyExample rt300@0: // rt300@0: // Created by Robert Tubb on 22/05/2013. rt300@0: // rt300@0: // rt300@0: rt300@0: #include "UIElement.h" rt300@12: rt300@0: //---------------------------------------------------------------------- rt300@0: UIElement::UIElement(){ rt300@0: // rt300@0: //cout << " UIElement default constructur BAD !!!\n"; rt300@0: init(); rt300@0: } rt300@0: //---------------------------------------------------------------------- rt300@12: rt300@12: rt300@12: //---------------------------------------------------------------------- rt300@0: UIElement::UIElement(float ax, rt300@0: float ay, rt300@0: float awidth, rt300@0: float aheight, rt300@0: const UIProps& props) : rt300@0: x(ax), rt300@0: y(ay), rt300@0: width(awidth), rt300@0: height(aheight), rt300@0: background(props.generalBackground) rt300@0: { rt300@0: //cout << " UIElement constructur with defs \n"; rt300@0: init(); rt300@0: verdana16 = props.verdana16; rt300@0: bigFont = props.bigFont; rt300@0: rt300@0: } rt300@0: //---------------------------------------------------------------------- rt300@0: UIElement::UIElement(float ax, rt300@0: float ay, rt300@0: float awidth, rt300@0: float aheight, rt300@0: ofColor bg) : rt300@0: x(ax), rt300@0: y(ay), rt300@0: width(awidth), rt300@0: height(aheight), rt300@0: background(bg) rt300@0: { rt300@0: init(); rt300@0: rt300@0: } rt300@0: //---------------------------------------------------------------------- rt300@0: void UIElement::init(){ rt300@0: rt300@0: rt300@0: hidden = false; rt300@0: inactive = false; rt300@26: zLayer = 0; rt300@41: onlyOneTouchAllowed = false; rt300@0: } rt300@0: //---------------------------------------------------------------------- rt300@0: void UIElement::draw(){ rt300@0: if(hidden) return; rt300@0: //cout<<"element draw\n"; rt300@0: ofSetColor(background); rt300@0: ofRect(x,y,width,height); rt300@0: rt300@0: rt300@0: }; rt300@0: //---------------------------------------------------------------------- rt300@0: bool UIElement::touch(int tx, int ty, touchType ttype, int touchID){ rt300@0: if(isMyTouch(tx,ty,ttype,touchID)){ rt300@0: handleMyTouch(tx, ty, ttype,touchID); rt300@0: return true; rt300@0: }else{ rt300@0: return false; rt300@0: } rt300@0: } rt300@41: bool UIElement::atLeastOneTouchAlready(){ rt300@41: if (myTouchIDs.size() >= 1){ rt300@41: return true; rt300@41: }else{ rt300@41: return false; rt300@41: } rt300@41: } rt300@0: //---------------------------------------------------------------------- rt300@0: // called first from all subclasses rt300@0: bool UIElement::isMyTouch(int tx, int ty, touchType ttype, int touchID){ rt300@43: if(hidden || inactive){ rt300@43: // what if get made hidden or inactive when you're touching it ? rt300@43: myTouchIDs.clear(); // just release them all rt300@43: return false; rt300@43: } rt300@0: rt300@0: if(ttype == TOUCH_DOWN){ rt300@0: if (touchIsInMyArea(tx, ty)){ rt300@0: if (!isExistingTouchID(touchID)){ rt300@0: //cout << "Touchdown in area, grabbing focus " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl; rt300@41: if (onlyOneTouchAllowed && atLeastOneTouchAlready()){ rt300@41: cout << "ignoring xtra touch" << endl; rt300@42: return false; rt300@41: }else{ rt300@41: addTouchID(touchID); rt300@42: return true; rt300@41: } rt300@42: rt300@0: rt300@0: }else{ rt300@0: //shouldn't happen? rt300@0: return true; rt300@0: } rt300@0: rt300@0: }else{ rt300@0: //cout << "Touchdown outside area, ignoring " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl; rt300@0: return false; rt300@0: rt300@0: } rt300@0: } rt300@0: if(ttype == TOUCH_UP){ rt300@0: if (isExistingTouchID(touchID)){ rt300@0: //cout << "Touch Up for my ID, handling " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl; rt300@0: //myTouchID = -1; rt300@0: removeTouchID(touchID); rt300@0: return true; rt300@0: }else{ rt300@0: //cout << "Touch Up NOT my ID, ignoring " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl; rt300@0: return false; rt300@0: } rt300@0: } rt300@0: rt300@0: if(ttype == TOUCH_MOVED){ rt300@0: if(isExistingTouchID(touchID)){ rt300@0: //cout << "Touch moved for my ID, handling " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl; rt300@0: return true; rt300@0: }else{ rt300@0: //cout << "Touch moved NOT my ID, ignore " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl; rt300@0: return false; rt300@0: } rt300@0: } rt300@0: rt300@0: cout << "UNHANDLED SITUATION!" << labelName << endl; rt300@0: return false; rt300@0: rt300@0: } rt300@0: rt300@0: //---------------------------------------------------------------------- rt300@0: bool UIElement::touchIsInMyArea(int tx, int ty){ rt300@0: rt300@0: // work out relative coords rt300@0: double relx = tx - x; rt300@0: double rely = ty - y; rt300@0: return !(relx < 0 || relx > width || rely < 0 || rely > height); rt300@0: } rt300@0: //---------------------------------------------------------------------- rt300@0: bool UIElement::isCoordInMyRegion(double ax, double ay){ rt300@0: if(hidden) return false; rt300@0: rt300@0: if( (ax > x && ax < x+width) && (ay > y && ay < y+height)){ rt300@0: return true; rt300@0: }else{ rt300@0: return false; rt300@0: } rt300@0: rt300@0: } rt300@0: //---------------------------------------------------------------------- rt300@0: //---------------------------------------------------------------------- rt300@0: //---------------------------------------------------------------------- rt300@0: //---------------------------------------------------------------------- rt300@0: //----------------------------------------------------------------------