u8g2-copy/csrc/u8x8_message.c

153 lines
4.3 KiB
C
Raw Permalink Normal View History

2016-05-23 03:44:20 +08:00
/*
2016-05-29 22:24:50 +08:00
u8x8_message.c
2016-05-23 03:44:20 +08:00
Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
Copyright (c) 2016, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "u8x8.h"
2016-05-24 04:35:04 +08:00
uint8_t u8x8_draw_button_line(u8x8_t *u8x8, uint8_t y, uint8_t w, uint8_t cursor, const char *s)
2016-05-23 03:44:20 +08:00
{
2016-05-24 04:35:04 +08:00
uint8_t i;
uint8_t cnt;
uint8_t total;
uint8_t d;
uint8_t x;
cnt = u8x8_GetStringLineCnt(s);
/* calculate the width of the button */
total = 0;
for( i = 0; i < cnt; i++ )
{
total += u8x8_GetUTF8Len(u8x8, u8x8_GetStringLineStart(i, s));
2016-05-24 04:35:04 +08:00
}
total += (cnt-1); /* had one space between the buttons */
/* calculate the left offset */
d = 0;
if ( total < w )
{
d = w;
d -= total;
d /= 2;
}
/* draw the buttons */
x = d;
u8x8_SetInverseFont(u8x8, 0);
2016-05-24 04:35:04 +08:00
for( i = 0; i < cnt; i++ )
{
if ( i == cursor )
u8x8_SetInverseFont(u8x8, 1);
x+=u8x8_DrawUTF8(u8x8, x, y, u8x8_GetStringLineStart(i, s));
u8x8_SetInverseFont(u8x8, 0);
x+=u8x8_DrawUTF8(u8x8, x, y, " ");
2016-05-24 04:35:04 +08:00
}
/* return the number of buttons */
return cnt;
2016-05-23 03:44:20 +08:00
}
/*
title1: Multiple lines,separated by '\n'
title2: A single line/string which is terminated by '\0' or '\n' . "title2" accepts the return value from u8x8_GetStringLineStart()
title3: Multiple lines,separated by '\n'
buttons: one more more buttons separated by '\n' and terminated with '\0'
*/
uint8_t u8x8_UserInterfaceMessage(u8x8_t *u8x8, const char *title1, const char *title2, const char *title3, const char *buttons)
2016-05-23 03:44:20 +08:00
{
2016-05-24 04:35:04 +08:00
uint8_t height;
uint8_t y;
uint8_t cursor = 0;
uint8_t button_cnt;
uint8_t event;
2016-05-30 01:27:24 +08:00
u8x8_SetInverseFont(u8x8, 0);
2016-05-24 04:35:04 +08:00
/* calculate overall height of the message box */
height = 1; /* button line */
height += u8x8_GetStringLineCnt(title1);
if ( title2 != NULL )
height ++;
height += u8x8_GetStringLineCnt(title3);
2016-05-24 04:35:04 +08:00
2016-05-30 01:27:24 +08:00
/* calculate offset from top */
2016-05-24 04:35:04 +08:00
y = 0;
if ( height < u8x8_GetRows(u8x8) )
2016-05-24 04:35:04 +08:00
{
y = u8x8_GetRows(u8x8);
y -= height;
2016-05-24 04:35:04 +08:00
y /= 2;
}
2016-05-30 01:27:24 +08:00
/* draw message box */
2016-05-24 04:35:04 +08:00
u8x8_ClearDisplay(u8x8); /* required, because not everything is filled */
2016-05-24 04:35:04 +08:00
y += u8x8_DrawUTF8Lines(u8x8, 0, y, u8x8_GetCols(u8x8), title1);
if ( title2 != NULL )
{
u8x8_DrawUTF8Line(u8x8, 0, y, u8x8_GetCols(u8x8), title2);
y++;
}
y += u8x8_DrawUTF8Lines(u8x8, 0, y, u8x8_GetCols(u8x8), title3);
2016-05-24 04:35:04 +08:00
button_cnt = u8x8_draw_button_line(u8x8, y, u8x8_GetCols(u8x8), cursor, buttons);
2016-05-23 03:44:20 +08:00
2016-05-24 04:35:04 +08:00
for(;;)
{
event = u8x8_GetMenuEvent(u8x8);
if ( event == U8X8_MSG_GPIO_MENU_SELECT )
return cursor+1;
else if ( event == U8X8_MSG_GPIO_MENU_HOME )
break;
2016-06-15 05:09:53 +08:00
else if ( event == U8X8_MSG_GPIO_MENU_NEXT || event == U8X8_MSG_GPIO_MENU_UP )
2016-05-24 04:35:04 +08:00
{
cursor++;
if ( cursor >= button_cnt )
cursor = 0;
u8x8_draw_button_line(u8x8, y, u8x8_GetCols(u8x8), cursor, buttons);
}
2016-06-15 05:09:53 +08:00
else if ( event == U8X8_MSG_GPIO_MENU_PREV || event == U8X8_MSG_GPIO_MENU_DOWN )
2016-05-24 04:35:04 +08:00
{
if ( cursor == 0 )
cursor = button_cnt;
cursor--;
u8x8_draw_button_line(u8x8, y, u8x8_GetCols(u8x8), cursor, buttons);
}
}
return 0;
2016-05-23 03:44:20 +08:00
}