~aleteoryx/muditaos

ref: 0823d82e5141f44812c54debf07245d0ca746124 muditaos/module-gui/gui/widgets/TextCursor.cpp -rw-r--r-- 6.7 KiB
0823d82e — Radoslaw Wicik [EGD-3743] Update copyrights in fies - add empty line after license 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "TextCursor.hpp"
#include "Common.hpp"
#include "Text.hpp"
#include "TextBlockCursor.hpp"
#include "TextDocument.hpp"
#include "log/log.hpp"
#include "TextLine.hpp"
#include <cassert>
#include <RawFont.hpp>

#define debug_text_cursor(...)
// #define debug_text_cursor(...) LOG_DEBUG(__VA_ARGS__)

namespace gui
{
    const unsigned int TextCursor::default_width = 2;

    TextCursor::TextCursor(gui::Text *parent, unsigned int pos, unsigned int block)
        : Rect(parent, 0, 0, default_width, 1),
          BlockCursor(parent != nullptr ? parent->document.get() : nullptr,
                      pos,
                      block,
                      parent != nullptr ? parent->getTextFormat().getFont() : nullptr),
          text(parent)
    {
        setFilled(true);
        setVisible(false);
    }

    TextCursor::Move TextCursor::moveCursor(NavigationDirection direction)
    {
        debug_text_cursor("cursor: screen pos: %d block: %d pos: %d %s",
                          pos_on_screen,
                          getBlockNr(),
                          BlockCursor::getPosition(),
                          atBegin() ? "at begin" : "middle");
        /// left & up - corner case
        if ((checkNpos() || atBegin()) &&
            (direction == NavigationDirection::LEFT || direction == NavigationDirection::UP)) {
            return Move::Start;
        }

        /// down & right - corner case
        if ((checkNpos() || atEnd()) &&
            (direction == NavigationDirection::RIGHT || direction == NavigationDirection::DOWN)) {
            return Move::End;
        }

        auto nr = getBlockNr();
        if (direction == NavigationDirection::LEFT) {
            operator--();
            if (nr == getBlockNr() || nr == text::npos) {
                --pos_on_screen;
            }
            else {
                auto block    = document->getBlock(this);
                auto len      = block->getText().length();
                pos_on_screen = len - 1;
                return Move::Up;
            }
            return Move::InLine;
        }

        if (direction == NavigationDirection::RIGHT) {
            operator++();
            if (nr == getBlockNr() || nr == text::npos) {
                ++pos_on_screen;
            }
            else {
                pos_on_screen = 0;
                return Move::Down;
            }
            return Move::InLine;
        }

        if (direction == NavigationDirection::DOWN) {

            if (document->isEmpty()) {
                return Move::Error;
            }

            operator++();

            auto block = document->getBlock(this);

            if (block == nullptr) {
                return Move::Error;
            }

            auto len = block->getText().length();

            if (len < pos_on_screen) {
                pos_on_screen = len;
            }

            return Move::Down;
        }

        return Move::Error;
    }

    std::tuple<const TextLine *, unsigned int, unsigned int> TextCursor::getLine()
    {
        unsigned int offset_pos = 0;
        unsigned int row        = 0;

        if (text == nullptr) {
            return {nullptr, text::npos, text::npos};
        }

        auto block = getBlockNr();

        for (auto &line : text->lines->get()) {
            if (line.getBlockNr() == block) {
                if (offset_pos + line.length() >= pos_on_screen) {
                    auto column = pos_on_screen - offset_pos;
                    return {&line, column, row};
                }
                offset_pos += line.length();
            }
            ++row;
        }

        return {nullptr, text::npos, text::npos};
    }

    void TextCursor::updateView()
    {
        int32_t x = 0, y = 0;
        uint32_t w = default_width, h = 0;
        if (text == nullptr) {
            setArea({x, y, w, h});
            return;
        }
        auto default_font = text->format.getFont();
        if (document->isEmpty() && default_font != nullptr) {
            h += default_font->info.line_height;
            x = getAxisAlignmentValue(Axis::X, w);
            y = getAxisAlignmentValue(Axis::Y, h);
        }
        else if (text != nullptr || text->lines->size() > 0) {
            auto [line, column, row] = getLine();
            if (line == nullptr || column == text::npos || row == text::npos) {
                setArea({x, y, w, h});
                return;
            }
            auto el = line->getElement(column);
            assert(el != nullptr);
            x += line->getX() + line->getWidthTo(column);
            y += el->getY();
            h += el->getHeight();
        }
        setArea({x, y, w, h});
    }

    void TextCursor::addChar(uint32_t utf_val)
    {
        BlockCursor::addChar(utf_val);
        if (utf_val == text::newline) {
            moveCursor(NavigationDirection::DOWN);
        }
        else {
            moveCursor(NavigationDirection::RIGHT);
        }
    }

    TextCursor &TextCursor::operator<<(const UTF8 &text)
    {
        for (unsigned int i = 0; i < text.length(); ++i) {
            addChar(text[i]);
        }
        return *this;
    }

    TextCursor &TextCursor::operator<<(TextBlock textblock)
    {
        auto len = textblock.length();
        BlockCursor::addTextBlock(std::move(textblock));
        // +1 is for block barier
        for (unsigned int i = 0; i < len + 1; ++i) {
            moveCursor(NavigationDirection::RIGHT);
        }
        return *this;
    }

    void TextCursor::removeChar()
    {
        moveCursor(NavigationDirection::LEFT);
        BlockCursor::removeChar();
    }

    InputBound TextCursor::processBound(InputBound bound, const InputEvent &event)
    {
        if (bound == InputBound::CAN_MOVE) {
            if (event.isShortPress()) {
                moveCursor(inputToNavigation(event));
            }
        }

        if (bound == InputBound::CAN_REMOVE) {
            if (event.isShortPress()) {
                text->handleBackspace(event);
            }
        }

        if (bound == InputBound::CAN_ADD) {
            if (event.isLongPress()) {
                auto val = toNumeric(event.keyCode);
                if (val != InvalidNumericKeyCode) {
                    addChar(intToAscii(val));
                }
            }
            else {
                text->handleAddChar(event);
            }
        }

        return bound;
    }

} // namespace gui

const char *c_str(enum gui::TextCursor::Move what)
{
    switch (what) {
    case gui::TextCursor::Move::Start:
        return "Start";
    case gui::TextCursor::Move::End:
        return "End";
    case gui::TextCursor::Move::Up:
        return "Up";
    case gui::TextCursor::Move::Down:
        return "Down";
    case gui::TextCursor::Move::InLine:
        return "InLine";
    case gui::TextCursor::Move::Error:
        return "Error";
    }
    return "";
}