forked from scarsty/kys-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperMenuText.cpp
More file actions
320 lines (299 loc) · 8.27 KB
/
SuperMenuText.cpp
File metadata and controls
320 lines (299 loc) · 8.27 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "SuperMenuText.h"
#include "../others/Hanz2Piny.h"
#include "OpenCCConverter.h"
#include "PotConv.h"
#include "strfunc.h"
#include <algorithm>
#include <cmath>
#include <utility>
SuperMenuText::SuperMenuText(const std::string& title, int font_size, const std::vector<std::pair<int, std::string>>& allItems, int itemsPerPage) : InputBox(title, font_size), items_(allItems), itemsPerPage_(itemsPerPage)
{
previous_ = std::make_shared<Button>();
previous_->setText("上一頁PgUp");
next_ = std::make_shared<Button>();
next_->setText("下一頁PgDown");
addChild(previous_);
addChild(next_);
selections_ = std::make_shared<MenuText>();
addChild(selections_);
setAllChildState(NodeNormal);
defaultPage();
std::function<bool(const std::string&, const std::string&)> match = [&](const std::string& text, const std::string& name) -> bool
{
std::string pinyin = Hanz2Piny::hanz2pinyin(name);
int p = 0;
for (int i = 0; i < text.size(); i++)
{
int p1 = pinyin.find_first_of(text[i], p);
if (p1 < 0)
{
return false;
}
p = p1 + 1;
}
return true;
};
setMatchFunction(match);
for (const auto& pairName : items_)
{
//// 拼音
//auto u8Name = PotConv::cp936toutf8(pairName.second);
//std::string pinyin = Hanz2Piny::hanz2pinyin(u8Name);
//auto pys = strfunc::splitString(pinyin, " ");
//std::vector<std::string> acceptables;
//std::function<void(const std::string& curStr, int idx)> comboGenerator = [&](const std::string& curStr, int idx)
//{
// if (idx == pys.size())
// {
// acceptables.push_back(curStr);
// return;
// }
// comboGenerator(curStr + pys[idx][0], idx + 1);
// comboGenerator(curStr + pys[idx], idx + 1);
//};
//comboGenerator("", 0);
//for (auto& acc : acceptables)
//{
// for (int i = 0; i < acc.size(); i++)
// {
// matches_[acc.substr(0, i + 1)].insert(pairName.second);
// }
//}
//// 直接对字,可以两个跳,但是不管了
//for (int i = 0; i < pairName.second.size(); i++)
//{
// matches_[pairName.second.substr(0, i + 1)].insert(pairName.second);
//}
// 对id
std::string strID = std::to_string(pairName.first);
for (int i = 0; i < strID.size(); i++)
{
matches_[strID.substr(0, i + 1)].insert(pairName.second);
}
}
}
void SuperMenuText::setInputPosition(int x, int y)
{
InputBox::setInputPosition(x, y);
selections_->setPosition(x, y + font_size_ * 3);
previous_->setPosition(x, y - font_size_ * 1.5);
next_->setPosition(x + font_size_ * 5, y - font_size_ * 1.5);
}
void SuperMenuText::addDrawableOnCall(std::shared_ptr<DrawableOnCall> doc)
{
docs_.push_back(doc);
addChild(doc);
}
void SuperMenuText::setMatchFunction(std::function<bool(const std::string&, const std::string&)> match)
{
matchFunc_ = match;
}
void SuperMenuText::defaultPage()
{
if (curDefault_)
{
return;
}
std::vector<std::string> displays;
searchResultIndices_.clear();
for (int i = 0; i < items_.size(); i++)
{
if (i < std::min((std::size_t)itemsPerPage_, items_.size()))
{
activeIndices_.push_back(i);
displays.push_back(items_[i].second);
}
searchResultIndices_.push_back(i);
}
curPage_ = 0;
updateMaxPages();
selections_->setStrings(displays);
if (displays.size() != 0)
{
selections_->forceActiveChild(0);
}
curDefault_ = true;
}
void SuperMenuText::flipPage(int pInc)
{
if (curPage_ + pInc >= 0 && curPage_ + pInc < maxPages_)
{
// 允许你翻页!
curPage_ += pInc;
int startIdx = curPage_ * itemsPerPage_;
std::vector<std::string> displays;
activeIndices_.clear();
for (std::size_t i = startIdx; i < std::min(searchResultIndices_.size(), (std::size_t)startIdx + itemsPerPage_); i++)
{
activeIndices_.push_back(searchResultIndices_[i]);
displays.push_back(items_[searchResultIndices_[i]].second);
}
selections_->setStrings(displays);
}
// curDefault_ = false;
}
bool SuperMenuText::defaultMatch(const std::string& input, const std::string& name)
{
auto iterInput = matches_.find(input);
if (iterInput != matches_.end())
{
auto iterName = iterInput->second.find(name);
if (iterName != iterInput->second.end())
{
return true;
}
}
return false;
}
void SuperMenuText::search(const std::string& text)
{
std::vector<std::string> results;
activeIndices_.clear();
searchResultIndices_.clear();
// 只返回itemsPerPage_数量
// 更高级的Trie,LCS编辑距离等等 有缘人来搞
for (int i = 0; i < items_.size(); i++)
{
bool matched = false;
auto& opt = items_[i];
if (matchFunc_ && matchFunc_(text, opt.second))
{
matched = true;
}
else
{
matched = defaultMatch(text, opt.second);
}
if (matched)
{
if (results.size() < itemsPerPage_)
{
results.emplace_back(opt.second);
activeIndices_.push_back(i);
}
searchResultIndices_.push_back(i);
}
}
updateMaxPages();
curPage_ = 0;
selections_->setStrings(results);
selections_->forceActiveChild(0);
curDefault_ = false;
}
void SuperMenuText::updateMaxPages()
{
maxPages_ = std::ceil((searchResultIndices_.size() / (double)itemsPerPage_));
}
void SuperMenuText::dealEvent(BP_Event& e)
{
// get不到result 为何
// 不知道这玩意儿在干嘛,瞎搞即可
if (previous_->getState() == NodePress && e.type == BP_MOUSEBUTTONUP)
{
flipPage(-1);
previous_->setState(NodeNormal);
previous_->setResult(-1);
}
else if (next_->getState() == NodePress && e.type == BP_MOUSEBUTTONUP)
{
flipPage(1);
next_->setState(NodeNormal);
next_->setResult(-1);
}
bool research = false;
// 为什么switch自动缩进是这样
switch (e.type)
{
case BP_TEXTINPUT:
{
auto converted = OpenCCConverter::getInstance()->UTF8s2t(e.text.text);
converted = PotConv::conv(converted, "utf-8", "cp936");
text_ += converted;
research = true;
break;
}
case BP_TEXTEDITING:
{
break;
}
case BP_KEYDOWN:
{
if (e.key.keysym.sym == BPK_BACKSPACE)
{
if (text_.size() >= 1)
{
text_.pop_back();
if (text_.size() >= 1 && uint8_t(text_.back()) >= 128)
{
text_.pop_back();
}
}
research = true;
}
break;
}
case BP_KEYUP:
{
switch (e.key.keysym.sym)
{
case BPK_PAGEUP:
{
flipPage(-1);
break;
}
case BPK_PAGEDOWN:
{
flipPage(1);
break;
}
}
break;
}
case BP_MOUSEWHEEL:
{
if (e.wheel.y > 0)
{
flipPage(-1);
}
else if (e.wheel.y < 0)
{
flipPage(1);
}
break;
}
}
if (text_.empty())
{
defaultPage();
}
if (research)
{
search(text_);
}
int selectionIdx = selections_->getActiveChildIndex();
if (selectionIdx >= 0 && selectionIdx < activeIndices_.size())
{
int idx = activeIndices_[selectionIdx];
for (auto& doc : docs_)
{
doc->updateScreenWithID(items_[idx].first);
}
}
else
{
for (auto& doc : docs_)
{
doc->updateScreenWithID(-1);
}
}
if (selections_->getResult() >= 0)
{
auto selected = selections_->getResultString();
result_ = activeIndices_[selections_->getResult()];
if (result_ >= 0)
{
result_ = items_[result_].first;
}
setExit(true);
}
}