add possibility to clear bars

This commit is contained in:
Sem van der Hoeven
2024-11-12 22:57:38 +01:00
parent cf24296f63
commit 21c166aadf
3 changed files with 88 additions and 20 deletions

View File

@@ -1,6 +1,6 @@
#include "bars.h"
void draw_bar_horizontal(int start_x, int start_y, int width, int height, int value, int max_value, uint color, char fill_outline, UTFT *display)
void bar_draw_horizontal(int start_x, int start_y, int width, int height, int value, int max_value, uint color, char fill_outline, UTFT *display)
{
display->setColor(color);
int bar_width = (int)((float)value / (float)max_value * width);
@@ -11,7 +11,7 @@ void draw_bar_horizontal(int start_x, int start_y, int width, int height, int va
display->fillRect(start_x, start_y, start_x + bar_width, start_y + height);
}
void draw_bar_vertical(int start_x, int start_y, int width, int height, int value, int max_value, uint color, char fill_outline, UTFT *display)
void bar_draw_vertical(int start_x, int start_y, int width, int height, int value, int max_value, uint color, char fill_outline, UTFT *display)
{
display->setColor(color);
int bar_height = (int)((float)value / (float)max_value * height);
@@ -20,4 +20,42 @@ void draw_bar_vertical(int start_x, int start_y, int width, int height, int valu
display->drawRect(start_x, start_y, start_x + width, start_y + height);
}
display->fillRect(start_x, (start_y + height) - bar_height, start_x + width, start_y + height);
}
void bar_clear_part_horizontal(int start_x, int start_y, int width, int height, int value_from, int max_value, char clear_color, char clear_outline, UTFT *display)
{
display->setColor(clear_color);
if (!clear_outline)
{
/**
* If we want to clear the outline, the part we need to clear becomes 1px smaller on each side.
* (that's not part of the bar to be filled, the "empty space" inside the bar)
* Because the start_y gets incremented, the end of the bar on the y side would be 2px too big.
* That's why we need to decrement the height by 2.
*/
start_y++;
height -= 2;
width--;
}
int bar_width = (int)((float)value_from / (float)max_value * width);
display->fillRect(start_x + bar_width, start_y, start_x + width, start_y + height);
}
void bar_clear_part_vertical(int start_x, int start_y, int width, int height, int value_from, int max_value, char clear_color, char clear_outline, UTFT *display)
{
display->setColor(clear_color);
if (!clear_outline)
{
/**
* If we want to clear the outline, the part we need to clear becomes 1px smaller on each side.
* (that's not part of the bar to be filled, the "empty space" inside the bar)
* Because the start_x gets incremented, the end of the bar on the x side would be 2px too big.
* That's why we need to decrement the width by 2.
*/
start_x++;
start_y++;
width-= 2;
}
int bar_height = (int)((float)value_from / (float)max_value * height);
display->fillRect(start_x, start_y, start_x + width, start_y + bar_height);
}