-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathInputBox.cpp
More file actions
131 lines (119 loc) · 2.81 KB
/
Copy pathInputBox.cpp
File metadata and controls
131 lines (119 loc) · 2.81 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
#include "InputBox.h"
#include "Button.h"
#include "Engine.h"
#include "Font.h"
#include "Menu.h"
InputBox::InputBox()
{
}
InputBox::InputBox(const std::string& title, int font_size) :
title_(title)
{
font_size_ = font_size;
}
InputBox::~InputBox()
{
}
void InputBox::dealEvent(EngineEvent& e)
{
switch (e.type)
{
case EVENT_TEXT_INPUT:
{
//auto converted = Font::getInstance()->T2S(e.text.text);
//converted = PotConv::conv(converted, "utf-8", "cp936");
//LOG("input %s\n", converted.c_str());
text_ += e.text.text;
break;
}
case EVENT_TEXT_EDITING:
{
//看起来不太正常,待查
//auto composition = e.edit.text;
//auto cursor = e.edit.start;
//auto selection_len = e.edit.length;
//LOG("editing %s\n", e.edit.text);
break;
}
case EVENT_KEY_DOWN:
if (e.key.key == K_BACKSPACE)
{
if (text_.size() >= 1)
{
uint8_t c = text_.back();
text_.pop_back();
if (c >= 128 && text_.size() >= 2 && uint8_t(text_.back()) >= 128)
{
text_.pop_back();
text_.pop_back();
}
}
}
break;
case EVENT_KEY_UP:
if (e.key.key == K_RETURN)
{
result_ = 0;
exit_ = true;
}
break;
}
}
void InputBox::draw()
{
Font::getInstance()->drawWithBox(title_, font_size_, x_, y_, color_, 255);
Font::getInstance()->drawWithBox(text_ + "_", font_size_, text_x_, text_y_, color_, 255);
Engine::getInstance()->setTextInputArea(text_x_, text_y_, font_size_ * 15, font_size_);
}
void InputBox::setInputPosition(int x, int y)
{
x_ = x;
y_ = y;
text_x_ = x;
text_y_ = y + font_size_ * 1.5;
if (action_menu_)
{
action_menu_->setPosition(x + font_size_ * 4.5, text_y_ + font_size_ * 2);
}
}
void InputBox::setActionButtons(bool enabled)
{
if (!enabled || action_menu_)
{
return;
}
action_menu_ = std::make_shared<Menu>();
action_menu_->setSize(font_size_ * 6, font_size_);
action_menu_->addChild<Button>(0, 0)->setText("確定");
action_menu_->addChild<Button>(font_size_ * 3, 0)->setText("取消");
addChild(action_menu_);
setInputPosition(x_, y_);
}
void InputBox::onPressedOK()
{
if (!action_menu_ || exit_)
{
return;
}
int action = action_menu_->getResult();
if (action == 0)
{
exitWithResult(0);
}
else if (action == 1)
{
exitWithResult(-1);
}
}
void InputBox::onEntrance()
{
Engine::getInstance()->startTextInput();
if (action_menu_)
{
action_menu_->onEntrance();
}
}
void InputBox::onExit()
{
Engine::getInstance()->stopTextInput();
}