~aleteoryx/muditaos

ref: b7eb4365ea80ed4d147911962a31fd8ec743d575 muditaos/module-gui/gui/widgets/Text.hpp -rw-r--r-- 6.4 KiB
b7eb4365 — Przemyslaw Brudny [EGD-3799] Messages queries cleanups. 5 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#pragma once

#include <memory>
#include <vector>

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

#include "BoxLayout.hpp"
#include "InputMode.hpp"
#include "Label.hpp"
#include "Rect.hpp"
#include "Style.hpp"
#include "TextCursor.hpp"
#include "TextDocument.hpp"
#include "TextLine.hpp"
#include "Translator.hpp"

namespace gui
{

    ///  @brief Widget that holds multiple lines of text.
    ///
    ///  Can expand horizontally to it's max size if it needs to fit more text in line
    ///  Can expand vertically if needed to hold lines of text.
    ///  Handles input
    ///        1. Provides text navigation
    ///        2. Provides new letter input with gui::KeyInputMappedTranslation
    ///        3. handles deletion of character
    ///
    ///   This widget stores both: whole text provided to it, and text visible on the screen
    ///   screen visible on the screen is a set of labels
    ///
    class Text : public Rect
    {
        friend TextCursor;

      protected:
        // holds list of labels for displaying currently visible text lines.
        class Lines
        {
            Item *parent = nullptr;
            std::list<TextLine> lines;

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

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

            Lines(Item *parent)
            {
                this->parent = parent;
            }

            const auto &get()
            {
                return lines;
            }

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

            auto size()
            {
                return lines.size();
            }

            auto maxWidth()
            {
                unsigned int w = 0;
                // could be std::max_element
                for (auto &el : lines) {
                    w = el.width() > w ? el.width() : w;
                }
                return w;
            }

            auto linesHeight()
            {
                unsigned int h = 0;
                for (auto &el : lines) {
                    h += el.height();
                }
                return h;
            }

            auto linesVAlign(Length parentSize)
            {
                for (auto &line : lines) {
                    line.alignV(parent->getAlignment(Axis::Y), parentSize, linesHeight());
                }
            }

            auto linesHAlign(Length parentSize)
            {
                for (auto &line : lines) {
                    line.alignH(parent->getAlignment(Axis::X), parentSize);
                }
            }

        } lines;

        TextCursor *cursor                     = nullptr;
        std::unique_ptr<TextDocument> document = std::make_unique<TextDocument>(std::list<TextBlock>());
        InputMode *mode                        = nullptr;

        void buildDocument(const UTF8 &text);
        void buildDocument(std::unique_ptr<TextDocument> &&document);
        void buildCursor();
        /// show cursor if cursor should be visible
        void showCursor(bool focus);

        EditMode editMode = EditMode::EDIT;
        [[nodiscard]] bool isMode(EditMode edit) const
        {
            return editMode == edit;
        }
        KeyCode key_signs_remove = KeyCode::KEY_PND;

      public:
        ExpandMode expandMode = ExpandMode::EXPAND_NONE;

      protected:
        TextType textType = TextType::MULTI_LINE;
        /// points to default text font to use
        RawFont *font = nullptr;
        Color textColor;
        bool underline = false;

        bool moveCursor(const NavigationDirection &direction, std::unique_ptr<TextDocument> &document);
        bool handleNavigation(const InputEvent &inputEvent);
        bool handleEnter();

        std::list<DrawCommand *> buildDrawList() override;
        /// redrawing lines
        /// it redraws visible lines on screen and if needed requests resize in parent
        virtual void drawLines();
        /// redrawing cursor
        void drawCursor();

      public:
        Text();
        Text(Item *parent,
             const uint32_t &x,
             const uint32_t &y,
             const uint32_t &w,
             const uint32_t &h,
             const UTF8 &text      = "",
             ExpandMode expandMode = ExpandMode::EXPAND_NONE,
             TextType textType     = TextType::MULTI_LINE);
        ~Text() override;

        void setEditMode(EditMode mode);
        void setTextType(TextType type);
        void setUnderline(const bool val);
        virtual void setText(const UTF8 &text);
        void setText(std::unique_ptr<TextDocument> &&document);

        void addText(const UTF8 &text);
        void addText(TextBlock text);
        virtual void clear();
        bool isEmpty();
        virtual UTF8 getText();
        /// saves text from widget to file at specified path
        virtual bool saveText(UTF8 path);
        void setFont(const UTF8 &fontName);
        void setFont(RawFont *font);

        // virtual methods from Item
        bool onInput(const InputEvent &inputEvent) override;
        /// move ownership of mode ptr to Text
        void setInputMode(InputMode *&&mode)
        {
            if (this->mode != nullptr) {
                delete this->mode;
            }
            this->mode = mode;
        };
        bool onFocus(bool state) override;
        bool onDimensionChanged(const BoundingBox &oldDim, const BoundingBox &newDim) override;
        void setRadius(int value) override;
        void setAlignment(const Alignment &value) override;
        void setPadding(const Padding &value) override;

      private:
        gui::KeyInputMappedTranslation translator;
        bool handleRotateInputMode(const InputEvent &inputEvent);
        bool handleRestoreInputModeUI(const InputEvent &inputEvent);
        bool handleSelectSpecialChar(const InputEvent &inputEvent);
        bool handleActivation(const InputEvent &inputEvent);
        bool handleBackspace(const InputEvent &inputEvent);
        bool handleAddChar(const InputEvent &inputEvent);
        bool handleDigitLongPress(const InputEvent &inputEvent);

        bool addChar(uint32_t utf8);
        bool removeChar();
    };

} /* namespace gui */