Choice can now accept an array of const char * as values argument

This commit is contained in:
Bertrand Songis
2020-03-31 15:52:12 +02:00
parent 68fa08ae83
commit 8e3a41d796
2 changed files with 62 additions and 10 deletions
+54 -9
View File
@@ -21,10 +21,9 @@
#include "menu.h"
#include "theme.h"
Choice::Choice(FormGroup * parent, const rect_t & rect, const char * values, int16_t vmin, int16_t vmax,
std::function<int16_t()> getValue, std::function<void(int16_t)> setValue, WindowFlags windowFlags) :
Choice::Choice(FormGroup * parent, const rect_t & rect, int16_t vmin, int16_t vmax,
std::function<int16_t()> getValue, std::function<void(int16_t)> setValue, WindowFlags windowFlags) :
ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags),
values(values),
vmin(vmin),
vmax(vmax),
getValue(std::move(getValue)),
@@ -32,10 +31,56 @@ Choice::Choice(FormGroup * parent, const rect_t & rect, const char * values, int
{
}
Choice::Choice(FormGroup * parent, const rect_t & rect, const char * values[], int16_t vmin, int16_t vmax,
std::function<int16_t()> getValue, std::function<void(int16_t)> setValue, WindowFlags windowFlags) :
ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags),
vmin(vmin),
vmax(vmax),
getValue(std::move(getValue)),
setValue(std::move(setValue))
{
const char ** value = &values[0];
for (int i = vmin; i <= vmax; i++) {
this->values.emplace_back(*value++);
}
}
Choice::Choice(FormGroup * parent, const rect_t & rect, const char * values, int16_t vmin, int16_t vmax,
std::function<int16_t()> getValue, std::function<void(int16_t)> setValue, WindowFlags windowFlags) :
ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags),
vmin(vmin),
vmax(vmax),
getValue(std::move(getValue)),
setValue(std::move(setValue))
{
if (values) {
uint8_t len = values[0];
const char * value = &values[1];
for (int i = vmin; i <= vmax; i++) {
this->values.emplace_back(std::string(value, min<uint8_t>(len, strlen(value))));
value += len;
}
}
}
void Choice::addItem(const char * item)
{
values.emplace_back(item);
vmax += 1;
}
void Choice::addItems(const char * items[], uint8_t count)
{
values.reserve(values.size() + count);
for (uint8_t i = 0; i < count; i++)
values.emplace_back(items[i]);
vmax += count;
}
void Choice::paint(BitmapBuffer * dc)
{
FormField::paint(dc);
theme->drawChoice(dc, this, textHandler ? textHandler(getValue()).c_str() : TEXT_AT_INDEX(values, getValue() - vmin).c_str());
theme->drawChoice(dc, this, textHandler ? textHandler(getValue()).c_str() : values[getValue() - vmin].c_str());
}
#if defined(HARDWARE_KEYS)
@@ -67,12 +112,12 @@ void Choice::openMenu()
continue;
if (textHandler) {
menu->addLine(textHandler(i), [=]() {
setValue(i);
setValue(i);
});
}
else {
menu->addLine(TEXT_AT_INDEX(values, i - vmin), [=]() {
setValue(i);
menu->addLine(values[i - vmin], [=]() {
setValue(i);
});
}
if (value == i) {
@@ -86,8 +131,8 @@ void Choice::openMenu()
}
menu->setCloseHandler([=]() {
editMode = false;
setFocus();
editMode = false;
setFocus();
});
setEditMode(true);
+8 -1
View File
@@ -20,6 +20,7 @@
#ifndef _CHOICE_H_
#define _CHOICE_H_
#include <vector>
#include "form.h"
enum ChoiceType {
@@ -46,8 +47,14 @@ class ChoiceBase : public FormField {
class Choice : public ChoiceBase {
public:
Choice(FormGroup * parent, const rect_t & rect, int16_t vmin, int16_t vmax, std::function<int16_t()> getValue, std::function<void(int16_t)> setValue = nullptr, WindowFlags windowFlags = 0);
Choice(FormGroup * parent, const rect_t & rect, const char * values[], int16_t vmin, int16_t vmax, std::function<int16_t()> getValue, std::function<void(int16_t)> setValue = nullptr, WindowFlags windowFlags = 0);
Choice(FormGroup * parent, const rect_t & rect, const char * values, int16_t vmin, int16_t vmax, std::function<int16_t()> getValue, std::function<void(int16_t)> setValue = nullptr, WindowFlags windowFlags = 0);
void addItem(const char * item);
void addItems(const char * items[], uint8_t count);
#if defined(DEBUG_WINDOWS)
std::string getName() override
{
@@ -86,7 +93,7 @@ class Choice : public ChoiceBase {
}
protected:
const char * values = nullptr;
std::vector<std::string> values;
int16_t vmin = 0;
int16_t vmax = 0;
std::string menuTitle;