-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathitemsTemplates.hpp
235 lines (212 loc) · 7.57 KB
/
itemsTemplates.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#pragma once
#include "menuDefs.h"
////////////////////////////////////////////////////////////////////////
// template implementation
#include "menuIo.h"
#include "nav.h"
namespace Menu {
#ifdef MENU_ASYNC
template<typename T>
bool menuValue<T>::async(const char*uri,navRoot& root,idx_t lvl) {
trace(MENU_DEBUG_OUT<<(*(prompt*)this)<<" menuValue::async! lvl:"<<lvl<<" navRoot.level:"<<root.level<<" navFocus:"<<(*(prompt*)root.navFocus)<<endl);
return prompt::async(uri,root,lvl);
}
template<typename T>
const char* menuValue<T>::typeName() const {return "menuValue";}
#endif
template<typename T>
bool menuField<T>::canTune() {return !!tune();}
template<typename T>
void menuField<T>::constrainField() {target() = constrain(target(), low(), high());}
template<typename T>
void menuField<T>::clearChanged(const navNode &nav,const menuOut& out,bool sub) {
fieldBase::clearChanged(nav,out,sub);
reflex=target();
}
template<typename T>
bool menuField<T>::changed(const navNode &nav,const menuOut& out,bool sub,bool test) {
trace(if (test&&dirty) MENU_DEBUG_OUT<<"field dirty"<<endl);
trace(if (test&&(reflex!=target())) MENU_DEBUG_OUT<<"reflex!=target reflex:"<<reflex<<" target:"<<target()<<endl);
return dirty||(reflex!=target());
}
template<typename T>
idx_t menuField<T>::printReflex(menuOut& o) const {return o.print(reflex);}
template<typename T>
Used menuField<T>::printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr) {
trace(print_P(out,getText());MENU_DEBUG_OUT<<" menuField<T>::printTo "<<reflex<<endl);
reflex=target();
return fieldBase::printTo(root,sel,out,idx,len,panelNr);
}
template<typename T>
void menuField<T>::parseInput(navNode& nav,menuIn& in) {
//TODO: on a cmd based nav (not streams) this mess will be pushed to stream input only
//can not enter negative number literals by serial, using steps or web is ok
bool neg=false;
char nc=in.peek();
if (nc=='-') {
in.read();
if (!strchr(numericChars,in.peek())) {
doNav(nav,downCmd);
return;
}
// Serial.println("NEGATIVE NUMBER PARSE THEN!");
neg=true;
}
if (neg||strchr(numericChars,nc)) {//a numeric value was entered
if (in.numValueInput) {
target()=(T)((neg?-1:1)*in.parseFloat());//TODO: use template specialization and proper convertion
tunning=true;
doNav(nav,enterCmd);
} else doNav(nav,idxCmd);
} else doNav(nav,nav.navKeys(in.read()));
}
#ifdef MENU_ASYNC
template<typename T>
void menuField<T>::printValue(menuOut& o) const {o.print(reflex);}
template<typename T>
void menuField<T>::printHigh(menuOut& o) const {o.print(high());}
template<typename T>
void menuField<T>::printLow(menuOut& o) const {o.print(low());}
template<typename T>
void menuField<T>::printStep(menuOut& o) const {o.print(step());}
template<typename T>
void menuField<T>::printTune(menuOut& o) const {o.print(tune());}
template<typename T>
const char* menuField<T>::typeName() const {return typeStr<T>();};
#endif
template<typename T>
void menuField<T>::stepit(int dir) {
dir*=options->invertFieldKeys?-1:1;
T thisstep = tunning?tune():step();
dirty=true;
//by default they are inverted.. now buttons and joystick have to flip them
if (dir > 0) {
if ((high()-target()) < thisstep)
target() = canWrap()?low():high();
else
target() += thisstep;
} else {
if ((target()-low()) < thisstep)
target() = canWrap()?high():low();
else
target() -= thisstep;
}
}
template<uint8_t dps>
template<typename T>
idx_t decPlaces<dps>::menuField<T>::printReflex(menuOut& o) const {return o.print(Menu::menuField<T>::reflex,dps);}
template<typename T>
Used choose<T>::printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr) {
bool ed=this==root.navFocus;
return ed?
prompt::printTo(root,sel,out,idx,len,panelNr)
:menuVariantBase::printTo(root,sel,out,idx,len,panelNr);
}
template<typename T>
Used toggle<T>::printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr) {
return menuVariantBase::togglePrintTo(root,sel,out,idx,len,panelNr);
}
#ifdef MENU_ASYNC
template<typename T>
const char* toggle<T>::typeName() const {return "toggle";}
template<typename T>
bool toggle<T>::async(const char*uri,navRoot& root,idx_t lvl) {
_trace(MENU_DEBUG_OUT<<(*(prompt*)this)<<" toggle::async! uri:"<<uri<<endl);
if(uri[0]) {
_trace(Serial<<"selecting value by index!"<<endl);
idx_t n=menuNode::parseUriNode(uri);
_trace(MENU_DEBUG_OUT<<"n:"<<n<<" sel:"<<root.path[lvl].sel<<endl);
menuVariant<T>::sync(n);//sync to index n!
// root.node().event(root.useUpdateEvent?updateEvent:enterEvent);
return true;
}
//if not by index then do the toggle
_trace(Serial<<"toggle proceed..."<<endl);
// root.doNav(navCmd(enterCmd));
// return true;
// bool r=prompt::async(uri,root,lvl);
// root.node().event(root.useUpdateEvent?updateEvent:enterEvent);
// return r;
return prompt::async(uri,root,lvl);
}
#endif
template<typename T>
result toggle<T>::sysHandler(SYS_FUNC_PARAMS) {
_trace(Serial<<"toggle sysHandler!"<<endl;);
switch(event) {
case activateEvent: {
idx_t at=menuVariant<T>::sync();
assert(at!=-1);
at++;
if (at>=menuNode::sz()) at=0;
menuVariant<T>::sync(at);
prompt::dirty=true;
(menuNode::operator[](at))(FUNC_VALUES);
}
default:
return proceed;
}
}
#ifdef MENU_FMT_WRAPS
template<typename T>
classes toggle<T>::type() const {return toggleClass;}
#endif
template<typename T>
idx_t menuVariant<T>::sync() {
for(idx_t i=0;i<sz();i++)
if (((menuValue<T>*)&operator[](i))->target()==target()) return i;
#ifdef MENU_DEBUG
MENU_DEBUG_OUT.print("value out of range ");
MENU_DEBUG_OUT.println(target());MENU_DEBUG_OUT.flush();
assert(false);
#endif
return -1;
}
template<typename T>
idx_t menuVariant<T>::sync(idx_t i) {
#ifdef MENU_DEBUG
if (!(i>=0&&i<sz())){
print_P(MENU_DEBUG_OUT,getText());
MENU_DEBUG_OUT.print(" : value out of range ");
MENU_DEBUG_OUT.println(i);
}
assert(i>=0&&i<sz());
#endif
if (i!=reflex) dirty=true;
reflex=i;
target()=((menuValue<T>*)&operator[](i))->target();
return i;
}
template<typename T>
bool menuVariant<T>::changed(const navNode &nav,const menuOut& out,bool sub,bool test) {
return dirty||((menuValue<T>*)&operator[](reflex))->target()!=target();
}
#ifdef MENU_ASYNC
template<typename T>
idx_t menuVariant<T>::selected() const {return reflex;}
template<typename T>
const char* menuVariant<T>::typeName() const {return "menuVariant";}
#endif
template<typename T>
result choose<T>::sysHandler(SYS_FUNC_PARAMS) {
switch(event) {
case updateEvent:
case enterEvent:
nav.sel=menuVariant<T>::sync();
default:
return proceed;
}
}
template<typename T>
bool choose<T>::changed(const navNode &nav,const menuOut& out,bool sub,bool test) {
return menuVariant<T>::changed(nav,out)||menuNode::changed(nav,out);
}
#ifdef MENU_FMT_WRAPS
template<typename T>
classes choose<T>::type() const {return chooseClass;}
#endif
#ifdef MENU_ASYNC
template<typename T>
const char* choose<T>::typeName() const {return "choose";}
#endif
}//namespace Menu