Hello Nuklear #938
-
|
I want to learn Nuklear and thought a simple project to get started would be to try to recreate the Hello World app from GTK: I might be missing something obvious, but I'm still struggling to create this simple app after spending a good amount of time reading the docs and source code. I think I've finally managed a create a shrink-to-fit button in a full-size window, but centering things is apparently always hard! if (nk_begin(ctx, "Hello", nk_rect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT), NK_WINDOW_NO_SCROLLBAR))
{
const char *btn_label = "Hello Nuklear";
const struct nk_user_font *font = ctx->style.font;
float btn_width = font->width(font->userdata, font->height, btn_label, nk_strlen(btn_label));
float padding_x = ctx->style.window.padding.x;
btn_width += 8 * padding_x; /* TODO: Correct padding? */
nk_layout_row_static(ctx, 0, btn_width, 1);
if (nk_button_label(ctx, btn_label))
fprintf(stdout, "Hello Nuklear\n");
}
nk_end(ctx); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
You really know how to pick one of the more awkward things to do in Nuklear don't you? Sorry for the slow reply, as you can see we don't use discussions much. What you have is definitely more complicated than necessary. Centering things vertically is always a bit of a pain in Nuklear, but since Nuklear is row based, you centering things horizontally is easier. Also you should rarely have to call I can think of a couple ways to do this. One is to use As you can see, I just picked 100 pixels wide and didn't bother with font->width(). That code gets you this: Screencast.from.2026-04-16.01-09-04.mp4The other way you could do it is just inserting an "empty" row above the button (use To make it fill the center third horizontally. Change 3 to whatever you want, or use Another idea I just had is you could also temporarily increase the window x padding which could also center it. I've never done that but you could theoretically just temporarily change window padding for both x and y to center the button in horizontally and vertically at the same time, even just using a single I'll have to try that sometime. But I would recommend the 2 earlier methods. |
Beta Was this translation helpful? Give feedback.

You really know how to pick one of the more awkward things to do in Nuklear don't you? Sorry for the slow reply, as you can see we don't use discussions much.
What you have is definitely more complicated than necessary. Centering things vertically is always a bit of a pain in Nuklear, but since Nuklear is row based, you centering things horizontally is easier.
Also you should rarely have to call
font->width()manually, especially for something simple like this. Usually you just pick a pixel width that works, or possibly a fraction if you're doing one of thenk_layout_row_xxxornk_layout_row()methods.I can think of a couple ways to do this. One is to use
nk_layout_space*():