From fe65342fad6b0ed27e899d1aa17ae77fcc1a6e62 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Sun, 24 May 2026 02:32:41 +0200 Subject: [PATCH 1/2] message: move marquee functions from nuklear_console.h to nuklear_console_message.h --- nuklear_console.h | 71 -------------------------------------- nuklear_console_message.h | 72 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 71 deletions(-) diff --git a/nuklear_console.h b/nuklear_console.h index fccd040..be978a8 100644 --- a/nuklear_console.h +++ b/nuklear_console.h @@ -475,77 +475,6 @@ static void* nk_console_cvector_memmove(void* dest, const void* src, nk_size cou #endif #include CVECTOR_H -#ifndef NK_CONSOLE_MARQUEE_SCROLL_SPEED -#define NK_CONSOLE_MARQUEE_SCROLL_SPEED 60.0f -#endif -#ifndef NK_CONSOLE_MARQUEE_SCROLL_PAUSE -#define NK_CONSOLE_MARQUEE_SCROLL_PAUSE 1.5f -#endif - -/** - * Advance a marquee scroll offset and return a pointer to the visible slice of text. - * Returns `text` unchanged when the text fits or delta time is unavailable. - * The caller owns `buf` (minimum `buf_size` bytes). - */ -static const char* nk_console_marquee_slice( - struct nk_context* ctx, - const char* text, int text_len, - float full_text_width, float avail_width, - float speed, float pause, - float* scroll_x, - char* buf, int buf_size) -{ - if (full_text_width <= avail_width || ctx->delta_time_seconds <= 0) { - return text; - } - float pause_pixels = pause * speed; - float total_cycle = full_text_width + pause_pixels; - *scroll_x += ctx->delta_time_seconds * speed; - if (*scroll_x > total_cycle) { - *scroll_x -= total_cycle; - } - float offset = *scroll_x - pause_pixels; - if (offset <= 0.0f) { - return text; - } - int start = 0; - for (int i = 1; i <= text_len; i++) { - float w = ctx->style.font->width(ctx->style.font->userdata, ctx->style.font->height, text, i); - if (w >= offset) { start = i - 1; break; } - if (i == text_len) { start = text_len; } - } - int copy_len = text_len - start; - if (copy_len >= buf_size) { - copy_len = buf_size - 1; - } - NK_MEMCPY(buf, text + start, (nk_size)copy_len); - buf[copy_len] = '\0'; - return buf; -} - -/** - * Render a single-line marquee tooltip of `tooltip_width` at the current mock mouse position. - */ -static void nk_console_tooltip_render_marquee( - struct nk_context* ctx, - const char* text, int text_len, - float full_text_width, - float tooltip_width, float text_height, - float speed, float pause, - float* scroll_x) -{ - float avail_width = tooltip_width - ctx->style.window.padding.x * 2.0f; - char display_buf[256]; - const char* display_text = nk_console_marquee_slice(ctx, text, text_len, - full_text_width, avail_width, speed, pause, scroll_x, display_buf, (int)sizeof(display_buf)); - struct nk_vec2 zero; - nk_zero_struct(zero); - if (nk_tooltip_begin_offset(ctx, tooltip_width, NK_TOP_LEFT, zero)) { - nk_layout_row_dynamic(ctx, text_height, 1); - nk_label(ctx, display_text, NK_TEXT_LEFT); - nk_tooltip_end(ctx); - } -} #ifdef __cplusplus extern "C" { diff --git a/nuklear_console_message.h b/nuklear_console_message.h index fdabadb..ee52ee5 100644 --- a/nuklear_console_message.h +++ b/nuklear_console_message.h @@ -27,6 +27,78 @@ NK_API void nk_console_show_message(nk_console* console, const char* text); extern "C" { #endif +#ifndef NK_CONSOLE_MARQUEE_SCROLL_SPEED +#define NK_CONSOLE_MARQUEE_SCROLL_SPEED 60.0f +#endif +#ifndef NK_CONSOLE_MARQUEE_SCROLL_PAUSE +#define NK_CONSOLE_MARQUEE_SCROLL_PAUSE 1.5f +#endif + +/** + * Advance a marquee scroll offset and return a pointer to the visible slice of text. + * Returns `text` unchanged when the text fits or delta time is unavailable. + * The caller owns `buf` (minimum `buf_size` bytes). + */ +static const char* nk_console_marquee_slice( + struct nk_context* ctx, + const char* text, int text_len, + float full_text_width, float avail_width, + float speed, float pause, + float* scroll_x, + char* buf, int buf_size) +{ + if (full_text_width <= avail_width || ctx->delta_time_seconds <= 0) { + return text; + } + float pause_pixels = pause * speed; + float total_cycle = full_text_width + pause_pixels; + *scroll_x += ctx->delta_time_seconds * speed; + if (*scroll_x > total_cycle) { + *scroll_x -= total_cycle; + } + float offset = *scroll_x - pause_pixels; + if (offset <= 0.0f) { + return text; + } + int start = 0; + for (int i = 1; i <= text_len; i++) { + float w = ctx->style.font->width(ctx->style.font->userdata, ctx->style.font->height, text, i); + if (w >= offset) { start = i - 1; break; } + if (i == text_len) { start = text_len; } + } + int copy_len = text_len - start; + if (copy_len >= buf_size) { + copy_len = buf_size - 1; + } + NK_MEMCPY(buf, text + start, (nk_size)copy_len); + buf[copy_len] = '\0'; + return buf; +} + +/** + * Render a single-line marquee tooltip of `tooltip_width` at the current mock mouse position. + */ +static void nk_console_tooltip_render_marquee( + struct nk_context* ctx, + const char* text, int text_len, + float full_text_width, + float tooltip_width, float text_height, + float speed, float pause, + float* scroll_x) +{ + float avail_width = tooltip_width - ctx->style.window.padding.x * 2.0f; + char display_buf[256]; + const char* display_text = nk_console_marquee_slice(ctx, text, text_len, + full_text_width, avail_width, speed, pause, scroll_x, display_buf, (int)sizeof(display_buf)); + struct nk_vec2 zero; + nk_zero_struct(zero); + if (nk_tooltip_begin_offset(ctx, tooltip_width, NK_TOP_LEFT, zero)) { + nk_layout_row_dynamic(ctx, text_height, 1); + nk_label(ctx, display_text, NK_TEXT_LEFT); + nk_tooltip_end(ctx); + } +} + #ifndef NK_CONSOLE_MESSAGE_DURATION /** * A float determining how many seconds messages should be shown for. From f7b8df4a992e3d34931ed504b338fd335aa4b16f Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Sun, 24 May 2026 03:14:27 +0200 Subject: [PATCH 2/2] marquee: move to nuklear_console_marquee.h, rename to nk_console_marquee_tooltip_render --- nuklear_console.h | 4 +- nuklear_console_marquee.h | 83 +++++++++++++++++++++++++++++++++++++++ nuklear_console_message.h | 74 +--------------------------------- 3 files changed, 87 insertions(+), 74 deletions(-) create mode 100644 nuklear_console_marquee.h diff --git a/nuklear_console.h b/nuklear_console.h index be978a8..4eed886 100644 --- a/nuklear_console.h +++ b/nuklear_console.h @@ -297,6 +297,7 @@ NK_API nk_bool nk_console_navigate_to_path(nk_console* console, const char* path #include "nuklear_console_knob.h" #include "nuklear_console_label.h" #include "nuklear_console_list_view.h" +#include "nuklear_console_marquee.h" #include "nuklear_console_message.h" #include "nuklear_console_progress.h" #include "nuklear_console_property.h" @@ -493,6 +494,7 @@ extern "C" { #include "nuklear_console_knob.h" #include "nuklear_console_label.h" #include "nuklear_console_list_view.h" +#include "nuklear_console_marquee.h" #include "nuklear_console_message.h" #include "nuklear_console_progress.h" #include "nuklear_console_property.h" @@ -890,7 +892,7 @@ static void nk_console_tooltip_display(nk_console* console, const char* text) { int text_len = nk_strlen(text); float full_text_width = style->font->width(style->font->userdata, style->font->height, text, text_len); - nk_console_tooltip_render_marquee(ctx, text, text_len, full_text_width, + nk_console_marquee_tooltip_render(ctx, text, text_len, full_text_width, windowbounds.w - style->window.border, text_height, NK_CONSOLE_TOOLTIP_SCROLL_SPEED, NK_CONSOLE_TOOLTIP_SCROLL_PAUSE, &data->tooltip_scroll_x); diff --git a/nuklear_console_marquee.h b/nuklear_console_marquee.h new file mode 100644 index 0000000..443e949 --- /dev/null +++ b/nuklear_console_marquee.h @@ -0,0 +1,83 @@ +#ifndef NK_CONSOLE_MARQUEE_H__ +#define NK_CONSOLE_MARQUEE_H__ + +#ifndef NK_CONSOLE_MARQUEE_SCROLL_SPEED +#define NK_CONSOLE_MARQUEE_SCROLL_SPEED 60.0f +#endif +#ifndef NK_CONSOLE_MARQUEE_SCROLL_PAUSE +#define NK_CONSOLE_MARQUEE_SCROLL_PAUSE 1.5f +#endif + +#endif // NK_CONSOLE_MARQUEE_H__ + +#if defined(NK_CONSOLE_IMPLEMENTATION) && !defined(NK_CONSOLE_HEADER_ONLY) +#ifndef NK_CONSOLE_MARQUEE_IMPLEMENTATION_ONCE +#define NK_CONSOLE_MARQUEE_IMPLEMENTATION_ONCE + +/** + * Advance a marquee scroll offset and return a pointer to the visible slice of text. + * Returns `text` unchanged when the text fits or delta time is unavailable. + * The caller owns `buf` (minimum `buf_size` bytes). + */ +static const char* nk_console_marquee_slice( + struct nk_context* ctx, + const char* text, int text_len, + float full_text_width, float avail_width, + float speed, float pause, + float* scroll_x, + char* buf, int buf_size) +{ + if (full_text_width <= avail_width || ctx->delta_time_seconds <= 0) { + return text; + } + float pause_pixels = pause * speed; + float total_cycle = full_text_width + pause_pixels; + *scroll_x += ctx->delta_time_seconds * speed; + if (*scroll_x > total_cycle) { + *scroll_x -= total_cycle; + } + float offset = *scroll_x - pause_pixels; + if (offset <= 0.0f) { + return text; + } + int start = 0; + for (int i = 1; i <= text_len; i++) { + float w = ctx->style.font->width(ctx->style.font->userdata, ctx->style.font->height, text, i); + if (w >= offset) { start = i - 1; break; } + if (i == text_len) { start = text_len; } + } + int copy_len = text_len - start; + if (copy_len >= buf_size) { + copy_len = buf_size - 1; + } + NK_MEMCPY(buf, text + start, (nk_size)copy_len); + buf[copy_len] = '\0'; + return buf; +} + +/** + * Render a single-line marquee tooltip of `tooltip_width` at the current mock mouse position. + */ +static void nk_console_marquee_tooltip_render( + struct nk_context* ctx, + const char* text, int text_len, + float full_text_width, + float tooltip_width, float text_height, + float speed, float pause, + float* scroll_x) +{ + float avail_width = tooltip_width - ctx->style.window.padding.x * 2.0f; + char display_buf[256]; + const char* display_text = nk_console_marquee_slice(ctx, text, text_len, + full_text_width, avail_width, speed, pause, scroll_x, display_buf, (int)sizeof(display_buf)); + struct nk_vec2 zero; + nk_zero_struct(zero); + if (nk_tooltip_begin_offset(ctx, tooltip_width, NK_TOP_LEFT, zero)) { + nk_layout_row_dynamic(ctx, text_height, 1); + nk_label(ctx, display_text, NK_TEXT_LEFT); + nk_tooltip_end(ctx); + } +} + +#endif // NK_CONSOLE_MARQUEE_IMPLEMENTATION_ONCE +#endif // NK_CONSOLE_IMPLEMENTATION diff --git a/nuklear_console_message.h b/nuklear_console_message.h index ee52ee5..bce3e02 100644 --- a/nuklear_console_message.h +++ b/nuklear_console_message.h @@ -27,78 +27,6 @@ NK_API void nk_console_show_message(nk_console* console, const char* text); extern "C" { #endif -#ifndef NK_CONSOLE_MARQUEE_SCROLL_SPEED -#define NK_CONSOLE_MARQUEE_SCROLL_SPEED 60.0f -#endif -#ifndef NK_CONSOLE_MARQUEE_SCROLL_PAUSE -#define NK_CONSOLE_MARQUEE_SCROLL_PAUSE 1.5f -#endif - -/** - * Advance a marquee scroll offset and return a pointer to the visible slice of text. - * Returns `text` unchanged when the text fits or delta time is unavailable. - * The caller owns `buf` (minimum `buf_size` bytes). - */ -static const char* nk_console_marquee_slice( - struct nk_context* ctx, - const char* text, int text_len, - float full_text_width, float avail_width, - float speed, float pause, - float* scroll_x, - char* buf, int buf_size) -{ - if (full_text_width <= avail_width || ctx->delta_time_seconds <= 0) { - return text; - } - float pause_pixels = pause * speed; - float total_cycle = full_text_width + pause_pixels; - *scroll_x += ctx->delta_time_seconds * speed; - if (*scroll_x > total_cycle) { - *scroll_x -= total_cycle; - } - float offset = *scroll_x - pause_pixels; - if (offset <= 0.0f) { - return text; - } - int start = 0; - for (int i = 1; i <= text_len; i++) { - float w = ctx->style.font->width(ctx->style.font->userdata, ctx->style.font->height, text, i); - if (w >= offset) { start = i - 1; break; } - if (i == text_len) { start = text_len; } - } - int copy_len = text_len - start; - if (copy_len >= buf_size) { - copy_len = buf_size - 1; - } - NK_MEMCPY(buf, text + start, (nk_size)copy_len); - buf[copy_len] = '\0'; - return buf; -} - -/** - * Render a single-line marquee tooltip of `tooltip_width` at the current mock mouse position. - */ -static void nk_console_tooltip_render_marquee( - struct nk_context* ctx, - const char* text, int text_len, - float full_text_width, - float tooltip_width, float text_height, - float speed, float pause, - float* scroll_x) -{ - float avail_width = tooltip_width - ctx->style.window.padding.x * 2.0f; - char display_buf[256]; - const char* display_text = nk_console_marquee_slice(ctx, text, text_len, - full_text_width, avail_width, speed, pause, scroll_x, display_buf, (int)sizeof(display_buf)); - struct nk_vec2 zero; - nk_zero_struct(zero); - if (nk_tooltip_begin_offset(ctx, tooltip_width, NK_TOP_LEFT, zero)) { - nk_layout_row_dynamic(ctx, text_height, 1); - nk_label(ctx, display_text, NK_TEXT_LEFT); - nk_tooltip_end(ctx); - } -} - #ifndef NK_CONSOLE_MESSAGE_DURATION /** * A float determining how many seconds messages should be shown for. @@ -189,7 +117,7 @@ NK_API void nk_console_message_render(nk_console* console, nk_console_message* m float tooltip_width = bounds.w - ctx->style.window.border; int text_len = nk_strlen(message->text); float full_text_width = ctx->style.font->width(ctx->style.font->userdata, ctx->style.font->height, message->text, text_len); - nk_console_tooltip_render_marquee(ctx, message->text, text_len, full_text_width, + nk_console_marquee_tooltip_render(ctx, message->text, text_len, full_text_width, tooltip_width, text_height, NK_CONSOLE_MESSAGE_SCROLL_SPEED, NK_CONSOLE_MESSAGE_SCROLL_PAUSE, &message->scroll_x);