u8g2_message.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. u8g2_message.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2016, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "u8g2.h"
  28. #define SPACE_BETWEEN_BUTTONS_IN_PIXEL 6
  29. #define SPACE_BETWEEN_TEXT_AND_BUTTONS_IN_PIXEL 3
  30. uint8_t u8g2_draw_button_line(u8g2_t *u8g2, u8g2_uint_t y, u8g2_uint_t w, uint8_t cursor, const char *s)
  31. {
  32. u8g2_uint_t button_line_width;
  33. uint8_t i;
  34. uint8_t cnt;
  35. uint8_t is_invert;
  36. u8g2_uint_t d;
  37. u8g2_uint_t x;
  38. cnt = u8x8_GetStringLineCnt(s);
  39. /* calculate the width of the button line */
  40. button_line_width = 0;
  41. for( i = 0; i < cnt; i++ )
  42. {
  43. button_line_width += u8g2_GetUTF8Width(u8g2, u8x8_GetStringLineStart(i, s));
  44. }
  45. button_line_width += (cnt-1)*SPACE_BETWEEN_BUTTONS_IN_PIXEL; /* add some space between the buttons */
  46. /* calculate the left offset */
  47. d = 0;
  48. if ( button_line_width < w )
  49. {
  50. d = w;
  51. d -= button_line_width;
  52. d /= 2;
  53. }
  54. /* draw the buttons */
  55. x = d;
  56. for( i = 0; i < cnt; i++ )
  57. {
  58. is_invert = 0;
  59. if ( i == cursor )
  60. is_invert = 1;
  61. u8g2_DrawUTF8Line(u8g2, x, y, 0, u8x8_GetStringLineStart(i, s), 1, is_invert);
  62. x += u8g2_GetUTF8Width(u8g2, u8x8_GetStringLineStart(i, s));
  63. x += SPACE_BETWEEN_BUTTONS_IN_PIXEL;
  64. }
  65. /* return the number of buttons */
  66. return cnt;
  67. }
  68. /*
  69. title1: Multiple lines,separated by '\n'
  70. title2: A single line/string which is terminated by '\0' or '\n' . "title2" accepts the return value from u8x8_GetStringLineStart()
  71. title3: Multiple lines,separated by '\n'
  72. buttons: one more more buttons separated by '\n' and terminated with '\0'
  73. side effects:
  74. u8g2_SetFontDirection(u8g2, 0);
  75. u8g2_SetFontPosBaseline(u8g2);
  76. */
  77. uint8_t u8g2_UserInterfaceMessage(u8g2_t *u8g2, const char *title1, const char *title2, const char *title3, const char *buttons)
  78. {
  79. uint8_t height;
  80. uint8_t line_height;
  81. u8g2_uint_t pixel_height;
  82. u8g2_uint_t y, yy;
  83. uint8_t cursor = 0;
  84. uint8_t button_cnt;
  85. uint8_t event;
  86. /* only horizontal strings are supported, so force this here */
  87. u8g2_SetFontDirection(u8g2, 0);
  88. /* force baseline position */
  89. u8g2_SetFontPosBaseline(u8g2);
  90. /* calculate line height */
  91. line_height = u8g2_GetAscent(u8g2);
  92. line_height -= u8g2_GetDescent(u8g2);
  93. /* calculate overall height of the message box in lines*/
  94. height = 1; /* button line */
  95. height += u8x8_GetStringLineCnt(title1);
  96. if ( title2 != NULL )
  97. height++;
  98. height += u8x8_GetStringLineCnt(title3);
  99. /* calculate the height in pixel */
  100. pixel_height = height;
  101. pixel_height *= line_height;
  102. /* ... and add the space between the text and the buttons */
  103. pixel_height +=SPACE_BETWEEN_TEXT_AND_BUTTONS_IN_PIXEL;
  104. /* calculate offset from top */
  105. y = 0;
  106. if ( pixel_height < u8g2_GetDisplayHeight(u8g2) )
  107. {
  108. y = u8g2_GetDisplayHeight(u8g2);
  109. y -= pixel_height;
  110. y /= 2;
  111. }
  112. y += u8g2_GetAscent(u8g2);
  113. for(;;)
  114. {
  115. u8g2_FirstPage(u8g2);
  116. do
  117. {
  118. yy = y;
  119. /* draw message box */
  120. yy += u8g2_DrawUTF8Lines(u8g2, 0, yy, u8g2_GetDisplayWidth(u8g2), line_height, title1);
  121. if ( title2 != NULL )
  122. {
  123. u8g2_DrawUTF8Line(u8g2, 0, yy, u8g2_GetDisplayWidth(u8g2), title2, 0, 0);
  124. yy+=line_height;
  125. }
  126. yy += u8g2_DrawUTF8Lines(u8g2, 0, yy, u8g2_GetDisplayWidth(u8g2), line_height, title3);
  127. yy += SPACE_BETWEEN_TEXT_AND_BUTTONS_IN_PIXEL;
  128. button_cnt = u8g2_draw_button_line(u8g2, yy, u8g2_GetDisplayWidth(u8g2), cursor, buttons);
  129. } while( u8g2_NextPage(u8g2) );
  130. #ifdef U8G2_REF_MAN_PIC
  131. return 0;
  132. #endif
  133. for(;;)
  134. {
  135. event = u8x8_GetMenuEvent(u8g2_GetU8x8(u8g2));
  136. if ( event == U8X8_MSG_GPIO_MENU_SELECT )
  137. return cursor+1;
  138. else if ( event == U8X8_MSG_GPIO_MENU_HOME )
  139. return 0;
  140. else if ( event == U8X8_MSG_GPIO_MENU_NEXT || event == U8X8_MSG_GPIO_MENU_DOWN )
  141. {
  142. cursor++;
  143. if ( cursor >= button_cnt )
  144. cursor = 0;
  145. break;
  146. }
  147. else if ( event == U8X8_MSG_GPIO_MENU_PREV || event == U8X8_MSG_GPIO_MENU_UP )
  148. {
  149. if ( cursor == 0 )
  150. cursor = button_cnt;
  151. cursor--;
  152. break;
  153. }
  154. }
  155. }
  156. /* never reached */
  157. //return 0;
  158. }