mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-06-21 05:14:26 -04:00
Add a TestField widget that supports a cursor.
This commit is contained in:
parent
5c4e27ea29
commit
4b37f1bb2f
6 changed files with 218 additions and 80 deletions
|
@ -45,76 +45,191 @@ void text_prompt(NavigationView& nav, std::string& str, const size_t max_length,
|
|||
}*/
|
||||
}
|
||||
|
||||
void TextEntryView::update_text() {
|
||||
if (cursor_pos < 30)
|
||||
text_input.set(_str + std::string(_max_length - _str.length(), ' '));
|
||||
else
|
||||
text_input.set('<' + _str.substr(cursor_pos - 29, 29));
|
||||
|
||||
draw_cursor();
|
||||
/* TextField ***********************************************************/
|
||||
|
||||
TextField::TextField(
|
||||
std::string& str,
|
||||
size_t max_length,
|
||||
Point position,
|
||||
uint32_t length
|
||||
) : Widget{ { position, { 8 * static_cast<int>(length), 16 } } },
|
||||
text_{ str },
|
||||
max_length_{ std::max<size_t>(max_length, 1) },
|
||||
char_count_{ std::max<uint32_t>(length, 1) },
|
||||
cursor_pos_{ text_.length() },
|
||||
insert_mode_{ true }
|
||||
{
|
||||
set_focusable(true);
|
||||
}
|
||||
|
||||
const std::string& TextField::value() const {
|
||||
return text_;
|
||||
}
|
||||
|
||||
void TextField::set(const std::string& str) {
|
||||
// Assume that setting the string implies we want the whole thing.
|
||||
max_length_ = std::max(max_length_, str.length());
|
||||
|
||||
text_ = str;
|
||||
cursor_pos_ = str.length();
|
||||
set_cursor(str.length());
|
||||
}
|
||||
|
||||
void TextField::set_cursor(uint32_t pos) {
|
||||
cursor_pos_ = std::min<size_t>(pos, text_.length());
|
||||
set_dirty();
|
||||
}
|
||||
|
||||
void TextField::set_max_length(size_t max_length) {
|
||||
// Doesn't make sense, ignore.
|
||||
if (max_length == 0)
|
||||
return;
|
||||
|
||||
if (max_length < text_.length()) {
|
||||
text_.erase(max_length - 1);
|
||||
text_.shrink_to_fit();
|
||||
} else {
|
||||
text_.reserve(max_length);
|
||||
}
|
||||
|
||||
max_length_ = max_length;
|
||||
set_cursor(cursor_pos_);
|
||||
}
|
||||
|
||||
void TextField::set_insert_mode() {
|
||||
insert_mode_ = true;
|
||||
}
|
||||
|
||||
void TextField::set_overwrite_mode() {
|
||||
insert_mode_ = false;
|
||||
}
|
||||
|
||||
void TextField::char_add(char c) {
|
||||
// Don't add if inserting and at max_length and
|
||||
// don't overwrite if past the end of the text.
|
||||
if ((text_.length() >= max_length_ && insert_mode_) ||
|
||||
(cursor_pos_ >= text_.length() && !insert_mode_))
|
||||
return;
|
||||
|
||||
if (insert_mode_)
|
||||
text_.insert(cursor_pos_, 1, c);
|
||||
else
|
||||
text_[cursor_pos_] = c;
|
||||
|
||||
cursor_pos_++;
|
||||
set_dirty();
|
||||
}
|
||||
|
||||
void TextField::char_delete() {
|
||||
if (cursor_pos_ == 0)
|
||||
return;
|
||||
|
||||
cursor_pos_--;
|
||||
text_.erase(cursor_pos_, 1);
|
||||
set_dirty();
|
||||
}
|
||||
|
||||
void TextField::paint(Painter& painter) {
|
||||
constexpr int char_width = 8;
|
||||
|
||||
auto rect = screen_rect();
|
||||
auto text_style = has_focus() ? style().invert() : style();
|
||||
auto offset = 0;
|
||||
|
||||
// Does the string need to be shifted?
|
||||
if (cursor_pos_ >= char_count_)
|
||||
offset = cursor_pos_ - char_count_ + 1;
|
||||
|
||||
// Clear the control.
|
||||
painter.fill_rectangle(rect, text_style.background);
|
||||
|
||||
// Draw the text starting at the offset.
|
||||
for (uint32_t i = 0; i < char_count_ && i + offset < text_.length(); i++) {
|
||||
painter.draw_char(
|
||||
{ rect.location().x() + (static_cast<int>(i) * char_width), rect.location().y() },
|
||||
text_style,
|
||||
text_[i + offset]
|
||||
);
|
||||
}
|
||||
|
||||
// Determine cursor position on screen (either the cursor position or the last char).
|
||||
int32_t cursor_x = char_width * (offset > 0 ? char_count_ - 1 : cursor_pos_);
|
||||
Point cursor_point{ screen_pos().x() + cursor_x, screen_pos().y() };
|
||||
auto cursor_style = text_style.invert();
|
||||
|
||||
// Invert the cursor character when in overwrite mode.
|
||||
if (!insert_mode_ && (cursor_pos_) < text_.length())
|
||||
painter.draw_char(cursor_point, cursor_style, text_[cursor_pos_]);
|
||||
|
||||
// Draw the cursor.
|
||||
Rect cursor_box{ cursor_point, { char_width, 16 } };
|
||||
painter.draw_rectangle(cursor_box, cursor_style.background);
|
||||
}
|
||||
|
||||
bool TextField::on_key(const KeyEvent key) {
|
||||
if (key == KeyEvent::Left && cursor_pos_ > 0)
|
||||
cursor_pos_--;
|
||||
else if (key == KeyEvent::Right && cursor_pos_ < text_.length())
|
||||
cursor_pos_++;
|
||||
else if (key == KeyEvent::Select)
|
||||
insert_mode_ = !insert_mode_;
|
||||
else
|
||||
return false;
|
||||
|
||||
set_dirty();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TextField::on_encoder(const EncoderEvent delta) {
|
||||
int32_t new_pos = cursor_pos_ + delta;
|
||||
|
||||
// Let the encoder wrap around the ends of the text.
|
||||
if (new_pos < 0)
|
||||
new_pos = text_.length();
|
||||
else if (static_cast<size_t>(new_pos) > text_.length())
|
||||
new_pos = 0;
|
||||
|
||||
set_cursor(new_pos);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TextField::on_touch(const TouchEvent event) {
|
||||
if (event.type == TouchEvent::Type::Start)
|
||||
focus();
|
||||
|
||||
set_dirty();
|
||||
return true;
|
||||
}
|
||||
|
||||
/* TextEntryView ***********************************************************/
|
||||
|
||||
void TextEntryView::char_delete() {
|
||||
if (!cursor_pos) return;
|
||||
|
||||
cursor_pos--;
|
||||
_str.resize(cursor_pos);
|
||||
text_input.char_delete();
|
||||
}
|
||||
|
||||
void TextEntryView::char_add(const char c) {
|
||||
if (cursor_pos >= _max_length) return;
|
||||
|
||||
_str += c;
|
||||
cursor_pos++;
|
||||
}
|
||||
|
||||
void TextEntryView::draw_cursor() {
|
||||
Point draw_pos;
|
||||
|
||||
draw_pos = { text_input.screen_rect().location().x() + std::min((Coord)cursor_pos, (Coord)28) * 8,
|
||||
text_input.screen_rect().location().y() + 16 };
|
||||
|
||||
// Erase previous
|
||||
display.fill_rectangle(
|
||||
{ { text_input.screen_rect().location().x(), draw_pos.y() }, { text_input.screen_rect().size().width(), 4 } },
|
||||
Color::black()
|
||||
);
|
||||
// Draw new
|
||||
display.fill_rectangle(
|
||||
{ draw_pos, { 8, 4 } },
|
||||
Color::white()
|
||||
);
|
||||
text_input.char_add(c);
|
||||
}
|
||||
|
||||
void TextEntryView::focus() {
|
||||
button_ok.focus();
|
||||
text_input.focus();
|
||||
}
|
||||
|
||||
TextEntryView::TextEntryView(
|
||||
NavigationView& nav,
|
||||
std::string& str,
|
||||
size_t max_length
|
||||
) : _str(str),
|
||||
_max_length(max_length)
|
||||
) : text_input{ str, max_length, { 0, 0 } }
|
||||
{
|
||||
|
||||
// Trim from right
|
||||
//_str->erase(std::find_if(_str->rbegin(), _str->rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), _str->end());
|
||||
if (_str.length() > _max_length)
|
||||
_str.resize(_max_length);
|
||||
_str.reserve(_max_length);
|
||||
|
||||
cursor_pos = _str.length();
|
||||
|
||||
add_children({
|
||||
&text_input,
|
||||
&button_ok
|
||||
});
|
||||
|
||||
button_ok.on_select = [this, &nav](Button&) {
|
||||
_str.resize(cursor_pos);
|
||||
|
||||
button_ok.on_select = [this, &str, &nav](Button&) {
|
||||
str.shrink_to_fit(); // NB: str is the TextField string.
|
||||
if (on_changed)
|
||||
on_changed(_str);
|
||||
on_changed(str);
|
||||
nav.pop();
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue