Skip to content

Commit 232b8d9

Browse files
committed
allow main menus to not use Exit option
1 parent 609d23f commit 232b8d9

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

menu.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ char menu::enabledCursor='>';
99
char menu::disabledCursor='-';
1010
prompt menu::exitOption(menu::exit);
1111

12-
int menu::menuKeys(menuOut &p,Stream& c) {
12+
int menu::menuKeys(menuOut &p,Stream& c,bool canExit) {
1313
int op=-2;
1414
do {
1515
while(!c.available());// delay(20);
@@ -20,13 +20,13 @@ int menu::menuKeys(menuOut &p,Stream& c) {
2020
if (sel>0) {
2121
sel--;
2222
if (sel+1>=p.maxY) p.top=sel-p.maxY;
23-
printMenu(p);
23+
printMenu(p,canExit);
2424
}
2525
} else if (ch=='+') {
26-
if (sel<sz) {
26+
if (sel<(sz-(canExit?0:1))) {
2727
sel++;
2828
if ((sz-sel+1)>=p.maxY) p.top=sel;
29-
printMenu(p);
29+
printMenu(p,canExit);
3030
}
3131
} else if (ch==27) {
3232
op=-1;
@@ -37,23 +37,23 @@ int menu::menuKeys(menuOut &p,Stream& c) {
3737
c.read();
3838
op=sel==sz?-1:sel;
3939
}
40-
} while(op<-1||op>=sz);
40+
} while(canExit&&(op<-1||op>=sz));
4141
return op;
4242
}
4343

44-
void menu::activate(menuOut& p,Stream& c) {
44+
void menu::activate(menuOut& p,Stream& c,bool canExit) {
4545
sel=0;
4646
p.top=0;
4747
int op=-1;
4848
do {
49-
printMenu(p);
49+
printMenu(p,canExit);
5050
c.flush();//reset the encoder
5151
while(c.available()) c.read();//clean the stream
52-
op=menuKeys(p,c);
52+
op=menuKeys(p,c,canExit);
5353
if (op>=0&&op<sz) {
5454
sel=op;
5555
if (data[op]->enabled)
56-
data[op]->activate(p,c);
56+
data[op]->activate(p,c,true);
5757
p.drawn=0;//redraw menu
5858
}
5959
} while(op!=-1);

menu.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ for encoders, joysticks, keyboards or touch a stream must be made out of them
118118
virtual void print(int)=0;
119119
virtual void println(int)=0;
120120
virtual void print(prompt &o,bool selected,int idx,int posY,int width)=0;
121-
virtual void printMenu(menu&)=0;
121+
virtual void printMenu(menu&,bool drawExit)=0;
122122
};
123123

124124
////////////////////////////////////////////////////////////////////
@@ -156,7 +156,7 @@ for encoders, joysticks, keyboards or touch a stream must be made out of them
156156
prompt(const char * text,promptAction action)
157157
:text(text),action(action),enabled(true) {}
158158
virtual size_t printTo(Print& p) {p.print(text);return strlen(text);}
159-
virtual void activate(menuOut& p,Stream&c) {action(*this,p,c);}
159+
virtual void activate(menuOut& p,Stream&c,bool) {action(*this,p,c);}
160160
};
161161

162162
//a menu or sub-menu
@@ -172,10 +172,10 @@ for encoders, joysticks, keyboards or touch a stream must be made out of them
172172
prompt* const* data;
173173
menu(const char * text,int sz,prompt* const data[]):prompt(text),sz(sz),data(data),sel(0),width(16) {}
174174

175-
int menuKeys(menuOut &p,Stream& c);
176-
void printMenu(menuOut& p) {p.printMenu(*this);}
175+
int menuKeys(menuOut &p,Stream& c,bool drawExit);
176+
inline void printMenu(menuOut& p,bool drawExit) {p.printMenu(*this,drawExit);}
177177

178-
void activate(menuOut& p,Stream& c);
178+
void activate(menuOut& p,Stream& c,bool canExit=false);
179179
};
180180

181181
#endif

menuLCD.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
print(selected?(o.enabled?menu::enabledCursor:menu::disabledCursor):' ');
2121
print(o.text);
2222
}
23-
virtual void printMenu(menu& m) {
23+
virtual void printMenu(menu& m,bool drawExit) {
2424
clear();
2525
if (m.sel-top>=maxY) top=m.sel-maxY+1;//selected option outside device (bottom)
2626
else if (m.sel<top) top=m.sel;//selected option outside device (top)
@@ -30,7 +30,7 @@
3030
print(*m.data[i],i==m.sel,i+1,i-top,m.width);
3131
}
3232
}
33-
if (i-top<maxY)
33+
if (drawExit&&i-top<maxY)
3434
print(menu::exitOption,m.sel==m.sz,0,i-top,m.width);
3535
}
3636
};

menuPrint.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
print(selected?(o.enabled?menu::enabledCursor:menu::disabledCursor):' ');
2222
println(o.text);
2323
}
24-
virtual void printMenu(menu& m) {
24+
virtual void printMenu(menu& m,bool drawExit) {
2525
clear();
2626
int i=0;for(;i<m.sz;i++)
2727
print(*m.data[i],i==m.sel,i+1,i-top,m.width);
28-
print(menu::exitOption,m.sel==m.sz,0,i-top,m.width);
28+
if (drawExit) print(menu::exitOption,m.sel==m.sz,0,i-top,m.width);
2929
}
3030
};
3131

0 commit comments

Comments
 (0)