diff --git a/example/ask.c b/example/ask.c index 371776a..d84d242 100644 --- a/example/ask.c +++ b/example/ask.c @@ -28,15 +28,15 @@ i32 main(i32 argc, char** argv) { tim_frame(0, 0, ~0, ~0, CFR); // draw message - label(argv[1], A, 1, msg.width, msg.lines, CTXT); + tim_label(argv[1], A, 1, msg.width, msg.lines, CTXT); // draw 'yes' button, return 0 when clicked - if (button("[y] Yes", 2, ~1, A, A, CYES) || tim_is_key_press('y')) { + if (tim_button("[y] Yes", 2, ~1, A, A, CYES) || tim_is_key_press('y')) { exit(0); } // draw 'no' button, return 1 when clicked - if (button("[n] No", ~2, ~1, A, A, CNO) || tim_is_key_press('n')) { + if (tim_button("[n] No", ~2, ~1, A, A, CNO) || tim_is_key_press('n')) { exit(1); } diff --git a/example/hello.c b/example/hello.c index 2864034..328de35 100644 --- a/example/hello.c +++ b/example/hello.c @@ -5,8 +5,8 @@ i32 main(void) { tim_scope(A, A, 24, 8) { // centered 24x8 scope u64 c = 0x0a060f; // three colors tim_frame(0, 0, ~0, ~0, c); // draw frame for scope - label("Greetings!", A, 2, A, A, c); // label in top center - if (button("OK", A, ~1, 8, A, c)) // button in bottom center + tim_label("Greetings!", A, 2, A, A, c); // label in top center + if (tim_button("OK", A, ~1, 8, A, c)) // button in bottom center return 0; // exit on button click if (tim_is_key_press('q')) // ctrl-c is masked return 0; // exit on 'q' press diff --git a/example/snek.c b/example/snek.c index 63a86e7..278be84 100644 --- a/example/snek.c +++ b/example/snek.c @@ -102,14 +102,14 @@ static void menu(void) { tim_scope(A, A, 20, 13) { char* lbl = snek.state == OVER ? "GAME OVER" : "SNEK - THE GAME"; char* btn = snek.state == PAUSE ? "Resume" : "Play"; - label(lbl, A, 0, A, A, BTN); - if (button(btn, A, 2, 20, 5, BTN) || tim_is_key_press(TimKey_Enter)) { + tim_label(lbl, A, 0, A, A, BTN); + if (tim_button(btn, A, 2, 20, 5, BTN) || tim_is_key_press(TimKey_Enter)) { if (snek.state != PAUSE) { start(); } snek.state = RUN; } - if (button("Exit", A, 8, 20, 5, BTN) || tim_is_key_press('q') || tim_is_key_press(TimKey_Escape)) { + if (tim_button("Exit", A, 8, 20, 5, BTN) || tim_is_key_press('q') || tim_is_key_press(TimKey_Escape)) { exit(0); } } diff --git a/include/tim.h b/include/tim.h index dcc7ea5..89e72df 100644 --- a/include/tim.h +++ b/include/tim.h @@ -78,7 +78,6 @@ enum { TimKey_Tab = 9, TimKey_Enter = 13, TimKey_Escape = 27, - /* printable utf8 characters */ TimKey_Insert = -1, TimKey_Delete = -2, TimKey_Home = -3, @@ -90,6 +89,7 @@ enum { TimKey_Left = -9, TimKey_Right = -10, }; +// key code or 32-bit unicode char typedef i32 TimKey; #pragma endregion @@ -222,36 +222,36 @@ void tim_frame(i32 x, i32 y, i32 w, i32 h, u64 color); // text label // str : text - supports multiple lines // color: background, text -void label(cstr s, i32 x, i32 y, i32 w, i32 h, u64 color); +void tim_label(cstr s, i32 x, i32 y, i32 w, i32 h, u64 color); // button - returns true on click // color: frame, background, text -bool button(cstr txt, i32 x, i32 y, i32 w, i32 h, u64 color); +bool tim_button(cstr txt, i32 x, i32 y, i32 w, i32 h, u64 color); // check box - returns true when clicked // txt : text label // state: persistent state, 0 unchecked, -1 semi checked, !0: checked // color: check, background, text -bool check(cstr txt, i32* state, i32 x, i32 y, i32 w, u64 color); +bool tim_checkbox(cstr txt, i32* state, i32 x, i32 y, i32 w, u64 color); // radio button - return true when clicked // txt : text label // state: persistent state, selected if *state == v // v : value // color: radio, background, text -bool radio(cstr txt, i32* state, i32 v, i32 x, i32 y, i32 w, u64 color); +bool tim_radiobutton(cstr txt, i32* state, i32 v, i32 x, i32 y, i32 w, u64 color); /// text edit - value in state /// @param e persistent edit state, use TimEditState_init() to create new state /// @param color frame, background, text /// @return key id or 0 -i32 edit(TimEditState* e, i32 x, i32 y, i32 w, u64 color); +i32 tim_edit(TimEditState* e, i32 x, i32 y, i32 w, u64 color); void TimEditState_init(TimEditState* e, i32 capacity, cstr initial_content); -void edit_insert(TimEditState* e, cstr s); +void TimEditState_insert(TimEditState* e, cstr s); -void edit_delete(TimEditState* e); +void TimEditState_delete(TimEditState* e); #pragma endregion diff --git a/src/edit.c b/src/edit.c index 00a3407..1077f6c 100755 --- a/src/edit.c +++ b/src/edit.c @@ -11,7 +11,7 @@ void TimEditState_init(TimEditState* e, i32 capacity, cstr initial_content){ e->s[byte_len] = 0; } -void edit_insert(TimEditState* e, cstr s) { +void TimEditState_insert(TimEditState* e, cstr s) { i32 dst_size = tim_ztrlen(e->s); i32 src_size = tim_ztrlen(s); if (dst_size + src_size < e->capacity) { @@ -25,7 +25,7 @@ void edit_insert(TimEditState* e, cstr s) { } } -void edit_delete(TimEditState* e) { +void TimEditState_delete(TimEditState* e) { i32 size = tim_ztrlen(e->s); i32 cur = tim_utf8_pos(e->s, e->cursor); i32 len = tim_utf8_pos(e->s + cur, 1); @@ -55,12 +55,12 @@ static i32 edit_event(TimEditState* e, TimRect r) { tim->focus = 0; // release focus break; case TimKey_Delete: - edit_delete(e); + TimEditState_delete(e); break; case TimKey_Backspace: if (e->cursor > 0) { e->cursor -= 1; - edit_delete(e); + TimEditState_delete(e); } break; case TimKey_Left: @@ -77,7 +77,7 @@ static i32 edit_event(TimEditState* e, TimRect r) { break; default: if (tim->event.key >= ' ') { - edit_insert(e, tim->event.s); + TimEditState_insert(e, tim->event.s); } break; } @@ -85,7 +85,7 @@ static i32 edit_event(TimEditState* e, TimRect r) { return tim->event.key; } -i32 edit(TimEditState* e, i32 x, i32 y, i32 w, u64 color) { +i32 tim_edit(TimEditState* e, i32 x, i32 y, i32 w, u64 color) { TimRect r = tim_scope_rect_to_absolute(x, y, w, 3); if (tim->event.type == TimEvent_Draw) { diff --git a/src/unix.c b/src/unix.c index acfcd3c..236b4d1 100755 --- a/src/unix.c +++ b/src/unix.c @@ -68,7 +68,7 @@ bool parse_input(event* restrict e, i32 n) { if (n == 1 || s[0] != 27) { // regular key press e->type = TimEvent_Key; - e->key = s[0] == 127 ? TimKey_Backspace : tim_utf8_to_i32(s); + e->key = s[0] == 127 ? TimKey_Backspace : tim_utf8_to_i32(s); return true; } @@ -110,7 +110,7 @@ bool parse_input(event* restrict e, i32 n) { for (i32 i = 0; i < (i32)ARRAY_SIZE(key_table); i++) { if (!memcmp(s + 1, key_table[i].s, n - 1)) { e->type = TimEvent_Key; - e->key = key_table[i].k; + e->key = key_table[i].k; return true; } } diff --git a/src/widgets.c b/src/widgets.c index 69f6b01..eacb1ff 100755 --- a/src/widgets.c +++ b/src/widgets.c @@ -7,7 +7,7 @@ void tim_frame(i32 x, i32 y, i32 w, i32 h, u64 color) { } } -void label(cstr s, i32 x, i32 y, i32 w, i32 h, u64 color) { +void tim_label(cstr s, i32 x, i32 y, i32 w, i32 h, u64 color) { if (tim->event.type == TimEvent_Draw) { TimText t = tim_scan_str(s); w = (w == A) ? t.width : w; @@ -22,7 +22,7 @@ void label(cstr s, i32 x, i32 y, i32 w, i32 h, u64 color) { } } -bool button(cstr txt, i32 x, i32 y, i32 w, i32 h, u64 color) { +bool tim_button(cstr txt, i32 x, i32 y, i32 w, i32 h, u64 color) { i32 tw = tim_utf8_len(txt); w = (w == A) ? (tw + 4) : w; h = (h == A) ? 3 : h; @@ -35,7 +35,7 @@ bool button(cstr txt, i32 x, i32 y, i32 w, i32 h, u64 color) { return tim_is_mouse_click_over(r); } -bool check(cstr txt, i32* state, i32 x, i32 y, i32 w, u64 color) { +bool tim_checkbox(cstr txt, i32* state, i32 x, i32 y, i32 w, u64 color) { w = (w == A) ? tim_utf8_len(txt) + 4 : w; TimRect r = tim_scope_rect_to_absolute(x, y, w, 1); @@ -52,7 +52,7 @@ bool check(cstr txt, i32* state, i32 x, i32 y, i32 w, u64 color) { } -bool radio(cstr txt, i32* state, i32 v, i32 x, i32 y, i32 w, u64 color) { +bool tim_radiobutton(cstr txt, i32* state, i32 v, i32 x, i32 y, i32 w, u64 color) { w = (w == A) ? tim_utf8_len(txt) + 4 : w; TimRect r = tim_scope_rect_to_absolute(x, y, w, 1); diff --git a/src/windows.c b/src/windows.c index b7d98b0..987ee42 100755 --- a/src/windows.c +++ b/src/windows.c @@ -149,7 +149,7 @@ void tim_read_event(i32 timeout_ms) { } tim_update_screen_size(); // workaround, see WINDOW_BUFFER_SIZE_EVENT e->type = TimEvent_Mouse; - e->key = TimKey_MouseButtonLeft; + e->key = TimKey_MouseButtonLeft; e->x = rec.Event.MouseEvent.dwMousePosition.X - tim->window.Left; e->y = rec.Event.MouseEvent.dwMousePosition.Y - tim->window.Top; return; diff --git a/test/test.c b/test/test.c index f03350f..33f0a8d 100644 --- a/test/test.c +++ b/test/test.c @@ -10,55 +10,55 @@ static inline void test_screen(TimEvent* e) { me = (e->type == MOUSE_EVENT) ? *e : me; // positioning - label("+", 0, 0, A, A, 0xf); - label("+", ~0, 0, A, A, 0xf); - label("+", 0, ~0, A, A, 0xf); - label("+", ~0, ~0, A, A, 0xf); - label("+", A, A, A, A, 0xf); - label("-", 0, A, A, A, 0xf); - label("-", ~0, A, A, A, 0xf); - label("|", A, 0, A, A, 0xf); - label("|", A, ~0, A, A, 0xf); + tim_label("+", 0, 0, A, A, 0xf); + tim_label("+", ~0, 0, A, A, 0xf); + tim_label("+", 0, ~0, A, A, 0xf); + tim_label("+", ~0, ~0, A, A, 0xf); + tim_label("+", A, A, A, A, 0xf); + tim_label("-", 0, A, A, A, 0xf); + tim_label("-", ~0, A, A, A, 0xf); + tim_label("|", A, 0, A, A, 0xf); + tim_label("|", A, ~0, A, A, 0xf); // some information sprintf(buf, "screen: %dx%d", tim->w, tim->h); - label(buf, 2, 0, A, A, 0xf); + tim_label(buf, 2, 0, A, A, 0xf); sprintf(buf, "frame : [%c] %d", ": "[tim->frame & 1], tim->frame); - label(buf, 2, 1, A, A, 0xf); + tim_label(buf, 2, 1, A, A, 0xf); sprintf(buf, "key : [%d] %s", ke.key, ke.s + (ke.key < 32)); - label(buf, 2, 2, A, A, 0xf); + tim_label(buf, 2, 2, A, A, 0xf); sprintf(buf, "mouse : [%d] %d:%d", me.key, me.x, me.y); - label(buf, 2, 3, A, A, 0xf); + tim_label(buf, 2, 3, A, A, 0xf); 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]); - label(buf, 2, 4, A, A, 0xf); + tim_label(buf, 2, 4, A, A, 0xf); // lower right render_us += tim->render_us; sprintf(buf, "%d µs (Ø %d µs)", tim->render_us, render_us / MAX(tim->frame, 1)); - label(buf, ~2, ~2, A, A, 0xf); + tim_label(buf, ~2, ~2, A, A, 0xf); sprintf(buf, "%d cells (%.0f %%)", tim->w * tim->h, 100.0 * tim->w * tim->h / TIM_MAX_CELLS); - label(buf, ~2, ~1, A, A, 0xf); + tim_label(buf, ~2, ~1, A, A, 0xf); sprintf(buf, "%d bytes (%.0f %%)", tim->buf_size, 100.0 * tim->buf_size / TIM_MAX_BUF); - label(buf, ~2, ~0, A, A, 0xf); + tim_label(buf, ~2, ~0, A, A, 0xf); // multi line label - label("multi\nliñe\nlabël", 24, 1, A, A, 0xf); + tim_label("multi\nliñe\nlabël", 24, 1, A, A, 0xf); // colors tim_scope(1, 5, 16, 5) { tim_frame(0, 0, ~0, ~0, 0xf); - label(" Red ", 1, 1, 7, A, 0x0900); - label(" ", 8, 1, 7, A, 0xc400); - label(" Green ", 1, 2, 7, A, 0x0a00); - label(" ", 8, 2, 7, A, 0x2e00); - label(" Blue ", 1, 3, 7, A, 0x0c00); - label(" ", 8, 3, 7, A, 0x1500); + tim_label(" Red ", 1, 1, 7, A, 0x0900); + tim_label(" ", 8, 1, 7, A, 0xc400); + tim_label(" Green ", 1, 2, 7, A, 0x0a00); + tim_label(" ", 8, 2, 7, A, 0x2e00); + tim_label(" Blue ", 1, 3, 7, A, 0x0c00); + tim_label(" ", 8, 3, 7, A, 0x1500); } // button static u64 bc = 0x100; - if (button("Click Me", 17, 5, 16, 5, bc)) { + if (tim_button("Click Me", 17, 5, 16, 5, bc)) { bc = (bc + 0x100) & 0xff00; } @@ -67,23 +67,23 @@ static inline void test_screen(TimEvent* e) { static TimEditState ed2; TimEditState_init(&ed1, 32, "Edit 1"); TimEditState_init(&ed2, 32, ""); - edit(&ed1, 1, 10, 32, 0xff00ff); + tim_edit(&ed1, 1, 10, 32, 0xff00ff); sprintf(buf, "cursor: %d length: %d", ed1.cursor, ed1.length); - label(buf, 2, 13, A, A, 0xf); - edit(&ed2, 1, 14, 32, 0xff00ff); - label(ed2.s, 2, 17, A, A, 0xf); + tim_label(buf, 2, 13, A, A, 0xf); + tim_edit(&ed2, 1, 14, 32, 0xff00ff); + tim_label(ed2.s, 2, 17, A, A, 0xf); // checkbox static i32 chk[2] = {-1, 1}; - check("Check 1", &chk[0], 1, 18, A, 0xa000f); - check("Check 2", &chk[1], 14, 18, A, 0xa000f); + tim_checkbox("Check 1", &chk[0], 1, 18, A, 0xa000f); + tim_checkbox("Check 2", &chk[1], 14, 18, A, 0xa000f); // radiobox static i32 rad = 0; - radio("Radio 1", &rad, 1, 1, 19, A, 0xa000f); - radio("Radio 2", &rad, 2, 14, 19, A, 0xa000f); - radio("Radio 3", &rad, 3, 1, 20, A, 0xa000f); - radio("Radio 4", &rad, 4, 14, 20, A, 0xa000f); + tim_radiobutton("Radio 1", &rad, 1, 1, 19, A, 0xa000f); + tim_radiobutton("Radio 2", &rad, 2, 14, 19, A, 0xa000f); + tim_radiobutton("Radio 3", &rad, 3, 1, 20, A, 0xa000f); + tim_radiobutton("Radio 4", &rad, 4, 14, 20, A, 0xa000f); // scope nesting tim_scope(~1, 1, 20, 10) { @@ -104,9 +104,9 @@ static inline void test_screen(TimEvent* e) { // funny characters tim_scope(~1, ~3, 11, 5) { tim_frame(0, 0, ~0, ~0, 0xf); - label("123456789", 1, 1, 9, A, 0x0f05); - label("$£ह€𐍈6789", 1, 2, A, A, 0x0f05); - label("圍棋56789", 1, 3, A, A, 0x0f05); + tim_label("123456789", 1, 1, 9, A, 0x0f05); + tim_label("$£ह€𐍈6789", 1, 2, A, A, 0x0f05); + tim_label("圍棋56789", 1, 3, A, A, 0x0f05); } }