Sort data fields alphabetically

This commit is contained in:
Jasper Blanckenburg 2023-05-24 00:24:30 +02:00
parent a3265543a5
commit 58dd4ae8d3
2 changed files with 43 additions and 18 deletions

View File

@ -46,11 +46,14 @@ private:
DriverViewField &getField(size_t i);
void updateSelectedField(size_t select);
void updateSelectedFieldType(size_t select, bool animate = true);
void updateSelectedFieldIndex(size_t index, bool animate = true);
void updateSelectedFieldType(DataFieldType type, bool animate = true);
size_t selectedField;
size_t selectedFieldType;
size_t selectedFieldIndex;
DataFieldType fieldTypes[NUM_FIELDS];
size_t indexByFieldType[DataFieldType_COUNT];
DataFieldType fieldTypeByIndex[DataFieldType_COUNT];
touchgfx::Unicode::UnicodeChar r2dProgBuffer[16];
};

View File

@ -8,13 +8,30 @@
#include "touchgfx/Unicode.hpp"
#include <gui/driverview_screen/DriverViewView.hpp>
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <iterator>
#include "vehicle_state.h"
struct DFAlphabeticComp {
bool operator()(const DataFieldType &a, const DataFieldType &b) const {
return strcmp(dataFieldDescs[a].title, dataFieldDescs[b].title) < 0;
}
};
DriverViewView::DriverViewView()
: fieldTypes{DF_MinCellVolt, DF_Speed, DF_TSCurrent} {}
: fieldTypes{DF_MinCellVolt, DF_Speed, DF_TSCurrent} {
for (size_t i = 0; i < DataFieldType_COUNT; i++) {
fieldTypeByIndex[i] = static_cast<DataFieldType>(i);
}
std::sort(std::begin(fieldTypeByIndex), std::end(fieldTypeByIndex),
DFAlphabeticComp());
for (size_t i = 0; i < DataFieldType_COUNT; i++) {
indexByFieldType[fieldTypeByIndex[i]] = i;
}
}
void DriverViewView::setupScreen() {
DriverViewViewBase::setupScreen();
@ -58,8 +75,9 @@ void DriverViewView::tearDownScreen() { DriverViewViewBase::tearDownScreen(); }
void DriverViewView::fieldTypeSelectionUpdateItem(
DriverViewFieldSelection &item, int16_t itemIndex) {
item.setName(dataFieldDescs[itemIndex].title);
item.setSelected(itemIndex == (int)selectedFieldType);
DataFieldType fieldType = fieldTypeByIndex[itemIndex];
item.setName(dataFieldDescs[fieldType].title);
item.setSelected(itemIndex == (int)selectedFieldIndex);
}
void DriverViewView::setFieldType(size_t i, DataFieldType type) {
@ -186,7 +204,7 @@ void DriverViewView::selectNextFieldType() {
return;
}
updateSelectedFieldType((selectedFieldType + 1) % DataFieldType_COUNT);
updateSelectedFieldIndex((selectedFieldIndex + 1) % DataFieldType_COUNT);
}
void DriverViewView::selectPrevFieldType() {
@ -194,10 +212,10 @@ void DriverViewView::selectPrevFieldType() {
return;
}
if (selectedFieldType == 0) {
updateSelectedFieldType(DataFieldType_COUNT - 1);
if (selectedFieldIndex == 0) {
updateSelectedFieldIndex(DataFieldType_COUNT - 1);
} else {
updateSelectedFieldType(selectedFieldType - 1);
updateSelectedFieldIndex(selectedFieldIndex - 1);
}
}
@ -206,11 +224,11 @@ void DriverViewView::confirmFieldType() {
return;
}
DataFieldType fieldType = fieldTypeByIndex[selectedFieldIndex];
DriverViewField &field = getField(selectedField);
presenter->setFieldType(selectedField,
static_cast<DataFieldType>(selectedFieldType));
presenter->setFieldType(selectedField, fieldType);
field.setSelected(false);
fieldTypes[selectedField] = static_cast<DataFieldType>(selectedFieldType);
fieldTypes[selectedField] = fieldType;
fieldTypeSelection.setVisible(false);
fieldTypeSelection.invalidate();
}
@ -237,19 +255,23 @@ void DriverViewView::updateSelectedField(size_t selected) {
updateSelectedFieldType(fieldTypes[selectedField], false);
}
void DriverViewView::updateSelectedFieldType(size_t selected, bool animate) {
size_t previousSelected = selectedFieldType;
selectedFieldType = selected;
void DriverViewView::updateSelectedFieldType(DataFieldType type, bool animate) {
updateSelectedFieldIndex(indexByFieldType[type], animate);
}
void DriverViewView::updateSelectedFieldIndex(size_t selected, bool animate) {
size_t previousSelected = selectedFieldIndex;
selectedFieldIndex = selected;
// Trigger update of the background
fieldTypeSelection.itemChanged(previousSelected);
fieldTypeSelection.itemChanged(selectedFieldType);
fieldTypeSelection.itemChanged(selectedFieldIndex);
int16_t steps = animate ? 2 : 0;
if (selectedFieldType == 0) {
if (selectedFieldIndex == 0) {
// Just animating to 0 looks very broken when scrolling forwards. Animating
// to DataFieldType_COUNT looks good in both directions.
fieldTypeSelection.animateToItem(DataFieldType_COUNT, steps);
} else {
fieldTypeSelection.animateToItem(selectedFieldType, steps);
fieldTypeSelection.animateToItem(selectedFieldIndex, steps);
}
}