Choice can now accept an array of const char * as values argument
This commit is contained in:
+54
-9
@@ -21,10 +21,9 @@
|
|||||||
#include "menu.h"
|
#include "menu.h"
|
||||||
#include "theme.h"
|
#include "theme.h"
|
||||||
|
|
||||||
Choice::Choice(FormGroup * parent, const rect_t & rect, const char * values, int16_t vmin, int16_t vmax,
|
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) :
|
std::function<int16_t()> getValue, std::function<void(int16_t)> setValue, WindowFlags windowFlags) :
|
||||||
ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags),
|
ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags),
|
||||||
values(values),
|
|
||||||
vmin(vmin),
|
vmin(vmin),
|
||||||
vmax(vmax),
|
vmax(vmax),
|
||||||
getValue(std::move(getValue)),
|
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)
|
void Choice::paint(BitmapBuffer * dc)
|
||||||
{
|
{
|
||||||
FormField::paint(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)
|
#if defined(HARDWARE_KEYS)
|
||||||
@@ -67,12 +112,12 @@ void Choice::openMenu()
|
|||||||
continue;
|
continue;
|
||||||
if (textHandler) {
|
if (textHandler) {
|
||||||
menu->addLine(textHandler(i), [=]() {
|
menu->addLine(textHandler(i), [=]() {
|
||||||
setValue(i);
|
setValue(i);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
menu->addLine(TEXT_AT_INDEX(values, i - vmin), [=]() {
|
menu->addLine(values[i - vmin], [=]() {
|
||||||
setValue(i);
|
setValue(i);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (value == i) {
|
if (value == i) {
|
||||||
@@ -86,8 +131,8 @@ void Choice::openMenu()
|
|||||||
}
|
}
|
||||||
|
|
||||||
menu->setCloseHandler([=]() {
|
menu->setCloseHandler([=]() {
|
||||||
editMode = false;
|
editMode = false;
|
||||||
setFocus();
|
setFocus();
|
||||||
});
|
});
|
||||||
|
|
||||||
setEditMode(true);
|
setEditMode(true);
|
||||||
|
|||||||
+8
-1
@@ -20,6 +20,7 @@
|
|||||||
#ifndef _CHOICE_H_
|
#ifndef _CHOICE_H_
|
||||||
#define _CHOICE_H_
|
#define _CHOICE_H_
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
#include "form.h"
|
#include "form.h"
|
||||||
|
|
||||||
enum ChoiceType {
|
enum ChoiceType {
|
||||||
@@ -46,8 +47,14 @@ class ChoiceBase : public FormField {
|
|||||||
|
|
||||||
class Choice : public ChoiceBase {
|
class Choice : public ChoiceBase {
|
||||||
public:
|
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);
|
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)
|
#if defined(DEBUG_WINDOWS)
|
||||||
std::string getName() override
|
std::string getName() override
|
||||||
{
|
{
|
||||||
@@ -86,7 +93,7 @@ class Choice : public ChoiceBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const char * values = nullptr;
|
std::vector<std::string> values;
|
||||||
int16_t vmin = 0;
|
int16_t vmin = 0;
|
||||||
int16_t vmax = 0;
|
int16_t vmax = 0;
|
||||||
std::string menuTitle;
|
std::string menuTitle;
|
||||||
|
|||||||
Reference in New Issue
Block a user