Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sources/Application/Views/BaseClasses/UITextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class UITextField : public UIField, public Observable {
virtual ~UITextField();
void Draw(GUIWindow &w, int offset = 0);
void ProcessArrow(unsigned short mask);
void ProcessClear();
void OnClick();
void OnEditClick();
etl::string<MaxLength> GetString();
Expand All @@ -36,6 +37,7 @@ class UITextField : public UIField, public Observable {
private:
int selected_;
uint8_t currentChar_ = 0;
uint8_t lastUsedChar_ = 'A';
Variable *src_; // Pointer instead of reference
const etl::string<MAX_UITEXTFIELD_LABEL_LENGTH> label_;
uint8_t fourcc_;
Expand Down
84 changes: 48 additions & 36 deletions sources/Application/Views/BaseClasses/UITextField.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -80,54 +80,67 @@ template <uint8_t MaxLength> void UITextField<MaxLength>::OnEditClick() {
reinterpret_cast<I_ObservableData *>(static_cast<uintptr_t>(fourcc_)));
};

template <uint8_t MaxLength>
Comment thread
maks marked this conversation as resolved.
Outdated
void UITextField<MaxLength>::ProcessClear() {
etl::string<MAX_VARIABLE_STRING_LENGTH> buffer(src_->GetString());

if (buffer.empty() || buffer.compare(defaultValue_) == 0) {
// handle empty state
buffer = "A";
currentChar_ = 0;
} else {
// jump to lastUsedChar_character block depending on the current character
Comment thread
n1LS marked this conversation as resolved.
Outdated
if (lastUsedChar_>= 'A' && lastUsedChar_<= 'Z') {
lastUsedChar_= 'a';
} else if (lastUsedChar_>= 'a' && lastUsedChar_<= 'z') {
lastUsedChar_= '0';
} else if (lastUsedChar_>= '0' && lastUsedChar_<= '9') {
lastUsedChar_= '-';
} else {
lastUsedChar_= 'A';
}
}

buffer[currentChar_] = lastUsedChar_;
src_->SetString(buffer.c_str(), true);
SetChanged();
NotifyObservers(
reinterpret_cast<I_ObservableData *>(static_cast<uintptr_t>(fourcc_)));
};

template <uint8_t MaxLength>
void UITextField<MaxLength>::ProcessArrow(unsigned short mask) {
auto buffer(src_->GetString());
etl::string<MAX_VARIABLE_STRING_LENGTH> buffer(src_->GetString());
auto applyAndNotify = [&]() {
src_->SetString(buffer.c_str(), true);
SetChanged();
NotifyObservers(
reinterpret_cast<I_ObservableData *>(static_cast<uintptr_t>(fourcc_)));
};

// If the variable's value is empty, we need to initialize it when the user
// starts editing
bool isEmptyBuffer = buffer.empty();

switch (mask) {
case EPBM_UP:
// If buffer is empty or matches default, initialize with 'A'
if (isEmptyBuffer || buffer.compare(defaultValue_) == 0) {
currentChar_ = 0;
buffer.clear();
buffer.append("A");
src_->SetString(buffer.c_str(), true);
} else {
buffer[currentChar_] = getNext(buffer.c_str()[currentChar_], false);
src_->SetString(buffer.c_str(), true);
}
SetChanged();
NotifyObservers(
reinterpret_cast<I_ObservableData *>(static_cast<uintptr_t>(fourcc_)));
break;
case EPBM_DOWN:
// If buffer is empty or matches default, initialize with 'A'
if (isEmptyBuffer || buffer.compare(defaultValue_) == 0) {
currentChar_ = 0;
buffer.clear();
buffer.append("A");
src_->SetString(buffer.c_str(), true);
buffer = "A";
} else {
buffer[currentChar_] = getNext((char)buffer.c_str()[currentChar_], true);
src_->SetString(buffer.c_str(), true);
buffer[currentChar_] =
getNext(buffer.c_str()[currentChar_], mask == EPBM_DOWN);
}
SetChanged();
NotifyObservers(
reinterpret_cast<I_ObservableData *>(static_cast<uintptr_t>(fourcc_)));
applyAndNotify();
break;
case EPBM_LEFT:
// If we're showing the default value and user presses left, initialize with
// the default
if (isEmptyBuffer) {
buffer = defaultValue_;
src_->SetString(buffer.c_str(), true);
SetChanged();
NotifyObservers(reinterpret_cast<I_ObservableData *>(
static_cast<uintptr_t>(fourcc_)));
applyAndNotify();
}
if (currentChar_ > 0) {
currentChar_--;
Expand All @@ -138,24 +151,23 @@ void UITextField<MaxLength>::ProcessArrow(unsigned short mask) {
// with the default
if (isEmptyBuffer) {
buffer = defaultValue_;
src_->SetString(buffer.c_str(), true);
SetChanged();
NotifyObservers(reinterpret_cast<I_ObservableData *>(
static_cast<uintptr_t>(fourcc_)));
applyAndNotify();
}
if (currentChar_ < (buffer.length() - 1)) {
currentChar_++;
// -1 to allow for adding 1 more char
} else if (currentChar_ < (MaxLength - 1)) {
currentChar_++;
buffer.append("A");
src_->SetString(buffer.c_str(), true);
SetChanged();
NotifyObservers(reinterpret_cast<I_ObservableData *>(
static_cast<uintptr_t>(fourcc_)));
char str[2] = {lastUsedChar_, 0};
buffer.append(str);
applyAndNotify();
}
break;
};

// remember last used char for appending when user moves right at the end
// of the string
lastUsedChar_= buffer.c_str()[currentChar_];
};

template <uint8_t MaxLength>
Expand Down
Loading