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@26
|
59 zLayer = 0;
|
rt300@0
|
60 }
|
rt300@0
|
61 //----------------------------------------------------------------------
|
rt300@0
|
62 void UIElement::draw(){
|
rt300@0
|
63 if(hidden) return;
|
rt300@0
|
64 //cout<<"element draw\n";
|
rt300@0
|
65 ofSetColor(background);
|
rt300@0
|
66 ofRect(x,y,width,height);
|
rt300@0
|
67
|
rt300@0
|
68
|
rt300@0
|
69 };
|
rt300@0
|
70 //----------------------------------------------------------------------
|
rt300@0
|
71 bool UIElement::touch(int tx, int ty, touchType ttype, int touchID){
|
rt300@0
|
72 if(isMyTouch(tx,ty,ttype,touchID)){
|
rt300@0
|
73 handleMyTouch(tx, ty, ttype,touchID);
|
rt300@0
|
74 return true;
|
rt300@0
|
75 }else{
|
rt300@0
|
76 return false;
|
rt300@0
|
77 }
|
rt300@0
|
78 }
|
rt300@0
|
79 //----------------------------------------------------------------------
|
rt300@0
|
80 // called first from all subclasses
|
rt300@0
|
81 bool UIElement::isMyTouch(int tx, int ty, touchType ttype, int touchID){
|
rt300@0
|
82 if(hidden || inactive) return false;
|
rt300@0
|
83
|
rt300@0
|
84 if(ttype == TOUCH_DOWN){
|
rt300@0
|
85 if (touchIsInMyArea(tx, ty)){
|
rt300@0
|
86 if (!isExistingTouchID(touchID)){
|
rt300@0
|
87 //cout << "Touchdown in area, grabbing focus " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
|
rt300@0
|
88 addTouchID(touchID);
|
rt300@0
|
89 return true;
|
rt300@0
|
90
|
rt300@0
|
91 }else{
|
rt300@0
|
92 //shouldn't happen?
|
rt300@0
|
93 return true;
|
rt300@0
|
94 }
|
rt300@0
|
95
|
rt300@0
|
96 }else{
|
rt300@0
|
97 //cout << "Touchdown outside area, ignoring " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
|
rt300@0
|
98 return false;
|
rt300@0
|
99
|
rt300@0
|
100 }
|
rt300@0
|
101 }
|
rt300@0
|
102 if(ttype == TOUCH_UP){
|
rt300@0
|
103 if (isExistingTouchID(touchID)){
|
rt300@0
|
104 //cout << "Touch Up for my ID, handling " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
|
rt300@0
|
105 //myTouchID = -1;
|
rt300@0
|
106 removeTouchID(touchID);
|
rt300@0
|
107 return true;
|
rt300@0
|
108 }else{
|
rt300@0
|
109 //cout << "Touch Up NOT my ID, ignoring " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
|
rt300@0
|
110 return false;
|
rt300@0
|
111 }
|
rt300@0
|
112 }
|
rt300@0
|
113
|
rt300@0
|
114 if(ttype == TOUCH_MOVED){
|
rt300@0
|
115 if(isExistingTouchID(touchID)){
|
rt300@0
|
116 //cout << "Touch moved for my ID, handling " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
|
rt300@0
|
117 return true;
|
rt300@0
|
118 }else{
|
rt300@0
|
119 //cout << "Touch moved NOT my ID, ignore " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
|
rt300@0
|
120 return false;
|
rt300@0
|
121 }
|
rt300@0
|
122 }
|
rt300@0
|
123
|
rt300@0
|
124 cout << "UNHANDLED SITUATION!" << labelName << endl;
|
rt300@0
|
125 return false;
|
rt300@0
|
126
|
rt300@0
|
127 }
|
rt300@0
|
128
|
rt300@0
|
129 //----------------------------------------------------------------------
|
rt300@0
|
130 bool UIElement::touchIsInMyArea(int tx, int ty){
|
rt300@0
|
131
|
rt300@0
|
132 // work out relative coords
|
rt300@0
|
133 double relx = tx - x;
|
rt300@0
|
134 double rely = ty - y;
|
rt300@0
|
135 return !(relx < 0 || relx > width || rely < 0 || rely > height);
|
rt300@0
|
136 }
|
rt300@0
|
137 //----------------------------------------------------------------------
|
rt300@0
|
138 bool UIElement::isCoordInMyRegion(double ax, double ay){
|
rt300@0
|
139 if(hidden) return false;
|
rt300@0
|
140
|
rt300@0
|
141 if( (ax > x && ax < x+width) && (ay > y && ay < y+height)){
|
rt300@0
|
142 return true;
|
rt300@0
|
143 }else{
|
rt300@0
|
144 return false;
|
rt300@0
|
145 }
|
rt300@0
|
146
|
rt300@0
|
147 }
|
rt300@0
|
148 //----------------------------------------------------------------------
|
rt300@0
|
149 //----------------------------------------------------------------------
|
rt300@0
|
150 //----------------------------------------------------------------------
|
rt300@0
|
151 //----------------------------------------------------------------------
|
rt300@0
|
152 //---------------------------------------------------------------------- |