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