Files
tim/test/test.c

143 lines
5.2 KiB
C

#include "tim.h"
static TimEditState ed1;
static TimEditState ed2;
static inline void test_screen(TimEvent* e) {
static TimEvent me = {0};
static TimEvent ke = {0};
static i32 render_us = 0;
char buf[64] = {0};
if(e->type == TimEvent_Key)
ke = *e;
if(e->type == TimEvent_Mouse)
me = *e;
// positioning
static TimStyle style_default = { .fg = TimColor16_White };
tim_label("+", 0, 0, A, A, style_default);
tim_label("+", ~0, 0, A, A, style_default);
tim_label("+", 0, ~0, A, A, style_default);
tim_label("+", ~0, ~0, A, A, style_default);
tim_label("+", A, A, A, A, style_default);
tim_label("-", 0, A, A, A, style_default);
tim_label("-", ~0, A, A, A, style_default);
tim_label("|", A, 0, A, A, style_default);
tim_label("|", A, ~0, A, A, style_default);
// some information
sprintf(buf, "screen: %dx%d", tim->w, tim->h);
tim_label(buf, 2, 0, A, A, style_default);
sprintf(buf, "frame : [%c] %d", ": "[tim->frame & 1], tim->frame);
tim_label(buf, 2, 1, A, A, style_default);
sprintf(buf, "key : [%d] %s", ke.key, ke.s + (ke.key < 32));
tim_label(buf, 2, 2, A, A, style_default);
sprintf(buf, "mouse : [%d] %d:%d", me.key, me.x, me.y);
tim_label(buf, 2, 3, A, A, style_default);
sprintf(buf, "input : %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx",
e->s[0], e->s[1], e->s[2], e->s[3], e->s[4], e->s[5], e->s[6], e->s[7]);
tim_label(buf, 2, 4, A, A, style_default);
// replace unprintable characters with space
i32 s_len = tim_utf8_len(e->s);
for(i32 i = 0; i < s_len; i++){
i32 pos = tim_utf8_pos(e->s, i);
u8 uc = e->s[pos];
if(uc < (u8)' '){
e->s[pos] = ' ';
}
}
tim_label(e->s, 2, 5, A, A, style_default);
// lower right
render_us += tim->render_us;
sprintf(buf, "%d µs (Ø %d µs)", tim->render_us, render_us / MAX(tim->frame, 1));
tim_label(buf, ~2, ~2, A, A, style_default);
sprintf(buf, "%d cells (%.0f %%)", tim->w * tim->h, 100.0 * tim->w * tim->h / TIM_MAX_CELLS);
tim_label(buf, ~2, ~1, A, A, style_default);
sprintf(buf, "%d bytes (%.0f %%)", tim->buf_size, 100.0 * tim->buf_size / TIM_MAX_BUF);
tim_label(buf, ~2, ~0, A, A, style_default);
// multi line label
tim_label("multi\nliñe\nlabël", 24, 1, A, A, style_default);
// colors
tim_scope(1, 6, 16, 5) {
tim_frame(0, 0, ~0, ~0, style_default);
tim_label(" Red ", 1, 1, 7, A, (TimStyle){ .bg = 0x09 });
tim_label(" ", 8, 1, 7, A, (TimStyle){ .bg = 0xc4 });
tim_label(" Green ", 1, 2, 7, A, (TimStyle){ .bg = 0x0a });
tim_label(" ", 8, 2, 7, A, (TimStyle){ .bg = 0x2e });
tim_label(" Blue ", 1, 3, 7, A, (TimStyle){ .bg = 0x0c });
tim_label(" ", 8, 3, 7, A, (TimStyle){ .bg = 0x15 });
}
// button
static TimStyle style_button = { .bg = 0x01 };
if (tim_button("Click Me", 17, 6, 16, 5, style_button)) {
style_button.bg = (style_button.bg + 1) & 0xff;
}
// edit
static TimStyle style_edit = { .brd = 0xff, .fg = 0xff };
tim_edit(&ed1, 1, 11, 32, style_edit);
sprintf(buf, "cursor: %d length: %d", ed1.cursor, ed1.length);
tim_label(buf, 2, 12, A, A, style_default);
tim_edit(&ed2, 1, 15, 32, style_edit);
tim_label(ed2.s, 2, 18, A, A, style_default);
// checkbox
static TimStyle style_checkbox = { .brd = TimColor16_Cyan, .fg = TimColor16_White };
static i32 chk[2] = {-1, 1};
tim_checkbox("Check 1", &chk[0], 1, 19, A, style_checkbox);
tim_checkbox("Check 2", &chk[1], 14, 19, A, style_checkbox);
// radiobox
static i32 rad = 0;
tim_radiobutton("Radio 1", &rad, 1, 1, 20, A, style_checkbox);
tim_radiobutton("Radio 2", &rad, 2, 14, 20, A, style_checkbox);
tim_radiobutton("Radio 3", &rad, 3, 1, 21, A, style_checkbox);
tim_radiobutton("Radio 4", &rad, 4, 14, 21, A, style_checkbox);
// scope nesting
tim_scope(~1, 1, 20, 10) {
tim_scope(0, 0, 10, 5) {
tim_frame(0, 0, ~0, ~0, (TimStyle){ .brd = TimColor16_Red });
}
tim_scope(~0, 0, 10, 5) {
tim_frame(0, 0, ~0, ~0, (TimStyle){ .brd = TimColor16_Green });
}
tim_scope(~0, ~0, 10, 5) {
tim_frame(0, 0, ~0, ~0, (TimStyle){ .brd = TimColor16_Yellow });
}
tim_scope(0, ~0, 10, 5) {
tim_frame(0, 0, ~0, ~0, (TimStyle){ .brd = TimColor16_Blue });
}
}
// funny characters
static TimStyle style_funny = { .bg = TimColor16_White, .fg = TimColor16_Magenta };
tim_scope(~1, ~3, 11, 5) {
tim_frame(0, 0, ~0, ~0, style_default);
tim_label("123456789", 1, 1, 9, A, style_funny);
tim_label("$£ह€𐍈6789", 1, 2, A, A, style_funny);
tim_label("圍棋56789", 1, 3, A, A, style_funny);
}
}
i32 main(void) {
char ed1_buf[32];
char ed2_buf[32];
TimEditState_construct(&ed1, ed1_buf, ARRAY_SIZE(ed1_buf), "Edit 1");
TimEditState_construct(&ed2, ed2_buf, ARRAY_SIZE(ed2_buf), NULL);
while (tim_run(1.5)) {
test_screen(&tim->event);
if (tim_is_key_press('q') || tim_is_key_press(TimKey_Escape)) {
break;
}
}
return 0;
}