Virtual.h
Go to the documentation of this file.
1 /****
2  * Virtual.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 <Graphics/Device.h>
25 #include <Graphics/AddressWindow.h>
27 
28 class CSocket;
29 
30 namespace Graphics
31 {
32 namespace Display
33 {
34 class VirtualSurface;
35 
41 class Virtual : public AbstractDisplay
42 {
43 public:
44  enum class Mode {
45  Normal,
46  Debug,
47  };
48 
49  using TouchCallback = Delegate<void(const void* buffer, size_t len)>;
50 
53 
54  bool begin(uint16_t width = 240, uint16_t height = 320);
55  bool begin(const String& ipaddr, uint16_t port, uint16_t width = 240, uint16_t height = 320);
56 
57  void setMode(Mode mode)
58  {
59  this->mode = mode;
60  }
61 
62  Mode getMode() const
63  {
64  return mode;
65  }
66 
67  bool setDisplaySize(uint16_t width, uint16_t height, Orientation orientation);
68 
69  /* Device */
70 
71  String getName() const override
72  {
73  return F("Virtual Screen");
74  }
75  Size getNativeSize() const override
76  {
77  return nativeSize;
78  }
80 
81  /* RenderTarget */
82 
83  Size getSize() const override
84  {
85  return rotate(nativeSize, orientation);
86  }
87 
88  PixelFormat getPixelFormat() const override
89  {
90  return PixelFormat::BGR24;
91  }
92 
93  bool setScrollMargins(uint16_t top, uint16_t bottom) override;
94  bool scroll(int16_t y) override;
95 
96  Surface* createSurface(size_t bufferSize = 0) override;
97 
98  void onTouch(TouchCallback callback)
99  {
100  touchCallback = callback;
101  }
102 
103 private:
104  class NetworkThread;
105  friend NetworkThread;
106  friend VirtualSurface;
107 
108  struct ScrollMargins {
109  uint16_t top;
110  uint16_t bottom;
111  };
112 
113  bool sizeChanged();
114 
115  void handleTouch(const void* buffer, size_t length)
116  {
117  if(touchCallback) {
118  touchCallback(buffer, length);
119  }
120  }
121 
122  std::unique_ptr<NetworkThread> thread;
123  Size nativeSize{};
124  AddressWindow addrWindow{};
125  ScrollMargins scrollMargins;
126  TouchCallback touchCallback;
127  Mode mode;
128 };
129 
130 } // namespace Display
131 } // namespace Graphics
#define F(string_literal)
Wrap a string literal stored in flash and access it using a String object.
Definition: WString.h:109
Definition: Display.h:22
Definition: AbstractDisplay.h:31
Orientation orientation
Definition: Libraries/Graphics/src/include/Graphics/Device.h:79
Virtual display device for Host.
Definition: Virtual.h:42
bool begin(uint16_t width=240, uint16_t height=320)
Size getNativeSize() const override
Get physical size of display.
Definition: Virtual.h:75
Surface * createSurface(size_t bufferSize=0) override
Create a surface for use with this render target.
bool setScrollMargins(uint16_t top, uint16_t bottom) override
Set margins for hardware scrolling.
Mode getMode() const
Definition: Virtual.h:62
bool scroll(int16_t y) override
Scroll region of display up or down using hardware scrolling.
Mode
Definition: Virtual.h:44
@ Normal
Aim to produce similar performance to real display.
@ Debug
Use standard software renderers, may run slower and less smoothly.
void onTouch(TouchCallback callback)
Definition: Virtual.h:98
String getName() const override
Get name of display.
Definition: Virtual.h:71
PixelFormat getPixelFormat() const override
All surfaces support the same pixel format.
Definition: Virtual.h:88
Size getSize() const override
Get target dimensions.
Definition: Virtual.h:83
bool setOrientation(Orientation orientation) override
Set display orientation.
bool setDisplaySize(uint16_t width, uint16_t height, Orientation orientation)
Delegate< void(const void *buffer, size_t len)> TouchCallback
Definition: Virtual.h:49
bool begin(const String &ipaddr, uint16_t port, uint16_t width=240, uint16_t height=320)
void setMode(Mode mode)
Definition: Virtual.h:57
Interface for a drawing surface.
Definition: Surface.h:42
The String class.
Definition: WString.h:133
Definition: Virtual.h:31
Orientation
Defines orientation of display.
Definition: Libraries/Graphics/src/include/Graphics/Types.h:47
constexpr Size rotate(Size size, Orientation orientation)
Definition: Libraries/Graphics/src/include/Graphics/Types.h:122
PixelFormat
Definition: Colors.h:295
Size of rectangular area (width x height)
Definition: Libraries/Graphics/src/include/Graphics/Types.h:105