Chris Mumford | 54514cf | 2022-11-01 20:23:47 +0000 | [diff] [blame] | 1 | // Copyright 2022 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | #pragma once |
| 15 | |
| 16 | #include <array> |
| 17 | |
| 18 | #include "pw_color/color.h" |
Anthony DiGirolamo | d1b0bbb | 2024-04-10 20:15:41 +0000 | [diff] [blame] | 19 | #include "pw_geometry/size.h" |
| 20 | #include "pw_geometry/vector2.h" |
Chris Mumford | 54514cf | 2022-11-01 20:23:47 +0000 | [diff] [blame] | 21 | #include "pw_result/result.h" |
| 22 | |
| 23 | constexpr size_t kNumCharsWide = 52; |
| 24 | constexpr size_t kNumRows = 9; |
| 25 | |
| 26 | constexpr pw::color::color_rgb565_t kBlackColor = 0x0000; |
| 27 | constexpr pw::color::color_rgb565_t kWhiteColor = 0xffff; |
| 28 | |
| 29 | // Maintain a scrolling buffer of text. The "drawing" cursor location starts |
| 30 | // at (0,0), and new characters are inserted right-to-left. Newline ('\n') |
| 31 | // characters cause the text to be scrolled up - eventually rolling off the |
| 32 | // top of the buffer to make space for new text rows at the bottom. |
| 33 | class TextBuffer { |
| 34 | public: |
| 35 | // An ASCII character with a foreground and background color. |
| 36 | struct Char { |
| 37 | // Set to a cleared default state. |
| 38 | void Reset() { |
| 39 | ch = '\0'; |
| 40 | foreground_color = kWhiteColor; |
| 41 | background_color = kBlackColor; |
| 42 | } |
| 43 | |
| 44 | char ch = '\0'; |
| 45 | pw::color::color_rgb565_t foreground_color = kWhiteColor; |
| 46 | pw::color::color_rgb565_t background_color = kBlackColor; |
| 47 | }; |
| 48 | |
| 49 | // A row of ASCII text characters. |
| 50 | struct TextRow { |
| 51 | void Clear() { |
| 52 | for (Char& ch : chars) { |
| 53 | ch.Reset(); |
| 54 | } |
| 55 | } |
| 56 | std::array<Char, kNumCharsWide> chars; |
| 57 | }; |
| 58 | |
| 59 | // Insert a character at the current cursor location. The cursor will be |
| 60 | // moved right by one slot. Newline ('\n') characters will move the cursor to |
| 61 | // the next line, at column 0. |
| 62 | void DrawCharacter(const Char& ch); |
| 63 | |
| 64 | // Return the size, in characters, of the text buffer. |
Anthony DiGirolamo | d1b0bbb | 2024-04-10 20:15:41 +0000 | [diff] [blame] | 65 | pw::geometry::Size<int> GetSize() const { |
| 66 | return pw::geometry::Size<int>{kNumCharsWide, kNumRows}; |
Chris Mumford | 54514cf | 2022-11-01 20:23:47 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Return the character at the specified location. |
Anthony DiGirolamo | d1b0bbb | 2024-04-10 20:15:41 +0000 | [diff] [blame] | 70 | pw::Result<Char> GetChar(pw::geometry::Vector2<int> loc) const; |
Chris Mumford | 54514cf | 2022-11-01 20:23:47 +0000 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | void ScrollUp(); |
| 74 | void InsertNewline(); |
| 75 | |
Anthony DiGirolamo | d1b0bbb | 2024-04-10 20:15:41 +0000 | [diff] [blame] | 76 | pw::geometry::Vector2<int> cursor_ = {0, 0}; |
Chris Mumford | 54514cf | 2022-11-01 20:23:47 +0000 | [diff] [blame] | 77 | bool character_wrap_enabled_ = false; |
| 78 | std::array<TextRow, kNumRows> text_rows_; |
| 79 | }; |