annotate UI code/UIElement.mm @ 12:af71bf84660f

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