source / switch choices: new toolbar implementation

This commit is contained in:
Raphael Coeffic
2022-06-11 15:38:08 +02:00
committed by Raphael Coeffic
parent 4c8bb99e73
commit 77f53e8457
5 changed files with 14 additions and 65 deletions
+8 -25
View File
@@ -29,11 +29,11 @@
#include "draw_functions.h" #include "draw_functions.h"
#include "opentx.h" #include "opentx.h"
class SourceChoiceMenuToolbar : public MenuToolbar<SourceChoice> class SourceChoiceMenuToolbar : public MenuToolbar
{ {
public: public:
SourceChoiceMenuToolbar(SourceChoice * choice, Menu * menu): SourceChoiceMenuToolbar(SourceChoice * choice, Menu * menu):
MenuToolbar<SourceChoice>(choice, menu) MenuToolbar(choice, menu)
{ {
addButton(STR_CHAR_INPUT, MIXSRC_FIRST_INPUT, MIXSRC_LAST_INPUT); addButton(STR_CHAR_INPUT, MIXSRC_FIRST_INPUT, MIXSRC_LAST_INPUT);
#if defined(LUA_MODEL_SCRIPTS) #if defined(LUA_MODEL_SCRIPTS)
@@ -71,7 +71,9 @@ SourceChoice::SourceChoice(Window* parent, const rect_t &rect, int16_t vmin,
menu->setWaitHandler([=]() { menu->setWaitHandler([=]() {
int16_t val = getMovedSource(vmin); int16_t val = getMovedSource(vmin);
if (val) { if (val) {
fillMenu(menu, val); fillMenu(menu);
menu->select(getIndexFromValue(val));
// TODO: reset toolbar
} }
#if defined(AUTOSWITCH) #if defined(AUTOSWITCH)
else { else {
@@ -79,7 +81,9 @@ SourceChoice::SourceChoice(Window* parent, const rect_t &rect, int16_t vmin,
if (swtch && !IS_SWITCH_MULTIPOS(swtch)) { if (swtch && !IS_SWITCH_MULTIPOS(swtch)) {
val = switchToMix(swtch); val = switchToMix(swtch);
if (val && (val >= vmin) && (val <= vmax)) { if (val && (val >= vmin) && (val <= vmax)) {
fillMenu(menu, val); fillMenu(menu);
menu->select(getIndexFromValue(val));
// TODO: reset toolbar
} }
} }
} }
@@ -97,24 +101,3 @@ SourceChoice::SourceChoice(Window* parent, const rect_t &rect, int16_t vmin,
setAvailableHandler([](int v){ return isSourceAvailable(v); }); setAvailableHandler([](int v){ return isSourceAvailable(v); });
} }
void SourceChoice::fillMenu(Menu *menu, int16_t value, const FilterFct& filter)
{
int count = 0;
int current = 0;
menu->removeLines();
for (int i = vmin; i <= vmax; ++i) {
if (filter && !filter(i)) continue;
if (isValueAvailable && !isValueAvailable(i)) continue;
menu->addLine(getSourceString(i), [=]() { setValue(i); });
if (value == i) {
current = count;
}
++count;
}
if (current >= 0) {
menu->select(current);
}
}
-7
View File
@@ -29,9 +29,6 @@ bool isSourceAvailable(int source);
class SourceChoice : public ChoiceEx class SourceChoice : public ChoiceEx
{ {
template <class T>
friend class MenuToolbar;
public: public:
SourceChoice(Window* parent, const rect_t& rect, int16_t vmin, SourceChoice(Window* parent, const rect_t& rect, int16_t vmin,
int16_t vmax, std::function<int16_t()> getValue, int16_t vmax, std::function<int16_t()> getValue,
@@ -41,10 +38,6 @@ class SourceChoice : public ChoiceEx
#if defined(DEBUG_WINDOWS) #if defined(DEBUG_WINDOWS)
std::string getName() const override { return "SourceChoice"; } std::string getName() const override { return "SourceChoice"; }
#endif #endif
protected:
typedef std::function<bool(int16_t)> FilterFct;
void fillMenu(Menu* menu, int16_t value, const FilterFct& condition = nullptr);
}; };
#endif // _SOURCECHOICE_H_ #endif // _SOURCECHOICE_H_
+5 -26
View File
@@ -28,11 +28,11 @@
#include "opentx.h" #include "opentx.h"
#include "strhelpers.h" #include "strhelpers.h"
class SwitchChoiceMenuToolbar : public MenuToolbar<SwitchChoice> class SwitchChoiceMenuToolbar : public MenuToolbar
{ {
public: public:
SwitchChoiceMenuToolbar(SwitchChoice* choice, Menu* menu) : SwitchChoiceMenuToolbar(SwitchChoice* choice, Menu* menu) :
MenuToolbar<SwitchChoice>(choice, menu) MenuToolbar(choice, menu)
{ {
addButton(STR_CHAR_SWITCH, SWSRC_FIRST_SWITCH, SWSRC_LAST_SWITCH); addButton(STR_CHAR_SWITCH, SWSRC_FIRST_SWITCH, SWSRC_LAST_SWITCH);
addButton(STR_CHAR_TRIM, SWSRC_FIRST_TRIM, SWSRC_LAST_TRIM); addButton(STR_CHAR_TRIM, SWSRC_FIRST_TRIM, SWSRC_LAST_TRIM);
@@ -62,7 +62,9 @@ SwitchChoice::SwitchChoice(Window* parent, const rect_t& rect, int vmin,
val = swtch; val = swtch;
} }
if (val && (!isValueAvailable || isValueAvailable(val))) { if (val && (!isValueAvailable || isValueAvailable(val))) {
fillMenu(menu, val); fillMenu(menu);
menu->select(getIndexFromValue(val));
// TODO: reset toolbar
} }
} }
}); });
@@ -86,26 +88,3 @@ SwitchChoice::SwitchChoice(Window* parent, const rect_t& rect, int vmin,
setAvailableHandler(isSwitchAvailableInMixes); setAvailableHandler(isSwitchAvailableInMixes);
} }
void SwitchChoice::fillMenu(Menu* menu, int16_t value, const FilterFct& filter)
{
int count = 0;
int current = 0;
menu->removeLines();
for (int i = vmin; i <= vmax; ++i) {
if (filter && !filter(i)) continue;
if (isValueAvailable && !isValueAvailable(i)) continue;
menu->addLine(getSwitchPositionName(i), [=]() { setValue(i); });
if (value == i) {
current = count;
}
++count;
}
if (current >= 0) {
menu->select(current);
}
}
-6
View File
@@ -27,8 +27,6 @@
class SwitchChoice : public ChoiceEx class SwitchChoice : public ChoiceEx
{ {
template <class T> friend class MenuToolbar;
public: public:
SwitchChoice(Window* parent, const rect_t& rect, int vmin, int vmax, SwitchChoice(Window* parent, const rect_t& rect, int vmin, int vmax,
std::function<int16_t()> getValue, std::function<int16_t()> getValue,
@@ -37,10 +35,6 @@ class SwitchChoice : public ChoiceEx
#if defined(DEBUG_WINDOWS) #if defined(DEBUG_WINDOWS)
std::string getName() const override { return "SwitchChoice"; } std::string getName() const override { return "SwitchChoice"; }
#endif #endif
protected:
typedef std::function<bool(int16_t)> FilterFct;
void fillMenu(Menu* menu, int16_t value, const FilterFct& filter = nullptr);
}; };
#endif // _SWITCHCHOICE_H_ #endif // _SWITCHCHOICE_H_