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