annotate UI code/UIElement.mm @ 0:a223551fdc1f

First commit - copy from tweakathlon.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 10 Oct 2014 11:46:42 +0100
parents
children af71bf84660f
rev   line source
rt300@0 1 //
rt300@0 2 // UIElement.cpp
rt300@0 3 // emptyExample
rt300@0 4 //
rt300@0 5 // Created by Robert Tubb on 22/05/2013.
rt300@0 6 //
rt300@0 7 //
rt300@0 8
rt300@0 9 #include "UIElement.h"
rt300@0 10 //----------------------------------------------------------------------
rt300@0 11 UIElement::UIElement(){
rt300@0 12 //
rt300@0 13 //cout << " UIElement default constructur BAD !!!\n";
rt300@0 14 init();
rt300@0 15 }
rt300@0 16 //----------------------------------------------------------------------
rt300@0 17 UIElement::UIElement(float ax,
rt300@0 18 float ay,
rt300@0 19 float awidth,
rt300@0 20 float aheight,
rt300@0 21 const UIProps& props) :
rt300@0 22 x(ax),
rt300@0 23 y(ay),
rt300@0 24 width(awidth),
rt300@0 25 height(aheight),
rt300@0 26 background(props.generalBackground)
rt300@0 27 {
rt300@0 28 //cout << " UIElement constructur with defs \n";
rt300@0 29 init();
rt300@0 30 verdana16 = props.verdana16;
rt300@0 31 bigFont = props.bigFont;
rt300@0 32
rt300@0 33 }
rt300@0 34 //----------------------------------------------------------------------
rt300@0 35 UIElement::UIElement(float ax,
rt300@0 36 float ay,
rt300@0 37 float awidth,
rt300@0 38 float aheight,
rt300@0 39 ofColor bg) :
rt300@0 40 x(ax),
rt300@0 41 y(ay),
rt300@0 42 width(awidth),
rt300@0 43 height(aheight),
rt300@0 44 background(bg)
rt300@0 45 {
rt300@0 46 init();
rt300@0 47
rt300@0 48 }
rt300@0 49 //----------------------------------------------------------------------
rt300@0 50 void UIElement::init(){
rt300@0 51
rt300@0 52
rt300@0 53 hidden = false;
rt300@0 54 inactive = false;
rt300@0 55 }
rt300@0 56 //----------------------------------------------------------------------
rt300@0 57 void UIElement::draw(){
rt300@0 58 if(hidden) return;
rt300@0 59 //cout<<"element draw\n";
rt300@0 60 ofSetColor(background);
rt300@0 61 ofRect(x,y,width,height);
rt300@0 62
rt300@0 63
rt300@0 64 };
rt300@0 65 //----------------------------------------------------------------------
rt300@0 66 bool UIElement::touch(int tx, int ty, touchType ttype, int touchID){
rt300@0 67 if(isMyTouch(tx,ty,ttype,touchID)){
rt300@0 68 handleMyTouch(tx, ty, ttype,touchID);
rt300@0 69 return true;
rt300@0 70 }else{
rt300@0 71 return false;
rt300@0 72 }
rt300@0 73 }
rt300@0 74 //----------------------------------------------------------------------
rt300@0 75 // called first from all subclasses
rt300@0 76 bool UIElement::isMyTouch(int tx, int ty, touchType ttype, int touchID){
rt300@0 77 if(hidden || inactive) return false;
rt300@0 78
rt300@0 79 if(ttype == TOUCH_DOWN){
rt300@0 80 if (touchIsInMyArea(tx, ty)){
rt300@0 81 if (!isExistingTouchID(touchID)){
rt300@0 82 //cout << "Touchdown in area, grabbing focus " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
rt300@0 83 addTouchID(touchID);
rt300@0 84 return true;
rt300@0 85
rt300@0 86 }else{
rt300@0 87 //shouldn't happen?
rt300@0 88 return true;
rt300@0 89 }
rt300@0 90
rt300@0 91 }else{
rt300@0 92 //cout << "Touchdown outside area, ignoring " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
rt300@0 93 return false;
rt300@0 94
rt300@0 95 }
rt300@0 96 }
rt300@0 97 if(ttype == TOUCH_UP){
rt300@0 98 if (isExistingTouchID(touchID)){
rt300@0 99 //cout << "Touch Up for my ID, handling " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
rt300@0 100 //myTouchID = -1;
rt300@0 101 removeTouchID(touchID);
rt300@0 102 return true;
rt300@0 103 }else{
rt300@0 104 //cout << "Touch Up NOT my ID, ignoring " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
rt300@0 105 return false;
rt300@0 106 }
rt300@0 107 }
rt300@0 108
rt300@0 109 if(ttype == TOUCH_MOVED){
rt300@0 110 if(isExistingTouchID(touchID)){
rt300@0 111 //cout << "Touch moved for my ID, handling " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
rt300@0 112 return true;
rt300@0 113 }else{
rt300@0 114 //cout << "Touch moved NOT my ID, ignore " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
rt300@0 115 return false;
rt300@0 116 }
rt300@0 117 }
rt300@0 118
rt300@0 119 cout << "UNHANDLED SITUATION!" << labelName << endl;
rt300@0 120 return false;
rt300@0 121
rt300@0 122 }
rt300@0 123
rt300@0 124 //----------------------------------------------------------------------
rt300@0 125 bool UIElement::touchIsInMyArea(int tx, int ty){
rt300@0 126
rt300@0 127 // work out relative coords
rt300@0 128 double relx = tx - x;
rt300@0 129 double rely = ty - y;
rt300@0 130 return !(relx < 0 || relx > width || rely < 0 || rely > height);
rt300@0 131 }
rt300@0 132 //----------------------------------------------------------------------
rt300@0 133 bool UIElement::isCoordInMyRegion(double ax, double ay){
rt300@0 134 if(hidden) return false;
rt300@0 135
rt300@0 136 if( (ax > x && ax < x+width) && (ay > y && ay < y+height)){
rt300@0 137 return true;
rt300@0 138 }else{
rt300@0 139 return false;
rt300@0 140 }
rt300@0 141
rt300@0 142 }
rt300@0 143 //----------------------------------------------------------------------
rt300@0 144 //----------------------------------------------------------------------
rt300@0 145 //----------------------------------------------------------------------
rt300@0 146 //----------------------------------------------------------------------
rt300@0 147 //----------------------------------------------------------------------