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); DriverViewField &getField(size_t i);
void updateSelectedField(size_t select); 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 selectedField;
size_t selectedFieldType; size_t selectedFieldIndex;
DataFieldType fieldTypes[NUM_FIELDS]; DataFieldType fieldTypes[NUM_FIELDS];
size_t indexByFieldType[DataFieldType_COUNT];
DataFieldType fieldTypeByIndex[DataFieldType_COUNT];
touchgfx::Unicode::UnicodeChar r2dProgBuffer[16]; touchgfx::Unicode::UnicodeChar r2dProgBuffer[16];
}; };

View File

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