| 1 | /* |
| 2 | |
| 3 | u8g2_button.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 | |
| 39 | /* |
| 40 | |
| 41 | Description: |
| 42 | Draws normal or inverted text with optional frame around text. |
| 43 | The text (and the frame) can be horizontally centered around the provided reference position. |
| 44 | This procedure will use the current draw color and current font. The height of the frame |
| 45 | depends on the setting of setFontRefHeightText(), setFontRefHeightExtendedText() or setFontRefHeightAll() |
| 46 | |
| 47 | |
| 48 | Note 1: drawColor 2 (XOR) is not supported. Result will be broken with draw color 2. |
| 49 | Note 2: This procedure will enforce font mode 1 (transparent mode) |
| 50 | Note 3: Some fonts may add an extra gap on the right side. This is a font problem and can not be avoided (for example inb16 has this problem). |
| 51 | |
| 52 | |
| 53 | The height of the button is defined by the current font and |
| 54 | setFontRefHeightText, |
| 55 | setFontRefHeightExtendedText |
| 56 | setFontRefHeightAll |
| 57 | Right padding might be influenced by the font. |
| 58 | For example u8g2_font_inb16 may add an extra pixel gap (increase padding by one) |
| 59 | on the right side. |
| 60 | The current draw color is considered, but only draw color 0 and 1 are supported |
| 61 | |
| 62 | This function requires transparent font mode: setFontMode(1) |
| 63 | |
| 64 | Side effect: Font transparent mode is enabled setFontMode(1) |
| 65 | |
| 66 | */ |
| 67 | |
| 68 | |
| 69 | |
| 70 | /* |
| 71 | |
| 72 | flags: |
| 73 | |
| 74 | U8G2_BTN_BW1 0x01 |
| 75 | U8G2_BTN_BW2 0x02 |
| 76 | U8G2_BTN_BW3 0x03 |
| 77 | |
| 78 | |
| 79 | U8G2_BTN_SHADOW0 0x08 |
| 80 | U8G2_BTN_SHADOW1 0x10 |
| 81 | U8G2_BTN_SHADOW2 0x18 |
| 82 | |
| 83 | U8G2_BTN_INV 0x20 |
| 84 | |
| 85 | U8G2_BTN_HCENTER 0x40 |
| 86 | |
| 87 | U8G2_BTN_XFRAME 0x80 |
| 88 | |
| 89 | Total size without shadow: width+2*padding_h+2*border |
| 90 | Total size with U8G2_BTN_SHADOW0: width+2*padding_h+3*border |
| 91 | Total size with U8G2_BTN_SHADOW1: width+2*padding_h+3*border+1 |
| 92 | Total size with U8G2_BTN_SHADOW2: width+2*padding_h+3*border+2 |
| 93 | |
| 94 | U8G2_BTN_XFRAME: |
| 95 | draw another one pixel frame with one pixel gap, will not look good with shadow |
| 96 | */ |
| 97 | |
| 98 | void u8g2_DrawButtonFrame(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t flags, u8g2_uint_t text_width, u8g2_uint_t padding_h, u8g2_uint_t padding_v) |
| 99 | { |
| 100 | u8g2_uint_t w = text_width; |
| 101 | |
| 102 | u8g2_uint_t xx, yy, ww, hh; |
| 103 | |
| 104 | u8g2_uint_t gap_frame = U8G2_BTN_BW_MASK+1; |
| 105 | |
| 106 | u8g2_uint_t border_width = flags & U8G2_BTN_BW_MASK; |
| 107 | |
| 108 | int8_t a = u8g2_GetAscent(u8g2); |
| 109 | int8_t d = u8g2_GetDescent(u8g2); |
| 110 | |
| 111 | uint8_t color_backup = u8g2->draw_color; |
| 112 | |
| 113 | |
| 114 | if ( flags & U8G2_BTN_XFRAME ) |
| 115 | { |
| 116 | border_width++; |
| 117 | gap_frame = border_width; |
| 118 | border_width++; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | for(;;) |
| 124 | { |
| 125 | |
| 126 | xx = x; |
| 127 | xx -= padding_h; |
| 128 | xx -= border_width; |
| 129 | ww = w+2*padding_h+2*border_width; |
| 130 | |
| 131 | yy = y; |
| 132 | yy += u8g2->font_calc_vref(u8g2); |
| 133 | yy -= a; |
| 134 | yy -= padding_v; |
| 135 | yy -= border_width; |
| 136 | hh = a-d+2*padding_v+2*border_width; |
| 137 | if ( border_width == 0 ) |
| 138 | break; |
| 139 | if ( border_width == gap_frame ) |
| 140 | { |
| 141 | u8g2_SetDrawColor(u8g2, color_backup == 0 ? 1 : 0); |
| 142 | } |
| 143 | u8g2_DrawFrame(u8g2, xx, yy, ww, hh); |
| 144 | u8g2_SetDrawColor(u8g2, color_backup); |
| 145 | |
| 146 | if ( flags & U8G2_BTN_SHADOW_MASK ) |
| 147 | { |
| 148 | if ( border_width == (flags & U8G2_BTN_BW_MASK) ) |
| 149 | { |
| 150 | u8g2_uint_t i; |
| 151 | u8g2_uint_t shadow_gap = (flags & U8G2_BTN_SHADOW_MASK) >> U8G2_BTN_SHADOW_POS; |
| 152 | shadow_gap--; |
| 153 | for( i = 0; i < border_width; i++ ) |
| 154 | { |
| 155 | u8g2_DrawHLine(u8g2, xx+border_width+shadow_gap,yy+hh+i+shadow_gap,ww); |
| 156 | u8g2_DrawVLine(u8g2, xx+ww+i+shadow_gap,yy+border_width+shadow_gap,hh); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | border_width--; |
| 161 | } /* for */ |
| 162 | |
| 163 | if ( flags & U8G2_BTN_INV ) |
| 164 | { |
| 165 | u8g2_SetDrawColor(u8g2, 2); /* XOR */ |
| 166 | u8g2_DrawBox(u8g2, xx, yy, ww, hh); |
| 167 | u8g2_SetDrawColor(u8g2, color_backup); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void u8g2_DrawButtonUTF8(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t flags, u8g2_uint_t width, u8g2_uint_t padding_h, u8g2_uint_t padding_v, const char *text) |
| 172 | { |
| 173 | u8g2_uint_t w = u8g2_GetUTF8Width(u8g2, text); |
| 174 | |
| 175 | u8g2_uint_t text_x_offset = 0; |
| 176 | |
| 177 | if ( flags & U8G2_BTN_HCENTER ) |
| 178 | x -= (w+1)/2; |
| 179 | |
| 180 | if ( w < width ) |
| 181 | { |
| 182 | if ( flags & U8G2_BTN_HCENTER ) |
| 183 | { |
| 184 | text_x_offset = (width-w)/2; |
| 185 | } |
| 186 | w = width; |
| 187 | } |
| 188 | |
| 189 | u8g2_SetFontMode(u8g2, 1); |
| 190 | u8g2_DrawUTF8(u8g2, x,y, text); |
| 191 | u8g2_DrawButtonFrame(u8g2, x-text_x_offset, y, flags, w, padding_h, padding_v); |
| 192 | |
| 193 | } |
| 194 | |
| 195 | |
| 196 | |
| 197 | #ifdef NOT_USED |
| 198 | void u8g2_Draw4Pixel(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w) |
| 199 | { |
| 200 | u8g2_DrawPixel(u8g2, x,y-1); |
| 201 | u8g2_DrawPixel(u8g2, x+w-1,y-1); |
| 202 | u8g2_DrawPixel(u8g2, x+w-1,y-w); |
| 203 | u8g2_DrawPixel(u8g2, x,y-w); |
| 204 | } |
| 205 | |
| 206 | void u8g2_DrawRadio(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t is_checked) |
| 207 | { |
| 208 | uint8_t color_backup = u8g2->draw_color; |
| 209 | u8g2_DrawCheckbox(u8g2, x,y,w,is_checked); |
| 210 | u8g2_SetDrawColor(u8g2, 2); |
| 211 | u8g2_Draw4Pixel(u8g2, x,y,w); |
| 212 | if ( is_checked ) |
| 213 | { |
| 214 | //u8g2_Draw4Pixel(u8g2, x+2,y-2,w-4); |
| 215 | } |
| 216 | |
| 217 | u8g2_SetDrawColor(u8g2, color_backup ); |
| 218 | } |
| 219 | #endif |
| 220 | |
| 221 | |
| 222 | #ifdef _THIS_CODE_SHOULD_BE_REWRITTEN_WITHOUT_PADWIDTH_ |
| 223 | |
| 224 | /* |
| 225 | Shadow is not supported |
| 226 | Note: radius must be at least as high as the border width |
| 227 | |
| 228 | border width | good radius values |
| 229 | 1 | 3, 5, 7, 8, ... |
| 230 | 2 | 3, 5, 7, 8, ... |
| 231 | |
| 232 | */ |
| 233 | |
| 234 | void u8g2_DrawRButtonUTF8(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t flags, u8g2_uint_t padding_h_or_width, u8g2_uint_t padding_v, u8g2_uint_t r, const char *text) |
| 235 | { |
| 236 | u8g2_uint_t w = u8g2_GetUTF8Width(u8g2, text); |
| 237 | //u8g2_uint_t w = u8g2_GetExactStrWidth(u8g2, text); |
| 238 | |
| 239 | u8g2_uint_t xx, yy, ww, hh; |
| 240 | |
| 241 | u8g2_uint_t border_width = flags & U8G2_BTN_BW_MASK; |
| 242 | u8g2_uint_t padding_h = padding_h_or_width; |
| 243 | u8g2_uint_t text_x_offset = 0; // used for U8G2_BTN_PADWIDTH mode |
| 244 | |
| 245 | int8_t a = u8g2_GetAscent(u8g2); |
| 246 | int8_t d = u8g2_GetDescent(u8g2); |
| 247 | uint8_t color_backup = u8g2->draw_color; |
| 248 | |
| 249 | |
| 250 | if ( flags & U8G2_BTN_HCENTER ) |
| 251 | x -= w/2; |
| 252 | |
| 253 | if ( flags & U8G2_BTN_PADWIDTH ) |
| 254 | { |
| 255 | padding_h = 0; |
| 256 | if ( w < padding_h_or_width ) |
| 257 | { |
| 258 | if ( flags & U8G2_BTN_HCENTER ) |
| 259 | { |
| 260 | text_x_offset = (padding_h_or_width-w)/2; |
| 261 | } |
| 262 | w = padding_h_or_width; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | |
| 267 | u8g2_SetFontMode(u8g2, 1); |
| 268 | |
| 269 | for(;;) |
| 270 | { |
| 271 | if ( padding_h >= u8g2_GetDisplayWidth(u8g2)/2 ) // padding_h is zero if U8G2_BTN_PADWIDTH is set |
| 272 | { |
| 273 | xx = (flags & U8G2_BTN_BW_MASK) - border_width; |
| 274 | ww = u8g2_GetDisplayWidth(u8g2); |
| 275 | ww -= 2*((flags & U8G2_BTN_BW_MASK) - border_width); |
| 276 | //printf("xx=%d ww=%d\n", xx, ww); |
| 277 | //printf("clip_x1=%d clip_x0=%d\n", u8g2->clip_x1, u8g2->clip_x0); |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | xx = x; |
| 282 | xx -= text_x_offset; |
| 283 | xx -= padding_h; |
| 284 | xx -= border_width; |
| 285 | ww = w+2*padding_h+2*border_width; |
| 286 | } |
| 287 | |
| 288 | yy = y; |
| 289 | yy += u8g2->font_calc_vref(u8g2); |
| 290 | yy -= a; |
| 291 | yy -= padding_v; |
| 292 | yy -= border_width; |
| 293 | hh = a-d+2*padding_v+2*border_width; |
| 294 | if ( border_width == 0 ) |
| 295 | break; |
| 296 | u8g2_DrawRFrame(u8g2, xx, yy, ww, hh, r); |
| 297 | if ( (flags & U8G2_BTN_BW_MASK) > 1 ) |
| 298 | u8g2_DrawRFrame(u8g2, xx, yy, ww, hh, r+1); |
| 299 | |
| 300 | border_width--; |
| 301 | if ( r > 1 ) |
| 302 | r--; |
| 303 | } |
| 304 | if ( flags & U8G2_BTN_INV ) |
| 305 | { |
| 306 | u8g2_DrawRBox(u8g2, xx, yy, ww, hh,r); |
| 307 | u8g2_SetDrawColor(u8g2, 1-u8g2->draw_color); |
| 308 | } |
| 309 | u8g2_DrawUTF8(u8g2, x,y, text); |
| 310 | u8g2_SetDrawColor(u8g2, color_backup); |
| 311 | } |
| 312 | |
| 313 | #endif |