Merge remote-tracking branch 'upstream/master'

Base class for text entry
This commit is contained in:
furrtek
2017-06-21 03:25:27 +01:00
131 changed files with 19385 additions and 5412 deletions

View File

@@ -22,11 +22,15 @@
#include "ui_textentry.hpp"
#include "portapack_persistent_memory.hpp"
#include "ui_handwrite.hpp"
#include "ui_alphanum.hpp"
using namespace portapack;
namespace ui {
void text_entry(NavigationView& nav, std::string * str, const size_t max_length, const std::function<void(std::string *)> on_done) {
if (portapack::persistent_memory::ui_config_textentry() == 0) {
void text_prompt(NavigationView& nav, std::string * str, const size_t max_length, const std::function<void(std::string *)> on_done) {
if (persistent_memory::ui_config_textentry() == 0) {
auto te_view = nav.push<AlphanumView>(str, max_length);
te_view->on_changed = [on_done](std::string * value) {
if (on_done)
@@ -41,4 +45,73 @@ void text_entry(NavigationView& nav, std::string * str, const size_t max_length,
}
}
void TextEntryView::update_text() {
if (_cursor_pos <= 28)
text_input.set(' ' + *_str + std::string(_max_length - _str->length(), ' '));
else
text_input.set('<' + _str->substr(_cursor_pos - 28, 28));
draw_cursor();
}
void TextEntryView::char_delete() {
if (!_cursor_pos) return;
_cursor_pos--;
_str->replace(_cursor_pos, 1, 1, 0);
}
void TextEntryView::char_add(const char c) {
if (_cursor_pos >= _max_length) return;
_str->replace(_cursor_pos, 1, 1, c);
_cursor_pos++;
}
void TextEntryView::draw_cursor() {
Point draw_pos;
draw_pos = { text_input.screen_rect().location().x() + 8 + std::min((Coord)_cursor_pos, (Coord)28) * 8,
text_input.screen_rect().location().y() + 16 };
display.fill_rectangle(
{ { text_input.screen_rect().location().x(), draw_pos.y() }, { text_input.screen_rect().size().width(), 4 } },
Color::black()
);
display.fill_rectangle(
{ draw_pos, { 8, 4 } },
Color::white()
);
}
void TextEntryView::focus() {
button_ok.focus();
}
TextEntryView::TextEntryView(
NavigationView& nav,
std::string * str,
size_t max_length
) : _str(str),
_max_length(max_length)
{
// Trim from right
_str->erase(std::find_if(_str->rbegin(), _str->rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), _str->end());
_cursor_pos = _str->length();
_str->resize(_max_length, 0);
add_children({
&text_input,
&button_ok
});
button_ok.on_select = [this, &nav](Button&) {
if (on_changed)
on_changed(_str);
nav.pop();
};
}
} /* namespace ui */