-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPJPTchObject.cpp
418 lines (366 loc) · 10.7 KB
/
PJPTchObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*!
\file PJPTchObject.cpp
\brief description of generic touch object for GUI on a touchscreen
*/
// Library header
#include "PJPTchObject.h"
#include "math.h"
// Code
namespace PJPTch{
/**
* @brief between checks if a val is the same or in between two other values
*
* @param uint16_t val: the value that is checked
* @param uint16_t first: the first value between which val should be
* @param uint16_t last: the last value between which val should be
* @return bool: true if val is the same or in between first and last, otherwise false
*/
bool between(int16_t val, int16_t first, int16_t last)
{
if(val >= first and val <= last)
return true;
else if(val <= first and val >= last)
return true;
else
return false;
}
//TVector ============================
//constructors
TVector::TVector():dx(1),dy(1){}
TVector::TVector(int16_t ds):dx(ds),dy(ds){}
TVector::TVector(int16_t vdx, int16_t vdy):dx(vdx),dy(vdy){}
TVector::TVector(TSize s):dx(s.width),dy(s.height){}
/**
* @brief operator*
*
* @param nothing
* @return resized version
*/
TVector TVector::operator*(float f) const
{
return TVector(f*dx, f*dy);
}
//TSize ============================
//constructors
TSize::TSize():width(),height(){}
TSize::TSize(uint16_t s):width(s),height(s){}
TSize::TSize(uint16_t w, uint16_t h):width(w),height(h){}
/**
* @brief operator*
*
* @param nothing
* @return resized version
*/
TSize TSize::operator*(float f) const
{
return TSize(f*width, f*height);
}
//TPoint ============================
//constructors
TPoint::TPoint() : TS_Point(){}
TPoint::TPoint(TS_Point p): TS_Point(p){}
TPoint::TPoint(int16_t cx, int16_t cy):TS_Point(cx,cy,1){}
//operators
TPoint TPoint::operator+(const TVector& v) const
{
return TPoint(x+v.dx,y+v.dy);
}
TPoint TPoint::operator+(const TSize& s) const
{
return TPoint(x+s.width,y+s.height);
}
TPoint TPoint::operator-(const TVector& v)const
{
return TPoint(x-v.dx,y-v.dy);
}
TVector TPoint::operator-(const TPoint& p)const
{
return TVector(x-p.x,y-p.y);
}
void TPoint::operator=(const TS_Point& p)
{
x=p.x;
y=p.y;
z=p.z;
}
//functions
TSize TPoint::Distance(const TPoint& p) const
{
return TSize(abs(x-p.x),abs(y-p.y));
}
//TBoundary ============================
TBoundary::TBoundary()
{}
TBoundary::TBoundary(const TBoundary& val):UL(val.UL),LR(val.LR)
{}
TBoundary::TBoundary(const TPoint& ul, const TPoint& lr):UL(ul),LR(lr)
{}
TBoundary::TBoundary(const TPoint& loc, const TSize& s): UL(loc.operator+(TVector(s).operator*(-0.5))),
LR(loc.operator+(TVector(s).operator*(0.5)))
{
}
TBoundary::TBoundary(const TSize& s,const TPoint& ul): UL(ul),LR(ul.operator+(s))
{
}
TBoundary& TBoundary::operator=(const TBoundary& other)
{
UL.operator=(other.UL);
LR.operator=(other.LR);
return *this;
}
bool TBoundary::Inside(const TPoint& p )const
{
if (between(p.x,UL.x,LR.x) and between(p.y,UL.y,LR.y))
return true;
else
return false;
}
uint16_t TBoundary::Width()const{return LR.x-UL.x;}
uint16_t TBoundary::Height()const{return LR.y-UL.y;}
//TchObject ============================
//constructors
TchObject::TchObject(Adafruit_ILI9341& tft, const TS_Point& loc, const TSize& s,const String oname):boundary_(loc,s),name_(oname),tft_(tft)
{
}
TchObject::TchObject(Adafruit_ILI9341& tft, const TSize& s, const TS_Point& ul,const String oname):boundary_(s,ul),name_(oname),tft_(tft)
{
}
TchObject::TchObject(const TchObject& obj):boundary_(obj.boundary_),value_(obj.value_),name_(obj.name_),
wasBeingTouched_(obj.wasBeingTouched_),active_(obj.active_),parent_(obj.parent_),tft_(obj.tft_),on_(obj.on_)
{}
TchObject& TchObject::operator=(const TchObject& obj)
{
boundary_=obj.boundary_;
value_=obj.value_;
name_=obj.name_;
wasBeingTouched_=obj.wasBeingTouched_;
active_=obj.active_;
parent_=obj.parent_;
//tft_=obj.tft_;
on_=obj.on_;
return *this;
}
//get & set functions=====================================
/**
* @brief get boundary of the object
*
* @param nothing
* @return boundary of the object
*/
TBoundary TchObject::Boundary() const
{
return boundary_;
}
/**
* @brief set boundary of the object
*
* @param val is a TBoundary, de boundary of the object
* @return nothing
*/
void TchObject::Boundary(const TBoundary& val)
{
boundary_.operator=(val);
}
/**
* @brief set boundary of the object
*
* @param UL is a TPoint, the upper left corner of the object
* @param LR is a TPoint, the lower right corner of the object
* @return nothing
*/
void TchObject::Boundary(const TPoint& ul, const TPoint& lr)
{
boundary_.UL=ul;
boundary_.LR=lr;
}
/**
* @brief set boundary of the object based on given location and size
*
* @param location is a TPoint, the centre of the TchObject
* @param tsize is a TSize, the size of the TchObject
* @return nothing
*/
void TchObject::Boundary(TPoint location,const TSize& tsize)
{
boundary_.UL=location.operator+(TVector(tsize).operator*(-0.5));
boundary_.LR=location.operator+(TVector(tsize).operator*(0.5));
}
/**
* @brief set boundary of the object based on size and location UL
*
* @param tsize is a TSize, the size of the TchObject
* @param TPoint ul is the upper left corner of the TchObject
* @return nothing
*/
void TchObject::Boundary(const TSize& tsize,TPoint ul)
{
boundary_.UL=ul;
boundary_.LR=ul.operator+(tsize);
}
/**
* @brief returns if the tchObject was already being touched
*
* @param nothing
* @return boolean: true=object was being touched allready, false=object wasn't touched yet
*/
boolean TchObject::WasBeingTouched()const
{
return wasBeingTouched_;
}
const String TchObject::Name() const
{
return name_;
}
void TchObject::Name(const String oname)
{
name_=oname;
}
const Adafruit_ILI9341& TchObject::TFT()const
{
return tft_;
}
boolean TchObject::On()const {return on_;}
void TchObject::On(boolean on){on_=on;}
int16_t TchObject::Value() const {return value_;}
int16_t TchObject::Value(int16_t val)
{
value_=val;
return value_;
}
//functions
/**
* @brief get upper left corner of boundary
*
* @param nothing
* @return const TPoint&, upper left corner of boundary
*/
TPoint TchObject::UL() const
{
return boundary_.UL;
}
/**
* @brief set upper left corner of boundary, without changing size
*
* @param TPoint ul: new location of upper left corner
* @return nothing
*/
void TchObject::UL(TPoint ul)
{
Move(TVector(ul.x-boundary_.UL.x,ul.y-boundary_.UL.y));
}
/**
* @brief get lower right corner of boundary
*
* @param nothing
* @return const TPoint&, lower right corner of boundary
*/
TPoint TchObject::LR()const
{
return boundary_.LR;
}
/**
* @brief set upper left corner of boundary, without changing size
*
* @param TPoint lr: new location of lower right corner
* @return nothing
*/
void TchObject::LR(TPoint lr)
{
Move(TVector(lr.x-boundary_.LR.x,lr.y-boundary_.LR.y));
}
/**
* @brief get location (centre) of TchObject
*
* @param nothing
* @return TPoint, centre of TchObject
*/
TPoint TchObject::Location() const
{
return TPoint(boundary_.UL.x+Width()/2,boundary_.UL.y+Height()/2);
}
/**
* @brief set location (centre) of TchObject
*
* @param TPoint, centre of TchObject
* @return Nothing
*/
void TchObject::Location(TPoint val)
{
TPoint centre=Location();
Move(TVector(val.x-centre.x,val.y-centre.y));
}
/**
* @brief set location (centre) of TchObject
*
* @param uint16_t x, x value of the coordinate of the centre
* @param uint16_t y, y value of the coordinate of the centre
* @return Nothing
*/
void TchObject::Location(uint16_t x,uint16_t y)
{
TPoint centre(Location());
Move(TVector(x-centre.x,y-centre.y));
}
/**
* @brief moves the TchObject according to TSize l
*
* @param TSize val, containing the width and height for moving the TchObject
* @return Nothing
*/
void TchObject::Move(const TVector& v)
{
boundary_.UL.operator=(boundary_.UL.operator+(v));
boundary_.LR.operator=(boundary_.LR.operator+(v));
}
uint16_t TchObject::Width()const
{
return abs(boundary_.LR.x-boundary_.UL.x);
}
uint16_t TchObject::Height()const
{
return abs(boundary_.LR.y-boundary_.UL.y);
}
/**
* @brief returns the current size of the TchObject
*
* @param nothing
* @return TSize, size of the tchobject
*/
TSize TchObject::Size() const
{
return boundary_.LR.Distance(boundary_.UL);
}
/**
* @brief resizes the TchObject according to TSize, while maintaining the same location (centre)
*
* @param TSize val, containing the width and height for resizing the TchObject
* @return Nothing
*/
void TchObject::Size(TSize val)
{
TPoint centre=Location();
boundary_.UL=centre.operator-(TVector(val).operator*(0.5));
boundary_.LR=centre.operator+(TVector(val).operator*(0.5));
}
/**
* @brief resizes the TchObject according to given width and height, while maintaining the same location (centre)
*
* @param TSize val, containing the width and height for resizing the TchObject
* @return Nothing
*/
void TchObject::Size(uint16_t width,uint16_t height)
{
Size(TSize(width, height));
}
//functions
/**
* @brief checks wether a point is inside the boundaries
*
* @param TPoint p: the locationt to be checked
* @return bool, true if p is inside (or on the edge) and false if p is located outside
*/
bool TchObject::Inside(TPoint p)const
{
return boundary_.Inside(p);
}
}