replaced confusing u64 joinded colors with struct TimStyle

This commit is contained in:
2026-01-09 09:45:00 +05:00
parent 161f655492
commit 1ce7090bd6
13 changed files with 285 additions and 181 deletions

View File

@@ -60,42 +60,42 @@ extern "C" {
#pragma region constants
#define TIM_ENABLE_DBUF 1 // double buffering
#define TIM_MAX_SCOPE 20 // max scope nesting
#define TIM_MAX_SCOPE 32 // max scope nesting
#define TIM_MAX_CELLS 0x20000 // size of screen buffer
#define TIM_MAX_BUF (TIM_MAX_CELLS * 4) // size of output buffer
#define A INT_MAX // auto center / width / height
typedef enum TimEventType {
TimEvent_Void, // an event was consumed
TimEvent_Draw, // draw screen
TimEvent_Key, // a key was pressed
TimEvent_Mouse, // mouse button, scroll or move
} TimEventType;
enum {
TimKey_MouseButtonLeft = 1,
TimKey_Backspace = 8,
TimKey_Tab = 9,
TimKey_Enter = 13,
TimKey_Escape = 27,
TimKey_Insert = -1,
TimKey_Delete = -2,
TimKey_Home = -3,
TimKey_End = -4,
TimKey_PageUp = -5,
TimKey_PageDown = -6,
TimKey_Up = -7,
TimKey_Down = -8,
TimKey_Left = -9,
TimKey_Right = -10,
};
// key code or 32-bit unicode char
typedef i32 TimKey;
#pragma endregion
#pragma region types
/* first 16 colors from xterm256 supported by any terminal emulator */
enum {
TimColor16_Black = 0x00,
TimColor16_DarkRed = 0x01,
TimColor16_DarkGreen = 0x02,
TimColor16_DarkYellow = 0x03,
TimColor16_DarkBlue = 0x04,
TimColor16_DarkMagenta = 0x05,
TimColor16_DarkCyan = 0x06,
TimColor16_Gray = 0x07,
TimColor16_DarkGray = 0x08,
TimColor16_Red = 0x09,
TimColor16_Green = 0x0a,
TimColor16_Yellow = 0x0b,
TimColor16_Blue = 0x0c,
TimColor16_Magenta = 0x0d,
TimColor16_Cyan = 0x0e,
TimColor16_White = 0x0f,
};
typedef struct TimStyle {
u8 brd; // border
u8 bg; // background
u8 fg; // foreground
} TimStyle;
typedef struct TimCell {
u8 fg; // foreground color
u8 bg; // background color
@@ -124,6 +124,33 @@ typedef struct TimLine {
i32 width; // line width in glyph
} TimLine;
typedef enum TimEventType {
TimEvent_Void, // an event was consumed
TimEvent_Draw, // draw screen
TimEvent_Key, // a key was pressed
TimEvent_Mouse, // mouse button, scroll or move
} TimEventType;
enum {
TimKey_MouseButtonLeft = 1,
TimKey_Backspace = 8,
TimKey_Tab = 9,
TimKey_Enter = 13,
TimKey_Escape = 27,
TimKey_Insert = -1,
TimKey_Delete = -2,
TimKey_Home = -3,
TimKey_End = -4,
TimKey_PageUp = -5,
TimKey_PageDown = -6,
TimKey_Up = -7,
TimKey_Down = -8,
TimKey_Left = -9,
TimKey_Right = -10,
};
// key code or 32-bit unicode char
typedef i32 TimKey;
typedef struct TimEvent {
TimEventType type;
TimKey key; // used by TimEvent_Key and TimEvent_Mouse
@@ -133,9 +160,9 @@ typedef struct TimEvent {
} TimEvent;
typedef struct TimEditState {
i32 cursor; // cursor position (utf8)
i32 length; // string length (utf8)
i32 capacity; // buffer size
i32 cursor; // cursor position (utf8)
i32 length; // string length (utf8)
i32 capacity; // buffer size in bytes
char* s; // zero terminated buffer
} TimEditState;
@@ -195,9 +222,17 @@ i64 tim_time_usec(void);
#pragma region scope
// for some stupid reason gcc requires more than 3 levels of macros to concat _i_ with line number
#define _tim_cat2(A, B) A##B
#define _tim_scope_line(L) _tim_cat2(_i_, L)
#define _tim_scope_i _tim_scope_line(__LINE__)
// enter layout scope
#define tim_scope(x, y, w, h) \
for (i32 _i = tim_enter_scope((x), (y), (w), (h)); _i; _i = tim_exit_scope())
#define tim_scope(x, y, w, h) for (\
i32 _tim_scope_i = tim_enter_scope((x), (y), (w), (h)); \
_tim_scope_i;\
_tim_scope_i = tim_exit_scope()\
) /* here goes your { code } */
// convert relative (scoped) to absolute (screen) coordinates
TimRect tim_scope_rect_to_absolute(i32 x, i32 y, i32 w, i32 h);
@@ -217,35 +252,35 @@ i32 tim_exit_scope(void);
// frame
// color: background, frame
void tim_frame(i32 x, i32 y, i32 w, i32 h, u64 color);
void tim_frame(i32 x, i32 y, i32 w, i32 h, TimStyle style);
// text label
// str : text - supports multiple lines
// color: background, text
void tim_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, TimStyle style);
// button - returns true on click
// color: frame, background, text
bool tim_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, TimStyle style);
// check box - returns true when clicked
// txt : text label
// state: persistent state, 0 unchecked, -1 semi checked, !0: checked
// color: check, background, text
bool tim_checkbox(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, TimStyle style);
// radio button - return true when clicked
// txt : text label
// state: persistent state, selected if *state == v
// v : value
// color: radio, background, text
bool tim_radiobutton(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, TimStyle style);
/// 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
TimKey tim_edit(TimEditState* e, i32 x, i32 y, i32 w, u64 color);
TimKey tim_edit(TimEditState* e, i32 x, i32 y, i32 w, TimStyle style);
void TimEditState_init(TimEditState* e, i32 capacity, cstr initial_content);