Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
//gui.c
#include "guitypes.h"
//intersection of positive intervals p0-p1 and p2-p3, result is stored in p4-p5
//condition (not checked): (p1 >= p0) && (p3 >= p2)
void interval_intersect_ui16(uint16_t* p)
{
if (p[0] < p[2]) //p0 is left
{
if (p[1] > p[2]) //intersection exists
{
p[4] = p[2]; //result left point is p2
if (p[1] > p[3]) //intersection is equal p2-p3
p[5] = p[3]; //result right point is p3
else //intersection is equal p2-p1
p[5] = p[1]; //result right point is p1
} else {
p[4] = 0;
p[5] = 0;
}
} else //p2 is left
{
if (p[3] > p[0]) //intersection exists
{
p[4] = p[0]; //result left point is p0
if (p[3] > p[1]) //intersection is equal p0-p1
p[5] = p[1]; //result right point is p1
else //intersection is equal p0-p3
p[5] = p[3]; //result right point is p3
} else {
p[4] = 0;
p[5] = 0;
}
}
}
rect_ui16_t rect_intersect_ui16(rect_ui16_t rc, rect_ui16_t rc1)
{
uint16_t x[6] = { rc.x, rc.x + rc.w, rc1.x, rc1.x + rc1.w, 0, 0 };
uint16_t y[6] = { rc.y, rc.y + rc.h, rc1.y, rc1.y + rc1.h, 0, 0 };
interval_intersect_ui16(x);
interval_intersect_ui16(y);
rect_ui16_t rc2 = { x[4], y[4], x[5] - x[4], y[5] - y[4] };
return rc2;
}
rect_ui16_t rect_ui16_add_padding_ui8(rect_ui16_t rc, padding_ui8_t pad)
{
rect_ui16_t rect = { 0, 0, rc.w + pad.left + pad.right, rc.h + pad.top + pad.bottom };
if (rc.x > pad.left)
rect.x = rc.x - pad.left;
if (rc.y > pad.top)
rect.y = rc.y - pad.top;
return rect;
}
rect_ui16_t rect_ui16_sub_padding_ui8(rect_ui16_t rc, padding_ui8_t pad)
{
rect_ui16_t rect = { rc.x + pad.left, rc.y + pad.top, 0, 0 };
if (rc.w > (pad.left + pad.right))
rect.w = rc.w - (pad.left + pad.right);
if (rc.h > (pad.top + pad.bottom))
rect.h = rc.h - (pad.top + pad.bottom);
return rect;
}
rect_ui16_t rect_align_ui16(rect_ui16_t rc, rect_ui16_t rc1, uint8_t align)
{
rect_ui16_t rect = rc1;
switch (align & ALIGN_HMASK) {
case ALIGN_LEFT:
rect.x = rc.x;
break;
case ALIGN_RIGHT:
rect.x = ((rc.x + rc.w) > rc1.w) ? ((rc.x + rc.w) - rc1.w) : 0;
break;
case ALIGN_HCENTER:
if (rc.w >= rc1.w)
rect.x = rc.x + ((rc.w - rc1.w) / 2);
else
rect.x = (rc.x > ((rc1.w - rc.w) / 2)) ? rc.x - ((rc1.w - rc.w) / 2) : 0;
break;
}
switch (align & ALIGN_VMASK) {
case ALIGN_TOP:
rect.y = rc.y;
break;
case ALIGN_BOTTOM:
rect.y = ((rc.y + rc.h) > rc1.h) ? ((rc.y + rc.h) - rc1.h) : 0;
break;
case ALIGN_VCENTER:
if (rc.h >= rc1.h)
rect.y = rc.y + ((rc.h - rc1.h) / 2);
else
rect.y = (rc.y > ((rc1.h - rc.h) / 2)) ? rc.y - ((rc1.h - rc.h) / 2) : 0;
break;
}
return rect;
}
point_ui16_t font_meas_text(font_t* pf, const char* str)
{
int x = 0;
int y = 0;
int w = 0;
int h = 0;
int char_w = pf->w;
int char_h = pf->h;
int len = strlen(str);
char c;
while (len--) {
c = *(str++);
if (c == '\n') {
if (x + char_w > w)
w = x + char_w;
y += char_h;
x = 0;
} else
x += char_w;
h = y + char_h;
}
if (x > w)
w = x;
return point_ui16((uint16_t)w, (uint16_t)h);
}
int font_line_chars(font_t* pf, const char* str, uint16_t line_width)
{
int w = 0;
int char_w = pf->w;
int len = strlen(str);
int n = 0;
char c;
// This is generally about finding the closest '\n' character within the current line to be drawn.
// Line is limited by pixel dimension, all characters have the same fixed pixel size
// Such character may not be found, so n becomes > len
while ((w + char_w) <= line_width) {
c = str[n++];
if (c == '\n')
break;
else
w += char_w;
}
// if the line width is >= than characters to be printed, skip further search
// and just return len - i.e. the whole string is to be printed at once.
if (n >= len)
return len; // must return here to prevent touching memory beyond str in the next while cycle
while ((n > 0) && ((str[n] != ' ') && (str[n] != '\n'))) {
n--;
}
if (n == 0)
n = line_width / char_w;
return n;
}
point_ui16_t icon_meas(const uint8_t* pi)
{
point_ui16_t wh = { 0, 0 };
if (memcmp(pi, "\x89PNG", 4) == 0) {
wh.x = swap_ui16(*((uint16_t*)(pi + 18)));
wh.y = swap_ui16(*((uint16_t*)(pi + 22)));
}
return wh;
}
extern const resource_entry_t resource_table[];
extern const uint16_t resource_table_size;
extern const uint16_t resource_count;
const uint8_t* resource_ptr(uint16_t id)
{
if (id < resource_count)
return resource_table[id].ptr;
return 0;
}
uint16_t resource_size(uint16_t id)
{
if (id < resource_count)
return resource_table[id].size;
return 0;
}
FILE* resource_fopen(uint16_t id, const char* opentype)
{
if (id < resource_count)
return fmemopen((uint8_t*)resource_table[id].ptr, resource_table[id].size, opentype);
return 0;
}
font_t* resource_font(uint16_t id)
{
if (id < resource_count)
return (font_t*)resource_table[id].ptr;
return 0;
}
efe9a7c8eThis file is pinned to the selected release. Download the original to open it in your design tool.