Touch.h
Go to the documentation of this file.
1 /****
2  * Touch.h
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming-Graphics Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: May 2021 - mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "Types.h"
25 
26 namespace Graphics
27 {
28 class Device;
29 
30 class Touch
31 {
32 public:
33  struct State {
34  Point pos;
36  };
37 
38  struct Calibration {
39  Point origin{0, 0};
40  Point num{1, 1};
41  Point den{1, 1};
42 
44  {
45  IntPoint p(pt);
46  p -= origin;
47  p *= num;
48  p /= den;
49  return Point(p);
50  }
51  };
52 
56  using Callback = Delegate<void()>;
57 
58  Touch(Device* device = nullptr) : device(device)
59  {
60  }
61 
66 
71  virtual Size getNativeSize() const = 0;
72 
76  virtual State getState() const = 0;
77 
81  virtual void setCallback(Callback callback)
82  {
83  this->callback = callback;
84  }
85 
89  Size getSize() const
90  {
91  auto size = getNativeSize();
93  std::swap(size.w, size.h);
94  }
95  return size;
96  }
97 
102  {
103  return orientation;
104  }
105 
106  void setCalibration(const Calibration& cal)
107  {
108  calibration = cal;
109  }
110 
114  Point translate(Point rawPos)
115  {
116  return calibration.translate(rawPos);
117  }
118 
119 protected:
122  Calibration calibration;
124 };
125 
126 } // namespace Graphics
127 
A physical display device.
Definition: Libraries/Graphics/src/include/Graphics/Device.h:51
Delegate< void()> Callback
Callback function.
Definition: Touch.h:75
The String class.
Definition: WString.h:136
Point translate(Point pt)
Definition: Touch.h:62
Orientation orientation
Definition: Touch.h:140
Point pos
Definition: Touch.h:53
Device * device
Definition: Touch.h:139
String toString(Graphics::Touch::State state)
Size of rectangular area (width x height)
Definition: Libraries/Graphics/src/include/Graphics/Types.h:124
Definition: Touch.h:52
Point origin
Definition: Touch.h:58
Definition: Virtual.h:30
TPoint< int16_t > Point
Definition: Libraries/Graphics/src/include/Graphics/Types.h:299
TPoint< int > IntPoint
Definition: Libraries/Graphics/src/include/Graphics/Types.h:300
Orientation getOrientation()
Get current display orientation.
Definition: Touch.h:120
virtual void setCallback(Callback callback)
Register callback to be invoked when state changes.
Definition: Touch.h:100
Callback callback
Definition: Touch.h:142
void setCalibration(const Calibration &cal)
Definition: Touch.h:125
Calibration calibration
Definition: Touch.h:141
Orientation
Defines orientation of display.
Definition: Libraries/Graphics/src/include/Graphics/Types.h:66
virtual State getState() const =0
Get current state.
Touch(Device *device=nullptr)
Definition: Touch.h:77
Size getSize() const
Get native dimensions for current orientation.
Definition: Touch.h:108
Point den
Definition: Touch.h:60
virtual Size getNativeSize() const =0
Get physical size of display.
Point num
Definition: Touch.h:59
uint16_t pressure
Definition: Touch.h:54
Point translate(Point rawPos)
Translate position into screen co-ordinates.
Definition: Touch.h:133
virtual bool setOrientation(Orientation orientation)
Set display orientation.