summaryrefslogtreecommitdiff
path: root/Layout/LayoutScrollSelect.cpp
blob: 5c7072eb687f0c959cce40714c26872a097259b3 (plain)
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
241
242
243
244
#include "LayoutScrollSelect.h"
#include "MTSLog.h"

LayoutScrollSelect::LayoutScrollSelect(DOGS102* lcd, Items items, std::string info1, std::string info2)
  : Layout(lcd),
    _lSw1(0, 7, "Scroll"),
    _lSw2(11, 7, "Select"),
    _lInfo1(0, 0, info1),
    _lInfo2(0, 1, info2),
    _lCursor(0, 4, "=>"),
    _fItem1(3, 2, 14),
    _fItem2(3, 3, 14),
    _fItem3(3, 4, 14),
    _fItem4(3, 5, 14),
    _fItem5(3, 6, 14),
    _items(items)
{
    _size = _items.size();
    _selected = 0;
}

LayoutScrollSelect::~LayoutScrollSelect() {}

void LayoutScrollSelect::display() {
    clear();
    startUpdate();

    writeLabel(_lSw1);
    writeLabel(_lSw2);
    writeLabel(_lInfo1);
    writeLabel(_lInfo2);
    writeLabel(_lCursor);

    switch (_size) {
        case 0:
            // special case - no items
            //    (empty)
            //    (empty)
            // => (empty)
            //    (empty)
            //    (empty)
            break;

        case 1:
            // special case - 1 item
            //    (empty)
            //    (empty)
            // => item1
            //    (empty)
            //    (empty)
            writeField(_fItem3, _items[0]);
            break;

        case 2:
            // special case - 2 items
            //    (empty)
            //    (empty)
            // => item1
            //    item2
            //    (empty)
            writeField(_fItem3, _items[0]);
            writeField(_fItem4, _items[1]);
            break;

        case 3:
            // special case - 3 items
            //    (empty)
            //    item3
            // => item1
            //    item2
            //    (empty)
            writeField(_fItem2, _items[2]);
            writeField(_fItem3, _items[0]);
            writeField(_fItem4, _items[1]);
            break;

        case 4:
            // special case - 4 items
            //    item3
            //    item4
            // => item1
            //    item2
            //    (empty)
            writeField(_fItem1, _items[2]);
            writeField(_fItem2, _items[3]);
            writeField(_fItem3, _items[0]);
            writeField(_fItem4, _items[1]);
            break;

        default:
            // this is the generic case - should handle lists of 5+ items correctly
            //    item4         item6           item9
            //    item5         item7           item10
            // => item1      => item1        => item1
            //    item2         item2           item2
            //    item3         item3           item3
            writeField(_fItem1, _items[_size - 2]);
            writeField(_fItem2, _items[_size - 1]);
            writeField(_fItem3, _items[0]);
            writeField(_fItem4, _items[1]);
            writeField(_fItem5, _items[2]);
            break;
    }

    endUpdate();
}

void LayoutScrollSelect::scroll() {
    size_t index;

    switch (_size) {
        case 0:
        case 1:
            // nothing to scroll
            break;

        case 2:
            // special case - 2 items
            //    (empty)  ->     (empty)
            //    (empty)  ->     (empty)
            // => item1    ->  => item2
            //    item2    ->     item1
            //    (empty)  ->     (empty)

            index = _selected;
            // keep selected item up to date
            increment(_selected);

            startUpdate();
            // previously selected item moves down to field4
            writeField(_fItem4, _items[index]);
            increment(index);
            // other item moves up to field3
            writeField(_fItem3, _items[index]);
            endUpdate();
            break;

        case 3:
            // special case - 3 items
            //    (empty)  ->     (empty)
            //    item3    ->     item1  
            // => item1    ->  => item2
            //    item2    ->     item3
            //    (empty)  ->     (empty)
            
            index = _selected;
            // keep selected item up to date
            increment(_selected);

            startUpdate();
            // previously selected item moves up to field2
            writeField(_fItem2, _items[index]);
            increment(index);
            // new selected item moves up to field3
            writeField(_fItem3, _items[index]);
            increment(index);
            // item from field2 moves down to field4
            writeField(_fItem4, _items[index]);
            endUpdate();
            break;

        case 4:
            // special case - 4 items
            //    item3    ->     item4  
            //    item4    ->     item1  
            // => item1    ->  => item2
            //    item2    ->     item3
            //    (empty)  ->     (empty)
            
            index = _selected;
            // keep selected item up to date
            increment(_selected);

            startUpdate();
            // previously selected item moves up to field2
            writeField(_fItem2, _items[index]);
            increment(index);
            // new selected item moves up to field3
            writeField(_fItem3, _items[index]);
            increment(index);
            // item from field1 moves down to field4
            writeField(_fItem4, _items[index]);
            increment(index);
            // item from field2 moves up to field1
            writeField(_fItem1, _items[index]);
            endUpdate();
            break;

        default:
            // this is the generic case - should handle lists of 5+ items correctly
            //    item4    ->     item5           item6    ->     item7
            //    item5    ->     item1           item7    ->     item1
            // => item1    ->  => item2        => item1    ->  => item2
            //    item2    ->     item3           item2    ->     item3
            //    item3    ->     item4           item3    ->     item4
            
            index = _selected;
            // keep selected item up to date
            increment(_selected);

            startUpdate();
            decrement(index);
            // item from field2 moves up to field1
            writeField(_fItem1, _items[index]);
            increment(index);
            // previously selected item moves up to field2
            writeField(_fItem2, _items[index]);
            increment(index);
            // new selected item moves up to field3
            writeField(_fItem3, _items[index]);
            increment(index);
            // item from field5 moves up to field4
            writeField(_fItem4, _items[index]);
            increment(index);
            // next item (from field1 or off screen) moves up to field5
            writeField(_fItem5, _items[index]);
            endUpdate();
            break;
      }
}

std::string LayoutScrollSelect::select() {
    std::string selected = "";
    if (_size > 0)
        selected = _items[_selected];

    return selected;
}

void LayoutScrollSelect::increment(size_t& index) {
    if (_size > 1) {
        index++;
        index %= _size;
    }
}

void LayoutScrollSelect::decrement(size_t& index) {
    if (_size > 1) {
        if (index == 0)
            index = _size - 1;
        else
            index--;
    }
}