AddressWindow.h
Go to the documentation of this file.
1 /****
2  * AddressWindow.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 {
37 struct AddressWindow {
38  // Last access mode for window
39  enum class Mode {
40  none,
41  write,
42  read,
43  };
44 
45  Rect bounds{};
46  uint16_t column{0};
47  Rect initial{};
48  Mode mode{};
49 
51  {
52  }
53 
54  AddressWindow(const Rect& rect) : bounds(rect), initial(rect)
55  {
56  }
57 
58  void reset()
59  {
60  column = 0;
61  bounds = initial;
62  }
63 
64  bool setMode(Mode mode)
65  {
66  if(this->mode == mode) {
67  return false;
68  }
69  this->mode = mode;
70  reset();
71  return true;
72  }
73 
74  AddressWindow& operator=(const Rect& rect)
75  {
76  initial = rect;
78  reset();
79  return *this;
80  }
81 
85  size_t getPixelCount() const
86  {
87  return bounds.w * bounds.h - column;
88  }
89 
90  uint16_t seek(uint16_t count)
91  {
92  if(bounds.h == 0) {
93  return 0;
94  }
95  auto pos = column;
96  uint16_t res{0};
97  column += count;
98  while(column >= bounds.w && bounds.h != 0) {
99  column -= bounds.w;
100  ++bounds.y;
101  --bounds.h;
102  res += bounds.w;
103  }
104  return res + column - pos;
105  }
106 
107  Point pos() const
108  {
109  return Point(left(), top());
110  }
111 
112  uint16_t left() const
113  {
114  return bounds.left() + column;
115  }
116 
117  uint16_t top() const
118  {
119  return bounds.top();
120  }
121 
122  uint16_t right() const
123  {
124  return bounds.right();
125  }
126 
127  uint16_t bottom() const
128  {
129  return bounds.bottom();
130  }
131 };
132 
133 } // namespace Graphics
int16_t bottom() const
Definition: Libraries/Graphics/src/include/Graphics/Types.h:431
AddressWindow & operator=(const Rect &rect)
Definition: AddressWindow.h:112
Point pos() const
Definition: AddressWindow.h:145
Mode mode
Definition: AddressWindow.h:86
uint16_t column
Relative x position within window.
Definition: AddressWindow.h:84
bool setMode(Mode mode)
Definition: AddressWindow.h:102
int16_t right() const
Definition: Libraries/Graphics/src/include/Graphics/Types.h:421
uint16_t top() const
Definition: AddressWindow.h:155
Rect bounds
y and h are updated by seek()
Definition: AddressWindow.h:83
Mode
Definition: AddressWindow.h:77
uint16_t left() const
Definition: AddressWindow.h:150
void reset()
Definition: AddressWindow.h:96
Definition: Virtual.h:30
TPoint< int16_t > Point
Definition: Libraries/Graphics/src/include/Graphics/Types.h:299
int16_t y
Definition: Libraries/Graphics/src/include/Graphics/Types.h:308
uint16_t seek(uint16_t count)
Definition: AddressWindow.h:128
uint16_t right() const
Definition: AddressWindow.h:160
int16_t left() const
Definition: Libraries/Graphics/src/include/Graphics/Types.h:416
size_t getPixelCount() const
Get remaining pixels in window from current position.
Definition: AddressWindow.h:123
uint16_t w
Definition: Libraries/Graphics/src/include/Graphics/Types.h:309
Rect initial
Definition: AddressWindow.h:85
AddressWindow()
Definition: AddressWindow.h:88
uint16_t h
Definition: Libraries/Graphics/src/include/Graphics/Types.h:310
int16_t top() const
Definition: Libraries/Graphics/src/include/Graphics/Types.h:426
uint16_t bottom() const
Definition: AddressWindow.h:165