2025-09-28 13:46:18 +00:00
|
|
|
/* $Id$ */
|
2025-09-29 00:10:20 +00:00
|
|
|
#include <Mw/Milsko.h>
|
2025-09-28 13:46:18 +00:00
|
|
|
|
2025-09-30 17:47:03 +00:00
|
|
|
#include <stb_image.h>
|
|
|
|
|
|
2025-09-28 13:46:18 +00:00
|
|
|
static int hex(const char* txt, int len) {
|
|
|
|
|
int i;
|
|
|
|
|
int r = 0;
|
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
|
char c = txt[i];
|
|
|
|
|
int n = 0;
|
|
|
|
|
if('a' <= c && c <= 'f') {
|
|
|
|
|
n = c - 'a' + 10;
|
|
|
|
|
} else if('A' <= c && c <= 'F') {
|
|
|
|
|
n = c - 'A' + 10;
|
|
|
|
|
} else if('0' <= c && c <= '9') {
|
|
|
|
|
n = c - '0';
|
|
|
|
|
}
|
|
|
|
|
r = r << 4;
|
|
|
|
|
r |= n;
|
|
|
|
|
}
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLColor MwParseColor(MwWidget handle, const char* text) {
|
2025-09-28 13:46:18 +00:00
|
|
|
int r = 0;
|
|
|
|
|
int g = 0;
|
|
|
|
|
int b = 0;
|
|
|
|
|
|
|
|
|
|
if(text[0] == '#' && strlen(text) == 4) {
|
|
|
|
|
r = hex(text + 1, 1);
|
|
|
|
|
g = hex(text + 2, 1);
|
|
|
|
|
b = hex(text + 3, 1);
|
|
|
|
|
|
|
|
|
|
r |= r << 4;
|
|
|
|
|
g |= g << 4;
|
|
|
|
|
b |= b << 4;
|
|
|
|
|
} else if(text[0] == '#' && strlen(text) == 7) {
|
|
|
|
|
r = hex(text + 1, 2);
|
|
|
|
|
g = hex(text + 3, 2);
|
|
|
|
|
b = hex(text + 5, 2);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
return MwLLAllocColor(handle->lowlevel, r, g, b);
|
2025-09-28 13:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwDrawRect(MwWidget handle, MwRect* rect, MwLLColor color) {
|
|
|
|
|
MwPoint p[4];
|
2025-09-28 13:46:18 +00:00
|
|
|
|
|
|
|
|
p[0].x = rect->x;
|
|
|
|
|
p[0].y = rect->y;
|
|
|
|
|
|
|
|
|
|
p[1].x = rect->x + rect->width;
|
|
|
|
|
p[1].y = rect->y;
|
|
|
|
|
|
|
|
|
|
p[2].x = rect->x + rect->width;
|
|
|
|
|
p[2].y = rect->y + rect->height;
|
|
|
|
|
|
|
|
|
|
p[3].x = rect->x;
|
|
|
|
|
p[3].y = rect->y + rect->height;
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLPolygon(handle->lowlevel, p, 4, color);
|
2025-09-28 13:46:18 +00:00
|
|
|
}
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwDrawFrame(MwWidget handle, MwRect* rect, MwLLColor color, int invert) {
|
2025-09-29 14:29:34 +00:00
|
|
|
const int border = 2;
|
|
|
|
|
|
|
|
|
|
MwDrawFrameEx(handle, rect, color, invert, border);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MwDrawFrameEx(MwWidget handle, MwRect* rect, MwLLColor color, int invert, int border) {
|
2025-09-29 00:10:20 +00:00
|
|
|
MwPoint p[6];
|
|
|
|
|
const int diff = 128;
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLColor darker = MwLLAllocColor(handle->lowlevel, color->red - diff, color->green - diff, color->blue - diff);
|
|
|
|
|
MwLLColor lighter = MwLLAllocColor(handle->lowlevel, color->red + diff, color->green + diff, color->blue + diff);
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 14:29:34 +00:00
|
|
|
p[0].x = rect->x;
|
|
|
|
|
p[0].y = rect->y;
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 14:29:34 +00:00
|
|
|
p[1].x = rect->x + rect->width;
|
|
|
|
|
p[1].y = rect->y;
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 14:29:34 +00:00
|
|
|
p[2].x = rect->x + rect->width - border;
|
|
|
|
|
p[2].y = rect->y + border;
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 14:29:34 +00:00
|
|
|
p[3].x = rect->x + border;
|
|
|
|
|
p[3].y = rect->y + border;
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 14:29:34 +00:00
|
|
|
p[4].x = rect->x + border;
|
|
|
|
|
p[4].y = rect->y + rect->height - border;
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 14:29:34 +00:00
|
|
|
p[5].x = rect->x;
|
|
|
|
|
p[5].y = rect->y + rect->height;
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLPolygon(handle->lowlevel, p, 6, invert ? darker : lighter);
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 14:29:34 +00:00
|
|
|
p[0].x = rect->x + rect->width;
|
|
|
|
|
p[0].y = rect->y;
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 14:29:34 +00:00
|
|
|
p[1].x = rect->x + rect->width - border;
|
|
|
|
|
p[1].y = rect->y + border;
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 14:29:34 +00:00
|
|
|
p[2].x = rect->x + rect->width - border;
|
|
|
|
|
p[2].y = rect->y + rect->height - border;
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 14:29:34 +00:00
|
|
|
p[3].x = rect->x + border;
|
|
|
|
|
p[3].y = rect->y + rect->height - border;
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 14:29:34 +00:00
|
|
|
p[4].x = rect->x;
|
|
|
|
|
p[4].y = rect->y + rect->height;
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 14:29:34 +00:00
|
|
|
p[5].x = rect->x + rect->width;
|
|
|
|
|
p[5].y = rect->y + rect->height;
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLPolygon(handle->lowlevel, p, 6, invert ? lighter : darker);
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLFreeColor(lighter);
|
|
|
|
|
MwLLFreeColor(darker);
|
2025-09-28 14:36:24 +00:00
|
|
|
|
2025-09-29 05:24:32 +00:00
|
|
|
rect->x += border;
|
|
|
|
|
rect->y += border;
|
|
|
|
|
rect->width -= border * 2;
|
|
|
|
|
rect->height -= border * 2;
|
2025-09-28 14:36:24 +00:00
|
|
|
}
|
2025-09-29 02:43:01 +00:00
|
|
|
|
2025-09-29 02:43:10 +00:00
|
|
|
void MwDrawText(MwWidget handle, MwPoint* point, const char* text, MwLLColor color) {
|
2025-09-29 02:54:08 +00:00
|
|
|
int i, x, y, sx, sy, sc = 1;
|
2025-09-29 09:16:44 +00:00
|
|
|
int fw = 7, fh = 14;
|
2025-09-29 02:43:01 +00:00
|
|
|
MwRect r;
|
|
|
|
|
|
2025-09-29 09:16:44 +00:00
|
|
|
sx = point->x - strlen(text) * fw * sc / 2;
|
|
|
|
|
sy = point->y - fh * sc / 2;
|
2025-09-29 02:43:01 +00:00
|
|
|
|
2025-09-29 02:43:10 +00:00
|
|
|
for(i = 0; text[i] != 0; i++) {
|
2025-09-29 09:16:44 +00:00
|
|
|
for(y = 0; y < fh; y++) {
|
|
|
|
|
for(x = 0; x < fw; x++) {
|
2025-09-29 02:54:08 +00:00
|
|
|
r.x = sx + x * sc;
|
|
|
|
|
r.y = sy + y * sc;
|
|
|
|
|
r.width = sc;
|
|
|
|
|
r.height = sc;
|
2025-09-29 02:43:01 +00:00
|
|
|
|
2025-09-29 09:16:44 +00:00
|
|
|
if(MwFontData[(unsigned char)text[i]].data[y] & (1 << ((fw - 1) - x))) {
|
2025-09-29 02:43:01 +00:00
|
|
|
MwDrawRect(handle, &r, color);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-29 14:29:34 +00:00
|
|
|
sx += fw * sc;
|
2025-09-29 02:43:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-30 17:47:03 +00:00
|
|
|
|
|
|
|
|
MwLLPixmap MwLoadImage(MwWidget handle, const char* path) {
|
|
|
|
|
int width, height, ch;
|
|
|
|
|
unsigned char* rgb = stbi_load(path, &width, &height, &ch, 3);
|
|
|
|
|
MwLLPixmap px;
|
|
|
|
|
|
|
|
|
|
if(rgb == NULL) return NULL;
|
|
|
|
|
|
|
|
|
|
px = MwLLCreatePixmap(handle->lowlevel, rgb, width, height);
|
|
|
|
|
|
|
|
|
|
free(rgb);
|
|
|
|
|
|
|
|
|
|
return px;
|
|
|
|
|
}
|