Skip to content

Commit 243d490

Browse files
committed
menu actions can now return bool to quit menu (true=quit)
1 parent 7b99212 commit 243d490

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

menu.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ int menu::menuKeys(menuOut &p,Stream& c,bool canExit) {
7272
// ...->draw -> input scan -> iterations -> [activate submenu or user function] -> ...
7373
// draw: call target device object
7474
//input scan: call the navigation function (self)
75-
void menu::activate(menuOut& p,Stream& c,bool canExit) {
75+
promptFeedback menu::activate(menuOut& p,Stream& c,bool canExit) {
7676
if (activeNode!=this) {
77-
action(*this,p,c);
77+
if (action(*this,p,c)) return true;
7878
previousMenu=(menu*)activeNode;
7979
activeNode=this;
8080
sel=0;
@@ -88,13 +88,18 @@ void menu::activate(menuOut& p,Stream& c,bool canExit) {
8888
sel=op;
8989
if (data[op]->enabled) {
9090
printMenu(p,canExit);//clearing old selection
91-
data[op]->activate(p,c,true);
91+
if (data[op]->activate(p,c,true)) {
92+
p.clear();
93+
activeNode=previousMenu;
94+
return true;
95+
}
9296
}
9397
} else if (op==-1) {//then exit
9498
p.clear();
9599
activeNode=previousMenu;
96100
c.flush();//reset the encoder
97101
}
102+
return 0;
98103
}
99104

100105
void menu::poll(menuOut& p,Stream& c,bool canExit) {

menu.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ v2.1 - Add full support of SetPosition(x,y) to move the menu inside the screen (
9292
template<>
9393
int (*)(prompt &p, menuOut &o, Stream &i) operator(void (*f)(prompt &p, menuOut &o, Stream &i)) {return devoid<f>;}*/
9494

95-
#define promptFeedback void
95+
#define promptFeedback bool
9696

9797
class promptAction {
9898
public:
@@ -114,7 +114,7 @@ v2.1 - Add full support of SetPosition(x,y) to move the menu inside the screen (
114114
class prompt {
115115
public:
116116
const char* text;
117-
static promptFeedback nothing() {}
117+
static promptFeedback nothing() {return false;}
118118
promptAction action;
119119
bool enabled;
120120
inline prompt(const char * text):text(text),enabled(true),action(nothing) {}
@@ -123,9 +123,8 @@ v2.1 - Add full support of SetPosition(x,y) to move the menu inside the screen (
123123
virtual void printTo(menuOut& p) {
124124
p.print(text);
125125
}
126-
virtual void activate(menuOut& p,Stream&c,bool) {
127-
//Serial.println("prompt::activate");
128-
action(*this,p,c);
126+
virtual promptFeedback activate(menuOut& p,Stream&c,bool) {
127+
return action(*this,p,c);
129128
}
130129
virtual bool isMenu() const {return false;}
131130
};
@@ -177,7 +176,7 @@ v2.1 - Add full support of SetPosition(x,y) to move the menu inside the screen (
177176
sel=(idx>=sz||idx<0)?sz-1:idx;//focus idx option if out of range then the last will be selected
178177
}
179178

180-
void activate(menuOut& p,Stream& c,bool canExit=false);
179+
promptFeedback activate(menuOut& p,Stream& c,bool canExit=false);
181180

182181
void poll(menuOut& p,Stream& c,bool canExit=false);
183182

menuFields.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,19 @@ v2.0 - Calling action on every elements
6868
else if (value>high) value=high;
6969
}
7070
//lazy drawing, we have no drawing position here... so we will ask the menu to redraw
71-
virtual void activate(menuOut& p,Stream&c,bool canExit=false) {
71+
virtual promptFeedback activate(menuOut& p,Stream&c,bool canExit=false) {
7272
if (activeNode!=this) {
73-
action(*this,p,c);
73+
if (action(*this,p,c)) return true;;
7474
ox=activeNode->ox;
7575
oy=activeNode->oy;
7676
previousMenu=(menu*)activeNode;
7777
activeNode=this;
7878
p.lastSel=-1;
7979
previousMenu->printMenu(p,previousMenu->canExit);
8080
}
81+
//printing here to solve U8GLX blank screen on field edit
8182
previousMenu->printMenu(p,previousMenu->canExit);
82-
if (!c.available()) return;
83+
if (!c.available()) return 0;
8384
if (strchr(numericChars,c.peek())) {//a numeric value was entered
8485
value=c.parseFloat();
8586
tunning=false;
@@ -104,6 +105,7 @@ v2.0 - Calling action on every elements
104105
p.lastSel=-1;
105106
previousMenu->printMenu(p,previousMenu->canExit);
106107
}
108+
return 0;
107109
}
108110
};
109111

@@ -133,9 +135,9 @@ v2.0 - Calling action on every elements
133135
menuSelect<T>(target,text,sz,data) {menuSelect<T>::sync();}
134136

135137
//ignore canExit (this exists by select), however we could use a cancel option instead of Exit
136-
void activate(menuOut& p,Stream& c,bool) {
138+
promptFeedback activate(menuOut& p,Stream& c,bool) {
137139
if (menu::activeNode!=this) {
138-
menuSelect<T>::action(*this,p,c);
140+
if (menuSelect<T>::action(*this,p,c)) return true;
139141
this->setPosition(menuNode::activeNode->ox,menuNode::activeNode->oy);
140142
this->menu::previousMenu=(menu*)menu::activeNode;
141143
menu::activeNode=this;
@@ -156,6 +158,7 @@ v2.0 - Calling action on every elements
156158
c.flush();//reset the encoder
157159
}
158160
}
161+
return 0;
159162
}
160163
};
161164
template<typename T>
@@ -165,16 +168,17 @@ v2.0 - Calling action on every elements
165168
menuToggle(const char *text,unsigned int sz,menuValue<T>* const data[],T& target):
166169
menuSelect<T>(target,text,sz,data) {menuSelect<T>::sync();}
167170

168-
void activate(menuOut& p,Stream& c,bool canExit) {
171+
promptFeedback activate(menuOut& p,Stream& c,bool canExit) {
169172
//Serial.println("entering toggle");
170-
menuSelect<T>::action(*this,p,c);
173+
if (menuSelect<T>::action(*this,p,c)) return true;
171174
/*ox=activeNode->ox;
172175
oy=activeNode->oy;*/
173176
this->menu::sel++;
174177
if (this->menu::sel>=this->menu::sz) this->menu::sel=0;
175178
p.lastSel=-1;//redraw only affected option
176179
this->menuSelect<T>::target=((menuValue<T>*)this->menu::data[menu::sel])->value;
177180
this->menu::data[this->menu::sel]->activate(p,c,true);
181+
return 0;
178182
}
179183
};
180184
#endif

0 commit comments

Comments
 (0)