~aleteoryx/muditaos

ref: 7fbaf735ed99f5b0d26adb686cfa6ed2bb4267d5 muditaos/module-gui/gui/widgets/Lines.hpp -rw-r--r-- 4.4 KiB
7fbaf735 — Przemyslaw Brudny [EGD-7813] Option Window titles localizations fix 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <memory>
#include <vector>
#include <numeric>

#include "Alignment.hpp"
#include "InputEvent.hpp"
#include "TextConstants.hpp"
#include "utf8/UTF8.hpp"

#include "InputMode.hpp"
#include "TextLine.hpp"
#include "Translator.hpp"

namespace gui
{
    using TextLineStartContition = std::tuple<unsigned int, unsigned int>;

    class Lines
    {
        Text *text = nullptr;
        std::list<TextLine> lines;
        UnderLineProperties underLineProperties;
        unsigned int linesSpacing = 0;

        void addToInvisibleLines(TextLine line);

      public:
        explicit Lines(Text *text) : text(text)
        {}

        ~Lines() = default;

        LinesDrawStop stopCondition                = LinesDrawStop::None;
        TextLineStartContition drawStartConditions = {0, 0};
        std::list<TextLineStartContition> previousLinesStart;

        void reset()
        {
            erase();

            stopCondition = LinesDrawStop::None;
            previousLinesStart.clear();
            drawStartConditions = {0, 0};
        }

        void erase()
        {
            if (text != nullptr) {
                for (auto &line : lines) {
                    line.erase();
                }
            }
            lines.clear();
        }

        void emplace(TextLine &&line)
        {
            lines.emplace_back(std::move(line));
        }

        [[nodiscard]] auto &get()
        {
            return lines;
        }

        [[nodiscard]] auto &last()
        {
            return lines.back();
        }

        [[nodiscard]] auto &first()
        {
            return lines.front();
        }

        [[nodiscard]] auto size() const noexcept
        {
            return lines.size();
        }

        [[nodiscard]] auto countVisible() const noexcept
        {
            return std::count_if(lines.begin(), lines.end(), [](const auto &line) { return line.isVisible(); });
        }

        [[nodiscard]] auto empty() const noexcept
        {
            return lines.empty();
        }

        [[nodiscard]] auto maxWidth() const noexcept
        {
            Length w = 0;
            // could be std::max_element
            for (auto &el : lines) {
                if (el.isVisible()) {
                    w = std::max(el.width(), w);
                }
            }
            return w;
        }

        Length calculateLineSpacingAddition() const noexcept
        {
            return empty() ? 0 : (size() - 1) * linesSpacing;
        }

        Length calculateInitialCursorPosition(Length initHeight) const noexcept
        {
            return (empty() ? initHeight : initHeight * size()) + calculateLineSpacingAddition() +
                   underLineProperties.underLinePadding;
        }

        [[nodiscard]] auto linesHeight() const noexcept
        {
            return calculateLineSpacingAddition() +
                   std::accumulate(lines.begin(), lines.end(), 0U, [](const auto sum, const auto &line) {
                       return line.isVisible() ? sum + line.height() : sum;
                   });
        }

        [[nodiscard]] auto linesLength() const noexcept
        {
            return std::accumulate(lines.begin(), lines.end(), 0U, [](const auto sum, const auto &line) {
                return line.isVisible() ? sum + line.length() : sum;
            });
        }

        auto setUnderLineProperties(UnderLineProperties val) -> void
        {
            underLineProperties = val;
        }

        [[nodiscard]] UnderLineProperties &getUnderLineProperties() noexcept
        {
            return underLineProperties;
        }

        auto setLineSpacing(unsigned int val) -> void
        {
            linesSpacing = val;
        }

        auto draw(BlockCursor &drawCursor, Length w, Length h, Position lineYPosition, Position lineXPosition) -> void;
        auto draw(BlockCursor &drawCursor,
                  Length w,
                  Length h,
                  Position lineYPosition,
                  Position lineXPosition,
                  unsigned int linesCount) -> void;

        auto linesHAlign(Length parentSize) -> void;
        auto linesVAlign(Length parentSize) -> void;

        auto addToPreviousLinesStartList(unsigned int lineStartBlockNumber, unsigned int lineStartBlockPosition)
            -> void;

        TextLine *getLine(unsigned int lineNr);
    };

} // namespace gui