~aleteoryx/muditaos

ref: b421d60aea9f8810196c3fb420fcec349c48bdf6 muditaos/module-gui/gui/widgets/TextSpinner.cpp -rw-r--r-- 2.7 KiB
b421d60a — Piotr Tański [EGD-7225] Forwarding namecard via BT removed 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "TextSpinner.hpp"

namespace gui
{
    TextSpinner::TextSpinner(const TextSpinner::TextRange &range, Boundaries boundaries)
        : textRange{range}, boundaries(boundaries)
    {
        setEditMode(EditMode::Browse);
        update();
    }
    void TextSpinner::setTextRange(const TextSpinner::TextRange &range)
    {
        textRange = range;
        update();
    }
    void TextSpinner::setCurrentPosition(Position pos)
    {
        if ((pos >= getRangeDownLimit()) && (pos <= getRangeUpLimit())) {
            currentPosition = pos;
        }
        update();
    }

    std::string TextSpinner::getCurrentText() const noexcept
    {
        return textRange[currentPosition];
    }

    void TextSpinner::stepUp()
    {
        if (currentPosition >= getRangeUpLimit()) {
            if (boundaries == Boundaries::Continuous) {
                currentPosition = getRangeDownLimit();
            }
            else {
                currentPosition = getRangeUpLimit();
            }
        }
        else {
            currentPosition++;
        }
        update();
    }
    void TextSpinner::stepDown()
    {
        if (currentPosition <= getRangeDownLimit()) {
            if (boundaries == Boundaries::Continuous) {
                currentPosition = getRangeUpLimit();
            }
            else {
                currentPosition = getRangeDownLimit();
            }
        }
        else {
            currentPosition--;
        }
        update();
    }
    void TextSpinner::update()
    {
        setText(textRange[currentPosition]);
    }
    bool TextSpinner::onInput(const InputEvent &inputEvent)
    {
        if (inputEvent.isShortRelease()) {
            switch (inputEvent.getKeyCode()) {
            case KeyCode::KEY_UP:
                stepUp();
                return true;
            case KeyCode::KEY_DOWN:
                stepDown();
                return true;
            default:
                break;
            }
        }
        return false;
    }
    bool TextSpinner::onFocus(bool state)
    {
        if (focus) {
            setEdges(RectangleEdge::Bottom);
        }
        else {
            setEdges(RectangleEdge::None);
        }
        showCursor(state);
        return true;
    }
    TextSpinner::Position TextSpinner::getRangeUpLimit() const
    {
        return static_cast<Position>(textRange.size() - 1);
    }
    TextSpinner::Position TextSpinner::getRangeDownLimit() const
    {
        return 0;
    }
    TextSpinner::Range TextSpinner::getValidRange() const noexcept
    {
        return Range{getRangeDownLimit(), getRangeUpLimit()};
    }

} // namespace gui