widgets.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /*
  2. * widgets.c
  3. *
  4. * Created on: Jan 12, 2021
  5. * Author: David Original work by Jose (PTDreamer), 2017
  6. */
  7. #include "widgets.h"
  8. #include "screen.h"
  9. #include "oled.h"
  10. #include "gui.h"
  11. char displayString[32];
  12. displayOnly_widget_t * extractDisplayPartFromWidget(widget_t *w) {
  13. comboBox_widget_t* combo;
  14. if(!w)
  15. return NULL;
  16. switch (w->type) {
  17. case widget_display:
  18. return (displayOnly_widget_t*)w->content;
  19. case widget_editable:
  20. case widget_multi_option:
  21. return &((editable_widget_t*)w->content)->inputData;
  22. case widget_combo:
  23. combo = (comboBox_widget_t*)w->content;
  24. if((combo->currentItem) && ((combo->currentItem->type==combo_Editable)||(combo->currentItem->type==combo_MultiOption))){
  25. return &((comboBox_widget_t*)w->content)->currentItem->widget->inputData;
  26. }
  27. default:
  28. return NULL;
  29. break;
  30. }
  31. }
  32. editable_widget_t * extractEditablePartFromWidget(widget_t *w) {
  33. comboBox_widget_t* combo;
  34. if(!w)
  35. return NULL;
  36. switch (w->type) {
  37. case widget_editable:
  38. case widget_multi_option:
  39. return (editable_widget_t*)w->content;
  40. case widget_combo:
  41. combo = (comboBox_widget_t*)w->content;
  42. if((combo->currentItem) && ((combo->currentItem->type==combo_Editable)||(combo->currentItem->type==combo_MultiOption))){
  43. return ((comboBox_widget_t*)w->content)->currentItem->widget;
  44. }
  45. default:
  46. return NULL;
  47. break;
  48. }
  49. }
  50. selectable_widget_t * extractSelectablePartFromWidget(widget_t *w) {
  51. if(!w)
  52. return NULL;
  53. switch (w->type) {
  54. case widget_editable:
  55. case widget_multi_option:
  56. return &((editable_widget_t*)w->content)->selectable;
  57. case widget_button:
  58. case widget_bmp_button:
  59. return &((button_widget_t*)w->content)->selectable;
  60. case widget_combo:
  61. return &((comboBox_widget_t*)w->content)->selectable;
  62. default:
  63. return NULL;
  64. break;
  65. }
  66. }
  67. void widgetDefaultsInit(widget_t *w, widgetType t, void* content){
  68. if(!w || !content){ return; }
  69. w->type = t;
  70. w->draw = &default_widgetDraw;
  71. w->update = &default_widgetUpdate;
  72. w->enabled = 1;
  73. w->frameType = frame_auto;
  74. w->refresh = refresh_idle;
  75. w->radius=-1;
  76. w->posX = 0;
  77. w->posY = 0;
  78. w->content = content;
  79. w->next_widget = NULL;
  80. selectable_widget_t *sel = extractSelectablePartFromWidget(w);
  81. displayOnly_widget_t *dis = extractDisplayPartFromWidget(w);
  82. editable_widget_t *edit = extractEditablePartFromWidget(w);
  83. if(sel) {
  84. sel->previous_state = widget_idle;
  85. sel->state = widget_idle;
  86. sel->tab = 0;
  87. sel->processInput = &default_widgetProcessInput;
  88. sel->longPressAction = NULL;
  89. }
  90. if(dis){
  91. dis->font = default_font;
  92. dis->getData = NULL;
  93. dis->reservedChars = 0;
  94. dis->number_of_dec = 0;
  95. dis->type = field_int32;
  96. dis->displayString=displayString;
  97. dis->endString=NULL;
  98. dis->stringStart=0;
  99. dis->last_value = 0;
  100. dis->textAlign = align_center;
  101. dis->dispAlign = align_disabled;
  102. }
  103. if(edit){
  104. edit->step=1;
  105. edit->big_step=1;
  106. edit->current_edit=0;
  107. edit->max_value=2147483647;
  108. edit->min_value=0;
  109. edit->setData=NULL;
  110. }
  111. switch (t) {
  112. case widget_multi_option:
  113. edit->max_value = 255;
  114. edit->numberOfOptions = 0;
  115. edit->options = NULL;
  116. break;
  117. case widget_combo:
  118. {
  119. comboBox_widget_t* combo = content;
  120. combo->currentItem = NULL;
  121. w->frameType = frame_combo;
  122. combo->first = NULL;
  123. w->draw = &comboBoxDraw;
  124. combo->currentScroll = 0;
  125. combo->selectable.processInput = &comboBoxProcessInput;
  126. w->parent->current_widget = w;
  127. combo->font = default_font;
  128. break;
  129. }
  130. case widget_button:
  131. {
  132. button_widget_t* button = (button_widget_t*)content;
  133. button->action = NULL;
  134. button->displayString = NULL;
  135. button->xbm = NULL;
  136. button->last_xbm = NULL;
  137. button->font = default_font;
  138. break;
  139. }
  140. default:
  141. break;
  142. }
  143. sel = extractSelectablePartFromWidget(w);
  144. dis = extractDisplayPartFromWidget(w);
  145. edit = extractEditablePartFromWidget(w);
  146. }
  147. void editableDefaultsInit(editable_widget_t* editable, widgetType type){
  148. widget_t w;
  149. widgetDefaultsInit(&w, type, editable);
  150. }
  151. static void insertDot(char *str, uint8_t dec) {
  152. for(int x = strlen(str); x > (int)strlen(str) - (int)dec - 2; --x) {
  153. str[x + 1] = str[x];
  154. }
  155. str[strlen(str) - dec - 1] = '.';
  156. }
  157. void widgetEnable(widget_t* w){
  158. if(w && !w->enabled){
  159. w->enabled = 1;
  160. w->refresh = refresh_triggered;
  161. }
  162. }
  163. void widgetDisable(widget_t* w){
  164. if(w && w->enabled){
  165. w->enabled = 0;
  166. widgetClearField(w);
  167. }
  168. }
  169. void default_widgetUpdate(widget_t *w) {
  170. if(!w){ return ; }
  171. if(!w->enabled){
  172. w->refresh=refresh_idle;
  173. return;
  174. }
  175. button_widget_t* button;
  176. bmp_widget_t* bmp;
  177. int32_t val_ui=0;
  178. displayOnly_widget_t* dis = extractDisplayPartFromWidget(w);
  179. editable_widget_t *edit = extractEditablePartFromWidget(w);
  180. switch(w->type){
  181. case widget_bmp_button:
  182. button = ((button_widget_t*)w->content);
  183. if(!button->xbm){
  184. widgetDisable(w);
  185. return;
  186. }
  187. break;
  188. case widget_bmp:
  189. bmp = ((bmp_widget_t*)w->content);
  190. if(!bmp->xbm){
  191. widgetDisable(w);
  192. return;
  193. }
  194. break;
  195. case widget_button:
  196. button = ((button_widget_t*)w->content);
  197. if(!button->displayString){
  198. widgetDisable(w);
  199. return;
  200. }
  201. val_ui=strsum(button->displayString); // Get string sum
  202. break;
  203. case widget_multi_option:
  204. if(!dis){
  205. widgetDisable(w);
  206. return;
  207. };
  208. val_ui=strsum(edit->options[*(uint8_t*)dis->getData()]); // Get string sum
  209. break;
  210. case widget_display:
  211. case widget_editable:
  212. if(!dis){
  213. widgetDisable(w);
  214. return;
  215. }
  216. if(dis->type == field_string){
  217. val_ui=strsum(dis->getData()); // Get string sum
  218. break;
  219. }
  220. else{
  221. val_ui = *(int32_t*)dis->getData();
  222. uint8_t sum = strsum(dis->endString);
  223. widgetDetectChange(w,(val_ui + sum)); // Check for changes in value and endString
  224. return;
  225. }
  226. break;
  227. default:
  228. return;
  229. }
  230. widgetDetectChange(w,val_ui);
  231. }
  232. void widgetDetectChange(widget_t* w, int32_t val){
  233. displayOnly_widget_t* dis;
  234. bmp_widget_t* bmp;
  235. button_widget_t* button;
  236. bool refresh=0;
  237. switch(w->type){
  238. case widget_display:
  239. case widget_editable:
  240. case widget_multi_option:
  241. dis=extractDisplayPartFromWidget(w);
  242. if(dis->last_value!=val){
  243. dis->last_value=val;
  244. refresh=1;
  245. }
  246. break;
  247. case widget_button:
  248. button = (button_widget_t*)w->content;
  249. if(button->last_value != val){
  250. button->last_value = val;
  251. refresh=1;
  252. }
  253. break;
  254. case widget_bmp_button:
  255. button = (button_widget_t*)w->content;
  256. if(button->last_xbm != button->xbm){
  257. button->last_xbm = button->xbm;
  258. refresh=1;
  259. }
  260. break;
  261. case widget_bmp:
  262. bmp = (bmp_widget_t*)w->content;
  263. if(bmp->last_xbm != bmp->xbm){
  264. bmp->last_xbm = bmp->xbm;
  265. refresh=1;
  266. }
  267. break;
  268. default:
  269. break;
  270. }
  271. if(refresh && w->refresh==refresh_idle){
  272. w->refresh=refresh_triggered;
  273. }
  274. }
  275. // Clear widget field before drawing widget with new data
  276. void widgetClearField(widget_t* w){
  277. if(!w){ return; }
  278. selectable_widget_t* sel = extractSelectablePartFromWidget(w);
  279. displayOnly_widget_t* dis = extractDisplayPartFromWidget(w);
  280. button_widget_t* button;
  281. bmp_widget_t* bmp;
  282. uint8_t cHeight = u8g2_GetMaxCharHeight(&u8g2);
  283. if(dis && dis->font==u8g2_font_iron){ cHeight+=3; } // TODO this font reports lower height, needs to be manually adjusted
  284. uint8_t r;
  285. if(w->frameType!=frame_combo){
  286. u8g2_SetDrawColor(&u8g2, BLACK);
  287. if(w->parent->refresh < screenRefresh_eraseNow ){
  288. switch((uint8_t)w->type){
  289. case widget_bmp:
  290. bmp = (bmp_widget_t*)w->content;
  291. u8g2_DrawBox(&u8g2, w->posX, w->posY, bmp->xbm[0], bmp->xbm[1]);
  292. break;
  293. case widget_bmp_button:
  294. button = (button_widget_t*)w->content;
  295. u8g2_DrawBox(&u8g2, w->posX, w->posY, button->xbm[0]+4, button->xbm[1]+4);
  296. break;
  297. case widget_button:
  298. button = (button_widget_t*)w->content;
  299. if(button->font==u8g2_font_iron){ cHeight+=3; } // TODO this font reports lower height, needs to be manually adjusted
  300. u8g2_DrawBox(&u8g2, w->posX, w->posY, w->width, cHeight+1);
  301. break;
  302. case widget_display:
  303. u8g2_DrawBox(&u8g2, w->posX, w->posY, w->width, cHeight+1); // Draw black square to erase previous data
  304. break;
  305. case widget_editable:
  306. case widget_multi_option:
  307. if(dis->font==u8g2_font_iron){ cHeight+=3; } // TODO this font reports lower height, needs to be manually adjusted
  308. u8g2_DrawBox(&u8g2, w->posX ,w->posY, w->width, cHeight+1);
  309. break;
  310. }
  311. }
  312. if( (sel && (sel->state==widget_edit) && (dis->type!=field_string) && (w->frameType==frame_auto)) || (w->frameType==frame_solid) ){
  313. u8g2_SetDrawColor(&u8g2, WHITE);
  314. if(w->radius<0){
  315. r=(cHeight-1)/2;
  316. }
  317. else{
  318. r = w->radius;
  319. }
  320. if(sel){
  321. u8g2_DrawRBox(&u8g2, w->posX ,w->posY, w->width, cHeight+1,r );
  322. }
  323. else{
  324. u8g2_DrawRBox(&u8g2, w->posX ,w->posY, w->width+2, cHeight+1, r);
  325. }
  326. u8g2_SetDrawColor(&u8g2, XOR);
  327. return;
  328. }
  329. u8g2_SetDrawColor(&u8g2, WHITE);
  330. }
  331. else{ // In combo
  332. if(sel->state==widget_edit){ // Being edited
  333. u8g2_SetDrawColor(&u8g2, XOR); // Set XOR color
  334. }
  335. else{ // Not editing
  336. u8g2_SetDrawColor(&u8g2, WHITE); // Set white color
  337. }
  338. }
  339. }
  340. void widgetAlign(widget_t* w){
  341. if(!w || w->frameType==frame_combo){ return; }
  342. uint8_t strWidth=0, stringStart=0, textAlign=align_disabled, dispAlign=align_disabled;
  343. displayOnly_widget_t *dis=extractDisplayPartFromWidget(w);
  344. selectable_widget_t *sel=extractSelectablePartFromWidget(w);
  345. editable_widget_t *edit = extractEditablePartFromWidget(w);
  346. button_widget_t* button = NULL;
  347. switch(w->type){
  348. case widget_button:
  349. button = (button_widget_t *)w->content;
  350. strWidth=u8g2_GetStrWidth(&u8g2, button->displayString);
  351. break;
  352. case widget_display:
  353. case widget_editable:
  354. if(dis->type == field_string){
  355. strWidth=u8g2_GetStrWidth(&u8g2, (char *)dis->getData());
  356. }
  357. else{
  358. strWidth=u8g2_GetStrWidth(&u8g2, displayString);
  359. }
  360. break;
  361. case widget_multi_option:
  362. strWidth=u8g2_GetStrWidth(&u8g2, (char *)edit->options[*(uint8_t*)dis->getData()]);
  363. break;
  364. default:
  365. return;
  366. }
  367. if(!strWidth){ // Empty widget, wrong settings
  368. widgetDisable(w); // Disable widget
  369. return;
  370. }
  371. if(dis){
  372. textAlign = dis->textAlign;
  373. dispAlign = dis->dispAlign;
  374. }
  375. else if(w->type == widget_button){
  376. textAlign = align_center;
  377. dispAlign = align_disabled;
  378. }
  379. else{
  380. return;
  381. }
  382. if(sel && (w->frameType!=frame_disabled)){ // If selectable, extra space for not overlapping the frame
  383. if(w->width < strWidth+7){ // If width too small
  384. if(strWidth+7<OledWidth){ // If fits oled size
  385. w->width=strWidth+7; // Use width from str width
  386. }
  387. else{
  388. w->width = strWidth; // Else, don't add extra space
  389. }
  390. }
  391. }
  392. else{
  393. if(w->width < strWidth){ // If width too small
  394. w->width=strWidth; // Use width from str width
  395. }
  396. }
  397. if(w->width > OledWidth){
  398. if(textAlign != align_disabled)
  399. textAlign = align_left;
  400. if(dispAlign != align_disabled)
  401. dispAlign = align_left;
  402. }
  403. switch(dispAlign){
  404. case align_disabled:
  405. break;
  406. case align_center:
  407. w->posX =(OledWidth - w->width)/2;
  408. break;
  409. case align_right:
  410. w->posX = OledWidth - w->width;
  411. break;
  412. case align_left:
  413. default:
  414. w->posX = 0;
  415. break;
  416. }
  417. switch(textAlign){
  418. case align_disabled:
  419. break;
  420. case align_center:
  421. stringStart = (w->posX + ((w->width-strWidth)/2));
  422. break;
  423. case align_right:
  424. stringStart =(w->posX + (w->width-strWidth));
  425. if(sel){
  426. if(stringStart >= 3){
  427. stringStart -= 3;
  428. }
  429. }
  430. break;
  431. case align_left:
  432. default:
  433. if(sel && ((w->posX+3)<=OledWidth)){
  434. stringStart=w->posX+3;
  435. }
  436. else{
  437. stringStart = w->posX;
  438. }
  439. break;
  440. }
  441. if(w->type == widget_button){
  442. button->stringStart=stringStart;
  443. }
  444. else if(dis){
  445. dis->stringStart = stringStart;
  446. }
  447. }
  448. void default_widgetDraw(widget_t *w) {
  449. if(!w || !w->enabled){ return; }
  450. bool frameDraw = 0;
  451. bool frameColor = BLACK;
  452. displayOnly_widget_t* dis = extractDisplayPartFromWidget(w);
  453. editable_widget_t* edit = extractEditablePartFromWidget(w);
  454. selectable_widget_t* sel = extractSelectablePartFromWidget(w);
  455. button_widget_t* button = NULL;
  456. bmp_widget_t* bmp = NULL;
  457. const uint8_t* font = NULL;
  458. uint8_t refresh = w->refresh | w->parent->refresh;
  459. uint8_t cHeight = 0;
  460. if( dis ){
  461. u8g2_SetFont(&u8g2, dis->font);
  462. cHeight = u8g2_GetMaxCharHeight(&u8g2);
  463. font = dis->font;
  464. }
  465. else if((w->type == widget_button)||(w->type == widget_bmp_button)){
  466. button = (button_widget_t*)w->content;
  467. if(w->type == widget_button){
  468. u8g2_SetFont(&u8g2, button->font);
  469. cHeight = u8g2_GetMaxCharHeight(&u8g2);
  470. font = button->font;
  471. }
  472. }
  473. else if(w->type == widget_bmp){
  474. bmp = (bmp_widget_t*)w->content;
  475. }
  476. if(w->frameType==frame_outline){
  477. frameDraw=1;
  478. frameColor = WHITE;
  479. }
  480. else if(w->frameType==frame_auto){
  481. if(sel) {
  482. if(refresh==refresh_idle){ // If not forced refresh, check for changes
  483. switch (sel->state) {
  484. case widget_selected:
  485. if(sel->previous_state != widget_selected){
  486. frameDraw=1;
  487. frameColor = WHITE;
  488. if(sel->previous_state == widget_edit){
  489. refresh=refresh_triggered;
  490. }
  491. }
  492. break;
  493. case widget_idle:
  494. if(sel->previous_state != widget_idle){
  495. frameDraw=1;
  496. }
  497. break;
  498. case widget_edit:
  499. if(sel->previous_state != widget_edit){
  500. refresh=refresh_triggered;
  501. }
  502. break;
  503. default:
  504. return;
  505. }
  506. }
  507. else{ // Else, redraw frames
  508. switch (sel->state) {
  509. case widget_selected:
  510. frameDraw=1;
  511. frameColor = WHITE;
  512. break;
  513. case widget_idle:
  514. case widget_edit:
  515. break;
  516. default:
  517. return;
  518. }
  519. }
  520. }
  521. }
  522. if(refresh){
  523. if(dis && dis->type==field_int32){
  524. int32_t val_ui = *(int32_t*)dis->getData();
  525. uint8_t decimals = dis->number_of_dec+1;
  526. if(val_ui<0){
  527. decimals++;
  528. }
  529. if(decimals>10){ decimals=10; }
  530. snprintf(dis->displayString, dis->reservedChars+1, "%0*ld", decimals, (int32_t)val_ui); // Convert value into string
  531. uint8_t dispLen=strlen(dis->displayString);
  532. uint8_t endLen=strlen(dis->endString);
  533. if(dis->number_of_dec){ // If there're decimals
  534. if(dis->reservedChars >= (dispLen+1)){ // Ensure there's enough space in the string for adding the decimal point
  535. insertDot(dis->displayString, dis->number_of_dec); // Insert decimal dot
  536. }
  537. }
  538. if(dis->reservedChars >= (dispLen+endLen)){ // Ensure there's enough space in the string for adding the end String
  539. strcat(dis->displayString, dis->endString); // Append endString
  540. }
  541. dis->displayString[dis->reservedChars]=0; // Ensure last string char is 0
  542. }
  543. widgetAlign(w); // Align
  544. widgetClearField(w); // Clear old field
  545. if(w->refresh==refresh_triggered){
  546. w->refresh=refresh_idle;
  547. }
  548. switch(w->type){
  549. case widget_bmp:
  550. u8g2_SetDrawColor(&u8g2, WHITE);
  551. u8g2_DrawXBMP(&u8g2, w->posX, w->posY, bmp->xbm[0], bmp->xbm[1], &bmp->xbm[2]);
  552. return;
  553. case widget_bmp_button:
  554. u8g2_SetDrawColor(&u8g2, WHITE);
  555. u8g2_DrawXBMP(&u8g2, w->posX+2, w->posY+2, button->xbm[0], button->xbm[1], &button->xbm[2]);
  556. break;
  557. case widget_button:
  558. u8g2_DrawStr(&u8g2, button->stringStart, w->posY+2, button->displayString);
  559. break;
  560. case widget_display:
  561. if(dis->type == field_string){
  562. u8g2_DrawStr(&u8g2, dis->stringStart, w->posY, dis->getData());
  563. }
  564. else{
  565. u8g2_DrawStr(&u8g2, dis->stringStart, w->posY, dis->displayString);
  566. }
  567. break;
  568. case widget_editable:
  569. if(dis->type == field_string){
  570. u8g2_DrawStr(&u8g2, dis->stringStart, w->posY+2, dis->getData());
  571. if(sel->state == widget_edit){
  572. char t[20];
  573. strcpy(t,dis->getData());
  574. t[edit->current_edit+1]=0;
  575. uint8_t x2=u8g2_GetStrWidth(&u8g2, t);
  576. t[edit->current_edit]=0;
  577. uint8_t x1=u8g2_GetStrWidth(&u8g2, t);
  578. u8g2_SetDrawColor(&u8g2, BLACK);
  579. u8g2_DrawBox(&u8g2, dis->stringStart, w->posY+ cHeight, w->width, 2);
  580. u8g2_SetDrawColor(&u8g2, WHITE);
  581. u8g2_DrawBox(&u8g2, dis->stringStart+x1, w->posY+ cHeight, x2-x1, 2);
  582. }
  583. else if(sel->previous_state == widget_edit){
  584. u8g2_SetDrawColor(&u8g2, BLACK);
  585. u8g2_DrawBox(&u8g2, dis->stringStart, w->posY+ cHeight, w->width, 2);
  586. }
  587. }
  588. else{
  589. u8g2_DrawStr(&u8g2,dis->stringStart, w->posY+2, dis->displayString);
  590. }
  591. break;
  592. case widget_multi_option:
  593. u8g2_DrawStr(&u8g2, dis->stringStart, w->posY+2, edit->options[*(uint8_t*)dis->getData()]);// Draw string
  594. break;
  595. default:
  596. return;
  597. }
  598. }
  599. if(sel) {
  600. sel->previous_state = sel->state;
  601. }
  602. if(frameDraw || w->frameType==frame_outline){
  603. if(font==u8g2_font_iron){ cHeight+=3; }
  604. switch(w->type){
  605. case widget_bmp_button:
  606. u8g2_SetDrawColor(&u8g2, frameColor);
  607. u8g2_DrawRFrame(&u8g2, w->posX, w->posY, button->xbm[0]+4, button->xbm[1]+4, 4);
  608. break;
  609. case widget_editable:
  610. case widget_button:
  611. case widget_multi_option:
  612. u8g2_SetDrawColor(&u8g2, frameColor);
  613. uint8_t r;
  614. if(w->radius<0){
  615. r=(cHeight-1)/2;
  616. }
  617. else{
  618. r = w->radius;
  619. }
  620. u8g2_DrawRFrame(&u8g2, w->posX ,w->posY, w->width, cHeight+1, r);
  621. default:
  622. break;
  623. }
  624. }
  625. u8g2_SetDrawColor(&u8g2, WHITE);
  626. }
  627. void comboBoxDraw(widget_t *w) {
  628. uint16_t yDim = OledHeight - w->posY;
  629. uint8_t height;
  630. int8_t frameY=0;
  631. int8_t posY;;
  632. uint8_t drawFrame=1;
  633. comboBox_widget_t* combo = (comboBox_widget_t*)w->content;
  634. comboBox_item_t *item = combo->first;
  635. uint8_t scroll = 0;
  636. uint8_t r;
  637. if(!w || ((w->refresh==refresh_idle) && (w->parent->refresh==screenRefresh_idle))){
  638. return;
  639. }
  640. if(w->refresh==refresh_triggered){
  641. w->refresh=refresh_idle;
  642. }
  643. if(w->parent->refresh < screenRefresh_eraseNow){ // If screen not erased already
  644. w->parent->refresh = screenRefresh_alreadyErased;
  645. FillBuffer(BLACK, fill_dma); // Erase fast using dma
  646. }
  647. if(!item){ return; } // Return if null item
  648. u8g2_SetFont(&u8g2, combo->font);
  649. height= u8g2_GetMaxCharHeight(&u8g2)+1; // +1 to allow separation between combobox items
  650. if(w->radius<0){
  651. r=(height-1)/2;
  652. }
  653. else{
  654. r = w->radius;
  655. }
  656. while(scroll < combo->currentScroll) {
  657. if(!item->next_item)
  658. break;
  659. item = item->next_item;
  660. if(item->enabled)
  661. ++scroll;
  662. }
  663. for(uint8_t y = 0; y < yDim / height; ++y) {
  664. // Store Y position
  665. if(item == combo->currentItem) { // If the current combo item is selected
  666. frameY=y * height + w->posY;
  667. }
  668. if ((item->type==combo_Screen)||(item->type==combo_Action)){ // If screen or action item, just draw the label
  669. u8g2_SetDrawColor(&u8g2, WHITE);
  670. if(item->dispAlign==align_left){
  671. u8g2_DrawStr(&u8g2, 4, y * height + w->posY +2, item->text);
  672. }
  673. else{
  674. uint8_t len = u8g2_GetStrWidth(&u8g2, item->text);
  675. if(item->dispAlign==align_right){
  676. u8g2_DrawStr(&u8g2, OledWidth-3-len, y * height + w->posY +2, item->text);
  677. }
  678. else{
  679. u8g2_DrawStr(&u8g2, (OledWidth-1-len)/2, y * height + w->posY +2, item->text);
  680. }
  681. }
  682. }
  683. else if ((item->type==combo_Editable)||(item->type==combo_MultiOption)){
  684. editable_widget_t* edit = item->widget;
  685. selectable_widget_t *sel = &edit->selectable;
  686. displayOnly_widget_t* dis = &edit->inputData;
  687. default_widgetUpdate(w);
  688. uint8_t len=0;
  689. if(sel->state==widget_edit){ // If edit mode
  690. drawFrame=0;
  691. u8g2_SetDrawColor(&u8g2, WHITE);
  692. u8g2_DrawRBox(&u8g2, 0, frameY, OledWidth, height, r);
  693. u8g2_SetDrawColor(&u8g2, XOR);
  694. }
  695. else{
  696. u8g2_SetDrawColor(&u8g2, WHITE);
  697. }
  698. if(item->type==combo_MultiOption){
  699. len = u8g2_GetStrWidth(&u8g2,edit->options[*(uint8_t*)dis->getData()]);
  700. }
  701. else if(item->type==combo_Editable){
  702. int32_t val_ui = *(int32_t*)dis->getData(); // Get data
  703. uint8_t decimals = dis->number_of_dec+1; // Load decimal count
  704. if(val_ui<0){ // If negative, add a decimal (decimals are just used as min char output in sprintf)
  705. decimals++;
  706. }
  707. if(decimals>10){ decimals=10; } // Limit max decimals
  708. snprintf(dis->displayString, dis->reservedChars+1, "%0*ld", decimals, (int32_t)val_ui); // Convert value into string
  709. uint8_t dispLen=strlen(dis->displayString); // Get string len
  710. uint8_t endLen=strlen(dis->endString); // Get endStr len
  711. if(dis->number_of_dec){ // If there're decimals
  712. if(dis->reservedChars >= (dispLen+1)){ // Ensure there's enough space in the string for adding the decimal point
  713. insertDot(dis->displayString, dis->number_of_dec); // Insert decimal dot
  714. }
  715. }
  716. if(dis->reservedChars >= (dispLen+endLen)){ // Ensure there's enough space in the string for adding the end String
  717. strcat(dis->displayString, dis->endString); // Append endString
  718. }
  719. dis->displayString[dis->reservedChars]=0; // Ensure last string char is 0
  720. len=u8g2_GetStrWidth(&u8g2,dis->displayString);
  721. }
  722. posY = y * height + w->posY; // Set widget Ypos same as the current combo option
  723. dis->stringStart = OledWidth-len-5; // Align to the left measuring actual string width
  724. if(item->type==combo_Editable){
  725. u8g2_DrawStr(&u8g2,dis->stringStart, posY+2, dis->displayString); // Draw widget data
  726. }
  727. else{
  728. u8g2_DrawStr(&u8g2,dis->stringStart, posY+2, edit->options[*(uint8_t*)dis->getData()]);
  729. }
  730. u8g2_DrawStr(&u8g2, 4, y * height + w->posY +2, item->text); // Draw the combo item label
  731. }
  732. do {
  733. item = item->next_item;
  734. }while(item && !item->enabled);
  735. if(!item){
  736. break;
  737. }
  738. }
  739. if(drawFrame){
  740. u8g2_SetDrawColor(&u8g2, WHITE);
  741. u8g2_DrawRFrame(&u8g2, 0, frameY, OledWidth, height, r);
  742. }
  743. return;
  744. }
  745. uint8_t comboItemToIndex(widget_t *combo, comboBox_item_t *item) {
  746. if(!combo || !item){ return 0; }
  747. uint8_t index = 0;
  748. comboBox_item_t *i = ((comboBox_widget_t*)combo->content)->first;
  749. while(i && i != item) {
  750. i = i->next_item;
  751. if(i->enabled)
  752. ++ index;
  753. }
  754. return index;
  755. }
  756. int comboBoxProcessInput(widget_t *w, RE_Rotation_t input, RE_State_t *state) {
  757. comboBox_widget_t* combo = (comboBox_widget_t*)w->content;
  758. if(!w || (input == Rotate_Nothing)){
  759. return -1;
  760. }
  761. u8g2_SetFont(&u8g2, combo->font);
  762. uint8_t firstIndex = combo->currentScroll;
  763. uint16_t yDim = OledHeight - w->posY;
  764. uint16_t height = u8g2_GetMaxCharHeight(&u8g2)+1;
  765. uint8_t maxIndex = yDim / height;
  766. uint8_t lastIndex = combo->currentScroll + maxIndex -1;
  767. selectable_widget_t *sel;
  768. if(w->refresh==refresh_idle){
  769. w->refresh=refresh_triggered; // Update in combo erases whole screen (to avoid possible leftovers)
  770. }
  771. if ((combo->currentItem->type==combo_Editable)||(combo->currentItem->type==combo_MultiOption)){ // If combo editable type
  772. sel = &combo->currentItem->widget->selectable; // Get selectable data
  773. }
  774. if((input == Click)||(input == LongClick)){ // If clicked
  775. if ((combo->currentItem->type==combo_Editable)||(combo->currentItem->type==combo_MultiOption)){ // If combo editable type
  776. if(sel->state==widget_idle){ // If widget idle
  777. sel->state=widget_edit; // Change to edit
  778. }
  779. else if(sel->state==widget_edit){ // If widget in edit mode
  780. sel->state=widget_idle; // Change to idle
  781. }
  782. return -1; // Do nothing else
  783. }
  784. if (combo->currentItem->type==combo_Action){ // If combo Action type
  785. return combo->currentItem->action(); // Process action
  786. }
  787. else if (combo->currentItem->type==combo_Screen){ // If combo screen type
  788. return combo->currentItem->action_screen; // Return screen index
  789. }
  790. }
  791. if((input == Rotate_Increment) || (input == Rotate_Decrement) || // If rotation data
  792. (input == Rotate_Increment_while_click) || (input == Rotate_Decrement_while_click)){
  793. if ((combo->currentItem->type==combo_Editable)||(combo->currentItem->type==combo_MultiOption)){ // If combo option type
  794. if(sel->state==widget_edit){ // If widget in edit mode
  795. default_widgetProcessInput(w, input, state); // Process widget
  796. return -1; // Return
  797. }
  798. }
  799. if(input == Rotate_Increment) {
  800. //Set comboBox item to next comboBox
  801. comboBox_item_t *current = combo->currentItem->next_item;
  802. // if comboBox is disabled, skip. While comboBox still valid
  803. while(current && !current->enabled) {
  804. current = current->next_item;
  805. }
  806. // If comboBox valid(didn't reach end)
  807. if(current) {
  808. combo->currentItem = current;
  809. uint8_t index = comboItemToIndex(w, current);
  810. if(index > lastIndex)
  811. ++combo->currentScroll;
  812. }
  813. }
  814. else if(input == Rotate_Decrement) {
  815. comboBox_item_t *current = NULL;
  816. // If comboBox is not the first element
  817. if(combo->currentItem != combo->first){
  818. do {
  819. current = combo->first;
  820. while(current->next_item != combo->currentItem) {
  821. current = current->next_item;
  822. }
  823. combo->currentItem = current;
  824. }while(!current->enabled);
  825. uint8_t index = comboItemToIndex(w, current);
  826. if(index < firstIndex)
  827. --combo->currentScroll;
  828. }
  829. }
  830. }
  831. return -1;
  832. }
  833. int32_t strsum(char* str){
  834. int32_t sum=0;
  835. while(*str){
  836. sum+=*str;
  837. str++;
  838. }
  839. return sum;
  840. }
  841. //returns -1 if processed, -2 if not processed, or next screen
  842. int default_widgetProcessInput(widget_t *w, RE_Rotation_t input, RE_State_t *state) {
  843. displayOnly_widget_t* dis = extractDisplayPartFromWidget(w);
  844. selectable_widget_t* sel = extractSelectablePartFromWidget(w);
  845. editable_widget_t* edit = extractEditablePartFromWidget(w);
  846. comboBox_widget_t* combo = NULL;
  847. if(!w || (input == Rotate_Nothing)){ return -1; }
  848. if(!sel) { return -2; }
  849. if(w->type==widget_combo){
  850. combo = (comboBox_widget_t*)w->content;
  851. }
  852. if(input == LongClick) {
  853. if(sel->longPressAction){
  854. return sel->longPressAction(w);
  855. }
  856. else{
  857. input = Click;
  858. }
  859. }
  860. if(input == Click) {
  861. switch (sel->state) {
  862. case widget_selected:
  863. if(w->refresh==refresh_idle){
  864. w->refresh = refresh_triggered;
  865. }
  866. if((w->type == widget_button)||(w->type == widget_bmp_button)){
  867. return ((button_widget_t*)w->content)->action(w);
  868. }
  869. if(dis->type == field_string){
  870. strcpy(dis->displayString, (char*)edit->inputData.getData());
  871. edit->current_edit = 0;
  872. }
  873. sel->state = widget_edit;
  874. sel->previous_state = widget_selected;
  875. break;
  876. case widget_edit:
  877. if(w->refresh==refresh_idle){
  878. w->refresh = refresh_triggered;
  879. }
  880. if(dis->type == field_string) {
  881. ++edit->current_edit;
  882. if(edit->current_edit== dis->reservedChars){
  883. sel->state = widget_selected;
  884. sel->previous_state = widget_edit;
  885. edit->current_edit = 0;
  886. }
  887. }
  888. else {
  889. sel->state = widget_selected;
  890. sel->previous_state = widget_edit;
  891. }
  892. break;
  893. default:
  894. break;
  895. }
  896. return -1;
  897. }
  898. if(((w->type == widget_editable) && (sel->state == widget_edit)) || ((w->type == widget_combo)&&combo->currentItem->type==combo_Editable)) { // Combo only calls this when incrementing or decrementing
  899. int32_t val_ui;
  900. int32_t inc;
  901. if(w->refresh==refresh_idle){
  902. w->refresh=refresh_triggered;
  903. }
  904. if(input == Rotate_Increment_while_click){
  905. input = Rotate_Increment;
  906. inc = edit->big_step;
  907. }
  908. else if(input == Rotate_Decrement_while_click){
  909. input = Rotate_Decrement;
  910. inc = edit->big_step;
  911. }
  912. else{
  913. inc = edit->step;
  914. }
  915. if(state->Diff < 0){
  916. inc = -inc;
  917. }
  918. if(edit->inputData.type==field_string){
  919. int16_t current_edit = (char)dis->displayString[edit->current_edit];
  920. current_edit += inc;
  921. switch(current_edit){// \0, ' ', 0-9, A-Z
  922. case '0'-1:
  923. current_edit =' ';
  924. break;
  925. case ' '-1:
  926. current_edit='Z';
  927. break;
  928. case 'Z'+1:
  929. current_edit=' ';
  930. break;
  931. case ' '+1:
  932. current_edit = '0';
  933. break;
  934. case '9'+1:
  935. current_edit ='A';
  936. break;
  937. case 'A'-1:
  938. current_edit ='9';
  939. break;
  940. default:
  941. break;
  942. }
  943. dis->displayString[edit->current_edit]=(char)current_edit;
  944. edit->setData(dis->displayString);
  945. }
  946. else if(edit->inputData.type==field_int32){
  947. if(!dis->displayString){ // If empty
  948. widgetDisable(w); // This shouldn't happen. Disable widget to avoid possible errors.
  949. return -1;
  950. }
  951. val_ui = *(int32_t*)dis->getData();
  952. if( (inc>0) && ((val_ui+inc) < val_ui) ){ // Check for overflow
  953. val_ui = edit->max_value;
  954. }
  955. else if( (inc<0) && ((val_ui+inc) > val_ui) ){ // Check for underflow
  956. val_ui = edit->min_value;
  957. }
  958. else{
  959. val_ui += inc;
  960. if(val_ui < edit->min_value) { // Check limits
  961. val_ui = edit->min_value;
  962. }
  963. if(val_ui > edit->max_value) {
  964. val_ui= edit->max_value;
  965. }
  966. }
  967. edit->setData(&val_ui);
  968. }
  969. return -1;
  970. }
  971. else if( ((w->type == widget_multi_option) && (sel->state == widget_edit)) || ((w->type == widget_combo)&&combo->currentItem->type==combo_MultiOption)) {
  972. if(w->refresh==refresh_idle){
  973. w->refresh=refresh_triggered;
  974. }
  975. int temp = *(uint8_t*)dis->getData();
  976. if(input == Rotate_Increment)
  977. ++temp;
  978. else if(input == Rotate_Decrement)
  979. --temp;
  980. if(temp < 0)
  981. temp = edit->numberOfOptions - 1;
  982. else if(temp > edit->numberOfOptions -1)
  983. temp = 0;
  984. edit->setData(&temp);
  985. }
  986. else if (sel->state == widget_selected) {
  987. uint8_t next = 0xFF;
  988. int previous = -1;
  989. widget_t *next_w = NULL;
  990. widget_t *previous_w = NULL;
  991. widget_t *first_w = w;
  992. widget_t *last_w = w;
  993. widget_t *scan = w->parent->widgets;
  994. while(scan) {
  995. selectable_widget_t *e = extractSelectablePartFromWidget(scan);
  996. if(e) {
  997. if((e->tab > sel->tab) && (e->tab < next) && scan->enabled) {
  998. next = e->tab;
  999. next_w =scan;
  1000. }
  1001. if((e->tab < sel->tab) && (e->tab > previous) && scan->enabled) {
  1002. previous = e->tab;
  1003. previous_w = scan;
  1004. }
  1005. if((e->tab < extractSelectablePartFromWidget(first_w)->tab) && scan->enabled)
  1006. first_w = scan;
  1007. if((e->tab > extractSelectablePartFromWidget(last_w)->tab) && scan->enabled)
  1008. last_w = scan;
  1009. }
  1010. scan = scan->next_widget;
  1011. }
  1012. if(next_w == NULL)
  1013. next_w = first_w;
  1014. if(previous_w == NULL)
  1015. previous_w = last_w;
  1016. if((input == Rotate_Increment) && next_w && (next_w != w)) {
  1017. sel->state = widget_idle;
  1018. sel->previous_state = widget_selected;
  1019. w->parent->current_widget = next_w;
  1020. extractSelectablePartFromWidget(next_w)->previous_state = extractSelectablePartFromWidget(next_w)->state;
  1021. extractSelectablePartFromWidget(next_w)->state = widget_selected;
  1022. return -1;
  1023. }
  1024. else if((input == Rotate_Decrement) && previous_w && (previous_w != w)) {
  1025. sel->state = widget_idle;
  1026. sel->previous_state = widget_selected;
  1027. w->parent->current_widget = previous_w;
  1028. extractSelectablePartFromWidget(previous_w)->previous_state = extractSelectablePartFromWidget(previous_w)->state;
  1029. extractSelectablePartFromWidget(previous_w)->state = widget_selected;
  1030. return -1;
  1031. }
  1032. }
  1033. return -2;
  1034. }
  1035. void comboAddScreen(comboBox_item_t* item,widget_t *combo, char *label, uint8_t actionScreen){
  1036. if(!item || !combo || !label){ return; }
  1037. comboBox_widget_t* comboW = (comboBox_widget_t*)combo->content;
  1038. if(combo->type==widget_combo){
  1039. item->text = label;
  1040. item->next_item = NULL;
  1041. item->action_screen = actionScreen;
  1042. item->type = combo_Screen;
  1043. item->enabled = 1;
  1044. item->dispAlign= align_center;
  1045. comboBox_item_t *next = comboW->first;
  1046. if(!next) {
  1047. comboW->first = item;
  1048. comboW->currentItem = item;
  1049. return;
  1050. }
  1051. while(next->next_item){
  1052. next = next->next_item;
  1053. }
  1054. next->next_item = item;
  1055. }
  1056. }
  1057. // Only allows Editable or multioption widgets
  1058. void comboAddEditable(comboBox_item_t* item, widget_t *combo, char *label, editable_widget_t *editable){
  1059. if(!item || !combo || !label || !editable ){
  1060. return;
  1061. }
  1062. comboBox_widget_t* comboW = (comboBox_widget_t*)combo->content;
  1063. item->text = label;
  1064. item->next_item = NULL;
  1065. item->widget = editable;
  1066. item->type = combo_Editable;
  1067. item->enabled = 1;
  1068. comboBox_item_t *next = comboW->first;
  1069. if(!next) {
  1070. comboW->first = item;
  1071. comboW->currentItem = item;
  1072. return;
  1073. }
  1074. while(next->next_item){
  1075. next = next->next_item;
  1076. }
  1077. next->next_item = item;
  1078. }
  1079. // Only allows Editable or multioption widgets
  1080. void comboAddMultiOption(comboBox_item_t* item, widget_t *combo, char *label, editable_widget_t *editable){
  1081. if(!item || !combo || !label || !editable ){
  1082. return;
  1083. }
  1084. comboBox_widget_t* comboW = (comboBox_widget_t*)combo->content;
  1085. item->text = label;
  1086. item->next_item = NULL;
  1087. item->widget = editable;
  1088. item->type = combo_MultiOption;
  1089. item->enabled = 1;
  1090. comboBox_item_t *next = comboW->first;
  1091. if(!next) {
  1092. comboW->first = item;
  1093. comboW->currentItem = item;
  1094. return;
  1095. }
  1096. while(next->next_item){
  1097. next = next->next_item;
  1098. }
  1099. next->next_item = item;
  1100. }
  1101. void comboAddAction(comboBox_item_t* item, widget_t *combo, char *label, int (*action)()){
  1102. if(!item || !combo || !label || !action){ return; }
  1103. comboBox_widget_t* comboW = (comboBox_widget_t*)combo->content;
  1104. if(action){ // If not null
  1105. item->text = label;
  1106. item->next_item = NULL;
  1107. item->action = action;
  1108. item->type = combo_Action;
  1109. item->enabled = 1;
  1110. item->dispAlign= align_center;
  1111. comboBox_item_t *next = comboW->first;
  1112. if(!next) {
  1113. comboW->first = item;
  1114. comboW->currentItem = item;
  1115. return;
  1116. }
  1117. while(next->next_item){
  1118. next = next->next_item;
  1119. }
  1120. next->next_item = item;
  1121. }
  1122. }
  1123. void comboResetIndex(widget_t *combo){
  1124. comboBox_widget_t* comboW = (comboBox_widget_t*)combo->content;
  1125. if((comboW->currentItem->type==combo_Editable)||(comboW->currentItem->type==combo_MultiOption)){
  1126. selectable_widget_t* sel = &comboW->currentItem->widget->selectable;
  1127. if(sel){
  1128. sel->state = widget_idle;
  1129. sel->previous_state = widget_idle;
  1130. }
  1131. }
  1132. comboW->currentItem = comboW->first;
  1133. comboW->currentScroll=0;
  1134. }