| 1 | /* |
| 2 | |
| 3 | u8x8_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 "u8x8.h" |
| 37 | |
| 38 | uint8_t u8x8_draw_button_line(u8x8_t *u8x8, uint8_t y, uint8_t w, uint8_t cursor, const char *s) |
| 39 | { |
| 40 | uint8_t i; |
| 41 | uint8_t cnt; |
| 42 | uint8_t total; |
| 43 | uint8_t d; |
| 44 | uint8_t x; |
| 45 | cnt = u8x8_GetStringLineCnt(s); |
| 46 | |
| 47 | /* calculate the width of the button */ |
| 48 | total = 0; |
| 49 | for( i = 0; i < cnt; i++ ) |
| 50 | { |
| 51 | total += u8x8_GetUTF8Len(u8x8, u8x8_GetStringLineStart(i, s)); |
| 52 | } |
| 53 | total += (cnt-1); /* had one space between the buttons */ |
| 54 | |
| 55 | /* calculate the left offset */ |
| 56 | d = 0; |
| 57 | if ( total < w ) |
| 58 | { |
| 59 | d = w; |
| 60 | d -= total; |
| 61 | d /= 2; |
| 62 | } |
| 63 | |
| 64 | /* draw the buttons */ |
| 65 | x = d; |
| 66 | u8x8_SetInverseFont(u8x8, 0); |
| 67 | for( i = 0; i < cnt; i++ ) |
| 68 | { |
| 69 | if ( i == cursor ) |
| 70 | u8x8_SetInverseFont(u8x8, 1); |
| 71 | |
| 72 | x+=u8x8_DrawUTF8(u8x8, x, y, u8x8_GetStringLineStart(i, s)); |
| 73 | u8x8_SetInverseFont(u8x8, 0); |
| 74 | x+=u8x8_DrawUTF8(u8x8, x, y, " "); |
| 75 | } |
| 76 | |
| 77 | /* return the number of buttons */ |
| 78 | return cnt; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | title1: Multiple lines,separated by '\n' |
| 83 | title2: A single line/string which is terminated by '\0' or '\n' . "title2" accepts the return value from u8x8_GetStringLineStart() |
| 84 | title3: Multiple lines,separated by '\n' |
| 85 | buttons: one more more buttons separated by '\n' and terminated with '\0' |
| 86 | */ |
| 87 | |
| 88 | uint8_t u8x8_UserInterfaceMessage(u8x8_t *u8x8, const char *title1, const char *title2, const char *title3, const char *buttons) |
| 89 | { |
| 90 | uint8_t height; |
| 91 | uint8_t y; |
| 92 | uint8_t cursor = 0; |
| 93 | uint8_t button_cnt; |
| 94 | uint8_t event; |
| 95 | |
| 96 | u8x8_SetInverseFont(u8x8, 0); |
| 97 | |
| 98 | /* calculate overall height of the message box */ |
| 99 | height = 1; /* button line */ |
| 100 | height += u8x8_GetStringLineCnt(title1); |
| 101 | if ( title2 != NULL ) |
| 102 | height ++; |
| 103 | height += u8x8_GetStringLineCnt(title3); |
| 104 | |
| 105 | /* calculate offset from top */ |
| 106 | y = 0; |
| 107 | if ( height < u8x8_GetRows(u8x8) ) |
| 108 | { |
| 109 | y = u8x8_GetRows(u8x8); |
| 110 | y -= height; |
| 111 | y /= 2; |
| 112 | } |
| 113 | |
| 114 | /* draw message box */ |
| 115 | |
| 116 | u8x8_ClearDisplay(u8x8); /* required, because not everything is filled */ |
| 117 | |
| 118 | y += u8x8_DrawUTF8Lines(u8x8, 0, y, u8x8_GetCols(u8x8), title1); |
| 119 | if ( title2 != NULL ) |
| 120 | { |
| 121 | u8x8_DrawUTF8Line(u8x8, 0, y, u8x8_GetCols(u8x8), title2); |
| 122 | y++; |
| 123 | } |
| 124 | y += u8x8_DrawUTF8Lines(u8x8, 0, y, u8x8_GetCols(u8x8), title3); |
| 125 | |
| 126 | button_cnt = u8x8_draw_button_line(u8x8, y, u8x8_GetCols(u8x8), cursor, buttons); |
| 127 | |
| 128 | for(;;) |
| 129 | { |
| 130 | event = u8x8_GetMenuEvent(u8x8); |
| 131 | if ( event == U8X8_MSG_GPIO_MENU_SELECT ) |
| 132 | return cursor+1; |
| 133 | else if ( event == U8X8_MSG_GPIO_MENU_HOME ) |
| 134 | break; |
| 135 | else if ( event == U8X8_MSG_GPIO_MENU_NEXT || event == U8X8_MSG_GPIO_MENU_UP ) |
| 136 | { |
| 137 | cursor++; |
| 138 | if ( cursor >= button_cnt ) |
| 139 | cursor = 0; |
| 140 | u8x8_draw_button_line(u8x8, y, u8x8_GetCols(u8x8), cursor, buttons); |
| 141 | } |
| 142 | else if ( event == U8X8_MSG_GPIO_MENU_PREV || event == U8X8_MSG_GPIO_MENU_DOWN ) |
| 143 | { |
| 144 | if ( cursor == 0 ) |
| 145 | cursor = button_cnt; |
| 146 | cursor--; |
| 147 | u8x8_draw_button_line(u8x8, y, u8x8_GetCols(u8x8), cursor, buttons); |
| 148 | } |
| 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | |