Merge remote-tracking branch 'upstream/master' into horizontal_rule
This commit is contained in:
commit
894f2776f7
|
@ -10,5 +10,5 @@ indent_size = 4
|
|||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[clib.json]
|
||||
[**.{json,yml}]
|
||||
indent_size = 2
|
||||
|
|
|
@ -5,6 +5,7 @@ example/bin/*
|
|||
docs/xml
|
||||
docs/build
|
||||
docs/src
|
||||
doc/doc*
|
||||
*.tmp
|
||||
*.swo
|
||||
*.swp
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "nuklear",
|
||||
"version": "4.9.6",
|
||||
"version": "4.10.6",
|
||||
"repo": "Immediate-Mode-UI/Nuklear",
|
||||
"description": "A small ANSI C gui toolkit",
|
||||
"keywords": ["gl", "ui", "toolkit"],
|
||||
|
|
|
@ -115,10 +115,18 @@ int main(void)
|
|||
ctx = nk_allegro5_init(font, display, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
|
||||
/* style.c */
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
#ifdef INCLUDE_STYLE
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
while(1)
|
||||
{
|
||||
|
|
|
@ -101,7 +101,7 @@ static float
|
|||
nk_allegro5_font_get_text_width(nk_handle handle, float height, const char *text, int len)
|
||||
{
|
||||
float width;
|
||||
char *strcpy;
|
||||
char *str;
|
||||
NkAllegro5Font *font = (NkAllegro5Font*)handle.ptr;
|
||||
NK_UNUSED(height);
|
||||
if (!font || !text) {
|
||||
|
@ -111,11 +111,11 @@ nk_allegro5_font_get_text_width(nk_handle handle, float height, const char *text
|
|||
as nuklear uses variable size buffers and al_get_text_width doesn't
|
||||
accept a length, it infers length from null-termination
|
||||
(which is unsafe API design by allegro devs!) */
|
||||
strcpy = malloc(len + 1);
|
||||
strncpy(strcpy, text, len);
|
||||
strcpy[len] = '\0';
|
||||
width = al_get_text_width(font->font, strcpy);
|
||||
free(strcpy);
|
||||
str = calloc((size_t)len + 1, 1);
|
||||
if(!str) return 0;
|
||||
strncpy(str, text, len);
|
||||
width = al_get_text_width(font->font, str);
|
||||
free(str);
|
||||
return width;
|
||||
}
|
||||
|
||||
|
@ -473,10 +473,9 @@ nk_allegro5_clipboard_copy(nk_handle usr, const char *text, int len)
|
|||
char *str = 0;
|
||||
(void)usr;
|
||||
if (!len) return;
|
||||
str = (char*)malloc((size_t)len+1);
|
||||
str = calloc((size_t)len + 1, 1);
|
||||
if (!str) return;
|
||||
memcpy(str, text, (size_t)len);
|
||||
str[len] = '\0';
|
||||
strncpy(str, text, len);
|
||||
al_set_clipboard_text(allegro5.dsp, str);
|
||||
free(str);
|
||||
}
|
||||
|
|
|
@ -1,3 +1,14 @@
|
|||
#include <string.h> // strcpy, strlen
|
||||
|
||||
#ifdef __unix__
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
# include <pwd.h>
|
||||
#endif
|
||||
|
||||
struct icons {
|
||||
struct nk_image desktop;
|
||||
struct nk_image home;
|
||||
|
@ -78,17 +89,6 @@ struct file_browser {
|
|||
struct media *media;
|
||||
};
|
||||
|
||||
#ifdef __unix__
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
# include <pwd.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static void
|
||||
die(const char *fmt, ...)
|
||||
{
|
||||
|
|
|
@ -3,30 +3,19 @@ overview(struct nk_context *ctx)
|
|||
{
|
||||
/* window flags */
|
||||
static int show_menu = nk_true;
|
||||
static int titlebar = nk_true;
|
||||
static int border = nk_true;
|
||||
static int resize = nk_true;
|
||||
static int movable = nk_true;
|
||||
static int no_scrollbar = nk_false;
|
||||
static int scale_left = nk_false;
|
||||
static nk_flags window_flags = 0;
|
||||
static int minimizable = nk_true;
|
||||
static nk_flags window_flags = NK_WINDOW_TITLE|NK_WINDOW_BORDER|NK_WINDOW_SCALABLE|NK_WINDOW_MOVABLE|NK_WINDOW_MINIMIZABLE;
|
||||
nk_flags actual_window_flags;
|
||||
|
||||
/* popups */
|
||||
static enum nk_style_header_align header_align = NK_HEADER_RIGHT;
|
||||
static int show_app_about = nk_false;
|
||||
|
||||
/* window flags */
|
||||
window_flags = 0;
|
||||
ctx->style.window.header.align = header_align;
|
||||
if (border) window_flags |= NK_WINDOW_BORDER;
|
||||
if (resize) window_flags |= NK_WINDOW_SCALABLE;
|
||||
if (movable) window_flags |= NK_WINDOW_MOVABLE;
|
||||
if (no_scrollbar) window_flags |= NK_WINDOW_NO_SCROLLBAR;
|
||||
if (scale_left) window_flags |= NK_WINDOW_SCALE_LEFT;
|
||||
if (minimizable) window_flags |= NK_WINDOW_MINIMIZABLE;
|
||||
|
||||
if (nk_begin(ctx, "Overview", nk_rect(10, 10, 400, 600), window_flags))
|
||||
actual_window_flags = window_flags;
|
||||
if (!(actual_window_flags & NK_WINDOW_TITLE))
|
||||
actual_window_flags &= ~(NK_WINDOW_MINIMIZABLE|NK_WINDOW_CLOSABLE);
|
||||
if (nk_begin(ctx, "Overview", nk_rect(10, 10, 400, 600), actual_window_flags))
|
||||
{
|
||||
if (show_menu)
|
||||
{
|
||||
|
@ -132,14 +121,14 @@ overview(struct nk_context *ctx)
|
|||
/* window flags */
|
||||
if (nk_tree_push(ctx, NK_TREE_TAB, "Window", NK_MINIMIZED)) {
|
||||
nk_layout_row_dynamic(ctx, 30, 2);
|
||||
nk_checkbox_label(ctx, "Titlebar", &titlebar);
|
||||
nk_checkbox_label(ctx, "Menu", &show_menu);
|
||||
nk_checkbox_label(ctx, "Border", &border);
|
||||
nk_checkbox_label(ctx, "Resizable", &resize);
|
||||
nk_checkbox_label(ctx, "Movable", &movable);
|
||||
nk_checkbox_label(ctx, "No Scrollbar", &no_scrollbar);
|
||||
nk_checkbox_label(ctx, "Minimizable", &minimizable);
|
||||
nk_checkbox_label(ctx, "Scale Left", &scale_left);
|
||||
nk_checkbox_flags_label(ctx, "Titlebar", &window_flags, NK_WINDOW_TITLE);
|
||||
nk_checkbox_flags_label(ctx, "Border", &window_flags, NK_WINDOW_BORDER);
|
||||
nk_checkbox_flags_label(ctx, "Resizable", &window_flags, NK_WINDOW_SCALABLE);
|
||||
nk_checkbox_flags_label(ctx, "Movable", &window_flags, NK_WINDOW_MOVABLE);
|
||||
nk_checkbox_flags_label(ctx, "No Scrollbar", &window_flags, NK_WINDOW_NO_SCROLLBAR);
|
||||
nk_checkbox_flags_label(ctx, "Minimizable", &window_flags, NK_WINDOW_MINIMIZABLE);
|
||||
nk_checkbox_flags_label(ctx, "Scale Left", &window_flags, NK_WINDOW_SCALE_LEFT);
|
||||
nk_tree_pop(ctx);
|
||||
}
|
||||
|
||||
|
|
|
@ -161,7 +161,7 @@ int main(void)
|
|||
|
||||
AdjustWindowRectEx(&rect, style, FALSE, exstyle);
|
||||
|
||||
wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear Demo",
|
||||
wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear Direct3D 11 Demo",
|
||||
style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
rect.right - rect.left, rect.bottom - rect.top,
|
||||
NULL, NULL, wc.hInstance, NULL);
|
||||
|
@ -210,10 +210,16 @@ int main(void)
|
|||
|
||||
/* style.c */
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
|
||||
|
|
|
@ -61,7 +61,7 @@ static struct
|
|||
struct nk_font_atlas atlas;
|
||||
struct nk_buffer cmds;
|
||||
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
unsigned int max_vertex_buffer;
|
||||
unsigned int max_index_buffer;
|
||||
|
||||
|
@ -137,7 +137,7 @@ nk_d3d11_render(ID3D11DeviceContext *context, enum nk_anti_aliasing AA)
|
|||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
config.null = d3d11.null;
|
||||
config.tex_null = d3d11.tex_null;
|
||||
|
||||
{/* setup buffers to load vertices and elements */
|
||||
struct nk_buffer vbuf, ibuf;
|
||||
|
@ -603,7 +603,7 @@ nk_d3d11_font_stash_end(void)
|
|||
assert(SUCCEEDED(hr));}
|
||||
ID3D11Texture2D_Release(font_texture);}
|
||||
|
||||
nk_font_atlas_end(&d3d11.atlas, nk_handle_ptr(d3d11.font_texture_view), &d3d11.null);
|
||||
nk_font_atlas_end(&d3d11.atlas, nk_handle_ptr(d3d11.font_texture_view), &d3d11.tex_null);
|
||||
if (d3d11.atlas.default_font)
|
||||
nk_style_set_font(&d3d11.ctx, &d3d11.atlas.default_font->handle);
|
||||
}
|
||||
|
|
|
@ -219,7 +219,7 @@ int main(void)
|
|||
|
||||
AdjustWindowRectEx(&rect, style, FALSE, exstyle);
|
||||
|
||||
wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear Demo",
|
||||
wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear Direct3D 12 Demo",
|
||||
style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
rect.right - rect.left, rect.bottom - rect.top,
|
||||
NULL, NULL, wc.hInstance, NULL);
|
||||
|
@ -302,10 +302,16 @@ int main(void)
|
|||
|
||||
/* style.c */
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
|
||||
|
|
|
@ -109,7 +109,7 @@ static struct
|
|||
struct nk_font_atlas atlas;
|
||||
struct nk_buffer cmds;
|
||||
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
unsigned int max_vertex_buffer;
|
||||
unsigned int max_index_buffer;
|
||||
unsigned int max_user_textures;
|
||||
|
@ -198,7 +198,7 @@ nk_d3d12_render(ID3D12GraphicsCommandList *command_list, enum nk_anti_aliasing A
|
|||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
config.null = d3d12.null;
|
||||
config.tex_null = d3d12.tex_null;
|
||||
|
||||
struct nk_buffer vbuf, ibuf;
|
||||
nk_buffer_init_fixed(&vbuf, &ptr_data[sizeof(float) * 4 * 4], (size_t)d3d12.max_vertex_buffer);
|
||||
|
@ -863,7 +863,7 @@ nk_d3d12_font_stash_end(ID3D12GraphicsCommandList *command_list)
|
|||
ID3D12Device_CreateShaderResourceView(d3d12.device, d3d12.font_texture, &srv_desc, srv_handle);
|
||||
|
||||
/* Done with nk atlas data. Atlas will be served with texture id 0 */
|
||||
nk_font_atlas_end(&d3d12.atlas, nk_handle_id(0), &d3d12.null);
|
||||
nk_font_atlas_end(&d3d12.atlas, nk_handle_id(0), &d3d12.tex_null);
|
||||
|
||||
/* Setup default font */
|
||||
if (d3d12.atlas.default_font)
|
||||
|
|
|
@ -191,7 +191,7 @@ int main(void)
|
|||
|
||||
AdjustWindowRectEx(&rect, style, FALSE, exstyle);
|
||||
|
||||
wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear Demo",
|
||||
wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear Direct3D 9 Demo",
|
||||
style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
rect.right - rect.left, rect.bottom - rect.top,
|
||||
NULL, NULL, wc.hInstance, NULL);
|
||||
|
@ -216,10 +216,16 @@ int main(void)
|
|||
|
||||
/* style.c */
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
|
||||
|
|
|
@ -58,7 +58,7 @@ static struct {
|
|||
struct nk_font_atlas atlas;
|
||||
struct nk_buffer cmds;
|
||||
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
|
||||
D3DVIEWPORT9 viewport;
|
||||
D3DMATRIX projection;
|
||||
|
@ -150,7 +150,7 @@ nk_d3d9_render(enum nk_anti_aliasing AA)
|
|||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
config.null = d3d9.null;
|
||||
config.tex_null = d3d9.tex_null;
|
||||
|
||||
/* convert shapes into vertexes */
|
||||
nk_buffer_init_default(&vbuf);
|
||||
|
@ -248,7 +248,7 @@ nk_d3d9_create_font_texture()
|
|||
hr = IDirect3DTexture9_UnlockRect(d3d9.texture, 0);
|
||||
NK_ASSERT(SUCCEEDED(hr));
|
||||
|
||||
nk_font_atlas_end(&d3d9.atlas, nk_handle_ptr(d3d9.texture), &d3d9.null);
|
||||
nk_font_atlas_end(&d3d9.atlas, nk_handle_ptr(d3d9.texture), &d3d9.tex_null);
|
||||
}
|
||||
|
||||
NK_API void
|
||||
|
|
|
@ -103,7 +103,7 @@ int main(void)
|
|||
atom = RegisterClassW(&wc);
|
||||
|
||||
AdjustWindowRectEx(&rect, style, FALSE, exstyle);
|
||||
wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear Demo",
|
||||
wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear GDI Demo",
|
||||
style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
rect.right - rect.left, rect.bottom - rect.top,
|
||||
NULL, NULL, wc.hInstance, NULL);
|
||||
|
@ -115,10 +115,16 @@ int main(void)
|
|||
|
||||
/* style.c */
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
while (running)
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <wingdi.h>
|
||||
|
||||
typedef struct GdiFont GdiFont;
|
||||
NK_API struct nk_context* nk_gdi_init(GdiFont *font, HDC window_dc, unsigned int width, unsigned int height);
|
||||
|
@ -391,6 +392,62 @@ nk_gdi_stroke_polyline(HDC dc, const struct nk_vec2i *pnts,
|
|||
}
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_stroke_arc(HDC dc, short cx, short cy, unsigned short r, float amin, float adelta, unsigned short line_thickness, struct nk_color col)
|
||||
{
|
||||
COLORREF color = convert_color(col);
|
||||
|
||||
/* setup pen */
|
||||
HPEN pen = NULL;
|
||||
if (line_thickness == 1)
|
||||
SetDCPenColor(dc, color);
|
||||
else
|
||||
{
|
||||
/* the flat endcap makes thick arcs look better */
|
||||
DWORD pen_style = PS_SOLID | PS_ENDCAP_FLAT | PS_GEOMETRIC;
|
||||
|
||||
LOGBRUSH brush;
|
||||
brush.lbStyle = BS_SOLID;
|
||||
brush.lbColor = color;
|
||||
brush.lbHatch = 0;
|
||||
|
||||
pen = ExtCreatePen(pen_style, line_thickness, &brush, 0, NULL);
|
||||
SelectObject(dc, pen);
|
||||
}
|
||||
|
||||
/* calculate arc and draw */
|
||||
int start_x = cx + (int) ((float)r*nk_cos(amin+adelta)),
|
||||
start_y = cy + (int) ((float)r*nk_sin(amin+adelta)),
|
||||
end_x = cx + (int) ((float)r*nk_cos(amin)),
|
||||
end_y = cy + (int) ((float)r*nk_sin(amin));
|
||||
|
||||
HGDIOBJ br = SelectObject(dc, GetStockObject(NULL_BRUSH));
|
||||
SetArcDirection(dc, AD_COUNTERCLOCKWISE);
|
||||
Pie(dc, cx-r, cy-r, cx+r, cy+r, start_x, start_y, end_x, end_y);
|
||||
SelectObject(dc, br);
|
||||
|
||||
if (pen)
|
||||
{
|
||||
SelectObject(dc, GetStockObject(DC_PEN));
|
||||
DeleteObject(pen);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_fill_arc(HDC dc, short cx, short cy, unsigned short r, float amin, float adelta, struct nk_color col)
|
||||
{
|
||||
COLORREF color = convert_color(col);
|
||||
SetDCBrushColor(dc, color);
|
||||
SetDCPenColor(dc, color);
|
||||
|
||||
int start_x = cx + (int) ((float)r*nk_cos(amin+adelta)),
|
||||
start_y = cy + (int) ((float)r*nk_sin(amin+adelta)),
|
||||
end_x = cx + (int) ((float)r*nk_cos(amin)),
|
||||
end_y = cy + (int) ((float)r*nk_sin(amin));
|
||||
|
||||
Pie(dc, cx-r, cy-r, cx+r, cy+r, start_x, start_y, end_x, end_y);
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_fill_circle(HDC dc, short x, short y, unsigned short w,
|
||||
unsigned short h, struct nk_color col)
|
||||
|
@ -414,8 +471,10 @@ nk_gdi_stroke_circle(HDC dc, short x, short y, unsigned short w,
|
|||
SelectObject(dc, pen);
|
||||
}
|
||||
|
||||
HGDIOBJ br = SelectObject(dc, GetStockObject(NULL_BRUSH));
|
||||
SetDCBrushColor(dc, OPAQUE);
|
||||
Ellipse(dc, x, y, x + w, y + h);
|
||||
SelectObject(dc, br);
|
||||
|
||||
if (pen) {
|
||||
SelectObject(dc, GetStockObject(DC_PEN));
|
||||
|
@ -466,6 +525,7 @@ nk_gdi_draw_text(HDC dc, short x, short y, unsigned short w, unsigned short h,
|
|||
wstr = (WCHAR*)_alloca(wsize * sizeof(wchar_t));
|
||||
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
|
||||
|
||||
SetBkMode(dc, TRANSPARENT); /* Transparent Text Background */
|
||||
SetBkColor(dc, convert_color(cbg));
|
||||
SetTextColor(dc, convert_color(cfg));
|
||||
|
||||
|
@ -717,6 +777,13 @@ nk_gdi_handle_event(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
|
|||
nk_input_key(&gdi.ctx, NK_KEY_SCROLL_UP, down);
|
||||
return 1;
|
||||
|
||||
case 'A':
|
||||
if (ctrl) {
|
||||
nk_input_key(&gdi.ctx, NK_KEY_TEXT_SELECT_ALL, down);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'C':
|
||||
if (ctrl) {
|
||||
nk_input_key(&gdi.ctx, NK_KEY_COPY, down);
|
||||
|
@ -859,6 +926,14 @@ nk_gdi_render(struct nk_color clear)
|
|||
const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
|
||||
nk_gdi_fill_circle(memory_dc, c->x, c->y, c->w, c->h, c->color);
|
||||
} break;
|
||||
case NK_COMMAND_ARC: {
|
||||
const struct nk_command_arc *q = (const struct nk_command_arc *)cmd;
|
||||
nk_gdi_stroke_arc(memory_dc, q->cx, q->cy, q->r, q->a[0], q->a[1], q->line_thickness, q->color);
|
||||
} break;
|
||||
case NK_COMMAND_ARC_FILLED: {
|
||||
const struct nk_command_arc_filled *q = (const struct nk_command_arc_filled *)cmd;
|
||||
nk_gdi_fill_arc(memory_dc, q->cx, q->cy, q->r, q->a[0], q->a[1], q->color);
|
||||
} break;
|
||||
case NK_COMMAND_TRIANGLE: {
|
||||
const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
|
||||
nk_gdi_stroke_triangle(memory_dc, t->a.x, t->a.y, t->b.x, t->b.y,
|
||||
|
@ -901,8 +976,7 @@ nk_gdi_render(struct nk_color clear)
|
|||
const struct nk_command_image *i = (const struct nk_command_image *)cmd;
|
||||
nk_gdi_draw_image(i->x, i->y, i->w, i->h, i->img, i->col);
|
||||
} break;
|
||||
case NK_COMMAND_ARC:
|
||||
case NK_COMMAND_ARC_FILLED:
|
||||
case NK_COMMAND_CUSTOM:
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
@echo off
|
||||
|
||||
rem This will use VS2015 for compiler
|
||||
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86
|
||||
|
||||
cl /nologo /W3 /O2 /fp:fast /Gm- /D_CRT_SECURE_NO_DEPRECATE /Fedemo.exe main.c user32.lib gdi32.lib Msimg32.lib /link /incremental:no
|
|
@ -0,0 +1,128 @@
|
|||
#include <windows.h>
|
||||
|
||||
#pragma comment(linker,"\"/manifestdependency:type='win32' \
|
||||
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
|
||||
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
|
||||
|
||||
/* Includes the default nuklear implementation
|
||||
* Includes the modified GDI backend (No more global state to allow multiple hwnd's)
|
||||
*/
|
||||
#define NK_INCLUDE_FIXED_TYPES
|
||||
#define NK_INCLUDE_STANDARD_IO
|
||||
#define NK_INCLUDE_STANDARD_VARARGS
|
||||
#define NK_INCLUDE_DEFAULT_ALLOCATOR
|
||||
#define NK_IMPLEMENTATION
|
||||
#define NK_GDI_IMPLEMENTATION
|
||||
#include "../../nuklear.h"
|
||||
#include "nuklear_gdi.h"
|
||||
|
||||
/* Include the window framework (the new fancy code of this demo) */
|
||||
#define NKGDI_IMPLEMENT_WINDOW
|
||||
#include "window.h"
|
||||
|
||||
/* This callback will be called when the window is draw
|
||||
* You will NOT need to call nk_begin(...) and nk_end(...)
|
||||
* begin and end are handled by the parent code calling this
|
||||
* callback
|
||||
*/
|
||||
int drawCallback(struct nk_context* ctx)
|
||||
{
|
||||
/* Code is from ../calculator.c */
|
||||
static int set = 0, prev = 0, op = 0;
|
||||
static const char numbers[] = "789456123";
|
||||
static const char ops[] = "+-*/";
|
||||
static double a = 0, b = 0;
|
||||
static double *current = &a;
|
||||
|
||||
size_t i = 0;
|
||||
int solve = 0;
|
||||
{int len; char buffer[256];
|
||||
nk_layout_row_dynamic(ctx, 35, 1);
|
||||
len = snprintf(buffer, 256, "%.2f", *current);
|
||||
nk_edit_string(ctx, NK_EDIT_SIMPLE, buffer, &len, 255, nk_filter_float);
|
||||
buffer[len] = 0;
|
||||
*current = atof(buffer);}
|
||||
|
||||
nk_layout_row_dynamic(ctx, 35, 4);
|
||||
for (i = 0; i < 16; ++i) {
|
||||
if (i >= 12 && i < 15) {
|
||||
if (i > 12) continue;
|
||||
if (nk_button_label(ctx, "C")) {
|
||||
a = b = op = 0; current = &a; set = 0;
|
||||
} if (nk_button_label(ctx, "0")) {
|
||||
*current = *current*10.0f; set = 0;
|
||||
} if (nk_button_label(ctx, "=")) {
|
||||
solve = 1; prev = op; op = 0;
|
||||
}
|
||||
} else if (((i+1) % 4)) {
|
||||
if (nk_button_text(ctx, &numbers[(i/4)*3+i%4], 1)) {
|
||||
*current = *current * 10.0f + numbers[(i/4)*3+i%4] - '0';
|
||||
set = 0;
|
||||
}
|
||||
} else if (nk_button_text(ctx, &ops[i/4], 1)) {
|
||||
if (!set) {
|
||||
if (current != &b) {
|
||||
current = &b;
|
||||
} else {
|
||||
prev = op;
|
||||
solve = 1;
|
||||
}
|
||||
}
|
||||
op = ops[i/4];
|
||||
set = 1;
|
||||
}
|
||||
}
|
||||
if (solve) {
|
||||
if (prev == '+') a = a + b;
|
||||
if (prev == '-') a = a - b;
|
||||
if (prev == '*') a = a * b;
|
||||
if (prev == '/') a = a / b;
|
||||
current = &a;
|
||||
if (set) current = &b;
|
||||
b = 0; set = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Main entry point - wWinMain used for UNICODE
|
||||
* (You can also use _tWinMain(...) to automaticaly use the ASCII or WIDE char entry point base on your build)
|
||||
*/
|
||||
INT WINAPI wWinMain(HINSTANCE _In_ hInstance, HINSTANCE _In_opt_ hPrevInstance, PWSTR _In_ cmdArgs, INT _In_ cmdShow)
|
||||
{
|
||||
/* Call this first to setup all required prerequisites */
|
||||
nkgdi_window_init();
|
||||
|
||||
/* Preparing two window contexts */
|
||||
struct nkgdi_window w1, w2;
|
||||
memset(&w1, 0x0, sizeof(struct nkgdi_window));
|
||||
memset(&w2, 0x0, sizeof(struct nkgdi_window));
|
||||
|
||||
/* Configure and create window 1.
|
||||
* Note: You can allways change the direct accesible parameters later as well!
|
||||
*/
|
||||
w1.allow_sizing = 0;
|
||||
w1.allow_maximize = 0;
|
||||
w1.allow_move = 0;
|
||||
w1.has_titlebar = 0;
|
||||
w1.cb_on_draw = &drawCallback;
|
||||
nkgdi_window_create(&w1, 500, 500, "F1", 10, 10);
|
||||
|
||||
/* Configure and create window 2 */
|
||||
w2.allow_sizing = 1;
|
||||
w2.allow_maximize = 1;
|
||||
w2.allow_move = 1;
|
||||
w2.has_titlebar = 1;
|
||||
w2.cb_on_draw = &drawCallback;
|
||||
nkgdi_window_create(&w2, 500, 500, "F2", 520, 10);
|
||||
|
||||
/* As long as both windows are valid (nkgdi_window_update returning 1) */
|
||||
while (nkgdi_window_update(&w1) && nkgdi_window_update(&w2)) Sleep(20);
|
||||
|
||||
/* Destroy both windows context */
|
||||
nkgdi_window_destroy(&w1);
|
||||
nkgdi_window_destroy(&w2);
|
||||
|
||||
/* Call nkgdi_window_shutdown to properly shutdown the gdi window framework */
|
||||
nkgdi_window_shutdown();
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,937 @@
|
|||
/*
|
||||
* Nuklear - 1.32.0 - public domain
|
||||
* no warrenty implied; use at your own risk.
|
||||
* authored from 2015-2016 by Micha Mettke
|
||||
*
|
||||
* Modified GDI backend 2022
|
||||
* Now based on a context that is required for each API function call.
|
||||
* Removes the global state --> you can have multiple windows :-)
|
||||
*
|
||||
*/
|
||||
/*
|
||||
* ==============================================================
|
||||
*
|
||||
* API
|
||||
*
|
||||
* ===============================================================
|
||||
*/
|
||||
#ifndef NK_GDI_H_
|
||||
#define NK_GDI_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"{
|
||||
#endif
|
||||
|
||||
typedef struct GdiFont GdiFont;
|
||||
struct _nk_gdi_ctx;
|
||||
typedef struct _nk_gdi_ctx* nk_gdi_ctx;
|
||||
|
||||
NK_API struct nk_context* nk_gdi_init(nk_gdi_ctx* gdi, GdiFont* font, HDC window_dc, unsigned int width, unsigned int height);
|
||||
NK_API int nk_gdi_handle_event(nk_gdi_ctx gdi, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
|
||||
NK_API void nk_gdi_render(nk_gdi_ctx gdi, struct nk_color clear);
|
||||
NK_API void nk_gdi_shutdown(nk_gdi_ctx gdi);
|
||||
|
||||
/* font */
|
||||
NK_API GdiFont* nk_gdifont_create(const char* name, int size);
|
||||
NK_API void nk_gdifont_del(GdiFont* font);
|
||||
NK_API void nk_gdi_set_font(nk_gdi_ctx gdi, GdiFont* font);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* ==============================================================
|
||||
*
|
||||
* IMPLEMENTATION
|
||||
*
|
||||
* ===============================================================
|
||||
*/
|
||||
#ifdef NK_GDI_IMPLEMENTATION
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
|
||||
struct GdiFont {
|
||||
struct nk_user_font nk;
|
||||
int height;
|
||||
HFONT handle;
|
||||
HDC dc;
|
||||
};
|
||||
|
||||
struct _nk_gdi_ctx {
|
||||
HBITMAP bitmap;
|
||||
HDC window_dc;
|
||||
HDC memory_dc;
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
struct nk_context ctx;
|
||||
};
|
||||
|
||||
static void
|
||||
nk_create_image(struct nk_image* image, const char* frame_buffer, const int width, const int height)
|
||||
{
|
||||
if (image && frame_buffer && (width > 0) && (height > 0))
|
||||
{
|
||||
const unsigned char* src = (const unsigned char*)frame_buffer;
|
||||
INT row = ((width * 3 + 3) & ~3);
|
||||
LPBYTE lpBuf, pb = NULL;
|
||||
BITMAPINFO bi = { 0 };
|
||||
HBITMAP hbm;
|
||||
int v, i;
|
||||
|
||||
image->w = width;
|
||||
image->h = height;
|
||||
image->region[0] = 0;
|
||||
image->region[1] = 0;
|
||||
image->region[2] = width;
|
||||
image->region[3] = height;
|
||||
|
||||
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
||||
bi.bmiHeader.biWidth = width;
|
||||
bi.bmiHeader.biHeight = height;
|
||||
bi.bmiHeader.biPlanes = 1;
|
||||
bi.bmiHeader.biBitCount = 24;
|
||||
bi.bmiHeader.biCompression = BI_RGB;
|
||||
bi.bmiHeader.biSizeImage = row * height;
|
||||
|
||||
hbm = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**)&lpBuf, NULL, 0);
|
||||
|
||||
pb = lpBuf + row * height;
|
||||
for (v = 0; v < height; v++)
|
||||
{
|
||||
pb -= row;
|
||||
for (i = 0; i < row; i += 3)
|
||||
{
|
||||
pb[i + 0] = src[0];
|
||||
pb[i + 1] = src[1];
|
||||
pb[i + 2] = src[2];
|
||||
src += 3;
|
||||
}
|
||||
}
|
||||
SetDIBits(NULL, hbm, 0, height, lpBuf, &bi, DIB_RGB_COLORS);
|
||||
image->handle.ptr = hbm;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
nk_delete_image(struct nk_image* image)
|
||||
{
|
||||
if (image && image->handle.id != 0)
|
||||
{
|
||||
HBITMAP hbm = (HBITMAP)image->handle.ptr;
|
||||
DeleteObject(hbm);
|
||||
memset(image, 0, sizeof(struct nk_image));
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_draw_image(nk_gdi_ctx gdi, short x, short y, unsigned short w, unsigned short h,
|
||||
struct nk_image img, struct nk_color col)
|
||||
{
|
||||
HBITMAP hbm = (HBITMAP)img.handle.ptr;
|
||||
HDC hDCBits;
|
||||
BITMAP bitmap;
|
||||
|
||||
if (!gdi->memory_dc || !hbm)
|
||||
return;
|
||||
|
||||
hDCBits = CreateCompatibleDC(gdi->memory_dc);
|
||||
GetObject(hbm, sizeof(BITMAP), (LPSTR)&bitmap);
|
||||
SelectObject(hDCBits, hbm);
|
||||
StretchBlt(gdi->memory_dc, x, y, w, h, hDCBits, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
|
||||
DeleteDC(hDCBits);
|
||||
}
|
||||
|
||||
static COLORREF
|
||||
convert_color(struct nk_color c)
|
||||
{
|
||||
return c.r | (c.g << 8) | (c.b << 16);
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_scissor(HDC dc, float x, float y, float w, float h)
|
||||
{
|
||||
SelectClipRgn(dc, NULL);
|
||||
IntersectClipRect(dc, (int)x, (int)y, (int)(x + w + 1), (int)(y + h + 1));
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_stroke_line(HDC dc, short x0, short y0, short x1,
|
||||
short y1, unsigned int line_thickness, struct nk_color col)
|
||||
{
|
||||
COLORREF color = convert_color(col);
|
||||
|
||||
HPEN pen = NULL;
|
||||
if (line_thickness == 1) {
|
||||
SetDCPenColor(dc, color);
|
||||
}
|
||||
else {
|
||||
pen = CreatePen(PS_SOLID, line_thickness, color);
|
||||
SelectObject(dc, pen);
|
||||
}
|
||||
|
||||
MoveToEx(dc, x0, y0, NULL);
|
||||
LineTo(dc, x1, y1);
|
||||
|
||||
if (pen) {
|
||||
SelectObject(dc, GetStockObject(DC_PEN));
|
||||
DeleteObject(pen);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_stroke_rect(HDC dc, short x, short y, unsigned short w,
|
||||
unsigned short h, unsigned short r, unsigned short line_thickness, struct nk_color col)
|
||||
{
|
||||
COLORREF color = convert_color(col);
|
||||
HGDIOBJ br;
|
||||
HPEN pen = NULL;
|
||||
|
||||
if (line_thickness == 1) {
|
||||
SetDCPenColor(dc, color);
|
||||
}
|
||||
else {
|
||||
pen = CreatePen(PS_SOLID, line_thickness, color);
|
||||
SelectObject(dc, pen);
|
||||
}
|
||||
|
||||
br = SelectObject(dc, GetStockObject(NULL_BRUSH));
|
||||
if (r == 0) {
|
||||
Rectangle(dc, x, y, x + w, y + h);
|
||||
}
|
||||
else {
|
||||
RoundRect(dc, x, y, x + w, y + h, r, r);
|
||||
}
|
||||
SelectObject(dc, br);
|
||||
|
||||
if (pen) {
|
||||
SelectObject(dc, GetStockObject(DC_PEN));
|
||||
DeleteObject(pen);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_fill_rect(HDC dc, short x, short y, unsigned short w,
|
||||
unsigned short h, unsigned short r, struct nk_color col)
|
||||
{
|
||||
COLORREF color = convert_color(col);
|
||||
|
||||
if (r == 0) {
|
||||
RECT rect;
|
||||
SetRect(&rect, x, y, x + w, y + h);
|
||||
SetBkColor(dc, color);
|
||||
ExtTextOutW(dc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
|
||||
}
|
||||
else {
|
||||
SetDCPenColor(dc, color);
|
||||
SetDCBrushColor(dc, color);
|
||||
RoundRect(dc, x, y, x + w, y + h, r, r);
|
||||
}
|
||||
}
|
||||
static void
|
||||
nk_gdi_set_vertexColor(PTRIVERTEX tri, struct nk_color col)
|
||||
{
|
||||
tri->Red = col.r << 8;
|
||||
tri->Green = col.g << 8;
|
||||
tri->Blue = col.b << 8;
|
||||
tri->Alpha = 0xff << 8;
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_rect_multi_color(nk_gdi_ctx gdi, HDC dc, short x, short y, unsigned short w,
|
||||
unsigned short h, struct nk_color left, struct nk_color top,
|
||||
struct nk_color right, struct nk_color bottom)
|
||||
{
|
||||
BLENDFUNCTION alphaFunction;
|
||||
// GRADIENT_RECT gRect;
|
||||
GRADIENT_TRIANGLE gTri[2];
|
||||
TRIVERTEX vt[4];
|
||||
alphaFunction.BlendOp = AC_SRC_OVER;
|
||||
alphaFunction.BlendFlags = 0;
|
||||
alphaFunction.SourceConstantAlpha = 0;
|
||||
alphaFunction.AlphaFormat = AC_SRC_ALPHA;
|
||||
|
||||
/* TODO: This Case Needs Repair.*/
|
||||
/* Top Left Corner */
|
||||
vt[0].x = x;
|
||||
vt[0].y = y;
|
||||
nk_gdi_set_vertexColor(&vt[0], left);
|
||||
/* Top Right Corner */
|
||||
vt[1].x = x + w;
|
||||
vt[1].y = y;
|
||||
nk_gdi_set_vertexColor(&vt[1], top);
|
||||
/* Bottom Left Corner */
|
||||
vt[2].x = x;
|
||||
vt[2].y = y + h;
|
||||
nk_gdi_set_vertexColor(&vt[2], right);
|
||||
|
||||
/* Bottom Right Corner */
|
||||
vt[3].x = x + w;
|
||||
vt[3].y = y + h;
|
||||
nk_gdi_set_vertexColor(&vt[3], bottom);
|
||||
|
||||
gTri[0].Vertex1 = 0;
|
||||
gTri[0].Vertex2 = 1;
|
||||
gTri[0].Vertex3 = 2;
|
||||
gTri[1].Vertex1 = 2;
|
||||
gTri[1].Vertex2 = 1;
|
||||
gTri[1].Vertex3 = 3;
|
||||
GdiGradientFill(dc, vt, 4, gTri, 2, GRADIENT_FILL_TRIANGLE);
|
||||
AlphaBlend(gdi->window_dc, x, y, x + w, y + h, gdi->memory_dc, x, y, x + w, y + h, alphaFunction);
|
||||
|
||||
}
|
||||
|
||||
static BOOL
|
||||
SetPoint(POINT* p, LONG x, LONG y)
|
||||
{
|
||||
if (!p)
|
||||
return FALSE;
|
||||
p->x = x;
|
||||
p->y = y;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_fill_triangle(HDC dc, short x0, short y0, short x1,
|
||||
short y1, short x2, short y2, struct nk_color col)
|
||||
{
|
||||
COLORREF color = convert_color(col);
|
||||
POINT points[3];
|
||||
|
||||
SetPoint(&points[0], x0, y0);
|
||||
SetPoint(&points[1], x1, y1);
|
||||
SetPoint(&points[2], x2, y2);
|
||||
|
||||
SetDCPenColor(dc, color);
|
||||
SetDCBrushColor(dc, color);
|
||||
Polygon(dc, points, 3);
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_stroke_triangle(HDC dc, short x0, short y0, short x1,
|
||||
short y1, short x2, short y2, unsigned short line_thickness, struct nk_color col)
|
||||
{
|
||||
COLORREF color = convert_color(col);
|
||||
POINT points[4];
|
||||
HPEN pen = NULL;
|
||||
|
||||
SetPoint(&points[0], x0, y0);
|
||||
SetPoint(&points[1], x1, y1);
|
||||
SetPoint(&points[2], x2, y2);
|
||||
SetPoint(&points[3], x0, y0);
|
||||
|
||||
if (line_thickness == 1) {
|
||||
SetDCPenColor(dc, color);
|
||||
}
|
||||
else {
|
||||
pen = CreatePen(PS_SOLID, line_thickness, color);
|
||||
SelectObject(dc, pen);
|
||||
}
|
||||
|
||||
Polyline(dc, points, 4);
|
||||
|
||||
if (pen) {
|
||||
SelectObject(dc, GetStockObject(DC_PEN));
|
||||
DeleteObject(pen);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_fill_polygon(HDC dc, const struct nk_vec2i* pnts, int count, struct nk_color col)
|
||||
{
|
||||
int i = 0;
|
||||
#define MAX_POINTS 64
|
||||
POINT points[MAX_POINTS];
|
||||
COLORREF color = convert_color(col);
|
||||
SetDCBrushColor(dc, color);
|
||||
SetDCPenColor(dc, color);
|
||||
for (i = 0; i < count && i < MAX_POINTS; ++i) {
|
||||
points[i].x = pnts[i].x;
|
||||
points[i].y = pnts[i].y;
|
||||
}
|
||||
Polygon(dc, points, i);
|
||||
#undef MAX_POINTS
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_stroke_polygon(HDC dc, const struct nk_vec2i* pnts, int count,
|
||||
unsigned short line_thickness, struct nk_color col)
|
||||
{
|
||||
COLORREF color = convert_color(col);
|
||||
HPEN pen = NULL;
|
||||
if (line_thickness == 1) {
|
||||
SetDCPenColor(dc, color);
|
||||
}
|
||||
else {
|
||||
pen = CreatePen(PS_SOLID, line_thickness, color);
|
||||
SelectObject(dc, pen);
|
||||
}
|
||||
|
||||
if (count > 0) {
|
||||
int i;
|
||||
MoveToEx(dc, pnts[0].x, pnts[0].y, NULL);
|
||||
for (i = 1; i < count; ++i)
|
||||
LineTo(dc, pnts[i].x, pnts[i].y);
|
||||
LineTo(dc, pnts[0].x, pnts[0].y);
|
||||
}
|
||||
|
||||
if (pen) {
|
||||
SelectObject(dc, GetStockObject(DC_PEN));
|
||||
DeleteObject(pen);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_stroke_polyline(HDC dc, const struct nk_vec2i* pnts,
|
||||
int count, unsigned short line_thickness, struct nk_color col)
|
||||
{
|
||||
COLORREF color = convert_color(col);
|
||||
HPEN pen = NULL;
|
||||
if (line_thickness == 1) {
|
||||
SetDCPenColor(dc, color);
|
||||
}
|
||||
else {
|
||||
pen = CreatePen(PS_SOLID, line_thickness, color);
|
||||
SelectObject(dc, pen);
|
||||
}
|
||||
|
||||
if (count > 0) {
|
||||
int i;
|
||||
MoveToEx(dc, pnts[0].x, pnts[0].y, NULL);
|
||||
for (i = 1; i < count; ++i)
|
||||
LineTo(dc, pnts[i].x, pnts[i].y);
|
||||
}
|
||||
|
||||
if (pen) {
|
||||
SelectObject(dc, GetStockObject(DC_PEN));
|
||||
DeleteObject(pen);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_fill_circle(HDC dc, short x, short y, unsigned short w,
|
||||
unsigned short h, struct nk_color col)
|
||||
{
|
||||
COLORREF color = convert_color(col);
|
||||
SetDCBrushColor(dc, color);
|
||||
SetDCPenColor(dc, color);
|
||||
Ellipse(dc, x, y, x + w, y + h);
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_stroke_circle(HDC dc, short x, short y, unsigned short w,
|
||||
unsigned short h, unsigned short line_thickness, struct nk_color col)
|
||||
{
|
||||
COLORREF color = convert_color(col);
|
||||
HPEN pen = NULL;
|
||||
if (line_thickness == 1) {
|
||||
SetDCPenColor(dc, color);
|
||||
}
|
||||
else {
|
||||
pen = CreatePen(PS_SOLID, line_thickness, color);
|
||||
SelectObject(dc, pen);
|
||||
}
|
||||
|
||||
SetDCBrushColor(dc, OPAQUE);
|
||||
Ellipse(dc, x, y, x + w, y + h);
|
||||
|
||||
if (pen) {
|
||||
SelectObject(dc, GetStockObject(DC_PEN));
|
||||
DeleteObject(pen);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_stroke_curve(HDC dc, struct nk_vec2i p1,
|
||||
struct nk_vec2i p2, struct nk_vec2i p3, struct nk_vec2i p4,
|
||||
unsigned short line_thickness, struct nk_color col)
|
||||
{
|
||||
COLORREF color = convert_color(col);
|
||||
POINT p[4];
|
||||
HPEN pen = NULL;
|
||||
|
||||
SetPoint(&p[0], p1.x, p1.y);
|
||||
SetPoint(&p[1], p2.x, p2.y);
|
||||
SetPoint(&p[2], p3.x, p3.y);
|
||||
SetPoint(&p[3], p4.x, p4.y);
|
||||
|
||||
if (line_thickness == 1) {
|
||||
SetDCPenColor(dc, color);
|
||||
}
|
||||
else {
|
||||
pen = CreatePen(PS_SOLID, line_thickness, color);
|
||||
SelectObject(dc, pen);
|
||||
}
|
||||
|
||||
SetDCBrushColor(dc, OPAQUE);
|
||||
PolyBezier(dc, p, 4);
|
||||
|
||||
if (pen) {
|
||||
SelectObject(dc, GetStockObject(DC_PEN));
|
||||
DeleteObject(pen);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_draw_text(HDC dc, short x, short y, unsigned short w, unsigned short h,
|
||||
const char* text, int len, GdiFont* font, struct nk_color cbg, struct nk_color cfg)
|
||||
{
|
||||
int wsize;
|
||||
WCHAR* wstr;
|
||||
|
||||
if (!text || !font || !len) return;
|
||||
|
||||
wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
|
||||
wstr = (WCHAR*)_alloca(wsize * sizeof(wchar_t));
|
||||
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
|
||||
|
||||
SetBkColor(dc, convert_color(cbg));
|
||||
SetTextColor(dc, convert_color(cfg));
|
||||
|
||||
SelectObject(dc, font->handle);
|
||||
ExtTextOutW(dc, x, y, ETO_OPAQUE, NULL, wstr, wsize, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_clear(nk_gdi_ctx gdi, HDC dc, struct nk_color col)
|
||||
{
|
||||
COLORREF color = convert_color(col);
|
||||
RECT rect;
|
||||
SetRect(&rect, 0, 0, gdi->width, gdi->height);
|
||||
SetBkColor(dc, color);
|
||||
|
||||
ExtTextOutW(dc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_blit(nk_gdi_ctx gdi, HDC dc)
|
||||
{
|
||||
BitBlt(dc, 0, 0, gdi->width, gdi->height, gdi->memory_dc, 0, 0, SRCCOPY);
|
||||
|
||||
}
|
||||
|
||||
GdiFont*
|
||||
nk_gdifont_create(const char* name, int size)
|
||||
{
|
||||
TEXTMETRICW metric;
|
||||
GdiFont* font = (GdiFont*)calloc(1, sizeof(GdiFont));
|
||||
if (!font)
|
||||
return NULL;
|
||||
font->dc = CreateCompatibleDC(0);
|
||||
font->handle = CreateFontA(size, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_DONTCARE, name);
|
||||
SelectObject(font->dc, font->handle);
|
||||
GetTextMetricsW(font->dc, &metric);
|
||||
font->height = metric.tmHeight;
|
||||
return font;
|
||||
}
|
||||
|
||||
static float
|
||||
nk_gdifont_get_text_width(nk_handle handle, float height, const char* text, int len)
|
||||
{
|
||||
GdiFont* font = (GdiFont*)handle.ptr;
|
||||
SIZE size;
|
||||
int wsize;
|
||||
WCHAR* wstr;
|
||||
if (!font || !text)
|
||||
return 0;
|
||||
|
||||
wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
|
||||
wstr = (WCHAR*)_alloca(wsize * sizeof(wchar_t));
|
||||
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
|
||||
if (GetTextExtentPoint32W(font->dc, wstr, wsize, &size))
|
||||
return (float)size.cx;
|
||||
return -1.0f;
|
||||
}
|
||||
|
||||
void
|
||||
nk_gdifont_del(GdiFont* font)
|
||||
{
|
||||
if (!font) return;
|
||||
DeleteObject(font->handle);
|
||||
DeleteDC(font->dc);
|
||||
free(font);
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_clipboard_paste(nk_handle usr, struct nk_text_edit* edit)
|
||||
{
|
||||
(void)usr;
|
||||
if (IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL))
|
||||
{
|
||||
HGLOBAL mem = GetClipboardData(CF_UNICODETEXT);
|
||||
if (mem)
|
||||
{
|
||||
SIZE_T size = GlobalSize(mem) - 1;
|
||||
if (size)
|
||||
{
|
||||
LPCWSTR wstr = (LPCWSTR)GlobalLock(mem);
|
||||
if (wstr)
|
||||
{
|
||||
int utf8size = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)(size / sizeof(wchar_t)), NULL, 0, NULL, NULL);
|
||||
if (utf8size)
|
||||
{
|
||||
char* utf8 = (char*)malloc(utf8size);
|
||||
if (utf8)
|
||||
{
|
||||
WideCharToMultiByte(CP_UTF8, 0, wstr, (int)(size / sizeof(wchar_t)), utf8, utf8size, NULL, NULL);
|
||||
nk_textedit_paste(edit, utf8, utf8size);
|
||||
free(utf8);
|
||||
}
|
||||
}
|
||||
GlobalUnlock(mem);
|
||||
}
|
||||
}
|
||||
}
|
||||
CloseClipboard();
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdi_clipboard_copy(nk_handle usr, const char* text, int len)
|
||||
{
|
||||
if (OpenClipboard(NULL))
|
||||
{
|
||||
int wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
|
||||
if (wsize)
|
||||
{
|
||||
HGLOBAL mem = (HGLOBAL)GlobalAlloc(GMEM_MOVEABLE, (wsize + 1) * sizeof(wchar_t));
|
||||
if (mem)
|
||||
{
|
||||
wchar_t* wstr = (wchar_t*)GlobalLock(mem);
|
||||
if (wstr)
|
||||
{
|
||||
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
|
||||
wstr[wsize] = 0;
|
||||
GlobalUnlock(mem);
|
||||
|
||||
SetClipboardData(CF_UNICODETEXT, mem);
|
||||
}
|
||||
}
|
||||
}
|
||||
CloseClipboard();
|
||||
}
|
||||
}
|
||||
|
||||
NK_API struct nk_context*
|
||||
nk_gdi_init(nk_gdi_ctx* gdi, GdiFont* gdifont, HDC window_dc, unsigned int width, unsigned int height)
|
||||
{
|
||||
*gdi = (nk_gdi_ctx)malloc(sizeof(struct _nk_gdi_ctx));
|
||||
|
||||
struct nk_user_font* font = &gdifont->nk;
|
||||
font->userdata = nk_handle_ptr(gdifont);
|
||||
font->height = (float)gdifont->height;
|
||||
font->width = nk_gdifont_get_text_width;
|
||||
|
||||
(*gdi)->bitmap = CreateCompatibleBitmap(window_dc, width, height);
|
||||
(*gdi)->window_dc = window_dc;
|
||||
(*gdi)->memory_dc = CreateCompatibleDC(window_dc);
|
||||
(*gdi)->width = width;
|
||||
(*gdi)->height = height;
|
||||
SelectObject((*gdi)->memory_dc, (*gdi)->bitmap);
|
||||
|
||||
nk_init_default(&(*gdi)->ctx, font);
|
||||
(*gdi)->ctx.clip.copy = nk_gdi_clipboard_copy;
|
||||
(*gdi)->ctx.clip.paste = nk_gdi_clipboard_paste;
|
||||
return &(*gdi)->ctx;
|
||||
}
|
||||
|
||||
NK_API void
|
||||
nk_gdi_set_font(nk_gdi_ctx gdi, GdiFont* gdifont)
|
||||
{
|
||||
struct nk_user_font* font = &gdifont->nk;
|
||||
font->userdata = nk_handle_ptr(gdifont);
|
||||
font->height = (float)gdifont->height;
|
||||
font->width = nk_gdifont_get_text_width;
|
||||
nk_style_set_font(&gdi->ctx, font);
|
||||
}
|
||||
|
||||
NK_API int
|
||||
nk_gdi_handle_event(nk_gdi_ctx gdi, HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
|
||||
{
|
||||
switch (msg)
|
||||
{
|
||||
case WM_SIZE:
|
||||
{
|
||||
unsigned width = LOWORD(lparam);
|
||||
unsigned height = HIWORD(lparam);
|
||||
if (width != gdi->width || height != gdi->height)
|
||||
{
|
||||
DeleteObject(gdi->bitmap);
|
||||
gdi->bitmap = CreateCompatibleBitmap(gdi->window_dc, width, height);
|
||||
gdi->width = width;
|
||||
gdi->height = height;
|
||||
SelectObject(gdi->memory_dc, gdi->bitmap);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_PAINT:
|
||||
{
|
||||
PAINTSTRUCT paint;
|
||||
HDC dc = BeginPaint(wnd, &paint);
|
||||
nk_gdi_blit(gdi, dc);
|
||||
EndPaint(wnd, &paint);
|
||||
return 1;
|
||||
}
|
||||
|
||||
case WM_KEYDOWN:
|
||||
case WM_KEYUP:
|
||||
case WM_SYSKEYDOWN:
|
||||
case WM_SYSKEYUP:
|
||||
{
|
||||
int down = !((lparam >> 31) & 1);
|
||||
int ctrl = GetKeyState(VK_CONTROL) & (1 << 15);
|
||||
|
||||
switch (wparam)
|
||||
{
|
||||
case VK_SHIFT:
|
||||
case VK_LSHIFT:
|
||||
case VK_RSHIFT:
|
||||
nk_input_key(&gdi->ctx, NK_KEY_SHIFT, down);
|
||||
return 1;
|
||||
|
||||
case VK_DELETE:
|
||||
nk_input_key(&gdi->ctx, NK_KEY_DEL, down);
|
||||
return 1;
|
||||
|
||||
case VK_RETURN:
|
||||
nk_input_key(&gdi->ctx, NK_KEY_ENTER, down);
|
||||
return 1;
|
||||
|
||||
case VK_TAB:
|
||||
nk_input_key(&gdi->ctx, NK_KEY_TAB, down);
|
||||
return 1;
|
||||
|
||||
case VK_LEFT:
|
||||
if (ctrl)
|
||||
nk_input_key(&gdi->ctx, NK_KEY_TEXT_WORD_LEFT, down);
|
||||
else
|
||||
nk_input_key(&gdi->ctx, NK_KEY_LEFT, down);
|
||||
return 1;
|
||||
|
||||
case VK_RIGHT:
|
||||
if (ctrl)
|
||||
nk_input_key(&gdi->ctx, NK_KEY_TEXT_WORD_RIGHT, down);
|
||||
else
|
||||
nk_input_key(&gdi->ctx, NK_KEY_RIGHT, down);
|
||||
return 1;
|
||||
|
||||
case VK_BACK:
|
||||
nk_input_key(&gdi->ctx, NK_KEY_BACKSPACE, down);
|
||||
return 1;
|
||||
|
||||
case VK_HOME:
|
||||
nk_input_key(&gdi->ctx, NK_KEY_TEXT_START, down);
|
||||
nk_input_key(&gdi->ctx, NK_KEY_SCROLL_START, down);
|
||||
return 1;
|
||||
|
||||
case VK_END:
|
||||
nk_input_key(&gdi->ctx, NK_KEY_TEXT_END, down);
|
||||
nk_input_key(&gdi->ctx, NK_KEY_SCROLL_END, down);
|
||||
return 1;
|
||||
|
||||
case VK_NEXT:
|
||||
nk_input_key(&gdi->ctx, NK_KEY_SCROLL_DOWN, down);
|
||||
return 1;
|
||||
|
||||
case VK_PRIOR:
|
||||
nk_input_key(&gdi->ctx, NK_KEY_SCROLL_UP, down);
|
||||
return 1;
|
||||
|
||||
case 'C':
|
||||
if (ctrl) {
|
||||
nk_input_key(&gdi->ctx, NK_KEY_COPY, down);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'V':
|
||||
if (ctrl) {
|
||||
nk_input_key(&gdi->ctx, NK_KEY_PASTE, down);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'X':
|
||||
if (ctrl) {
|
||||
nk_input_key(&gdi->ctx, NK_KEY_CUT, down);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
if (ctrl) {
|
||||
nk_input_key(&gdi->ctx, NK_KEY_TEXT_UNDO, down);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'R':
|
||||
if (ctrl) {
|
||||
nk_input_key(&gdi->ctx, NK_KEY_TEXT_REDO, down);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_CHAR:
|
||||
if (wparam >= 32)
|
||||
{
|
||||
nk_input_unicode(&gdi->ctx, (nk_rune)wparam);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case WM_LBUTTONDOWN:
|
||||
nk_input_button(&gdi->ctx, NK_BUTTON_LEFT, (short)LOWORD(lparam), (short)HIWORD(lparam), 1);
|
||||
SetCapture(wnd);
|
||||
return 1;
|
||||
|
||||
case WM_LBUTTONUP:
|
||||
nk_input_button(&gdi->ctx, NK_BUTTON_DOUBLE, (short)LOWORD(lparam), (short)HIWORD(lparam), 0);
|
||||
nk_input_button(&gdi->ctx, NK_BUTTON_LEFT, (short)LOWORD(lparam), (short)HIWORD(lparam), 0);
|
||||
ReleaseCapture();
|
||||
return 1;
|
||||
|
||||
case WM_RBUTTONDOWN:
|
||||
nk_input_button(&gdi->ctx, NK_BUTTON_RIGHT, (short)LOWORD(lparam), (short)HIWORD(lparam), 1);
|
||||
SetCapture(wnd);
|
||||
return 1;
|
||||
|
||||
case WM_RBUTTONUP:
|
||||
nk_input_button(&gdi->ctx, NK_BUTTON_RIGHT, (short)LOWORD(lparam), (short)HIWORD(lparam), 0);
|
||||
ReleaseCapture();
|
||||
return 1;
|
||||
|
||||
case WM_MBUTTONDOWN:
|
||||
nk_input_button(&gdi->ctx, NK_BUTTON_MIDDLE, (short)LOWORD(lparam), (short)HIWORD(lparam), 1);
|
||||
SetCapture(wnd);
|
||||
return 1;
|
||||
|
||||
case WM_MBUTTONUP:
|
||||
nk_input_button(&gdi->ctx, NK_BUTTON_MIDDLE, (short)LOWORD(lparam), (short)HIWORD(lparam), 0);
|
||||
ReleaseCapture();
|
||||
return 1;
|
||||
|
||||
case WM_MOUSEWHEEL:
|
||||
nk_input_scroll(&gdi->ctx, nk_vec2(0, (float)(short)HIWORD(wparam) / WHEEL_DELTA));
|
||||
return 1;
|
||||
|
||||
case WM_MOUSEMOVE:
|
||||
nk_input_motion(&gdi->ctx, (short)LOWORD(lparam), (short)HIWORD(lparam));
|
||||
return 1;
|
||||
|
||||
case WM_LBUTTONDBLCLK:
|
||||
nk_input_button(&gdi->ctx, NK_BUTTON_DOUBLE, (short)LOWORD(lparam), (short)HIWORD(lparam), 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
NK_API void
|
||||
nk_gdi_shutdown(nk_gdi_ctx gdi)
|
||||
{
|
||||
DeleteObject(gdi->memory_dc);
|
||||
DeleteObject(gdi->bitmap);
|
||||
nk_free(&gdi->ctx);
|
||||
}
|
||||
|
||||
NK_API void
|
||||
nk_gdi_render(nk_gdi_ctx gdi, struct nk_color clear)
|
||||
{
|
||||
const struct nk_command* cmd;
|
||||
|
||||
HDC memory_dc = gdi->memory_dc;
|
||||
SelectObject(memory_dc, GetStockObject(DC_PEN));
|
||||
SelectObject(memory_dc, GetStockObject(DC_BRUSH));
|
||||
nk_gdi_clear(gdi, memory_dc, clear);
|
||||
|
||||
nk_foreach(cmd, &gdi->ctx)
|
||||
{
|
||||
switch (cmd->type) {
|
||||
case NK_COMMAND_NOP: break;
|
||||
case NK_COMMAND_SCISSOR: {
|
||||
const struct nk_command_scissor* s = (const struct nk_command_scissor*)cmd;
|
||||
nk_gdi_scissor(memory_dc, s->x, s->y, s->w, s->h);
|
||||
} break;
|
||||
case NK_COMMAND_LINE: {
|
||||
const struct nk_command_line* l = (const struct nk_command_line*)cmd;
|
||||
nk_gdi_stroke_line(memory_dc, l->begin.x, l->begin.y, l->end.x,
|
||||
l->end.y, l->line_thickness, l->color);
|
||||
} break;
|
||||
case NK_COMMAND_RECT: {
|
||||
const struct nk_command_rect* r = (const struct nk_command_rect*)cmd;
|
||||
nk_gdi_stroke_rect(memory_dc, r->x, r->y, r->w, r->h,
|
||||
(unsigned short)r->rounding, r->line_thickness, r->color);
|
||||
} break;
|
||||
case NK_COMMAND_RECT_FILLED: {
|
||||
const struct nk_command_rect_filled* r = (const struct nk_command_rect_filled*)cmd;
|
||||
nk_gdi_fill_rect(memory_dc, r->x, r->y, r->w, r->h,
|
||||
(unsigned short)r->rounding, r->color);
|
||||
} break;
|
||||
case NK_COMMAND_CIRCLE: {
|
||||
const struct nk_command_circle* c = (const struct nk_command_circle*)cmd;
|
||||
nk_gdi_stroke_circle(memory_dc, c->x, c->y, c->w, c->h, c->line_thickness, c->color);
|
||||
} break;
|
||||
case NK_COMMAND_CIRCLE_FILLED: {
|
||||
const struct nk_command_circle_filled* c = (const struct nk_command_circle_filled*)cmd;
|
||||
nk_gdi_fill_circle(memory_dc, c->x, c->y, c->w, c->h, c->color);
|
||||
} break;
|
||||
case NK_COMMAND_TRIANGLE: {
|
||||
const struct nk_command_triangle* t = (const struct nk_command_triangle*)cmd;
|
||||
nk_gdi_stroke_triangle(memory_dc, t->a.x, t->a.y, t->b.x, t->b.y,
|
||||
t->c.x, t->c.y, t->line_thickness, t->color);
|
||||
} break;
|
||||
case NK_COMMAND_TRIANGLE_FILLED: {
|
||||
const struct nk_command_triangle_filled* t = (const struct nk_command_triangle_filled*)cmd;
|
||||
nk_gdi_fill_triangle(memory_dc, t->a.x, t->a.y, t->b.x, t->b.y,
|
||||
t->c.x, t->c.y, t->color);
|
||||
} break;
|
||||
case NK_COMMAND_POLYGON: {
|
||||
const struct nk_command_polygon* p = (const struct nk_command_polygon*)cmd;
|
||||
nk_gdi_stroke_polygon(memory_dc, p->points, p->point_count, p->line_thickness, p->color);
|
||||
} break;
|
||||
case NK_COMMAND_POLYGON_FILLED: {
|
||||
const struct nk_command_polygon_filled* p = (const struct nk_command_polygon_filled*)cmd;
|
||||
nk_gdi_fill_polygon(memory_dc, p->points, p->point_count, p->color);
|
||||
} break;
|
||||
case NK_COMMAND_POLYLINE: {
|
||||
const struct nk_command_polyline* p = (const struct nk_command_polyline*)cmd;
|
||||
nk_gdi_stroke_polyline(memory_dc, p->points, p->point_count, p->line_thickness, p->color);
|
||||
} break;
|
||||
case NK_COMMAND_TEXT: {
|
||||
const struct nk_command_text* t = (const struct nk_command_text*)cmd;
|
||||
nk_gdi_draw_text(memory_dc, t->x, t->y, t->w, t->h,
|
||||
(const char*)t->string, t->length,
|
||||
(GdiFont*)t->font->userdata.ptr,
|
||||
t->background, t->foreground);
|
||||
} break;
|
||||
case NK_COMMAND_CURVE: {
|
||||
const struct nk_command_curve* q = (const struct nk_command_curve*)cmd;
|
||||
nk_gdi_stroke_curve(memory_dc, q->begin, q->ctrl[0], q->ctrl[1],
|
||||
q->end, q->line_thickness, q->color);
|
||||
} break;
|
||||
case NK_COMMAND_RECT_MULTI_COLOR: {
|
||||
const struct nk_command_rect_multi_color* r = (const struct nk_command_rect_multi_color*)cmd;
|
||||
nk_gdi_rect_multi_color(gdi, memory_dc, r->x, r->y, r->w, r->h, r->left, r->top, r->right, r->bottom);
|
||||
} break;
|
||||
case NK_COMMAND_IMAGE: {
|
||||
const struct nk_command_image* i = (const struct nk_command_image*)cmd;
|
||||
nk_gdi_draw_image(gdi, i->x, i->y, i->w, i->h, i->img, i->col);
|
||||
} break;
|
||||
case NK_COMMAND_ARC:
|
||||
case NK_COMMAND_ARC_FILLED:
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
nk_gdi_blit(gdi, gdi->window_dc);
|
||||
nk_clear(&gdi->ctx);
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,387 @@
|
|||
#ifndef NK_GDI_WINDOW
|
||||
#define NK_GDI_WINDOW
|
||||
|
||||
#define NK_GDI_WINDOW_CLS L"WNDCLS_NkGdi"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
/* Functin pointer types for window callbacks */
|
||||
typedef int(*nkgdi_window_func_close)(void);
|
||||
typedef int(*nkgdi_window_func_draw)(struct nk_context*);
|
||||
|
||||
/* Window container / context */
|
||||
struct nkgdi_window
|
||||
{
|
||||
/* The window can be sized */
|
||||
int allow_sizing;
|
||||
/* The window can be maximized by double clicking the titlebar */
|
||||
int allow_maximize;
|
||||
/* The window is allowed to be moved by the user */
|
||||
int allow_move;
|
||||
/* The window will render it's title bar */
|
||||
int has_titlebar;
|
||||
|
||||
/* Callbacks */
|
||||
/* Called when the user or os requests a window close (return 1 to accept the reqest)*/
|
||||
nkgdi_window_func_close cb_on_close;
|
||||
/* Called each time the window content should be drawn. Here you will do your nuklear drawing code
|
||||
* but WITHOUT nk_begin and nk_end. Return 1 to keep the window open.
|
||||
*/
|
||||
nkgdi_window_func_draw cb_on_draw;
|
||||
|
||||
/* Internal Data */
|
||||
struct
|
||||
{
|
||||
/* Window handle */
|
||||
HWND window_handle;
|
||||
|
||||
/* Nuklear & GDI context */
|
||||
nk_gdi_ctx nk_gdi_ctx;
|
||||
struct nk_context* nk_ctx;
|
||||
|
||||
/* GDI required objects */
|
||||
GdiFont* gdi_font;
|
||||
HDC window_dc;
|
||||
|
||||
/* Internally used state variables */
|
||||
int is_open;
|
||||
int is_draggin;
|
||||
int ws_override;
|
||||
int is_maximized;
|
||||
POINT drag_offset;
|
||||
int width;
|
||||
int height;
|
||||
}_internal;
|
||||
};
|
||||
|
||||
/* API */
|
||||
/* This function will init all resources used by the implementation */
|
||||
void nkgdi_window_init(void);
|
||||
/* This function will free all globally used resources */
|
||||
void nkgdi_window_shutdown(void);
|
||||
/* Creates a new window (for the wnd context) */
|
||||
void nkgdi_window_create(struct nkgdi_window* wnd, unsigned int width, unsigned int height, const char* name, int posX, int posY);
|
||||
/* Updates the window (Windows message loop, nuklear loop and drawing). Returns one as long as the window is valid and open */
|
||||
int nkgdi_window_update(struct nkgdi_window* wnd);
|
||||
/* Destroys the window context wnd */
|
||||
void nkgdi_window_destroy(struct nkgdi_window* wnd);
|
||||
|
||||
#ifdef NKGDI_IMPLEMENT_WINDOW
|
||||
|
||||
/* Predeclare the windows window message procs */
|
||||
/* This proc will setup the pointer to the window context */
|
||||
LRESULT CALLBACK nkgdi_window_proc_setup(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam);
|
||||
/* This proc will take the window context pointer and performs operations on it*/
|
||||
LRESULT CALLBACK nkgdi_window_proc_run(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam);
|
||||
|
||||
void nkgdi_window_init(void)
|
||||
{
|
||||
/* Describe the window class */
|
||||
WNDCLASSEXW cls;
|
||||
cls.cbSize = sizeof(WNDCLASSEXW);
|
||||
cls.style = CS_OWNDC | CS_DBLCLKS;
|
||||
cls.lpfnWndProc = &nkgdi_window_proc_setup;
|
||||
cls.cbClsExtra = 0;
|
||||
cls.cbWndExtra = 0;
|
||||
cls.hInstance = GetModuleHandle(NULL);
|
||||
cls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
||||
cls.hCursor = LoadCursor(NULL, IDC_ARROW);
|
||||
cls.hbrBackground = NULL;
|
||||
cls.lpszMenuName = NULL;
|
||||
cls.lpszClassName = NK_GDI_WINDOW_CLS;
|
||||
cls.hIconSm = NULL;
|
||||
|
||||
/* Register the window class */
|
||||
RegisterClassExW(&cls);
|
||||
}
|
||||
|
||||
void nkgdi_window_shutdown(void)
|
||||
{
|
||||
/* Windows class no longer required, unregister it */
|
||||
UnregisterClassW(NK_GDI_WINDOW_CLS, GetModuleHandle(NULL));
|
||||
}
|
||||
|
||||
void nkgdi_window_create(struct nkgdi_window* wnd, unsigned int width, unsigned int height, const char* name, int posX, int posY)
|
||||
{
|
||||
DWORD styleEx = WS_EX_WINDOWEDGE;
|
||||
DWORD style = WS_POPUP;
|
||||
|
||||
/* Adjust window size to fit selected window styles */
|
||||
RECT cr;
|
||||
cr.left = 0;
|
||||
cr.top = 0;
|
||||
cr.right = width;
|
||||
cr.bottom = height;
|
||||
AdjustWindowRectEx(&cr, style, FALSE, styleEx);
|
||||
|
||||
/* Create the new window */
|
||||
wnd->_internal.window_handle = CreateWindowExW(
|
||||
styleEx,
|
||||
NK_GDI_WINDOW_CLS,
|
||||
L"NkGdi",
|
||||
style | WS_VISIBLE,
|
||||
posX, posY,
|
||||
cr.right - cr.left, cr.bottom - cr.top,
|
||||
NULL, NULL,
|
||||
GetModuleHandleW(NULL),
|
||||
wnd
|
||||
);
|
||||
|
||||
/* Give the window the ascii char name */
|
||||
SetWindowTextA(wnd->_internal.window_handle, name);
|
||||
|
||||
/* Extract the window dc for gdi drawing */
|
||||
wnd->_internal.window_dc = GetWindowDC(wnd->_internal.window_handle);
|
||||
|
||||
/* Create the gdi font required to draw text */
|
||||
wnd->_internal.gdi_font = nk_gdifont_create("Arial", 16);
|
||||
|
||||
/* Init the gdi backend */
|
||||
wnd->_internal.nk_ctx = nk_gdi_init(&wnd->_internal.nk_gdi_ctx, wnd->_internal.gdi_font, wnd->_internal.window_dc, width, height);
|
||||
|
||||
/* Bring all internal variables to a defined and valid initial state */
|
||||
wnd->_internal.is_open = 1;
|
||||
wnd->_internal.is_draggin = 0;
|
||||
wnd->_internal.ws_override = 0;
|
||||
wnd->_internal.is_maximized = 0;
|
||||
wnd->_internal.drag_offset.x = 0;
|
||||
wnd->_internal.drag_offset.y = 0;
|
||||
wnd->_internal.width = width;
|
||||
wnd->_internal.height = height;
|
||||
}
|
||||
|
||||
void nkgdi_window_destroy(struct nkgdi_window* wnd)
|
||||
{
|
||||
/* Destroy all objects in reverse order */
|
||||
if (wnd->_internal.nk_gdi_ctx)
|
||||
{
|
||||
nk_gdi_shutdown(wnd->_internal.nk_gdi_ctx);
|
||||
}
|
||||
if (wnd->_internal.gdi_font)
|
||||
{
|
||||
nk_gdifont_del(wnd->_internal.gdi_font);
|
||||
}
|
||||
if (wnd->_internal.window_dc)
|
||||
{
|
||||
ReleaseDC(wnd->_internal.window_handle, wnd->_internal.window_dc);
|
||||
}
|
||||
if (wnd->_internal.window_handle)
|
||||
{
|
||||
CloseWindow(wnd->_internal.window_handle);
|
||||
DestroyWindow(wnd->_internal.window_handle);
|
||||
}
|
||||
}
|
||||
|
||||
int nkgdi_window_update(struct nkgdi_window* wnd)
|
||||
{
|
||||
/* The window will only be updated when it is open / valid */
|
||||
if (wnd->_internal.is_open)
|
||||
{
|
||||
/* First all pending window events will be processed */
|
||||
MSG msg;
|
||||
nk_input_begin(wnd->_internal.nk_ctx);
|
||||
while (PeekMessage(&msg, wnd->_internal.window_handle, 0, 0, PM_REMOVE))
|
||||
{
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessageW(&msg);
|
||||
}
|
||||
nk_input_end(wnd->_internal.nk_ctx);
|
||||
|
||||
/* To setup the nuklear window we need the windows title */
|
||||
char title[1024];
|
||||
GetWindowTextA(wnd->_internal.window_handle, title, 1024);
|
||||
|
||||
/* The nk window flags are beeing create based on the context setup */
|
||||
nk_flags window_flags = NK_WINDOW_BORDER;
|
||||
if(wnd->has_titlebar)
|
||||
window_flags |= NK_WINDOW_CLOSABLE | NK_WINDOW_TITLE;
|
||||
if(!wnd->_internal.is_maximized && wnd->allow_sizing)
|
||||
window_flags |= NK_WINDOW_SCALABLE;
|
||||
|
||||
/* Override the nuklear windows size when required */
|
||||
if (wnd->_internal.ws_override)
|
||||
nk_window_set_bounds(wnd->_internal.nk_ctx, title, nk_rect(0, 0, wnd->_internal.width, wnd->_internal.height));
|
||||
|
||||
/* Start the nuklear window */
|
||||
if (nk_begin(wnd->_internal.nk_ctx, title, nk_rect(0, 0, wnd->_internal.width, wnd->_internal.height), window_flags))
|
||||
{
|
||||
/* Call user drawing callback */
|
||||
if(wnd->cb_on_draw && !wnd->cb_on_draw(wnd->_internal.nk_ctx))
|
||||
wnd->_internal.is_open = 0;
|
||||
|
||||
/* Update the windows window to reflect the nuklear windows size */
|
||||
struct nk_rect bounds = nk_window_get_bounds(wnd->_internal.nk_ctx);
|
||||
if(bounds.w != wnd->_internal.width || bounds.h != wnd->_internal.height)
|
||||
SetWindowPos(wnd->_internal.window_handle, NULL, 0, 0, bounds.w, bounds.h, SWP_NOMOVE | SWP_NOOWNERZORDER);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Nuklear window was closed. Handle close internally */
|
||||
if(!wnd->cb_on_close || wnd->cb_on_close())
|
||||
wnd->_internal.is_open = 0;
|
||||
}
|
||||
nk_end(wnd->_internal.nk_ctx);
|
||||
|
||||
/* We no longer need the window size override flag to be set */
|
||||
wnd->_internal.ws_override = 0;
|
||||
|
||||
/* Pass context to the nuklear gdi renderer */
|
||||
nk_gdi_render(wnd->_internal.nk_gdi_ctx, nk_rgb(0, 0, 0));
|
||||
}
|
||||
|
||||
return wnd->_internal.is_open;
|
||||
}
|
||||
|
||||
LRESULT CALLBACK nkgdi_window_proc_setup(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
/* Waiting to receive the NCCREATE message with the custom window data */
|
||||
if (msg == WM_NCCREATE)
|
||||
{
|
||||
/* Extracting the window context from message parameters */
|
||||
CREATESTRUCT* ptrCr = (CREATESTRUCT*)lParam;
|
||||
struct nkgdi_window* nkgdi_wnd = (struct nkgdi_window*)ptrCr->lpCreateParams;
|
||||
|
||||
/* Store the context in the window and redirect any further message to the run window proc*/
|
||||
SetWindowLongPtr(wnd, GWLP_USERDATA, (LONG_PTR)nkgdi_wnd);
|
||||
SetWindowLongPtr(wnd, GWLP_WNDPROC, (LONG_PTR)&nkgdi_window_proc_run);
|
||||
|
||||
/* Already call the run proc so that it gets the chance to handle NCCREATE as well */
|
||||
return nkgdi_window_proc_run(wnd, msg, wParam, lParam);
|
||||
}
|
||||
|
||||
/* Until we get WM_NCCREATE windows is going to handle the messages */
|
||||
return DefWindowProc(wnd, msg, wParam, lParam);
|
||||
}
|
||||
|
||||
LRESULT CALLBACK nkgdi_window_proc_run(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
/* The window context is extracted from the window data */
|
||||
struct nkgdi_window* nkwnd = (struct nkgdi_window*)GetWindowLongPtrW(wnd, GWLP_USERDATA);
|
||||
|
||||
/* Switch on the message code to handle all required messages */
|
||||
switch (msg)
|
||||
{
|
||||
/* Window close event */
|
||||
case WM_CLOSE:
|
||||
/* Call custom close callback */
|
||||
if(!nkwnd->cb_on_close || nkwnd->cb_on_close())
|
||||
nkwnd->_internal.is_open = 0;
|
||||
return 0; /* No default behaviour. We do it our own way */
|
||||
|
||||
/* Window sizing event (is currently beeing sized) */
|
||||
case WM_SIZING:
|
||||
{
|
||||
/* Size of the client / active are is extracted and stored */
|
||||
RECT cr;
|
||||
GetClientRect(wnd, &cr);
|
||||
nkwnd->_internal.width = cr.right - cr.left;
|
||||
nkwnd->_internal.height = cr.bottom - cr.top;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Window size event (done sizing, maximize, minimize, ...) */
|
||||
case WM_SIZE:
|
||||
{
|
||||
/* Window was maximized */
|
||||
if (wParam == SIZE_MAXIMIZED)
|
||||
{
|
||||
/* Get the nearest monitor and retrive its details */
|
||||
HMONITOR monitor = MonitorFromWindow(wnd, MONITOR_DEFAULTTOPRIMARY);
|
||||
MONITORINFO monitorInfo;
|
||||
monitorInfo.cbSize = sizeof(MONITORINFO);
|
||||
if (GetMonitorInfoW(monitor, &monitorInfo))
|
||||
{
|
||||
/* Adjust the window size and position by the monitor working area (without taskbar) */
|
||||
nkwnd->_internal.height = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
|
||||
nkwnd->_internal.width = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
|
||||
nkwnd->_internal.ws_override = 1; /* Sizing was done without nuklear beeing aware. So we need to override it */
|
||||
nkwnd->_internal.is_maximized = 1;
|
||||
SetWindowPos(wnd, NULL, 0, 0, nkwnd->_internal.width, nkwnd->_internal.height, SWP_NOMOVE | SWP_NOZORDER);
|
||||
}
|
||||
}
|
||||
/* Window was restored (no longer maximized) */
|
||||
else if (wParam == SIZE_RESTORED)
|
||||
{
|
||||
nkwnd->_internal.is_maximized = 0;
|
||||
}
|
||||
|
||||
/* Always get the new bounds of the window */
|
||||
RECT cr;
|
||||
GetClientRect(wnd, &cr);
|
||||
nkwnd->_internal.width = cr.right - cr.left;
|
||||
nkwnd->_internal.height = cr.bottom - cr.top;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Mouse started left click */
|
||||
case WM_LBUTTONDOWN:
|
||||
{
|
||||
/* Handle dragging when allowed, has titlebar and mouse is in titlebar (Y <= 30) */
|
||||
if (HIWORD(lParam) <= 30 && nkwnd->allow_move && nkwnd->has_titlebar)
|
||||
{
|
||||
/* Mark dragging internally and store mouse click offset */
|
||||
nkwnd->_internal.is_draggin = 1;
|
||||
nkwnd->_internal.drag_offset.x = LOWORD(lParam);
|
||||
nkwnd->_internal.drag_offset.y = HIWORD(lParam);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
/* Mouse stoped left click */
|
||||
case WM_LBUTTONUP:
|
||||
/* No longer dragging the window */
|
||||
nkwnd->_internal.is_draggin = 0;
|
||||
break;
|
||||
|
||||
/* Mouse is moving on the window */
|
||||
case WM_MOUSEMOVE:
|
||||
{
|
||||
/* When we are dragging and are not maximized process dragging */
|
||||
if (nkwnd->_internal.is_draggin && !nkwnd->_internal.is_maximized)
|
||||
{
|
||||
/* Get the current global position of the mouse */
|
||||
POINT cursorPos;
|
||||
GetCursorPos(&cursorPos);
|
||||
/* Substract the internal offset */
|
||||
cursorPos.x -= nkwnd->_internal.drag_offset.x;
|
||||
cursorPos.y -= nkwnd->_internal.drag_offset.y;
|
||||
/* Update position of out window and make sure window is in a movable state (= Restored) */
|
||||
ShowWindow(wnd, SW_RESTORE);
|
||||
SetWindowPos(wnd, NULL, cursorPos.x, cursorPos.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
/* Mouse double clicked */
|
||||
case WM_LBUTTONDBLCLK:
|
||||
{
|
||||
/* Will only affect window when on the titlebar */
|
||||
if (HIWORD(lParam) <= 30 && nkwnd->allow_maximize && nkwnd->has_titlebar)
|
||||
{
|
||||
/* When the window is already maximized restore it */
|
||||
if (nkwnd->_internal.is_maximized)
|
||||
{
|
||||
ShowWindow(wnd, SW_RESTORE);
|
||||
}
|
||||
/* Else we gonna do maximize it*/
|
||||
else
|
||||
{
|
||||
ShowWindow(wnd, SW_MAXIMIZE);
|
||||
}
|
||||
/* We overrideed the window size, make sure to affect the nk window as well */
|
||||
nkwnd->_internal.ws_override = 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* Allow nuklear to handle the message as well */
|
||||
if (nkwnd->_internal.nk_gdi_ctx && nk_gdi_handle_event(nkwnd->_internal.nk_gdi_ctx, wnd, msg, wParam, lParam))
|
||||
return 0;
|
||||
|
||||
/* In case we reach this line our code and nuklear did not respond to the message. Allow windows to handle it s*/
|
||||
return DefWindowProc(wnd, msg, wParam, lParam);
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -99,7 +99,7 @@ int main(void)
|
|||
|
||||
AdjustWindowRectEx(&rect, style, FALSE, exstyle);
|
||||
|
||||
wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear Demo",
|
||||
wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear GDI+ Demo",
|
||||
style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
rect.right - rect.left, rect.bottom - rect.top,
|
||||
NULL, NULL, wc.hInstance, NULL);
|
||||
|
@ -111,10 +111,16 @@ int main(void)
|
|||
|
||||
/* style.c */
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
while (running)
|
||||
|
|
|
@ -761,7 +761,7 @@ nk_gdipfont_get_text_width(nk_handle handle, float height, const char *text, int
|
|||
|
||||
(void)height;
|
||||
wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
|
||||
wstr = (WCHAR*)_alloca(wsize * sizeof(wchar_t));
|
||||
wstr = (WCHAR*)_malloca(wsize * sizeof(wchar_t));
|
||||
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
|
||||
|
||||
GdipMeasureString(gdip.memory, wstr, wsize, font->handle, &layout, gdip.format, &bbox, NULL, NULL);
|
||||
|
@ -777,93 +777,63 @@ nk_gdipfont_del(GdipFont *font)
|
|||
}
|
||||
|
||||
static void
|
||||
nk_gdip_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
|
||||
nk_gdip_clipboard_paste(nk_handle usr, struct nk_text_edit* edit)
|
||||
{
|
||||
HGLOBAL mem;
|
||||
SIZE_T size;
|
||||
LPCWSTR wstr;
|
||||
int utf8size;
|
||||
char* utf8;
|
||||
(void)usr;
|
||||
|
||||
if (!IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL))
|
||||
return;
|
||||
|
||||
mem = (HGLOBAL)GetClipboardData(CF_UNICODETEXT);
|
||||
if (!mem) {
|
||||
CloseClipboard();
|
||||
return;
|
||||
}
|
||||
|
||||
size = GlobalSize(mem) - 1;
|
||||
if (!size) {
|
||||
CloseClipboard();
|
||||
return;
|
||||
}
|
||||
|
||||
wstr = (LPCWSTR)GlobalLock(mem);
|
||||
if (!wstr) {
|
||||
CloseClipboard();
|
||||
return;
|
||||
}
|
||||
|
||||
utf8size = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)(size / sizeof(wchar_t)), NULL, 0, NULL, NULL);
|
||||
if (!utf8size) {
|
||||
GlobalUnlock(mem);
|
||||
CloseClipboard();
|
||||
return;
|
||||
}
|
||||
|
||||
utf8 = (char*)malloc(utf8size);
|
||||
if (!utf8) {
|
||||
GlobalUnlock(mem);
|
||||
CloseClipboard();
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL))
|
||||
{
|
||||
HGLOBAL mem = GetClipboardData(CF_UNICODETEXT);
|
||||
if (mem)
|
||||
{
|
||||
SIZE_T size = GlobalSize(mem) - 1;
|
||||
if (size)
|
||||
{
|
||||
LPCWSTR wstr = (LPCWSTR)GlobalLock(mem);
|
||||
if (wstr)
|
||||
{
|
||||
int utf8size = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)(size / sizeof(wchar_t)), NULL, 0, NULL, NULL);
|
||||
if (utf8size)
|
||||
{
|
||||
char* utf8 = (char*)malloc(utf8size);
|
||||
if (utf8)
|
||||
{
|
||||
WideCharToMultiByte(CP_UTF8, 0, wstr, (int)(size / sizeof(wchar_t)), utf8, utf8size, NULL, NULL);
|
||||
nk_textedit_paste(edit, utf8, utf8size);
|
||||
free(utf8);
|
||||
}
|
||||
}
|
||||
GlobalUnlock(mem);
|
||||
}
|
||||
}
|
||||
}
|
||||
CloseClipboard();
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
nk_gdip_clipboard_copy(nk_handle usr, const char *text, int len)
|
||||
nk_gdip_clipboard_copy(nk_handle usr, const char* text, int len)
|
||||
{
|
||||
HGLOBAL mem;
|
||||
wchar_t* wstr;
|
||||
int wsize;
|
||||
(void)usr;
|
||||
|
||||
if (!OpenClipboard(NULL))
|
||||
return;
|
||||
|
||||
wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
|
||||
if (!wsize) {
|
||||
CloseClipboard();
|
||||
return;
|
||||
}
|
||||
|
||||
mem = (HGLOBAL)GlobalAlloc(GMEM_MOVEABLE, (wsize + 1) * sizeof(wchar_t));
|
||||
if (!mem) {
|
||||
CloseClipboard();
|
||||
return;
|
||||
}
|
||||
|
||||
wstr = (wchar_t*)GlobalLock(mem);
|
||||
if (!wstr) {
|
||||
GlobalFree(mem);
|
||||
CloseClipboard();
|
||||
return;
|
||||
}
|
||||
|
||||
if (OpenClipboard(NULL))
|
||||
{
|
||||
int wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
|
||||
if (wsize)
|
||||
{
|
||||
HGLOBAL mem = (HGLOBAL)GlobalAlloc(GMEM_MOVEABLE, (wsize + 1) * sizeof(wchar_t));
|
||||
if (mem)
|
||||
{
|
||||
wchar_t* wstr = (wchar_t*)GlobalLock(mem);
|
||||
if (wstr)
|
||||
{
|
||||
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
|
||||
wstr[wsize] = 0;
|
||||
GlobalUnlock(mem);
|
||||
if (!SetClipboardData(CF_UNICODETEXT, mem))
|
||||
GlobalFree(mem);
|
||||
|
||||
SetClipboardData(CF_UNICODETEXT, mem);
|
||||
}
|
||||
}
|
||||
}
|
||||
CloseClipboard();
|
||||
}
|
||||
}
|
||||
|
||||
NK_API struct nk_context*
|
||||
|
@ -997,6 +967,13 @@ nk_gdip_handle_event(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
|
|||
nk_input_key(&gdip.ctx, NK_KEY_SCROLL_UP, down);
|
||||
return 1;
|
||||
|
||||
case 'A':
|
||||
if (ctrl) {
|
||||
nk_input_key(&gdip.ctx, NK_KEY_TEXT_SELECT_ALL, down);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'C':
|
||||
if (ctrl) {
|
||||
nk_input_key(&gdip.ctx, NK_KEY_COPY, down);
|
||||
|
|
|
@ -121,10 +121,16 @@ int main(void)
|
|||
/*nk_style_set_font(ctx, &droid->handle);*/}
|
||||
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
|
||||
|
|
|
@ -53,7 +53,7 @@ NK_API void nk_gflw3_scroll_callback(GLFWwindow *win, double xof
|
|||
|
||||
struct nk_glfw_device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint font_tex;
|
||||
};
|
||||
|
||||
|
@ -140,7 +140,7 @@ nk_glfw3_render(enum nk_anti_aliasing AA)
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_glfw_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_glfw_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -288,7 +288,7 @@ nk_glfw3_font_stash_end(void)
|
|||
const void *image; int w, h;
|
||||
image = nk_font_atlas_bake(&glfw.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
nk_glfw3_device_upload_atlas(image, w, h);
|
||||
nk_font_atlas_end(&glfw.atlas, nk_handle_id((int)glfw.ogl.font_tex), &glfw.ogl.null);
|
||||
nk_font_atlas_end(&glfw.atlas, nk_handle_id((int)glfw.ogl.font_tex), &glfw.ogl.tex_null);
|
||||
if (glfw.atlas.default_font)
|
||||
nk_style_set_font(&glfw.ctx, &glfw.atlas.default_font->handle);
|
||||
}
|
||||
|
|
|
@ -126,10 +126,16 @@ int main(void)
|
|||
/*nk_style_set_font(ctx, &droid->handle);*/}
|
||||
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
|
||||
|
|
|
@ -26,7 +26,7 @@ enum nk_glfw_init_state{
|
|||
|
||||
struct nk_glfw_device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint vbo, vao, ebo;
|
||||
GLuint prog;
|
||||
GLuint vert_shdr;
|
||||
|
@ -240,7 +240,7 @@ nk_glfw3_render(struct nk_glfw* glfw, enum nk_anti_aliasing AA, int max_vertex_b
|
|||
/* convert from command queue into draw list and draw to screen */
|
||||
const struct nk_draw_command *cmd;
|
||||
void *vertices, *elements;
|
||||
const nk_draw_index *offset = NULL;
|
||||
nk_size offset = 0;
|
||||
|
||||
/* allocate vertex and element buffer */
|
||||
glBindVertexArray(dev->vao);
|
||||
|
@ -266,7 +266,7 @@ nk_glfw3_render(struct nk_glfw* glfw, enum nk_anti_aliasing AA, int max_vertex_b
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_glfw_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_glfw_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -292,8 +292,8 @@ nk_glfw3_render(struct nk_glfw* glfw, enum nk_anti_aliasing AA, int max_vertex_b
|
|||
(GLint)((glfw->height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * glfw->fb_scale.y),
|
||||
(GLint)(cmd->clip_rect.w * glfw->fb_scale.x),
|
||||
(GLint)(cmd->clip_rect.h * glfw->fb_scale.y));
|
||||
glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
|
||||
offset += cmd->elem_count;
|
||||
glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, (const void*) offset);
|
||||
offset += cmd->elem_count * sizeof(nk_draw_index);
|
||||
}
|
||||
nk_clear(&glfw->ctx);
|
||||
nk_buffer_clear(&dev->cmds);
|
||||
|
@ -403,7 +403,7 @@ nk_glfw3_font_stash_end(struct nk_glfw* glfw)
|
|||
const void *image; int w, h;
|
||||
image = nk_font_atlas_bake(&glfw->atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
nk_glfw3_device_upload_atlas(glfw, image, w, h);
|
||||
nk_font_atlas_end(&glfw->atlas, nk_handle_id((int)glfw->ogl.font_tex), &glfw->ogl.null);
|
||||
nk_font_atlas_end(&glfw->atlas, nk_handle_id((int)glfw->ogl.font_tex), &glfw->ogl.tex_null);
|
||||
if (glfw->atlas.default_font)
|
||||
nk_style_set_font(&glfw->ctx, &glfw->atlas.default_font->handle);
|
||||
}
|
||||
|
|
|
@ -126,10 +126,16 @@ int main(void)
|
|||
/*nk_style_set_font(ctx, &droid->handle);*/}
|
||||
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Create bindless texture.
|
||||
|
|
|
@ -72,7 +72,7 @@ struct nk_glfw_vertex {
|
|||
|
||||
struct nk_glfw_device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint vbo, vao, ebo;
|
||||
GLuint prog;
|
||||
GLuint vert_shdr;
|
||||
|
@ -407,7 +407,7 @@ nk_glfw3_render(enum nk_anti_aliasing AA)
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_glfw_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_glfw_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -553,7 +553,7 @@ nk_glfw3_font_stash_end(void)
|
|||
const void *image; int w, h;
|
||||
image = nk_font_atlas_bake(&glfw.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
nk_glfw3_device_upload_atlas(image, w, h);
|
||||
nk_font_atlas_end(&glfw.atlas, nk_handle_id((int)glfw.ogl.font_tex_index), &glfw.ogl.null);
|
||||
nk_font_atlas_end(&glfw.atlas, nk_handle_id((int)glfw.ogl.font_tex_index), &glfw.ogl.tex_null);
|
||||
if (glfw.atlas.default_font)
|
||||
nk_style_set_font(&glfw.ctx, &glfw.atlas.default_font->handle);
|
||||
}
|
||||
|
|
|
@ -117,10 +117,16 @@ main(int argc, char *argv[])
|
|||
/*nk_style_set_font(ctx, &roboto->handle)*/;}
|
||||
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
|
||||
|
|
|
@ -33,7 +33,7 @@ NK_API void nk_sdl_shutdown(void);
|
|||
|
||||
struct nk_sdl_device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint font_tex;
|
||||
};
|
||||
|
||||
|
@ -120,7 +120,7 @@ nk_sdl_render(enum nk_anti_aliasing AA)
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_sdl_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_sdl_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -226,7 +226,7 @@ nk_sdl_font_stash_end(void)
|
|||
const void *image; int w, h;
|
||||
image = nk_font_atlas_bake(&sdl.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
nk_sdl_device_upload_atlas(image, w, h);
|
||||
nk_font_atlas_end(&sdl.atlas, nk_handle_id((int)sdl.ogl.font_tex), &sdl.ogl.null);
|
||||
nk_font_atlas_end(&sdl.atlas, nk_handle_id((int)sdl.ogl.font_tex), &sdl.ogl.tex_null);
|
||||
if (sdl.atlas.default_font)
|
||||
nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle);
|
||||
}
|
||||
|
|
|
@ -128,10 +128,16 @@ int main(int argc, char *argv[])
|
|||
|
||||
/* style.c */
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
|
||||
|
|
|
@ -40,7 +40,7 @@ NK_API void nk_sdl_device_create(void);
|
|||
|
||||
struct nk_sdl_device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint vbo, vao, ebo;
|
||||
GLuint prog;
|
||||
GLuint vert_shdr;
|
||||
|
@ -247,7 +247,7 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_sdl_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_sdl_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -334,7 +334,7 @@ nk_sdl_font_stash_end(void)
|
|||
const void *image; int w, h;
|
||||
image = nk_font_atlas_bake(&sdl.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
nk_sdl_device_upload_atlas(image, w, h);
|
||||
nk_font_atlas_end(&sdl.atlas, nk_handle_id((int)sdl.ogl.font_tex), &sdl.ogl.null);
|
||||
nk_font_atlas_end(&sdl.atlas, nk_handle_id((int)sdl.ogl.font_tex), &sdl.ogl.tex_null);
|
||||
if (sdl.atlas.default_font)
|
||||
nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle);
|
||||
|
||||
|
|
|
@ -209,10 +209,18 @@ int main(int argc, char* argv[])
|
|||
/*nk_style_set_font(ctx, &roboto->handle)*/;}
|
||||
|
||||
/* style.c */
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
#ifdef INCLUDE_STYLE
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
#include <emscripten.h>
|
||||
|
|
|
@ -43,7 +43,7 @@ NK_API void nk_sdl_device_create(void);
|
|||
|
||||
struct nk_sdl_device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint vbo, ebo;
|
||||
GLuint prog;
|
||||
GLuint vert_shdr;
|
||||
|
@ -245,7 +245,7 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_sdl_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_sdl_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -335,7 +335,7 @@ nk_sdl_font_stash_end(void)
|
|||
const void *image; int w, h;
|
||||
image = nk_font_atlas_bake(&sdl.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
nk_sdl_device_upload_atlas(image, w, h);
|
||||
nk_font_atlas_end(&sdl.atlas, nk_handle_id((int)sdl.ogl.font_tex), &sdl.ogl.null);
|
||||
nk_font_atlas_end(&sdl.atlas, nk_handle_id((int)sdl.ogl.font_tex), &sdl.ogl.tex_null);
|
||||
if (sdl.atlas.default_font)
|
||||
nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle);
|
||||
|
||||
|
|
|
@ -76,6 +76,7 @@ main(int argc, char *argv[])
|
|||
SDL_Renderer *renderer;
|
||||
int running = 1;
|
||||
int flags = 0;
|
||||
float font_scale = 1;
|
||||
|
||||
/* GUI */
|
||||
struct nk_context *ctx;
|
||||
|
@ -112,28 +113,58 @@ main(int argc, char *argv[])
|
|||
exit(-1);
|
||||
}
|
||||
|
||||
/* scale the renderer output for High-DPI displays */
|
||||
{
|
||||
int render_w, render_h;
|
||||
int window_w, window_h;
|
||||
float scale_x, scale_y;
|
||||
SDL_GetRendererOutputSize(renderer, &render_w, &render_h);
|
||||
SDL_GetWindowSize(win, &window_w, &window_h);
|
||||
scale_x = (float)(render_w) / (float)(window_w);
|
||||
scale_y = (float)(render_h) / (float)(window_h);
|
||||
SDL_RenderSetScale(renderer, scale_x, scale_y);
|
||||
font_scale = scale_y;
|
||||
}
|
||||
|
||||
/* GUI */
|
||||
ctx = nk_sdl_init(win, renderer);
|
||||
/* Load Fonts: if none of these are loaded a default font will be used */
|
||||
/* Load Cursor: if you uncomment cursor loading please hide the cursor */
|
||||
{struct nk_font_atlas *atlas;
|
||||
{
|
||||
struct nk_font_atlas *atlas;
|
||||
struct nk_font_config config = nk_font_config(0);
|
||||
struct nk_font *font;
|
||||
|
||||
/* set up the font atlas and add desired font; note that font sizes are
|
||||
* multiplied by font_scale to produce better results at higher DPIs */
|
||||
nk_sdl_font_stash_begin(&atlas);
|
||||
/*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
|
||||
/*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/
|
||||
/*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
|
||||
/*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
|
||||
/*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
|
||||
/*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
|
||||
font = nk_font_atlas_add_default(atlas, 13 * font_scale, &config);
|
||||
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14 * font_scale, &config);*/
|
||||
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16 * font_scale, &config);*/
|
||||
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13 * font_scale, &config);*/
|
||||
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12 * font_scale, &config);*/
|
||||
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10 * font_scale, &config);*/
|
||||
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13 * font_scale, &config);*/
|
||||
nk_sdl_font_stash_end();
|
||||
|
||||
/* this hack makes the font appear to be scaled down to the desired
|
||||
* size and is only necessary when font_scale > 1 */
|
||||
font->handle.height /= font_scale;
|
||||
/*nk_style_load_all_cursors(ctx, atlas->cursors);*/
|
||||
/*nk_style_set_font(ctx, &roboto->handle)*/;}
|
||||
nk_style_set_font(ctx, &font->handle);
|
||||
}
|
||||
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
|
||||
|
@ -212,4 +243,3 @@ cleanup:
|
|||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ NK_API void nk_sdl_shutdown(void);
|
|||
|
||||
struct nk_sdl_device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
SDL_Texture *font_tex;
|
||||
};
|
||||
|
||||
|
@ -113,7 +113,7 @@ nk_sdl_render(enum nk_anti_aliasing AA)
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_sdl_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_sdl_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -259,7 +259,7 @@ nk_sdl_font_stash_end(void)
|
|||
const void *image; int w, h;
|
||||
image = nk_font_atlas_bake(&sdl.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
nk_sdl_device_upload_atlas(image, w, h);
|
||||
nk_font_atlas_end(&sdl.atlas, nk_handle_ptr(sdl.ogl.font_tex), &sdl.ogl.null);
|
||||
nk_font_atlas_end(&sdl.atlas, nk_handle_ptr(sdl.ogl.font_tex), &sdl.ogl.tex_null);
|
||||
if (sdl.atlas.default_font)
|
||||
nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle);
|
||||
}
|
||||
|
|
|
@ -98,10 +98,16 @@ int main(void)
|
|||
|
||||
/* style.c */
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
struct nk_colorf bg;
|
||||
|
|
|
@ -34,7 +34,7 @@ NK_API void nk_sfml_shutdown(void);
|
|||
|
||||
struct nk_sfml_device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint font_tex;
|
||||
};
|
||||
|
||||
|
@ -118,7 +118,7 @@ nk_sfml_render(enum nk_anti_aliasing AA)
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_sfml_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_sfml_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -245,7 +245,7 @@ nk_sfml_font_stash_end()
|
|||
const void* img;
|
||||
img = nk_font_atlas_bake(&sfml.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
nk_sfml_device_upload_atlas(img, w, h);
|
||||
nk_font_atlas_end(&sfml.atlas, nk_handle_id((int)sfml.ogl.font_tex), &sfml.ogl.null);
|
||||
nk_font_atlas_end(&sfml.atlas, nk_handle_id((int)sfml.ogl.font_tex), &sfml.ogl.tex_null);
|
||||
if(sfml.atlas.default_font)
|
||||
nk_style_set_font(&sfml.ctx, &sfml.atlas.default_font->handle);
|
||||
}
|
||||
|
|
|
@ -104,10 +104,16 @@ int main(void)
|
|||
|
||||
/* style.c */
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
struct nk_colorf bg;
|
||||
|
|
|
@ -43,7 +43,7 @@ NK_API void nk_sfml_device_destroy(void);
|
|||
|
||||
struct nk_sfml_device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint vbo, vao, ebo;
|
||||
GLuint prog;
|
||||
GLuint vert_shdr;
|
||||
|
@ -248,7 +248,7 @@ nk_sfml_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_sfml_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_sfml_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -353,7 +353,7 @@ nk_sfml_font_stash_end()
|
|||
int w, h;
|
||||
image = nk_font_atlas_bake(&sfml.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
nk_sfml_device_upload_atlas(image, w, h);
|
||||
nk_font_atlas_end(&sfml.atlas, nk_handle_id((int)sfml.ogl.font_tex), &sfml.ogl.null);
|
||||
nk_font_atlas_end(&sfml.atlas, nk_handle_id((int)sfml.ogl.font_tex), &sfml.ogl.tex_null);
|
||||
if(sfml.atlas.default_font)
|
||||
nk_style_set_font(&sfml.ctx, &sfml.atlas.default_font->handle);
|
||||
}
|
||||
|
|
|
@ -153,10 +153,16 @@ main(void)
|
|||
ctx = nk_xlib_init(xw.font, xw.dpy, xw.screen, xw.win, xw.width, xw.height);
|
||||
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
while (running)
|
||||
|
|
|
@ -357,6 +357,29 @@ nk_xsurf_stroke_circle(XSurface *surf, short x, short y, unsigned short w,
|
|||
XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
|
||||
}
|
||||
|
||||
NK_INTERN void
|
||||
nk_xsurf_stroke_arc(XSurface *surf, short cx, short cy, unsigned short radius,
|
||||
float a_min, float a_max, unsigned short line_thickness, struct nk_color col)
|
||||
{
|
||||
unsigned long c = nk_color_from_byte(&col.r);
|
||||
XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
|
||||
XSetForeground(surf->dpy, surf->gc, c);
|
||||
XDrawArc(surf->dpy, surf->drawable, surf->gc, (int)(cx - radius), (int)(cy - radius),
|
||||
(unsigned)(radius * 2), (unsigned)(radius * 2),
|
||||
(int)(a_min * 180 * 64 / NK_PI), (int)(a_max * 180 * 64 / NK_PI));
|
||||
}
|
||||
|
||||
NK_INTERN void
|
||||
nk_xsurf_fill_arc(XSurface *surf, short cx, short cy, unsigned short radius,
|
||||
float a_min, float a_max, struct nk_color col)
|
||||
{
|
||||
unsigned long c = nk_color_from_byte(&col.r);
|
||||
XSetForeground(surf->dpy, surf->gc, c);
|
||||
XFillArc(surf->dpy, surf->drawable, surf->gc, (int)(cx - radius), (int)(cy - radius),
|
||||
(unsigned)(radius * 2), (unsigned)(radius * 2),
|
||||
(int)(a_min * 180 * 64 / NK_PI), (int)(a_max * 180 * 64 / NK_PI));
|
||||
}
|
||||
|
||||
NK_INTERN void
|
||||
nk_xsurf_stroke_curve(XSurface *surf, struct nk_vec2i p1,
|
||||
struct nk_vec2i p2, struct nk_vec2i p3, struct nk_vec2i p4,
|
||||
|
@ -385,15 +408,12 @@ nk_xsurf_stroke_curve(XSurface *surf, struct nk_vec2i p1,
|
|||
}
|
||||
|
||||
NK_INTERN void
|
||||
nk_xsurf_draw_text(XSurface *surf, short x, short y, unsigned short w, unsigned short h,
|
||||
const char *text, int len, XFont *font, struct nk_color cbg, struct nk_color cfg)
|
||||
nk_xsurf_draw_text(XSurface *surf, short x, short y, const char *text, int len,
|
||||
XFont *font, struct nk_color cfg)
|
||||
{
|
||||
int tx, ty;
|
||||
unsigned long bg = nk_color_from_byte(&cbg.r);
|
||||
unsigned long fg = nk_color_from_byte(&cfg.r);
|
||||
|
||||
XSetForeground(surf->dpy, surf->gc, bg);
|
||||
XFillRectangle(surf->dpy, surf->drawable, surf->gc, (int)x, (int)y, (unsigned)w, (unsigned)h);
|
||||
if(!text || !font || !len) return;
|
||||
|
||||
tx = (int)x;
|
||||
|
@ -915,6 +935,14 @@ nk_xlib_render(Drawable screen, struct nk_color clear)
|
|||
const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
|
||||
nk_xsurf_fill_circle(surf, c->x, c->y, c->w, c->h, c->color);
|
||||
} break;
|
||||
case NK_COMMAND_ARC: {
|
||||
const struct nk_command_arc *a = (const struct nk_command_arc *)cmd;
|
||||
nk_xsurf_stroke_arc(surf, a->cx, a->cy, a->r, a->a[0], a->a[1], a->line_thickness, a->color);
|
||||
} break;
|
||||
case NK_COMMAND_ARC_FILLED: {
|
||||
const struct nk_command_arc_filled *a = (const struct nk_command_arc_filled *)cmd;
|
||||
nk_xsurf_fill_arc(surf, a->cx, a->cy, a->r, a->a[0], a->a[1], a->color);
|
||||
} break;
|
||||
case NK_COMMAND_TRIANGLE: {
|
||||
const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
|
||||
nk_xsurf_stroke_triangle(surf, t->a.x, t->a.y, t->b.x, t->b.y,
|
||||
|
@ -939,10 +967,8 @@ nk_xlib_render(Drawable screen, struct nk_color clear)
|
|||
} break;
|
||||
case NK_COMMAND_TEXT: {
|
||||
const struct nk_command_text *t = (const struct nk_command_text*)cmd;
|
||||
nk_xsurf_draw_text(surf, t->x, t->y, t->w, t->h,
|
||||
(const char*)t->string, t->length,
|
||||
(XFont*)t->font->userdata.ptr,
|
||||
t->background, t->foreground);
|
||||
nk_xsurf_draw_text(surf, t->x, t->y, (const char*)t->string, t->length,
|
||||
(XFont*)t->font->userdata.ptr, t->foreground);
|
||||
} break;
|
||||
case NK_COMMAND_CURVE: {
|
||||
const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
|
||||
|
@ -954,8 +980,6 @@ nk_xlib_render(Drawable screen, struct nk_color clear)
|
|||
nk_xsurf_draw_image(surf, i->x, i->y, i->w, i->h, i->img, i->col);
|
||||
} break;
|
||||
case NK_COMMAND_RECT_MULTI_COLOR:
|
||||
case NK_COMMAND_ARC:
|
||||
case NK_COMMAND_ARC_FILLED:
|
||||
case NK_COMMAND_CUSTOM:
|
||||
default: break;
|
||||
}
|
||||
|
|
|
@ -172,6 +172,7 @@ int main(void)
|
|||
glXGetFBConfigAttrib(win.dpy, fbc[i], GLX_SAMPLES, &samples);
|
||||
if ((fb_best < 0) || (sample_buffer && samples > best_num_samples))
|
||||
fb_best = i, best_num_samples = samples;
|
||||
XFree(vi);
|
||||
}
|
||||
}
|
||||
win.fbc = fbc[fb_best];
|
||||
|
@ -256,10 +257,16 @@ int main(void)
|
|||
/*nk_style_set_font(ctx, &droid->handle);*/}
|
||||
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
|
||||
|
|
|
@ -60,7 +60,7 @@ struct nk_x11_vertex {
|
|||
|
||||
struct nk_x11_device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint font_tex;
|
||||
};
|
||||
|
||||
|
@ -153,7 +153,7 @@ nk_x11_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_x11_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_x11_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -226,7 +226,7 @@ nk_x11_font_stash_end(void)
|
|||
const void *image; int w, h;
|
||||
image = nk_font_atlas_bake(&x11.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
nk_x11_device_upload_atlas(image, w, h);
|
||||
nk_font_atlas_end(&x11.atlas, nk_handle_id((int)x11.ogl.font_tex), &x11.ogl.null);
|
||||
nk_font_atlas_end(&x11.atlas, nk_handle_id((int)x11.ogl.font_tex), &x11.ogl.tex_null);
|
||||
if (x11.atlas.default_font)
|
||||
nk_style_set_font(&x11.ctx, &x11.atlas.default_font->handle);
|
||||
}
|
||||
|
|
|
@ -170,6 +170,7 @@ int main(void)
|
|||
glXGetFBConfigAttrib(win.dpy, fbc[i], GLX_SAMPLES, &samples);
|
||||
if ((fb_best < 0) || (sample_buffer && samples > best_num_samples))
|
||||
fb_best = i, best_num_samples = samples;
|
||||
XFree(vi);
|
||||
}
|
||||
}
|
||||
win.fbc = fbc[fb_best];
|
||||
|
@ -253,10 +254,16 @@ int main(void)
|
|||
/*nk_style_set_font(ctx, &droid->handle);*/}
|
||||
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
|
||||
|
|
|
@ -173,7 +173,7 @@ struct nk_x11_device {
|
|||
struct opengl_info info;
|
||||
#endif
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint vbo, vao, ebo;
|
||||
GLuint prog;
|
||||
GLuint vert_shdr;
|
||||
|
@ -536,7 +536,7 @@ nk_x11_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_x11_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_x11_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -592,7 +592,7 @@ nk_x11_font_stash_end(void)
|
|||
const void *image; int w, h;
|
||||
image = nk_font_atlas_bake(&x11.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
nk_x11_device_upload_atlas(image, w, h);
|
||||
nk_font_atlas_end(&x11.atlas, nk_handle_id((int)x11.ogl.font_tex), &x11.ogl.null);
|
||||
nk_font_atlas_end(&x11.atlas, nk_handle_id((int)x11.ogl.font_tex), &x11.ogl.tex_null);
|
||||
if (x11.atlas.default_font)
|
||||
nk_style_set_font(&x11.ctx, &x11.atlas.default_font->handle);
|
||||
}
|
||||
|
|
|
@ -194,10 +194,16 @@ main(void)
|
|||
if (!rawfb) running = 0;
|
||||
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(&rawfb->ctx, THEME_WHITE);*/
|
||||
/*set_style(&rawfb->ctx, THEME_RED);*/
|
||||
/*set_style(&rawfb->ctx, THEME_BLUE);*/
|
||||
/*set_style(&rawfb->ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(&rawfb->ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(&rawfb->ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(&rawfb->ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(&rawfb->ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
while (running) {
|
||||
|
|
|
@ -157,10 +157,16 @@ main(void)
|
|||
xw.width, xw.height);
|
||||
|
||||
#ifdef INCLUDE_STYLE
|
||||
/*set_style(ctx, THEME_WHITE);*/
|
||||
/*set_style(ctx, THEME_RED);*/
|
||||
/*set_style(ctx, THEME_BLUE);*/
|
||||
/*set_style(ctx, THEME_DARK);*/
|
||||
/* ease regression testing during Nuklear release process; not needed for anything else */
|
||||
#ifdef STYLE_WHITE
|
||||
set_style(ctx, THEME_WHITE);
|
||||
#elif defined(STYLE_RED)
|
||||
set_style(ctx, THEME_RED);
|
||||
#elif defined(STYLE_BLUE)
|
||||
set_style(ctx, THEME_BLUE);
|
||||
#elif defined(STYLE_DARK)
|
||||
set_style(ctx, THEME_DARK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
while (running)
|
||||
|
|
|
@ -389,6 +389,29 @@ nk_xsurf_stroke_circle(XSurface *surf, short x, short y, unsigned short w,
|
|||
XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
|
||||
}
|
||||
|
||||
NK_INTERN void
|
||||
nk_xsurf_stroke_arc(XSurface *surf, short cx, short cy, unsigned short radius,
|
||||
float a_min, float a_max, unsigned short line_thickness, struct nk_color col)
|
||||
{
|
||||
unsigned long c = nk_color_from_byte(&col.r);
|
||||
XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
|
||||
XSetForeground(surf->dpy, surf->gc, c);
|
||||
XDrawArc(surf->dpy, surf->drawable, surf->gc, (int)(cx - radius), (int)(cy - radius),
|
||||
(unsigned)(radius * 2), (unsigned)(radius * 2),
|
||||
(int)(a_min * 180 * 64 / NK_PI), (int)(a_max * 180 * 64 / NK_PI));
|
||||
}
|
||||
|
||||
NK_INTERN void
|
||||
nk_xsurf_fill_arc(XSurface *surf, short cx, short cy, unsigned short radius,
|
||||
float a_min, float a_max, struct nk_color col)
|
||||
{
|
||||
unsigned long c = nk_color_from_byte(&col.r);
|
||||
XSetForeground(surf->dpy, surf->gc, c);
|
||||
XFillArc(surf->dpy, surf->drawable, surf->gc, (int)(cx - radius), (int)(cy - radius),
|
||||
(unsigned)(radius * 2), (unsigned)(radius * 2),
|
||||
(int)(a_min * 180 * 64 / NK_PI), (int)(a_max * 180 * 64 / NK_PI));
|
||||
}
|
||||
|
||||
NK_INTERN void
|
||||
nk_xsurf_stroke_curve(XSurface *surf, struct nk_vec2i p1,
|
||||
struct nk_vec2i p2, struct nk_vec2i p3, struct nk_vec2i p4,
|
||||
|
@ -417,8 +440,8 @@ nk_xsurf_stroke_curve(XSurface *surf, struct nk_vec2i p1,
|
|||
}
|
||||
|
||||
NK_INTERN void
|
||||
nk_xsurf_draw_text(XSurface *surf, short x, short y, unsigned short w, unsigned short h,
|
||||
const char *text, int len, XFont *font, struct nk_color cbg, struct nk_color cfg)
|
||||
nk_xsurf_draw_text(XSurface *surf, short x, short y, const char *text, int len,
|
||||
XFont *font, struct nk_color cfg)
|
||||
{
|
||||
#ifdef NK_XLIB_USE_XFT
|
||||
XRenderColor xrc;
|
||||
|
@ -426,11 +449,8 @@ nk_xsurf_draw_text(XSurface *surf, short x, short y, unsigned short w, unsigned
|
|||
#else
|
||||
unsigned long fg = nk_color_from_byte(&cfg.r);
|
||||
#endif
|
||||
unsigned long bg = nk_color_from_byte(&cbg.r);
|
||||
int tx, ty;
|
||||
|
||||
XSetForeground(surf->dpy, surf->gc, bg);
|
||||
XFillRectangle(surf->dpy, surf->drawable, surf->gc, (int)x, (int)y, (unsigned)w, (unsigned)h);
|
||||
if(!text || !font || !len) return;
|
||||
|
||||
tx = (int)x;
|
||||
|
@ -1000,6 +1020,14 @@ nk_xlib_render(Drawable screen, struct nk_color clear)
|
|||
const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
|
||||
nk_xsurf_fill_circle(surf, c->x, c->y, c->w, c->h, c->color);
|
||||
} break;
|
||||
case NK_COMMAND_ARC: {
|
||||
const struct nk_command_arc *a = (const struct nk_command_arc *)cmd;
|
||||
nk_xsurf_stroke_arc(surf, a->cx, a->cy, a->r, a->a[0], a->a[1], a->line_thickness, a->color);
|
||||
} break;
|
||||
case NK_COMMAND_ARC_FILLED: {
|
||||
const struct nk_command_arc_filled *a = (const struct nk_command_arc_filled *)cmd;
|
||||
nk_xsurf_fill_arc(surf, a->cx, a->cy, a->r, a->a[0], a->a[1], a->color);
|
||||
} break;
|
||||
case NK_COMMAND_TRIANGLE: {
|
||||
const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
|
||||
nk_xsurf_stroke_triangle(surf, t->a.x, t->a.y, t->b.x, t->b.y,
|
||||
|
@ -1024,10 +1052,8 @@ nk_xlib_render(Drawable screen, struct nk_color clear)
|
|||
} break;
|
||||
case NK_COMMAND_TEXT: {
|
||||
const struct nk_command_text *t = (const struct nk_command_text*)cmd;
|
||||
nk_xsurf_draw_text(surf, t->x, t->y, t->w, t->h,
|
||||
(const char*)t->string, t->length,
|
||||
(XFont*)t->font->userdata.ptr,
|
||||
t->background, t->foreground);
|
||||
nk_xsurf_draw_text(surf, t->x, t->y, (const char*)t->string, t->length,
|
||||
(XFont*)t->font->userdata.ptr, t->foreground);
|
||||
} break;
|
||||
case NK_COMMAND_CURVE: {
|
||||
const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
|
||||
|
@ -1039,8 +1065,6 @@ nk_xlib_render(Drawable screen, struct nk_color clear)
|
|||
nk_xsurf_draw_image(surf, i->x, i->y, i->w, i->h, i->img, i->col);
|
||||
} break;
|
||||
case NK_COMMAND_RECT_MULTI_COLOR:
|
||||
case NK_COMMAND_ARC:
|
||||
case NK_COMMAND_ARC_FILLED:
|
||||
case NK_COMMAND_CUSTOM:
|
||||
default: break;
|
||||
}
|
||||
|
|
|
@ -19,6 +19,8 @@ else
|
|||
endif
|
||||
endif
|
||||
|
||||
$(BIN):
|
||||
rm -f $(BIN) $(OBJS)
|
||||
$(BIN): clean
|
||||
$(CC) $(SRC) $(CFLAGS) -o $(BIN)
|
||||
|
||||
clean:
|
||||
rm -f $(BIN) $(OBJS)
|
||||
|
|
416
doc/index.html
416
doc/index.html
|
@ -1,6 +1,5 @@
|
|||
<meta charset='utf-8' emacsmode='-*- markdown -*-'>
|
||||
<link rel='stylesheet' href='https://casual-effects.com/markdeep/latest/apidoc.css?'>
|
||||
<title>Nuklear</title> <!--Page Title-->
|
||||
# Nuklear
|
||||
![](https://cloud.githubusercontent.com/assets/8057201/11761525/ae06f0ca-a0c6-11e5-819d-5610b25f6ef4.gif)
|
||||
## Contents
|
||||
|
@ -47,6 +46,7 @@ render backends it only focuses on the actual UI.
|
|||
- No global or hidden state
|
||||
- Customizable library modules (you can compile and use only what you need)
|
||||
- Optional font baker and vertex buffer output
|
||||
- [Code available on github](https://github.com/Immediate-Mode-UI/Nuklear/)
|
||||
## Features
|
||||
- Absolutely no platform dependent code
|
||||
- Memory management control ranging from/to
|
||||
|
@ -90,7 +90,8 @@ NK_PRIVATE | If defined declares all functions as static, s
|
|||
NK_INCLUDE_FIXED_TYPES | If defined it will include header `<stdint.h>` for fixed sized types otherwise nuklear tries to select the correct type. If that fails it will throw a compiler error and you have to select the correct types yourself.
|
||||
NK_INCLUDE_DEFAULT_ALLOCATOR | If defined it will include header `<stdlib.h>` and provide additional functions to use this library without caring for memory allocation control and therefore ease memory management.
|
||||
NK_INCLUDE_STANDARD_IO | If defined it will include header `<stdio.h>` and provide additional functions depending on file loading.
|
||||
NK_INCLUDE_STANDARD_VARARGS | If defined it will include header <stdio.h> and provide additional functions depending on file loading.
|
||||
NK_INCLUDE_STANDARD_VARARGS | If defined it will include header <stdarg.h> and provide additional functions depending on file loading.
|
||||
NK_INCLUDE_STANDARD_BOOL | If defined it will include header `<stdbool.h>` for nk_bool otherwise nuklear defines nk_bool as int.
|
||||
NK_INCLUDE_VERTEX_BUFFER_OUTPUT | Defining this adds a vertex draw command list backend to this library, which allows you to convert queue commands into vertex draw commands. This is mainly if you need a hardware accessible format for OpenGL, DirectX, Vulkan, Metal,...
|
||||
NK_INCLUDE_FONT_BAKING | Defining this adds `stb_truetype` and `stb_rect_pack` implementation to this library and provides font baking and rendering. If you already have font handling or do not want to use this font handler you don't have to define it.
|
||||
NK_INCLUDE_DEFAULT_FONT | Defining this adds the default font: ProggyClean.ttf into this library which can be loaded into a font atlas and allows using this library without having a truetype font
|
||||
|
@ -109,6 +110,7 @@ NK_KEYSTATE_BASED_INPUT | Define this if your backend uses key state for
|
|||
- NK_INCLUDE_FIXED_TYPES
|
||||
- NK_INCLUDE_DEFAULT_ALLOCATOR
|
||||
- NK_INCLUDE_STANDARD_VARARGS
|
||||
- NK_INCLUDE_STANDARD_BOOL
|
||||
- NK_INCLUDE_VERTEX_BUFFER_OUTPUT
|
||||
- NK_INCLUDE_FONT_BAKING
|
||||
- NK_INCLUDE_DEFAULT_FONT
|
||||
|
@ -132,7 +134,7 @@ Function | Description
|
|||
NK_ASSERT | If you don't define this, nuklear will use <assert.h> with assert().
|
||||
NK_MEMSET | You can define this to 'memset' or your own memset implementation replacement. If not nuklear will use its own version.
|
||||
NK_MEMCPY | You can define this to 'memcpy' or your own memcpy implementation replacement. If not nuklear will use its own version.
|
||||
NK_SQRT | You can define this to 'sqrt' or your own sqrt implementation replacement. If not nuklear will use its own slow and not highly accurate version.
|
||||
NK_INV_SQRT | You can define this to your own inverse sqrt implementation replacement. If not nuklear will use its own slow and not highly accurate version.
|
||||
NK_SIN | You can define this to 'sinf' or your own sine implementation replacement. If not nuklear will use its own approximation implementation.
|
||||
NK_COS | You can define this to 'cosf' or your own cosine implementation replacement. If not nuklear will use its own approximation implementation.
|
||||
NK_STRTOD | You can define this to `strtod` or your own string to double conversion implementation replacement. If not defined nuklear will use its own imprecise and possibly unsafe version (does not handle nan or infinity!).
|
||||
|
@ -221,7 +223,7 @@ __nk_set_user_data__| Utility function to pass user data to draw command
|
|||
Initializes a `nk_context` struct with a default standard library allocator.
|
||||
Should be used if you don't want to be bothered with memory management in nuklear.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_init_default(struct nk_context *ctx, const struct nk_user_font *font);
|
||||
nk_bool nk_init_default(struct nk_context *ctx, const struct nk_user_font *font);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|---------------------------------------------------------------
|
||||
|
@ -235,7 +237,7 @@ Especially recommended for system with little memory or systems with virtual mem
|
|||
For the later case you can just allocate for example 16MB of virtual memory
|
||||
and only the required amount of memory will actually be committed.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size, const struct nk_user_font *font);
|
||||
nk_bool nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size, const struct nk_user_font *font);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! Warning
|
||||
make sure the passed memory block is aligned correctly for `nk_draw_commands`.
|
||||
|
@ -251,7 +253,7 @@ Initializes a `nk_context` struct with memory allocation callbacks for nuklear t
|
|||
memory from. Used internally for `nk_init_default` and provides a kitchen sink allocation
|
||||
interface to nuklear. Can be useful for cases like monitoring memory consumption.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_init(struct nk_context *ctx, struct nk_allocator *alloc, const struct nk_user_font *font);
|
||||
nk_bool nk_init(struct nk_context *ctx, struct nk_allocator *alloc, const struct nk_user_font *font);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|---------------------------------------------------------------
|
||||
|
@ -264,7 +266,7 @@ Initializes a `nk_context` struct from two different either fixed or growing
|
|||
buffers. The first buffer is for allocating draw commands while the second buffer is
|
||||
used for allocating windows, panels and state tables.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font *font);
|
||||
nk_bool nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font *font);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|---------------------------------------------------------------
|
||||
|
@ -379,7 +381,7 @@ __y__ | Must hold an integer describing the current mouse cursor y-positio
|
|||
#### nk_input_key
|
||||
Mirrors the state of a specific key to nuklear
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
void nk_input_key(struct nk_context*, enum nk_keys key, int down);
|
||||
void nk_input_key(struct nk_context*, enum nk_keys key, nk_bool down);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -389,7 +391,7 @@ __down__ | Must be 0 for key is up and 1 for key is down
|
|||
#### nk_input_button
|
||||
Mirrors the state of a specific mouse button to nuklear
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
void nk_input_button(struct nk_context *ctx, enum nk_buttons btn, int x, int y, int down);
|
||||
void nk_input_button(struct nk_context *ctx, enum nk_buttons btn, int x, int y, nk_bool down);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -637,7 +639,7 @@ cfg.circle_segment_count = 22;
|
|||
cfg.curve_segment_count = 22;
|
||||
cfg.arc_segment_count = 22;
|
||||
cfg.global_alpha = 1.0f;
|
||||
cfg.null = dev->null;
|
||||
cfg.tex_null = dev->tex_null;
|
||||
//
|
||||
// setup buffers and convert
|
||||
struct nk_buffer cmds, verts, idx;
|
||||
|
@ -891,7 +893,7 @@ __NK_MAXIMIZED__| UI section is extended and visible until minimized
|
|||
Starts a new window; needs to be called every frame for every
|
||||
window (unless hidden) or otherwise the window gets removed
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
|
||||
nk_bool nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -905,7 +907,7 @@ until `nk_end` or `false(0)` otherwise for example if minimized
|
|||
Extended window start with separated title and identifier to allow multiple
|
||||
windows with same title but not name
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
|
||||
nk_bool nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -1086,7 +1088,7 @@ Returns if the currently processed window is currently active
|
|||
!!! WARNING
|
||||
Only call this function between calls `nk_begin_xxx` and `nk_end`
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_window_has_focus(const struct nk_context *ctx);
|
||||
nk_bool nk_window_has_focus(const struct nk_context *ctx);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -1097,7 +1099,7 @@ Return if the current window is being hovered
|
|||
!!! WARNING
|
||||
Only call this function between calls `nk_begin_xxx` and `nk_end`
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_window_is_hovered(struct nk_context *ctx);
|
||||
nk_bool nk_window_is_hovered(struct nk_context *ctx);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -1106,7 +1108,7 @@ Returns `true(1)` if current window is hovered or `false(0)` otherwise
|
|||
#### nk_window_is_collapsed
|
||||
Returns if the window with given name is currently minimized/collapsed
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_window_is_collapsed(struct nk_context *ctx, const char *name);
|
||||
nk_bool nk_window_is_collapsed(struct nk_context *ctx, const char *name);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -1117,7 +1119,7 @@ found or is not minimized
|
|||
#### nk_window_is_closed
|
||||
Returns if the window with given name was closed by calling `nk_close`
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_window_is_closed(struct nk_context *ctx, const char *name);
|
||||
nk_bool nk_window_is_closed(struct nk_context *ctx, const char *name);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -1127,7 +1129,7 @@ Returns `true(1)` if current window was closed or `false(0)` window not found or
|
|||
#### nk_window_is_hidden
|
||||
Returns if the window with given name is hidden
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_window_is_hidden(struct nk_context *ctx, const char *name);
|
||||
nk_bool nk_window_is_hidden(struct nk_context *ctx, const char *name);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -1137,7 +1139,7 @@ Returns `true(1)` if current window is hidden or `false(0)` window not found or
|
|||
#### nk_window_is_active
|
||||
Same as nk_window_has_focus for some reason
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_window_is_active(struct nk_context *ctx, const char *name);
|
||||
nk_bool nk_window_is_active(struct nk_context *ctx, const char *name);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -1147,7 +1149,7 @@ Returns `true(1)` if current window is active or `false(0)` window not found or
|
|||
#### nk_window_is_any_hovered
|
||||
Returns if the any window is being hovered
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_window_is_any_hovered(struct nk_context*);
|
||||
nk_bool nk_window_is_any_hovered(struct nk_context*);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -1158,7 +1160,7 @@ Returns if the any window is being hovered or any widget is currently active.
|
|||
Can be used to decide if input should be processed by UI or your specific input handling.
|
||||
Example could be UI and 3D camera to move inside a 3D space.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_item_is_any_active(struct nk_context*);
|
||||
nk_bool nk_item_is_any_active(struct nk_context*);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -1731,6 +1733,14 @@ Parameter | Description
|
|||
__ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
|
||||
__bounds__ | Rectangle to convert from layout space into screen space
|
||||
Returns transformed `nk_rect` in layout space coordinates
|
||||
#### nk_spacer
|
||||
Spacer is a dummy widget that consumes space as usual but doesn't draw anything
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
void nk_spacer(struct nk_context* );
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
__ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
|
||||
### Groups
|
||||
Groups are basically windows inside windows. They allow to subdivide space
|
||||
in a window to layout widgets as a group. Almost all more complex widget
|
||||
|
@ -1812,7 +1822,7 @@ nk_group_set_scroll | Sets the scroll offset for the given group
|
|||
#### nk_group_begin
|
||||
Starts a new widget group. Requires a previous layouting function to specify a pos/size.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_group_begin(struct nk_context*, const char *title, nk_flags);
|
||||
nk_bool nk_group_begin(struct nk_context*, const char *title, nk_flags);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -1823,7 +1833,7 @@ Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
|
|||
#### nk_group_begin_titled
|
||||
Starts a new widget group. Requires a previous layouting function to specify a pos/size.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_group_begin_titled(struct nk_context*, const char *name, const char *title, nk_flags);
|
||||
nk_bool nk_group_begin_titled(struct nk_context*, const char *name, const char *title, nk_flags);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -1844,7 +1854,7 @@ __ctx__ | Must point to an previously initialized `nk_context` struct
|
|||
starts a new widget group. requires a previous layouting function to specify
|
||||
a size. Does not keep track of scrollbar.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_group_scrolled_offset_begin(struct nk_context*, nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags);
|
||||
nk_bool nk_group_scrolled_offset_begin(struct nk_context*, nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -1858,7 +1868,7 @@ Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
|
|||
Starts a new widget group. requires a previous
|
||||
layouting function to specify a size. Does not keep track of scrollbar.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_group_scrolled_begin(struct nk_context*, struct nk_scroll *off, const char *title, nk_flags);
|
||||
nk_bool nk_group_scrolled_begin(struct nk_context*, struct nk_scroll *off, const char *title, nk_flags);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -1986,7 +1996,7 @@ Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
|
|||
Start a collapsible UI section with internal state management with full
|
||||
control over internal unique ID used to store state
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_tree_push_hashed(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states initial_state, const char *hash, int len,int seed);
|
||||
nk_bool nk_tree_push_hashed(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states initial_state, const char *hash, int len,int seed);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -2035,7 +2045,7 @@ Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
|
|||
Start a collapsible UI section with internal state management with full
|
||||
control over internal unique ID used to store state
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_tree_image_push_hashed(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states initial_state, const char *hash, int len,int seed);
|
||||
nk_bool nk_tree_image_push_hashed(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states initial_state, const char *hash, int len,int seed);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -2059,7 +2069,7 @@ __ctx__ | Must point to an previously initialized `nk_context` struct after
|
|||
#### nk_tree_state_push
|
||||
Start a collapsible UI section with external state management
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
|
||||
nk_bool nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -2071,7 +2081,7 @@ Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
|
|||
#### nk_tree_state_image_push
|
||||
Start a collapsible UI section with image and label header and external state management
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
int nk_tree_state_image_push(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states *state);
|
||||
nk_bool nk_tree_state_image_push(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states *state);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Parameter | Description
|
||||
------------|-----------------------------------------------------------
|
||||
|
@ -2260,6 +2270,302 @@ __max__ | Maximum value not allowed to be overflown
|
|||
__step__ | Increment added and subtracted on increment and decrement button
|
||||
__inc_per_pixel__ | Value per pixel added or subtracted on dragging
|
||||
Returns the new modified double value
|
||||
### Font
|
||||
Font handling in this library was designed to be quite customizable and lets
|
||||
you decide what you want to use and what you want to provide. There are three
|
||||
different ways to use the font atlas. The first two will use your font
|
||||
handling scheme and only requires essential data to run nuklear. The next
|
||||
slightly more advanced features is font handling with vertex buffer output.
|
||||
Finally the most complex API wise is using nuklear's font baking API.
|
||||
#### Using your own implementation without vertex buffer output
|
||||
|
||||
So first up the easiest way to do font handling is by just providing a
|
||||
`nk_user_font` struct which only requires the height in pixel of the used
|
||||
font and a callback to calculate the width of a string. This way of handling
|
||||
fonts is best fitted for using the normal draw shape command API where you
|
||||
do all the text drawing yourself and the library does not require any kind
|
||||
of deeper knowledge about which font handling mechanism you use.
|
||||
IMPORTANT: the `nk_user_font` pointer provided to nuklear has to persist
|
||||
over the complete life time! I know this sucks but it is currently the only
|
||||
way to switch between fonts.
|
||||
```c
|
||||
float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
|
||||
{
|
||||
your_font_type *type = handle.ptr;
|
||||
float text_width = ...;
|
||||
return text_width;
|
||||
}
|
||||
struct nk_user_font font;
|
||||
font.userdata.ptr = &your_font_class_or_struct;
|
||||
font.height = your_font_height;
|
||||
font.width = your_text_width_calculation;
|
||||
|
||||
struct nk_context ctx;
|
||||
nk_init_default(&ctx, &font);
|
||||
```
|
||||
#### Using your own implementation with vertex buffer output
|
||||
|
||||
While the first approach works fine if you don't want to use the optional
|
||||
vertex buffer output it is not enough if you do. To get font handling working
|
||||
for these cases you have to provide two additional parameters inside the
|
||||
`nk_user_font`. First a texture atlas handle used to draw text as subimages
|
||||
of a bigger font atlas texture and a callback to query a character's glyph
|
||||
information (offset, size, ...). So it is still possible to provide your own
|
||||
font and use the vertex buffer output.
|
||||
```c
|
||||
float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
|
||||
{
|
||||
your_font_type *type = handle.ptr;
|
||||
float text_width = ...;
|
||||
return text_width;
|
||||
}
|
||||
void query_your_font_glyph(nk_handle handle, float font_height, struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
|
||||
{
|
||||
your_font_type *type = handle.ptr;
|
||||
glyph.width = ...;
|
||||
glyph.height = ...;
|
||||
glyph.xadvance = ...;
|
||||
glyph.uv[0].x = ...;
|
||||
glyph.uv[0].y = ...;
|
||||
glyph.uv[1].x = ...;
|
||||
glyph.uv[1].y = ...;
|
||||
glyph.offset.x = ...;
|
||||
glyph.offset.y = ...;
|
||||
}
|
||||
|
||||
struct nk_user_font font;
|
||||
font.userdata.ptr = &your_font_class_or_struct;
|
||||
font.height = your_font_height;
|
||||
font.width = your_text_width_calculation;
|
||||
font.query = query_your_font_glyph;
|
||||
font.texture.id = your_font_texture;
|
||||
|
||||
struct nk_context ctx;
|
||||
nk_init_default(&ctx, &font);
|
||||
```
|
||||
#### Nuklear font baker
|
||||
|
||||
The final approach if you do not have a font handling functionality or don't
|
||||
want to use it in this library is by using the optional font baker.
|
||||
The font baker APIs can be used to create a font plus font atlas texture
|
||||
and can be used with or without the vertex buffer output.
|
||||
|
||||
It still uses the `nk_user_font` struct and the two different approaches
|
||||
previously stated still work. The font baker is not located inside
|
||||
`nk_context` like all other systems since it can be understood as more of
|
||||
an extension to nuklear and does not really depend on any `nk_context` state.
|
||||
|
||||
Font baker need to be initialized first by one of the nk_font_atlas_init_xxx
|
||||
functions. If you don't care about memory just call the default version
|
||||
`nk_font_atlas_init_default` which will allocate all memory from the standard library.
|
||||
If you want to control memory allocation but you don't care if the allocated
|
||||
memory is temporary and therefore can be freed directly after the baking process
|
||||
is over or permanent you can call `nk_font_atlas_init`.
|
||||
|
||||
After successfully initializing the font baker you can add Truetype(.ttf) fonts from
|
||||
different sources like memory or from file by calling one of the `nk_font_atlas_add_xxx`.
|
||||
functions. Adding font will permanently store each font, font config and ttf memory block(!)
|
||||
inside the font atlas and allows to reuse the font atlas. If you don't want to reuse
|
||||
the font baker by for example adding additional fonts you can call
|
||||
`nk_font_atlas_cleanup` after the baking process is over (after calling nk_font_atlas_end).
|
||||
|
||||
As soon as you added all fonts you wanted you can now start the baking process
|
||||
for every selected glyph to image by calling `nk_font_atlas_bake`.
|
||||
The baking process returns image memory, width and height which can be used to
|
||||
either create your own image object or upload it to any graphics library.
|
||||
No matter which case you finally have to call `nk_font_atlas_end` which
|
||||
will free all temporary memory including the font atlas image so make sure
|
||||
you created our texture beforehand. `nk_font_atlas_end` requires a handle
|
||||
to your font texture or object and optionally fills a `struct nk_draw_null_texture`
|
||||
which can be used for the optional vertex output. If you don't want it just
|
||||
set the argument to `NULL`.
|
||||
|
||||
At this point you are done and if you don't want to reuse the font atlas you
|
||||
can call `nk_font_atlas_cleanup` to free all truetype blobs and configuration
|
||||
memory. Finally if you don't use the font atlas and any of it's fonts anymore
|
||||
you need to call `nk_font_atlas_clear` to free all memory still being used.
|
||||
|
||||
```c
|
||||
struct nk_font_atlas atlas;
|
||||
nk_font_atlas_init_default(&atlas);
|
||||
nk_font_atlas_begin(&atlas);
|
||||
nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, 0);
|
||||
nk_font *font2 = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font2.ttf", 16, 0);
|
||||
const void* img = nk_font_atlas_bake(&atlas, &img_width, &img_height, NK_FONT_ATLAS_RGBA32);
|
||||
nk_font_atlas_end(&atlas, nk_handle_id(texture), 0);
|
||||
|
||||
struct nk_context ctx;
|
||||
nk_init_default(&ctx, &font->handle);
|
||||
while (1) {
|
||||
|
||||
}
|
||||
nk_font_atlas_clear(&atlas);
|
||||
```
|
||||
The font baker API is probably the most complex API inside this library and
|
||||
I would suggest reading some of my examples `example/` to get a grip on how
|
||||
to use the font atlas. There are a number of details I left out. For example
|
||||
how to merge fonts, configure a font with `nk_font_config` to use other languages,
|
||||
use another texture coordinate format and a lot more:
|
||||
```c
|
||||
struct nk_font_config cfg = nk_font_config(font_pixel_height);
|
||||
cfg.merge_mode = nk_false or nk_true;
|
||||
cfg.range = nk_font_korean_glyph_ranges();
|
||||
cfg.coord_type = NK_COORD_PIXEL;
|
||||
nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, &cfg);
|
||||
```
|
||||
### Memory Buffer
|
||||
A basic (double)-buffer with linear allocation and resetting as only
|
||||
freeing policy. The buffer's main purpose is to control all memory management
|
||||
inside the GUI toolkit and still leave memory control as much as possible in
|
||||
the hand of the user while also making sure the library is easy to use if
|
||||
not as much control is needed.
|
||||
In general all memory inside this library can be provided from the user in
|
||||
three different ways.
|
||||
|
||||
The first way and the one providing most control is by just passing a fixed
|
||||
size memory block. In this case all control lies in the hand of the user
|
||||
since he can exactly control where the memory comes from and how much memory
|
||||
the library should consume. Of course using the fixed size API removes the
|
||||
ability to automatically resize a buffer if not enough memory is provided so
|
||||
you have to take over the resizing. While being a fixed sized buffer sounds
|
||||
quite limiting, it is very effective in this library since the actual memory
|
||||
consumption is quite stable and has a fixed upper bound for a lot of cases.
|
||||
|
||||
If you don't want to think about how much memory the library should allocate
|
||||
at all time or have a very dynamic UI with unpredictable memory consumption
|
||||
habits but still want control over memory allocation you can use the dynamic
|
||||
allocator based API. The allocator consists of two callbacks for allocating
|
||||
and freeing memory and optional userdata so you can plugin your own allocator.
|
||||
|
||||
The final and easiest way can be used by defining
|
||||
NK_INCLUDE_DEFAULT_ALLOCATOR which uses the standard library memory
|
||||
allocation functions malloc and free and takes over complete control over
|
||||
memory in this library.
|
||||
### Text Editor
|
||||
Editing text in this library is handled by either `nk_edit_string` or
|
||||
`nk_edit_buffer`. But like almost everything in this library there are multiple
|
||||
ways of doing it and a balance between control and ease of use with memory
|
||||
as well as functionality controlled by flags.
|
||||
This library generally allows three different levels of memory control:
|
||||
First of is the most basic way of just providing a simple char array with
|
||||
string length. This method is probably the easiest way of handling simple
|
||||
user text input. Main upside is complete control over memory while the biggest
|
||||
downside in comparison with the other two approaches is missing undo/redo.
|
||||
For UIs that require undo/redo the second way was created. It is based on
|
||||
a fixed size nk_text_edit struct, which has an internal undo/redo stack.
|
||||
This is mainly useful if you want something more like a text editor but don't want
|
||||
to have a dynamically growing buffer.
|
||||
The final way is using a dynamically growing nk_text_edit struct, which
|
||||
has both a default version if you don't care where memory comes from and an
|
||||
allocator version if you do. While the text editor is quite powerful for its
|
||||
complexity I would not recommend editing gigabytes of data with it.
|
||||
It is rather designed for uses cases which make sense for a GUI library not for
|
||||
an full blown text editor.
|
||||
### Drawing
|
||||
This library was designed to be render backend agnostic so it does
|
||||
not draw anything to screen. Instead all drawn shapes, widgets
|
||||
are made of, are buffered into memory and make up a command queue.
|
||||
Each frame therefore fills the command buffer with draw commands
|
||||
that then need to be executed by the user and his own render backend.
|
||||
After that the command buffer needs to be cleared and a new frame can be
|
||||
started. It is probably important to note that the command buffer is the main
|
||||
drawing API and the optional vertex buffer API only takes this format and
|
||||
converts it into a hardware accessible format.
|
||||
|
||||
To use the command queue to draw your own widgets you can access the
|
||||
command buffer of each window by calling `nk_window_get_canvas` after
|
||||
previously having called `nk_begin`:
|
||||
|
||||
```c
|
||||
void draw_red_rectangle_widget(struct nk_context *ctx)
|
||||
{
|
||||
struct nk_command_buffer *canvas;
|
||||
struct nk_input *input = &ctx->input;
|
||||
canvas = nk_window_get_canvas(ctx);
|
||||
|
||||
struct nk_rect space;
|
||||
enum nk_widget_layout_states state;
|
||||
state = nk_widget(&space, ctx);
|
||||
if (!state) return;
|
||||
|
||||
if (state != NK_WIDGET_ROM)
|
||||
update_your_widget_by_user_input(...);
|
||||
nk_fill_rect(canvas, space, 0, nk_rgb(255,0,0));
|
||||
}
|
||||
|
||||
if (nk_begin(...)) {
|
||||
nk_layout_row_dynamic(ctx, 25, 1);
|
||||
draw_red_rectangle_widget(ctx);
|
||||
}
|
||||
nk_end(..)
|
||||
|
||||
```
|
||||
Important to know if you want to create your own widgets is the `nk_widget`
|
||||
call. It allocates space on the panel reserved for this widget to be used,
|
||||
but also returns the state of the widget space. If your widget is not seen and does
|
||||
not have to be updated it is '0' and you can just return. If it only has
|
||||
to be drawn the state will be `NK_WIDGET_ROM` otherwise you can do both
|
||||
update and draw your widget. The reason for separating is to only draw and
|
||||
update what is actually necessary which is crucial for performance.
|
||||
Draw List
|
||||
The optional vertex buffer draw list provides a 2D drawing context
|
||||
with antialiasing functionality which takes basic filled or outlined shapes
|
||||
or a path and outputs vertexes, elements and draw commands.
|
||||
The actual draw list API is not required to be used directly while using this
|
||||
library since converting the default library draw command output is done by
|
||||
just calling `nk_convert` but I decided to still make this library accessible
|
||||
since it can be useful.
|
||||
|
||||
The draw list is based on a path buffering and polygon and polyline
|
||||
rendering API which allows a lot of ways to draw 2D content to screen.
|
||||
In fact it is probably more powerful than needed but allows even more crazy
|
||||
things than this library provides by default.
|
||||
### Stack
|
||||
The style modifier stack can be used to temporarily change a
|
||||
property inside `nk_style`. For example if you want a special
|
||||
red button you can temporarily push the old button color onto a stack
|
||||
draw the button with a red color and then you just pop the old color
|
||||
back from the stack:
|
||||
|
||||
nk_style_push_style_item(ctx, &ctx->style.button.normal, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
nk_style_push_style_item(ctx, &ctx->style.button.hover, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
nk_style_push_style_item(ctx, &ctx->style.button.active, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
nk_style_push_vec2(ctx, &cx->style.button.padding, nk_vec2(2,2));
|
||||
|
||||
nk_button(...);
|
||||
|
||||
nk_style_pop_style_item(ctx);
|
||||
nk_style_pop_style_item(ctx);
|
||||
nk_style_pop_style_item(ctx);
|
||||
nk_style_pop_vec2(ctx);
|
||||
|
||||
Nuklear has a stack for style_items, float properties, vector properties,
|
||||
flags, colors, fonts and for button_behavior. Each has it's own fixed size stack
|
||||
which can be changed at compile time.
|
||||
### Math
|
||||
Since nuklear is supposed to work on all systems providing floating point
|
||||
math without any dependencies I also had to implement my own math functions
|
||||
for sqrt, sin and cos. Since the actual highly accurate implementations for
|
||||
the standard library functions are quite complex and I do not need high
|
||||
precision for my use cases I use approximations.
|
||||
Sqrt
|
||||
----
|
||||
For square root nuklear uses the famous fast inverse square root:
|
||||
https://en.wikipedia.org/wiki/Fast_inverse_square_root with
|
||||
slightly tweaked magic constant. While on today's hardware it is
|
||||
probably not faster it is still fast and accurate enough for
|
||||
nuklear's use cases. IMPORTANT: this requires float format IEEE 754
|
||||
Sine/Cosine
|
||||
-----------
|
||||
All constants inside both function are generated Remez's minimax
|
||||
approximations for value range 0...2*PI. The reason why I decided to
|
||||
approximate exactly that range is that nuklear only needs sine and
|
||||
cosine to generate circles which only requires that exact range.
|
||||
In addition I used Remez instead of Taylor for additional precision:
|
||||
www.lolengine.net/blog/2011/12/21/better-function-approximations.
|
||||
The tool I used to generate constants for both sine and cosine
|
||||
(it can actually approximate a lot more functions) can be
|
||||
found here: www.lolengine.net/wiki/oss/lolremez
|
||||
-XXX.XXX- X...X - X...X -X....X - X....X"
|
||||
X...XXXXXXXXXXXXX...X - "
|
||||
## License
|
||||
|
@ -2306,13 +2612,55 @@ X...XXXXXXXXXXXXX...X - "
|
|||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
## Changelog
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~none
|
||||
[date][x.yy.zz]-[description]
|
||||
-[date]: date on which the change has been pushed
|
||||
-[x.yy.zz]: Numerical version string representation. Each version number on the right
|
||||
resets back to zero if version on the left is incremented.
|
||||
[date] ([x.y.z]) - [description]
|
||||
- [date]: date on which the change has been pushed
|
||||
- [x.y.z]: Version string, represented in Semantic Versioning format
|
||||
- [x]: Major version with API and library breaking changes
|
||||
- [yy]: Minor version with non-breaking API and library changes
|
||||
- [zz]: Bug fix version with no direct changes to API
|
||||
- [y]: Minor version with non-breaking API and library changes
|
||||
- [z]: Patch version with no direct changes to the API
|
||||
- 2022/12/17 (4.10.5) - Fix nk_font_bake_pack() using TTC font offset incorrectly
|
||||
- 2022/10/24 (4.10.4) - Fix nk_str_{append,insert}_str_utf8 always returning 0
|
||||
- 2022/09/03 (4.10.3) - Renamed the `null` texture variable to `tex_null`
|
||||
- 2022/08/01 (4.10.2) - Fix Apple Silicon with incorrect NK_SITE_TYPE and NK_POINTER_TYPE
|
||||
- 2022/08/01 (4.10.1) - Fix cursor jumping back to beginning of text when typing more than
|
||||
nk_edit_xxx limit
|
||||
- 2022/05/27 (4.10.0) - Add nk_input_has_mouse_click_in_button_rect() to fix window move bug
|
||||
- 2022/04/18 (4.9.7) - Change button behavior when NK_BUTTON_TRIGGER_ON_RELEASE is defined to
|
||||
only trigger when the mouse position was inside the same button on down
|
||||
- 2022/02/03 (4.9.6) - Allow overriding the NK_INV_SQRT function, similar to NK_SIN and NK_COS
|
||||
- 2021/12/22 (4.9.5) - Revert layout bounds not accounting for padding due to regressions
|
||||
- 2021/12/22 (4.9.4) - Fix checking hovering when window is minimized
|
||||
- 2021/12/22 (4.09.3) - Fix layout bounds not accounting for padding
|
||||
- 2021/12/19 (4.09.2) - Update to stb_rect_pack.h v1.01 and stb_truetype.h v1.26
|
||||
- 2021/12/16 (4.09.1) - Fix the majority of GCC warnings
|
||||
- 2021/10/16 (4.09.0) - Added nk_spacer() widget
|
||||
- 2021/09/22 (4.08.6) - Fix "may be used uninitialized" warnings in nk_widget
|
||||
- 2021/09/22 (4.08.5) - GCC __builtin_offsetof only exists in version 4 and later
|
||||
- 2021/09/15 (4.08.4) - Fix "'num_len' may be used uninitialized" in nk_do_property
|
||||
- 2021/09/15 (4.08.3) - Fix "Templates cannot be declared to have 'C' Linkage"
|
||||
- 2021/09/08 (4.08.2) - Fix warnings in C89 builds
|
||||
- 2021/09/08 (4.08.1) - Use compiler builtins for NK_OFFSETOF when possible
|
||||
- 2021/08/17 (4.08.0) - Implemented 9-slice scaling support for widget styles
|
||||
- 2021/08/16 (4.07.5) - Replace usage of memset in nk_font_atlas_bake with NK_MEMSET
|
||||
- 2021/08/15 (4.07.4) - Fix conversion and sign conversion warnings
|
||||
- 2021/08/08 (4.07.3) - Fix crash when baking merged fonts
|
||||
- 2021/08/08 (4.07.2) - Fix Multiline Edit wrong offset
|
||||
- 2021/03/17 (4.07.1) - Fix warning about unused parameter
|
||||
- 2021/03/17 (4.07.0) - Fix nk_property hover bug
|
||||
- 2021/03/15 (4.06.4) - Change nk_propertyi back to int
|
||||
- 2021/03/15 (4.06.3) - Update documentation for functions that now return nk_bool
|
||||
- 2020/12/19 (4.06.2) - Fix additional C++ style comments which are not allowed in ISO C90.
|
||||
- 2020/10/11 (4.06.1) - Fix C++ style comments which are not allowed in ISO C90.
|
||||
- 2020/10/07 (4.06.0) - Fix nk_combo return type wrongly changed to nk_bool
|
||||
- 2020/09/05 (4.05.0) - Use the nk_font_atlas allocator for stb_truetype memory management.
|
||||
- 2020/09/04 (4.04.1) - Replace every boolean int by nk_bool
|
||||
- 2020/09/04 (4.04.0) - Add nk_bool with NK_INCLUDE_STANDARD_BOOL
|
||||
- 2020/06/13 (4.03.1) - Fix nk_pool allocation sizes.
|
||||
- 2020/06/04 (4.03.0) - Made nk_combo header symbols optional.
|
||||
- 2020/05/27 (4.02.5) - Fix nk_do_edit: Keep scroll position when re-activating edit widget.
|
||||
- 2020/05/09 (4.02.4) - Fix nk_menubar height calculation bug
|
||||
- 2020/05/08 (4.02.3) - Fix missing stdarg.h with NK_INCLUDE_STANDARD_VARARGS
|
||||
- 2020/04/30 (4.02.2) - Fix nk_edit border drawing bug
|
||||
- 2020/04/09 (4.02.1) - Removed unused nk_sqrt function to fix compiler warnings
|
||||
- Fixed compiler warnings if you bring your own methods for
|
||||
nk_cos/nk_sin/nk_strtod/nk_memset/nk_memcopy/nk_dtoa
|
||||
|
|
|
@ -52,7 +52,7 @@ struct nk_glfw_vertex {
|
|||
|
||||
struct device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint vbo, vao, ebo;
|
||||
GLuint prog;
|
||||
GLuint vert_shdr;
|
||||
|
@ -262,7 +262,7 @@ device_draw(struct device *dev, struct nk_context *ctx, int width, int height,
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_glfw_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_glfw_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -441,7 +441,7 @@ int main(int argc, char *argv[])
|
|||
font = nk_font_atlas_add_default(&atlas, 13, 0);
|
||||
image = nk_font_atlas_bake(&atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
device_upload_atlas(&device, image, w, h);
|
||||
nk_font_atlas_end(&atlas, nk_handle_id((int)device.font_tex), &device.null);
|
||||
nk_font_atlas_end(&atlas, nk_handle_id((int)device.font_tex), &device.tex_null);
|
||||
nk_init_default(&ctx, &font->handle);
|
||||
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
|
|
|
@ -478,7 +478,7 @@ struct nk_glfw_vertex {
|
|||
|
||||
struct device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint vbo, vao, ebo;
|
||||
GLuint prog;
|
||||
GLuint vert_shdr;
|
||||
|
@ -685,7 +685,7 @@ device_draw(struct device *dev, struct nk_context *ctx, int width, int height,
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_glfw_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_glfw_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -795,7 +795,7 @@ int main(int argc, char *argv[])
|
|||
media.font_22 = nk_font_atlas_add_from_file(&atlas, "../../extra_font/Roboto-Regular.ttf", 22.0f, &cfg);
|
||||
image = nk_font_atlas_bake(&atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
device_upload_atlas(&device, image, w, h);
|
||||
nk_font_atlas_end(&atlas, nk_handle_id((int)device.font_tex), &device.null);}
|
||||
nk_font_atlas_end(&atlas, nk_handle_id((int)device.font_tex), &device.tex_null);}
|
||||
nk_init_default(&ctx, &media.font_14->handle);}
|
||||
|
||||
/* icons */
|
||||
|
|
|
@ -527,7 +527,7 @@ struct nk_glfw_vertex {
|
|||
|
||||
struct device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint vbo, vao, ebo;
|
||||
GLuint prog;
|
||||
GLuint vert_shdr;
|
||||
|
@ -722,7 +722,7 @@ device_draw(struct device *dev, struct nk_context *ctx, int width, int height,
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_glfw_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_glfw_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -823,7 +823,7 @@ int main(int argc, char *argv[])
|
|||
else font = nk_font_atlas_add_default(&atlas, 13.0f, NULL);
|
||||
image = nk_font_atlas_bake(&atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
device_upload_atlas(&device, image, w, h);
|
||||
nk_font_atlas_end(&atlas, nk_handle_id((int)device.font_tex), &device.null);}
|
||||
nk_font_atlas_end(&atlas, nk_handle_id((int)device.font_tex), &device.tex_null);}
|
||||
nk_init_default(&ctx, &font->handle);}
|
||||
|
||||
/* icons */
|
||||
|
|
|
@ -79,7 +79,7 @@ struct nk_glfw_vertex {
|
|||
|
||||
struct device {
|
||||
struct nk_buffer cmds;
|
||||
struct nk_draw_null_texture null;
|
||||
struct nk_draw_null_texture tex_null;
|
||||
GLuint vbo, vao, ebo;
|
||||
GLuint prog;
|
||||
GLuint vert_shdr;
|
||||
|
@ -286,7 +286,7 @@ device_draw(struct device *dev, struct nk_context *ctx, int width, int height,
|
|||
config.vertex_layout = vertex_layout;
|
||||
config.vertex_size = sizeof(struct nk_glfw_vertex);
|
||||
config.vertex_alignment = NK_ALIGNOF(struct nk_glfw_vertex);
|
||||
config.null = dev->null;
|
||||
config.tex_null = dev->tex_null;
|
||||
config.circle_segment_count = 22;
|
||||
config.curve_segment_count = 22;
|
||||
config.arc_segment_count = 22;
|
||||
|
@ -388,7 +388,7 @@ int main(int argc, char *argv[])
|
|||
else font = nk_font_atlas_add_default(&atlas, 13.0f, NULL);
|
||||
image = nk_font_atlas_bake(&atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
||||
device_upload_atlas(&device, image, w, h);
|
||||
nk_font_atlas_end(&atlas, nk_handle_id((int)device.font_tex), &device.null);}
|
||||
nk_font_atlas_end(&atlas, nk_handle_id((int)device.font_tex), &device.tex_null);}
|
||||
nk_init_default(&ctx, &font->handle);}
|
||||
|
||||
{ /* skin */
|
||||
|
|
695
nuklear.h
695
nuklear.h
|
@ -48,6 +48,7 @@
|
|||
/// - No global or hidden state
|
||||
/// - Customizable library modules (you can compile and use only what you need)
|
||||
/// - Optional font baker and vertex buffer output
|
||||
/// - [Code available on github](https://github.com/Immediate-Mode-UI/Nuklear/)
|
||||
///
|
||||
/// ## Features
|
||||
/// - Absolutely no platform dependent code
|
||||
|
@ -372,7 +373,7 @@ extern "C" {
|
|||
#elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
|
||||
#define NK_SIZE_TYPE unsigned __int32
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
#if defined(__x86_64__) || defined(__ppc64__)
|
||||
#if defined(__x86_64__) || defined(__ppc64__) || defined(__aarch64__)
|
||||
#define NK_SIZE_TYPE unsigned long
|
||||
#else
|
||||
#define NK_SIZE_TYPE unsigned int
|
||||
|
@ -387,7 +388,7 @@ extern "C" {
|
|||
#elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
|
||||
#define NK_POINTER_TYPE unsigned __int32
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
#if defined(__x86_64__) || defined(__ppc64__)
|
||||
#if defined(__x86_64__) || defined(__ppc64__) || defined(__aarch64__)
|
||||
#define NK_POINTER_TYPE unsigned long
|
||||
#else
|
||||
#define NK_POINTER_TYPE unsigned int
|
||||
|
@ -1127,7 +1128,7 @@ NK_API void nk_input_end(struct nk_context*);
|
|||
/// cfg.curve_segment_count = 22;
|
||||
/// cfg.arc_segment_count = 22;
|
||||
/// cfg.global_alpha = 1.0f;
|
||||
/// cfg.null = dev->null;
|
||||
/// cfg.tex_null = dev->tex_null;
|
||||
/// //
|
||||
/// // setup buffers and convert
|
||||
/// struct nk_buffer cmds, verts, idx;
|
||||
|
@ -1177,7 +1178,7 @@ struct nk_convert_config {
|
|||
unsigned circle_segment_count; /* number of segments used for circles: default to 22 */
|
||||
unsigned arc_segment_count; /* number of segments used for arcs: default to 22 */
|
||||
unsigned curve_segment_count; /* number of segments used for curves: default to 22 */
|
||||
struct nk_draw_null_texture null; /* handle to texture with a white pixel for shape drawing */
|
||||
struct nk_draw_null_texture tex_null; /* handle to texture with a white pixel for shape drawing */
|
||||
const struct nk_draw_vertex_layout_element *vertex_layout; /* describes the vertex output format and packing */
|
||||
nk_size vertex_size; /* sizeof one vertex for vertex packing */
|
||||
nk_size vertex_alignment; /* vertex alignment: Can be obtained by NK_ALIGNOF */
|
||||
|
@ -2166,7 +2167,7 @@ NK_API void nk_rule_horizontal(struct nk_context *ctx, struct nk_color color, nk
|
|||
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
/// if (nk_begin_xxx(...) {
|
||||
/// // two rows with height: 30 composed of two widgets with width 60 and 40
|
||||
/// const float size[] = {60,40};
|
||||
/// const float ratio[] = {60,40};
|
||||
/// nk_layout_row(ctx, NK_STATIC, 30, 2, ratio);
|
||||
/// nk_widget(...);
|
||||
/// nk_widget(...);
|
||||
|
@ -3810,149 +3811,155 @@ NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune
|
|||
* FONT
|
||||
*
|
||||
* ===============================================================*/
|
||||
/* Font handling in this library was designed to be quite customizable and lets
|
||||
you decide what you want to use and what you want to provide. There are three
|
||||
different ways to use the font atlas. The first two will use your font
|
||||
handling scheme and only requires essential data to run nuklear. The next
|
||||
slightly more advanced features is font handling with vertex buffer output.
|
||||
Finally the most complex API wise is using nuklear's font baking API.
|
||||
|
||||
1.) Using your own implementation without vertex buffer output
|
||||
--------------------------------------------------------------
|
||||
So first up the easiest way to do font handling is by just providing a
|
||||
`nk_user_font` struct which only requires the height in pixel of the used
|
||||
font and a callback to calculate the width of a string. This way of handling
|
||||
fonts is best fitted for using the normal draw shape command API where you
|
||||
do all the text drawing yourself and the library does not require any kind
|
||||
of deeper knowledge about which font handling mechanism you use.
|
||||
IMPORTANT: the `nk_user_font` pointer provided to nuklear has to persist
|
||||
over the complete life time! I know this sucks but it is currently the only
|
||||
way to switch between fonts.
|
||||
|
||||
float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
|
||||
{
|
||||
your_font_type *type = handle.ptr;
|
||||
float text_width = ...;
|
||||
return text_width;
|
||||
}
|
||||
|
||||
struct nk_user_font font;
|
||||
font.userdata.ptr = &your_font_class_or_struct;
|
||||
font.height = your_font_height;
|
||||
font.width = your_text_width_calculation;
|
||||
|
||||
struct nk_context ctx;
|
||||
nk_init_default(&ctx, &font);
|
||||
|
||||
2.) Using your own implementation with vertex buffer output
|
||||
--------------------------------------------------------------
|
||||
While the first approach works fine if you don't want to use the optional
|
||||
vertex buffer output it is not enough if you do. To get font handling working
|
||||
for these cases you have to provide two additional parameters inside the
|
||||
`nk_user_font`. First a texture atlas handle used to draw text as subimages
|
||||
of a bigger font atlas texture and a callback to query a character's glyph
|
||||
information (offset, size, ...). So it is still possible to provide your own
|
||||
font and use the vertex buffer output.
|
||||
|
||||
float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
|
||||
{
|
||||
your_font_type *type = handle.ptr;
|
||||
float text_width = ...;
|
||||
return text_width;
|
||||
}
|
||||
void query_your_font_glyph(nk_handle handle, float font_height, struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
|
||||
{
|
||||
your_font_type *type = handle.ptr;
|
||||
glyph.width = ...;
|
||||
glyph.height = ...;
|
||||
glyph.xadvance = ...;
|
||||
glyph.uv[0].x = ...;
|
||||
glyph.uv[0].y = ...;
|
||||
glyph.uv[1].x = ...;
|
||||
glyph.uv[1].y = ...;
|
||||
glyph.offset.x = ...;
|
||||
glyph.offset.y = ...;
|
||||
}
|
||||
|
||||
struct nk_user_font font;
|
||||
font.userdata.ptr = &your_font_class_or_struct;
|
||||
font.height = your_font_height;
|
||||
font.width = your_text_width_calculation;
|
||||
font.query = query_your_font_glyph;
|
||||
font.texture.id = your_font_texture;
|
||||
|
||||
struct nk_context ctx;
|
||||
nk_init_default(&ctx, &font);
|
||||
|
||||
3.) Nuklear font baker
|
||||
------------------------------------
|
||||
The final approach if you do not have a font handling functionality or don't
|
||||
want to use it in this library is by using the optional font baker.
|
||||
The font baker APIs can be used to create a font plus font atlas texture
|
||||
and can be used with or without the vertex buffer output.
|
||||
|
||||
It still uses the `nk_user_font` struct and the two different approaches
|
||||
previously stated still work. The font baker is not located inside
|
||||
`nk_context` like all other systems since it can be understood as more of
|
||||
an extension to nuklear and does not really depend on any `nk_context` state.
|
||||
|
||||
Font baker need to be initialized first by one of the nk_font_atlas_init_xxx
|
||||
functions. If you don't care about memory just call the default version
|
||||
`nk_font_atlas_init_default` which will allocate all memory from the standard library.
|
||||
If you want to control memory allocation but you don't care if the allocated
|
||||
memory is temporary and therefore can be freed directly after the baking process
|
||||
is over or permanent you can call `nk_font_atlas_init`.
|
||||
|
||||
After successfully initializing the font baker you can add Truetype(.ttf) fonts from
|
||||
different sources like memory or from file by calling one of the `nk_font_atlas_add_xxx`.
|
||||
functions. Adding font will permanently store each font, font config and ttf memory block(!)
|
||||
inside the font atlas and allows to reuse the font atlas. If you don't want to reuse
|
||||
the font baker by for example adding additional fonts you can call
|
||||
`nk_font_atlas_cleanup` after the baking process is over (after calling nk_font_atlas_end).
|
||||
|
||||
As soon as you added all fonts you wanted you can now start the baking process
|
||||
for every selected glyph to image by calling `nk_font_atlas_bake`.
|
||||
The baking process returns image memory, width and height which can be used to
|
||||
either create your own image object or upload it to any graphics library.
|
||||
No matter which case you finally have to call `nk_font_atlas_end` which
|
||||
will free all temporary memory including the font atlas image so make sure
|
||||
you created our texture beforehand. `nk_font_atlas_end` requires a handle
|
||||
to your font texture or object and optionally fills a `struct nk_draw_null_texture`
|
||||
which can be used for the optional vertex output. If you don't want it just
|
||||
set the argument to `NULL`.
|
||||
|
||||
At this point you are done and if you don't want to reuse the font atlas you
|
||||
can call `nk_font_atlas_cleanup` to free all truetype blobs and configuration
|
||||
memory. Finally if you don't use the font atlas and any of it's fonts anymore
|
||||
you need to call `nk_font_atlas_clear` to free all memory still being used.
|
||||
|
||||
struct nk_font_atlas atlas;
|
||||
nk_font_atlas_init_default(&atlas);
|
||||
nk_font_atlas_begin(&atlas);
|
||||
nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, 0);
|
||||
nk_font *font2 = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font2.ttf", 16, 0);
|
||||
const void* img = nk_font_atlas_bake(&atlas, &img_width, &img_height, NK_FONT_ATLAS_RGBA32);
|
||||
nk_font_atlas_end(&atlas, nk_handle_id(texture), 0);
|
||||
|
||||
struct nk_context ctx;
|
||||
nk_init_default(&ctx, &font->handle);
|
||||
while (1) {
|
||||
|
||||
}
|
||||
nk_font_atlas_clear(&atlas);
|
||||
|
||||
The font baker API is probably the most complex API inside this library and
|
||||
I would suggest reading some of my examples `example/` to get a grip on how
|
||||
to use the font atlas. There are a number of details I left out. For example
|
||||
how to merge fonts, configure a font with `nk_font_config` to use other languages,
|
||||
use another texture coordinate format and a lot more:
|
||||
|
||||
struct nk_font_config cfg = nk_font_config(font_pixel_height);
|
||||
cfg.merge_mode = nk_false or nk_true;
|
||||
cfg.range = nk_font_korean_glyph_ranges();
|
||||
cfg.coord_type = NK_COORD_PIXEL;
|
||||
nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, &cfg);
|
||||
|
||||
/*/// ### Font
|
||||
/// Font handling in this library was designed to be quite customizable and lets
|
||||
/// you decide what you want to use and what you want to provide. There are three
|
||||
/// different ways to use the font atlas. The first two will use your font
|
||||
/// handling scheme and only requires essential data to run nuklear. The next
|
||||
/// slightly more advanced features is font handling with vertex buffer output.
|
||||
/// Finally the most complex API wise is using nuklear's font baking API.
|
||||
//
|
||||
/// #### Using your own implementation without vertex buffer output
|
||||
///
|
||||
/// So first up the easiest way to do font handling is by just providing a
|
||||
/// `nk_user_font` struct which only requires the height in pixel of the used
|
||||
/// font and a callback to calculate the width of a string. This way of handling
|
||||
/// fonts is best fitted for using the normal draw shape command API where you
|
||||
/// do all the text drawing yourself and the library does not require any kind
|
||||
/// of deeper knowledge about which font handling mechanism you use.
|
||||
/// IMPORTANT: the `nk_user_font` pointer provided to nuklear has to persist
|
||||
/// over the complete life time! I know this sucks but it is currently the only
|
||||
/// way to switch between fonts.
|
||||
///
|
||||
/// ```c
|
||||
/// float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
|
||||
/// {
|
||||
/// your_font_type *type = handle.ptr;
|
||||
/// float text_width = ...;
|
||||
/// return text_width;
|
||||
/// }
|
||||
///
|
||||
/// struct nk_user_font font;
|
||||
/// font.userdata.ptr = &your_font_class_or_struct;
|
||||
/// font.height = your_font_height;
|
||||
/// font.width = your_text_width_calculation;
|
||||
///
|
||||
/// struct nk_context ctx;
|
||||
/// nk_init_default(&ctx, &font);
|
||||
/// ```
|
||||
/// #### Using your own implementation with vertex buffer output
|
||||
///
|
||||
/// While the first approach works fine if you don't want to use the optional
|
||||
/// vertex buffer output it is not enough if you do. To get font handling working
|
||||
/// for these cases you have to provide two additional parameters inside the
|
||||
/// `nk_user_font`. First a texture atlas handle used to draw text as subimages
|
||||
/// of a bigger font atlas texture and a callback to query a character's glyph
|
||||
/// information (offset, size, ...). So it is still possible to provide your own
|
||||
/// font and use the vertex buffer output.
|
||||
///
|
||||
/// ```c
|
||||
/// float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
|
||||
/// {
|
||||
/// your_font_type *type = handle.ptr;
|
||||
/// float text_width = ...;
|
||||
/// return text_width;
|
||||
/// }
|
||||
/// void query_your_font_glyph(nk_handle handle, float font_height, struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
|
||||
/// {
|
||||
/// your_font_type *type = handle.ptr;
|
||||
/// glyph.width = ...;
|
||||
/// glyph.height = ...;
|
||||
/// glyph.xadvance = ...;
|
||||
/// glyph.uv[0].x = ...;
|
||||
/// glyph.uv[0].y = ...;
|
||||
/// glyph.uv[1].x = ...;
|
||||
/// glyph.uv[1].y = ...;
|
||||
/// glyph.offset.x = ...;
|
||||
/// glyph.offset.y = ...;
|
||||
/// }
|
||||
///
|
||||
/// struct nk_user_font font;
|
||||
/// font.userdata.ptr = &your_font_class_or_struct;
|
||||
/// font.height = your_font_height;
|
||||
/// font.width = your_text_width_calculation;
|
||||
/// font.query = query_your_font_glyph;
|
||||
/// font.texture.id = your_font_texture;
|
||||
///
|
||||
/// struct nk_context ctx;
|
||||
/// nk_init_default(&ctx, &font);
|
||||
/// ```
|
||||
///
|
||||
/// #### Nuklear font baker
|
||||
///
|
||||
/// The final approach if you do not have a font handling functionality or don't
|
||||
/// want to use it in this library is by using the optional font baker.
|
||||
/// The font baker APIs can be used to create a font plus font atlas texture
|
||||
/// and can be used with or without the vertex buffer output.
|
||||
///
|
||||
/// It still uses the `nk_user_font` struct and the two different approaches
|
||||
/// previously stated still work. The font baker is not located inside
|
||||
/// `nk_context` like all other systems since it can be understood as more of
|
||||
/// an extension to nuklear and does not really depend on any `nk_context` state.
|
||||
///
|
||||
/// Font baker need to be initialized first by one of the nk_font_atlas_init_xxx
|
||||
/// functions. If you don't care about memory just call the default version
|
||||
/// `nk_font_atlas_init_default` which will allocate all memory from the standard library.
|
||||
/// If you want to control memory allocation but you don't care if the allocated
|
||||
/// memory is temporary and therefore can be freed directly after the baking process
|
||||
/// is over or permanent you can call `nk_font_atlas_init`.
|
||||
///
|
||||
/// After successfully initializing the font baker you can add Truetype(.ttf) fonts from
|
||||
/// different sources like memory or from file by calling one of the `nk_font_atlas_add_xxx`.
|
||||
/// functions. Adding font will permanently store each font, font config and ttf memory block(!)
|
||||
/// inside the font atlas and allows to reuse the font atlas. If you don't want to reuse
|
||||
/// the font baker by for example adding additional fonts you can call
|
||||
/// `nk_font_atlas_cleanup` after the baking process is over (after calling nk_font_atlas_end).
|
||||
///
|
||||
/// As soon as you added all fonts you wanted you can now start the baking process
|
||||
/// for every selected glyph to image by calling `nk_font_atlas_bake`.
|
||||
/// The baking process returns image memory, width and height which can be used to
|
||||
/// either create your own image object or upload it to any graphics library.
|
||||
/// No matter which case you finally have to call `nk_font_atlas_end` which
|
||||
/// will free all temporary memory including the font atlas image so make sure
|
||||
/// you created our texture beforehand. `nk_font_atlas_end` requires a handle
|
||||
/// to your font texture or object and optionally fills a `struct nk_draw_null_texture`
|
||||
/// which can be used for the optional vertex output. If you don't want it just
|
||||
/// set the argument to `NULL`.
|
||||
///
|
||||
/// At this point you are done and if you don't want to reuse the font atlas you
|
||||
/// can call `nk_font_atlas_cleanup` to free all truetype blobs and configuration
|
||||
/// memory. Finally if you don't use the font atlas and any of it's fonts anymore
|
||||
/// you need to call `nk_font_atlas_clear` to free all memory still being used.
|
||||
///
|
||||
/// ```c
|
||||
/// struct nk_font_atlas atlas;
|
||||
/// nk_font_atlas_init_default(&atlas);
|
||||
/// nk_font_atlas_begin(&atlas);
|
||||
/// nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, 0);
|
||||
/// nk_font *font2 = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font2.ttf", 16, 0);
|
||||
/// const void* img = nk_font_atlas_bake(&atlas, &img_width, &img_height, NK_FONT_ATLAS_RGBA32);
|
||||
/// nk_font_atlas_end(&atlas, nk_handle_id(texture), 0);
|
||||
///
|
||||
/// struct nk_context ctx;
|
||||
/// nk_init_default(&ctx, &font->handle);
|
||||
/// while (1) {
|
||||
///
|
||||
/// }
|
||||
/// nk_font_atlas_clear(&atlas);
|
||||
/// ```
|
||||
/// The font baker API is probably the most complex API inside this library and
|
||||
/// I would suggest reading some of my examples `example/` to get a grip on how
|
||||
/// to use the font atlas. There are a number of details I left out. For example
|
||||
/// how to merge fonts, configure a font with `nk_font_config` to use other languages,
|
||||
/// use another texture coordinate format and a lot more:
|
||||
///
|
||||
/// ```c
|
||||
/// struct nk_font_config cfg = nk_font_config(font_pixel_height);
|
||||
/// cfg.merge_mode = nk_false or nk_true;
|
||||
/// cfg.range = nk_font_korean_glyph_ranges();
|
||||
/// cfg.coord_type = NK_COORD_PIXEL;
|
||||
/// nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, &cfg);
|
||||
/// ```
|
||||
*/
|
||||
struct nk_user_font_glyph;
|
||||
typedef float(*nk_text_width_f)(nk_handle, float h, const char*, int len);
|
||||
|
@ -4123,33 +4130,34 @@ NK_API void nk_font_atlas_clear(struct nk_font_atlas*);
|
|||
* MEMORY BUFFER
|
||||
*
|
||||
* ===============================================================*/
|
||||
/* A basic (double)-buffer with linear allocation and resetting as only
|
||||
freeing policy. The buffer's main purpose is to control all memory management
|
||||
inside the GUI toolkit and still leave memory control as much as possible in
|
||||
the hand of the user while also making sure the library is easy to use if
|
||||
not as much control is needed.
|
||||
In general all memory inside this library can be provided from the user in
|
||||
three different ways.
|
||||
|
||||
The first way and the one providing most control is by just passing a fixed
|
||||
size memory block. In this case all control lies in the hand of the user
|
||||
since he can exactly control where the memory comes from and how much memory
|
||||
the library should consume. Of course using the fixed size API removes the
|
||||
ability to automatically resize a buffer if not enough memory is provided so
|
||||
you have to take over the resizing. While being a fixed sized buffer sounds
|
||||
quite limiting, it is very effective in this library since the actual memory
|
||||
consumption is quite stable and has a fixed upper bound for a lot of cases.
|
||||
|
||||
If you don't want to think about how much memory the library should allocate
|
||||
at all time or have a very dynamic UI with unpredictable memory consumption
|
||||
habits but still want control over memory allocation you can use the dynamic
|
||||
allocator based API. The allocator consists of two callbacks for allocating
|
||||
and freeing memory and optional userdata so you can plugin your own allocator.
|
||||
|
||||
The final and easiest way can be used by defining
|
||||
NK_INCLUDE_DEFAULT_ALLOCATOR which uses the standard library memory
|
||||
allocation functions malloc and free and takes over complete control over
|
||||
memory in this library.
|
||||
/*/// ### Memory Buffer
|
||||
/// A basic (double)-buffer with linear allocation and resetting as only
|
||||
/// freeing policy. The buffer's main purpose is to control all memory management
|
||||
/// inside the GUI toolkit and still leave memory control as much as possible in
|
||||
/// the hand of the user while also making sure the library is easy to use if
|
||||
/// not as much control is needed.
|
||||
/// In general all memory inside this library can be provided from the user in
|
||||
/// three different ways.
|
||||
///
|
||||
/// The first way and the one providing most control is by just passing a fixed
|
||||
/// size memory block. In this case all control lies in the hand of the user
|
||||
/// since he can exactly control where the memory comes from and how much memory
|
||||
/// the library should consume. Of course using the fixed size API removes the
|
||||
/// ability to automatically resize a buffer if not enough memory is provided so
|
||||
/// you have to take over the resizing. While being a fixed sized buffer sounds
|
||||
/// quite limiting, it is very effective in this library since the actual memory
|
||||
/// consumption is quite stable and has a fixed upper bound for a lot of cases.
|
||||
///
|
||||
/// If you don't want to think about how much memory the library should allocate
|
||||
/// at all time or have a very dynamic UI with unpredictable memory consumption
|
||||
/// habits but still want control over memory allocation you can use the dynamic
|
||||
/// allocator based API. The allocator consists of two callbacks for allocating
|
||||
/// and freeing memory and optional userdata so you can plugin your own allocator.
|
||||
///
|
||||
/// The final and easiest way can be used by defining
|
||||
/// NK_INCLUDE_DEFAULT_ALLOCATOR which uses the standard library memory
|
||||
/// allocation functions malloc and free and takes over complete control over
|
||||
/// memory in this library.
|
||||
*/
|
||||
struct nk_memory_status {
|
||||
void *memory;
|
||||
|
@ -4274,28 +4282,29 @@ NK_API int nk_str_len_char(struct nk_str*);
|
|||
* TEXT EDITOR
|
||||
*
|
||||
* ===============================================================*/
|
||||
/* Editing text in this library is handled by either `nk_edit_string` or
|
||||
* `nk_edit_buffer`. But like almost everything in this library there are multiple
|
||||
* ways of doing it and a balance between control and ease of use with memory
|
||||
* as well as functionality controlled by flags.
|
||||
*
|
||||
* This library generally allows three different levels of memory control:
|
||||
* First of is the most basic way of just providing a simple char array with
|
||||
* string length. This method is probably the easiest way of handling simple
|
||||
* user text input. Main upside is complete control over memory while the biggest
|
||||
* downside in comparison with the other two approaches is missing undo/redo.
|
||||
*
|
||||
* For UIs that require undo/redo the second way was created. It is based on
|
||||
* a fixed size nk_text_edit struct, which has an internal undo/redo stack.
|
||||
* This is mainly useful if you want something more like a text editor but don't want
|
||||
* to have a dynamically growing buffer.
|
||||
*
|
||||
* The final way is using a dynamically growing nk_text_edit struct, which
|
||||
* has both a default version if you don't care where memory comes from and an
|
||||
* allocator version if you do. While the text editor is quite powerful for its
|
||||
* complexity I would not recommend editing gigabytes of data with it.
|
||||
* It is rather designed for uses cases which make sense for a GUI library not for
|
||||
* an full blown text editor.
|
||||
/*/// ### Text Editor
|
||||
/// Editing text in this library is handled by either `nk_edit_string` or
|
||||
/// `nk_edit_buffer`. But like almost everything in this library there are multiple
|
||||
/// ways of doing it and a balance between control and ease of use with memory
|
||||
/// as well as functionality controlled by flags.
|
||||
///
|
||||
/// This library generally allows three different levels of memory control:
|
||||
/// First of is the most basic way of just providing a simple char array with
|
||||
/// string length. This method is probably the easiest way of handling simple
|
||||
/// user text input. Main upside is complete control over memory while the biggest
|
||||
/// downside in comparison with the other two approaches is missing undo/redo.
|
||||
///
|
||||
/// For UIs that require undo/redo the second way was created. It is based on
|
||||
/// a fixed size nk_text_edit struct, which has an internal undo/redo stack.
|
||||
/// This is mainly useful if you want something more like a text editor but don't want
|
||||
/// to have a dynamically growing buffer.
|
||||
///
|
||||
/// The final way is using a dynamically growing nk_text_edit struct, which
|
||||
/// has both a default version if you don't care where memory comes from and an
|
||||
/// allocator version if you do. While the text editor is quite powerful for its
|
||||
/// complexity I would not recommend editing gigabytes of data with it.
|
||||
/// It is rather designed for uses cases which make sense for a GUI library not for
|
||||
/// an full blown text editor.
|
||||
*/
|
||||
#ifndef NK_TEXTEDIT_UNDOSTATECOUNT
|
||||
#define NK_TEXTEDIT_UNDOSTATECOUNT 99
|
||||
|
@ -4389,49 +4398,52 @@ NK_API void nk_textedit_redo(struct nk_text_edit*);
|
|||
* DRAWING
|
||||
*
|
||||
* ===============================================================*/
|
||||
/* This library was designed to be render backend agnostic so it does
|
||||
not draw anything to screen. Instead all drawn shapes, widgets
|
||||
are made of, are buffered into memory and make up a command queue.
|
||||
Each frame therefore fills the command buffer with draw commands
|
||||
that then need to be executed by the user and his own render backend.
|
||||
After that the command buffer needs to be cleared and a new frame can be
|
||||
started. It is probably important to note that the command buffer is the main
|
||||
drawing API and the optional vertex buffer API only takes this format and
|
||||
converts it into a hardware accessible format.
|
||||
|
||||
To use the command queue to draw your own widgets you can access the
|
||||
command buffer of each window by calling `nk_window_get_canvas` after
|
||||
previously having called `nk_begin`:
|
||||
|
||||
void draw_red_rectangle_widget(struct nk_context *ctx)
|
||||
{
|
||||
struct nk_command_buffer *canvas;
|
||||
struct nk_input *input = &ctx->input;
|
||||
canvas = nk_window_get_canvas(ctx);
|
||||
|
||||
struct nk_rect space;
|
||||
enum nk_widget_layout_states state;
|
||||
state = nk_widget(&space, ctx);
|
||||
if (!state) return;
|
||||
|
||||
if (state != NK_WIDGET_ROM)
|
||||
update_your_widget_by_user_input(...);
|
||||
nk_fill_rect(canvas, space, 0, nk_rgb(255,0,0));
|
||||
}
|
||||
|
||||
if (nk_begin(...)) {
|
||||
nk_layout_row_dynamic(ctx, 25, 1);
|
||||
draw_red_rectangle_widget(ctx);
|
||||
}
|
||||
nk_end(..)
|
||||
|
||||
Important to know if you want to create your own widgets is the `nk_widget`
|
||||
call. It allocates space on the panel reserved for this widget to be used,
|
||||
but also returns the state of the widget space. If your widget is not seen and does
|
||||
not have to be updated it is '0' and you can just return. If it only has
|
||||
to be drawn the state will be `NK_WIDGET_ROM` otherwise you can do both
|
||||
update and draw your widget. The reason for separating is to only draw and
|
||||
update what is actually necessary which is crucial for performance.
|
||||
/*/// ### Drawing
|
||||
/// This library was designed to be render backend agnostic so it does
|
||||
/// not draw anything to screen. Instead all drawn shapes, widgets
|
||||
/// are made of, are buffered into memory and make up a command queue.
|
||||
/// Each frame therefore fills the command buffer with draw commands
|
||||
/// that then need to be executed by the user and his own render backend.
|
||||
/// After that the command buffer needs to be cleared and a new frame can be
|
||||
/// started. It is probably important to note that the command buffer is the main
|
||||
/// drawing API and the optional vertex buffer API only takes this format and
|
||||
/// converts it into a hardware accessible format.
|
||||
///
|
||||
/// To use the command queue to draw your own widgets you can access the
|
||||
/// command buffer of each window by calling `nk_window_get_canvas` after
|
||||
/// previously having called `nk_begin`:
|
||||
///
|
||||
/// ```c
|
||||
/// void draw_red_rectangle_widget(struct nk_context *ctx)
|
||||
/// {
|
||||
/// struct nk_command_buffer *canvas;
|
||||
/// struct nk_input *input = &ctx->input;
|
||||
/// canvas = nk_window_get_canvas(ctx);
|
||||
///
|
||||
/// struct nk_rect space;
|
||||
/// enum nk_widget_layout_states state;
|
||||
/// state = nk_widget(&space, ctx);
|
||||
/// if (!state) return;
|
||||
///
|
||||
/// if (state != NK_WIDGET_ROM)
|
||||
/// update_your_widget_by_user_input(...);
|
||||
/// nk_fill_rect(canvas, space, 0, nk_rgb(255,0,0));
|
||||
/// }
|
||||
///
|
||||
/// if (nk_begin(...)) {
|
||||
/// nk_layout_row_dynamic(ctx, 25, 1);
|
||||
/// draw_red_rectangle_widget(ctx);
|
||||
/// }
|
||||
/// nk_end(..)
|
||||
///
|
||||
/// ```
|
||||
/// Important to know if you want to create your own widgets is the `nk_widget`
|
||||
/// call. It allocates space on the panel reserved for this widget to be used,
|
||||
/// but also returns the state of the widget space. If your widget is not seen and does
|
||||
/// not have to be updated it is '0' and you can just return. If it only has
|
||||
/// to be drawn the state will be `NK_WIDGET_ROM` otherwise you can do both
|
||||
/// update and draw your widget. The reason for separating is to only draw and
|
||||
/// update what is actually necessary which is crucial for performance.
|
||||
*/
|
||||
enum nk_command_type {
|
||||
NK_COMMAND_NOP,
|
||||
|
@ -4695,6 +4707,7 @@ struct nk_input {
|
|||
|
||||
NK_API nk_bool nk_input_has_mouse_click(const struct nk_input*, enum nk_buttons);
|
||||
NK_API nk_bool nk_input_has_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
|
||||
NK_API nk_bool nk_input_has_mouse_click_in_button_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
|
||||
NK_API nk_bool nk_input_has_mouse_click_down_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect, nk_bool down);
|
||||
NK_API nk_bool nk_input_is_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
|
||||
NK_API nk_bool nk_input_is_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id, struct nk_rect b, nk_bool down);
|
||||
|
@ -4715,18 +4728,19 @@ NK_API nk_bool nk_input_is_key_down(const struct nk_input*, enum nk_keys);
|
|||
*
|
||||
* ===============================================================*/
|
||||
#ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
|
||||
/* The optional vertex buffer draw list provides a 2D drawing context
|
||||
with antialiasing functionality which takes basic filled or outlined shapes
|
||||
or a path and outputs vertexes, elements and draw commands.
|
||||
The actual draw list API is not required to be used directly while using this
|
||||
library since converting the default library draw command output is done by
|
||||
just calling `nk_convert` but I decided to still make this library accessible
|
||||
since it can be useful.
|
||||
|
||||
The draw list is based on a path buffering and polygon and polyline
|
||||
rendering API which allows a lot of ways to draw 2D content to screen.
|
||||
In fact it is probably more powerful than needed but allows even more crazy
|
||||
things than this library provides by default.
|
||||
/* ### Draw List
|
||||
/// The optional vertex buffer draw list provides a 2D drawing context
|
||||
/// with antialiasing functionality which takes basic filled or outlined shapes
|
||||
/// or a path and outputs vertexes, elements and draw commands.
|
||||
/// The actual draw list API is not required to be used directly while using this
|
||||
/// library since converting the default library draw command output is done by
|
||||
/// just calling `nk_convert` but I decided to still make this library accessible
|
||||
/// since it can be useful.
|
||||
///
|
||||
/// The draw list is based on a path buffering and polygon and polyline
|
||||
/// rendering API which allows a lot of ways to draw 2D content to screen.
|
||||
/// In fact it is probably more powerful than needed but allows even more crazy
|
||||
/// things than this library provides by default.
|
||||
*/
|
||||
#ifdef NK_UINT_DRAW_INDEX
|
||||
typedef nk_uint nk_draw_index;
|
||||
|
@ -5504,27 +5518,28 @@ struct nk_window {
|
|||
/*==============================================================
|
||||
* STACK
|
||||
* =============================================================*/
|
||||
/* The style modifier stack can be used to temporarily change a
|
||||
* property inside `nk_style`. For example if you want a special
|
||||
* red button you can temporarily push the old button color onto a stack
|
||||
* draw the button with a red color and then you just pop the old color
|
||||
* back from the stack:
|
||||
*
|
||||
* nk_style_push_style_item(ctx, &ctx->style.button.normal, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
* nk_style_push_style_item(ctx, &ctx->style.button.hover, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
* nk_style_push_style_item(ctx, &ctx->style.button.active, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
* nk_style_push_vec2(ctx, &cx->style.button.padding, nk_vec2(2,2));
|
||||
*
|
||||
* nk_button(...);
|
||||
*
|
||||
* nk_style_pop_style_item(ctx);
|
||||
* nk_style_pop_style_item(ctx);
|
||||
* nk_style_pop_style_item(ctx);
|
||||
* nk_style_pop_vec2(ctx);
|
||||
*
|
||||
* Nuklear has a stack for style_items, float properties, vector properties,
|
||||
* flags, colors, fonts and for button_behavior. Each has it's own fixed size stack
|
||||
* which can be changed at compile time.
|
||||
/*/// ### Stack
|
||||
/// The style modifier stack can be used to temporarily change a
|
||||
/// property inside `nk_style`. For example if you want a special
|
||||
/// red button you can temporarily push the old button color onto a stack
|
||||
/// draw the button with a red color and then you just pop the old color
|
||||
/// back from the stack:
|
||||
///
|
||||
/// nk_style_push_style_item(ctx, &ctx->style.button.normal, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
/// nk_style_push_style_item(ctx, &ctx->style.button.hover, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
/// nk_style_push_style_item(ctx, &ctx->style.button.active, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
/// nk_style_push_vec2(ctx, &cx->style.button.padding, nk_vec2(2,2));
|
||||
///
|
||||
/// nk_button(...);
|
||||
///
|
||||
/// nk_style_pop_style_item(ctx);
|
||||
/// nk_style_pop_style_item(ctx);
|
||||
/// nk_style_pop_style_item(ctx);
|
||||
/// nk_style_pop_vec2(ctx);
|
||||
///
|
||||
/// Nuklear has a stack for style_items, float properties, vector properties,
|
||||
/// flags, colors, fonts and for button_behavior. Each has it's own fixed size stack
|
||||
/// which can be changed at compile time.
|
||||
*/
|
||||
#ifndef NK_BUTTON_BEHAVIOR_STACK_SIZE
|
||||
#define NK_BUTTON_BEHAVIOR_STACK_SIZE 8
|
||||
|
@ -6123,32 +6138,33 @@ nk_stbtt_free(void *ptr, void *user_data) {
|
|||
* MATH
|
||||
*
|
||||
* ===============================================================*/
|
||||
/* Since nuklear is supposed to work on all systems providing floating point
|
||||
math without any dependencies I also had to implement my own math functions
|
||||
for sqrt, sin and cos. Since the actual highly accurate implementations for
|
||||
the standard library functions are quite complex and I do not need high
|
||||
precision for my use cases I use approximations.
|
||||
|
||||
Sqrt
|
||||
----
|
||||
For square root nuklear uses the famous fast inverse square root:
|
||||
https://en.wikipedia.org/wiki/Fast_inverse_square_root with
|
||||
slightly tweaked magic constant. While on today's hardware it is
|
||||
probably not faster it is still fast and accurate enough for
|
||||
nuklear's use cases. IMPORTANT: this requires float format IEEE 754
|
||||
|
||||
Sine/Cosine
|
||||
-----------
|
||||
All constants inside both function are generated Remez's minimax
|
||||
approximations for value range 0...2*PI. The reason why I decided to
|
||||
approximate exactly that range is that nuklear only needs sine and
|
||||
cosine to generate circles which only requires that exact range.
|
||||
In addition I used Remez instead of Taylor for additional precision:
|
||||
www.lolengine.net/blog/2011/12/21/better-function-approximations.
|
||||
|
||||
The tool I used to generate constants for both sine and cosine
|
||||
(it can actually approximate a lot more functions) can be
|
||||
found here: www.lolengine.net/wiki/oss/lolremez
|
||||
/*/// ### Math
|
||||
/// Since nuklear is supposed to work on all systems providing floating point
|
||||
/// math without any dependencies I also had to implement my own math functions
|
||||
/// for sqrt, sin and cos. Since the actual highly accurate implementations for
|
||||
/// the standard library functions are quite complex and I do not need high
|
||||
/// precision for my use cases I use approximations.
|
||||
///
|
||||
/// Sqrt
|
||||
/// ----
|
||||
/// For square root nuklear uses the famous fast inverse square root:
|
||||
/// https://en.wikipedia.org/wiki/Fast_inverse_square_root with
|
||||
/// slightly tweaked magic constant. While on today's hardware it is
|
||||
/// probably not faster it is still fast and accurate enough for
|
||||
/// nuklear's use cases. IMPORTANT: this requires float format IEEE 754
|
||||
///
|
||||
/// Sine/Cosine
|
||||
/// -----------
|
||||
/// All constants inside both function are generated Remez's minimax
|
||||
/// approximations for value range 0...2*PI. The reason why I decided to
|
||||
/// approximate exactly that range is that nuklear only needs sine and
|
||||
/// cosine to generate circles which only requires that exact range.
|
||||
/// In addition I used Remez instead of Taylor for additional precision:
|
||||
/// www.lolengine.net/blog/2011/12/21/better-function-approximations.
|
||||
///
|
||||
/// The tool I used to generate constants for both sine and cosine
|
||||
/// (it can actually approximate a lot more functions) can be
|
||||
/// found here: www.lolengine.net/wiki/oss/lolremez
|
||||
*/
|
||||
#ifndef NK_INV_SQRT
|
||||
#define NK_INV_SQRT nk_inv_sqrt
|
||||
|
@ -8451,7 +8467,6 @@ nk_str_append_text_utf8(struct nk_str *str, const char *text, int len)
|
|||
NK_API int
|
||||
nk_str_append_str_utf8(struct nk_str *str, const char *text)
|
||||
{
|
||||
int runes = 0;
|
||||
int byte_len = 0;
|
||||
int num_runes = 0;
|
||||
int glyph_len = 0;
|
||||
|
@ -8465,7 +8480,7 @@ nk_str_append_str_utf8(struct nk_str *str, const char *text)
|
|||
num_runes++;
|
||||
}
|
||||
nk_str_append_text_char(str, text, byte_len);
|
||||
return runes;
|
||||
return num_runes;
|
||||
}
|
||||
NK_API int
|
||||
nk_str_append_text_runes(struct nk_str *str, const nk_rune *text, int len)
|
||||
|
@ -8580,7 +8595,6 @@ nk_str_insert_text_utf8(struct nk_str *str, int pos, const char *text, int len)
|
|||
NK_API int
|
||||
nk_str_insert_str_utf8(struct nk_str *str, int pos, const char *text)
|
||||
{
|
||||
int runes = 0;
|
||||
int byte_len = 0;
|
||||
int num_runes = 0;
|
||||
int glyph_len = 0;
|
||||
|
@ -8594,7 +8608,7 @@ nk_str_insert_str_utf8(struct nk_str *str, int pos, const char *text)
|
|||
num_runes++;
|
||||
}
|
||||
nk_str_insert_at_rune(str, pos, text, byte_len);
|
||||
return runes;
|
||||
return num_runes;
|
||||
}
|
||||
NK_API int
|
||||
nk_str_insert_text_runes(struct nk_str *str, int pos, const nk_rune *runes, int len)
|
||||
|
@ -9575,7 +9589,7 @@ nk_draw_list_add_clip(struct nk_draw_list *list, struct nk_rect rect)
|
|||
NK_ASSERT(list);
|
||||
if (!list) return;
|
||||
if (!list->cmd_count) {
|
||||
nk_draw_list_push_command(list, rect, list->config.null.texture);
|
||||
nk_draw_list_push_command(list, rect, list->config.tex_null.texture);
|
||||
} else {
|
||||
struct nk_draw_command *prev = nk_draw_list_command_last(list);
|
||||
if (prev->elem_count == 0)
|
||||
|
@ -9930,7 +9944,7 @@ nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *p
|
|||
|
||||
/* fill vertices */
|
||||
for (i = 0; i < points_count; ++i) {
|
||||
const struct nk_vec2 uv = list->config.null.uv;
|
||||
const struct nk_vec2 uv = list->config.tex_null.uv;
|
||||
vtx = nk_draw_vertex(vtx, &list->config, points[i], uv, col);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+0], uv, col_trans);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+1], uv, col_trans);
|
||||
|
@ -9995,7 +10009,7 @@ nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *p
|
|||
|
||||
/* add vertices */
|
||||
for (i = 0; i < points_count; ++i) {
|
||||
const struct nk_vec2 uv = list->config.null.uv;
|
||||
const struct nk_vec2 uv = list->config.tex_null.uv;
|
||||
vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+0], uv, col_trans);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+1], uv, col);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+2], uv, col);
|
||||
|
@ -10016,7 +10030,7 @@ nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *p
|
|||
|
||||
for (i1 = 0; i1 < count; ++i1) {
|
||||
float dx, dy;
|
||||
const struct nk_vec2 uv = list->config.null.uv;
|
||||
const struct nk_vec2 uv = list->config.tex_null.uv;
|
||||
const nk_size i2 = ((i1+1) == points_count) ? 0 : i1 + 1;
|
||||
const struct nk_vec2 p1 = points[i1];
|
||||
const struct nk_vec2 p2 = points[i2];
|
||||
|
@ -10126,7 +10140,7 @@ nk_draw_list_fill_poly_convex(struct nk_draw_list *list,
|
|||
|
||||
/* add vertices + indexes */
|
||||
for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
|
||||
const struct nk_vec2 uv = list->config.null.uv;
|
||||
const struct nk_vec2 uv = list->config.tex_null.uv;
|
||||
struct nk_vec2 n0 = normals[i0];
|
||||
struct nk_vec2 n1 = normals[i1];
|
||||
struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(n0, n1), 0.5f);
|
||||
|
@ -10163,7 +10177,7 @@ nk_draw_list_fill_poly_convex(struct nk_draw_list *list,
|
|||
|
||||
if (!vtx || !ids) return;
|
||||
for (i = 0; i < vtx_count; ++i)
|
||||
vtx = nk_draw_vertex(vtx, &list->config, points[i], list->config.null.uv, col);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, points[i], list->config.tex_null.uv, col);
|
||||
for (i = 2; i < points_count; ++i) {
|
||||
ids[0] = (nk_draw_index)index;
|
||||
ids[1] = (nk_draw_index)(index+ i - 1);
|
||||
|
@ -10192,8 +10206,8 @@ nk_draw_list_path_line_to(struct nk_draw_list *list, struct nk_vec2 pos)
|
|||
nk_draw_list_add_clip(list, nk_null_rect);
|
||||
|
||||
cmd = nk_draw_list_command_last(list);
|
||||
if (cmd && cmd->texture.ptr != list->config.null.texture.ptr)
|
||||
nk_draw_list_push_image(list, list->config.null.texture);
|
||||
if (cmd && cmd->texture.ptr != list->config.tex_null.texture.ptr)
|
||||
nk_draw_list_push_image(list, list->config.tex_null.texture);
|
||||
|
||||
points = nk_draw_list_alloc_path(list, 1);
|
||||
if (!points) return;
|
||||
|
@ -10395,7 +10409,7 @@ nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rec
|
|||
NK_ASSERT(list);
|
||||
if (!list) return;
|
||||
|
||||
nk_draw_list_push_image(list, list->config.null.texture);
|
||||
nk_draw_list_push_image(list, list->config.tex_null.texture);
|
||||
index = (nk_draw_index)list->vertex_count;
|
||||
vtx = nk_draw_list_alloc_vertices(list, 4);
|
||||
idx = nk_draw_list_alloc_elements(list, 6);
|
||||
|
@ -10405,10 +10419,10 @@ nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rec
|
|||
idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
|
||||
idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
|
||||
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y), list->config.null.uv, col_left);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y), list->config.null.uv, col_top);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y + rect.h), list->config.null.uv, col_right);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y + rect.h), list->config.null.uv, col_bottom);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y), list->config.tex_null.uv, col_left);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y), list->config.tex_null.uv, col_top);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y + rect.h), list->config.tex_null.uv, col_right);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y + rect.h), list->config.tex_null.uv, col_bottom);
|
||||
}
|
||||
NK_API void
|
||||
nk_draw_list_fill_triangle(struct nk_draw_list *list, struct nk_vec2 a,
|
||||
|
@ -16524,7 +16538,7 @@ nk_font_chinese_glyph_ranges(void)
|
|||
0x3000, 0x30FF,
|
||||
0x31F0, 0x31FF,
|
||||
0xFF00, 0xFFEF,
|
||||
0x4e00, 0x9FAF,
|
||||
0x4E00, 0x9FAF,
|
||||
0
|
||||
};
|
||||
return ranges;
|
||||
|
@ -16633,7 +16647,7 @@ nk_font_bake_pack(struct nk_font_baker *baker,
|
|||
struct stbtt_fontinfo *font_info = &baker->build[i++].info;
|
||||
font_info->userdata = alloc;
|
||||
|
||||
if (!stbtt_InitFont(font_info, (const unsigned char*)it->ttf_blob, 0))
|
||||
if (!stbtt_InitFont(font_info, (const unsigned char*)it->ttf_blob, stbtt_GetFontOffsetForIndex((const unsigned char*)it->ttf_blob, 0)))
|
||||
return nk_false;
|
||||
} while ((it = it->n) != config_iter);
|
||||
}
|
||||
|
@ -16793,7 +16807,6 @@ nk_font_bake(struct nk_font_baker *baker, void *image_memory, int width, int hei
|
|||
|
||||
/* query glyph bounds from stb_truetype */
|
||||
const stbtt_packedchar *pc = &range->chardata_for_range[char_idx];
|
||||
if (!pc->x0 && !pc->x1 && !pc->y0 && !pc->y1) continue;
|
||||
codepoint = (nk_rune)(range->first_unicode_codepoint_in_range + char_idx);
|
||||
stbtt_GetPackedQuad(range->chardata_for_range, (int)width,
|
||||
(int)height, char_idx, &dummy_x, &dummy_y, &q, 0);
|
||||
|
@ -17715,20 +17728,20 @@ failed:
|
|||
}
|
||||
NK_API void
|
||||
nk_font_atlas_end(struct nk_font_atlas *atlas, nk_handle texture,
|
||||
struct nk_draw_null_texture *null)
|
||||
struct nk_draw_null_texture *tex_null)
|
||||
{
|
||||
int i = 0;
|
||||
struct nk_font *font_iter;
|
||||
NK_ASSERT(atlas);
|
||||
if (!atlas) {
|
||||
if (!null) return;
|
||||
null->texture = texture;
|
||||
null->uv = nk_vec2(0.5f,0.5f);
|
||||
if (!tex_null) return;
|
||||
tex_null->texture = texture;
|
||||
tex_null->uv = nk_vec2(0.5f,0.5f);
|
||||
}
|
||||
if (null) {
|
||||
null->texture = texture;
|
||||
null->uv.x = (atlas->custom.x + 0.5f)/(float)atlas->tex_width;
|
||||
null->uv.y = (atlas->custom.y + 0.5f)/(float)atlas->tex_height;
|
||||
if (tex_null) {
|
||||
tex_null->texture = texture;
|
||||
tex_null->uv.x = (atlas->custom.x + 0.5f)/(float)atlas->tex_width;
|
||||
tex_null->uv.y = (atlas->custom.y + 0.5f)/(float)atlas->tex_height;
|
||||
}
|
||||
for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
|
||||
font_iter->texture = texture;
|
||||
|
@ -17897,6 +17910,10 @@ nk_input_button(struct nk_context *ctx, enum nk_buttons id, int x, int y, nk_boo
|
|||
btn->clicked_pos.y = (float)y;
|
||||
btn->down = down;
|
||||
btn->clicked++;
|
||||
|
||||
/* Fix Click-Drag for touch events. */
|
||||
in->mouse.delta.x = 0;
|
||||
in->mouse.delta.y = 0;
|
||||
#ifdef NK_BUTTON_TRIGGER_ON_RELEASE
|
||||
if (down == 1 && id == NK_BUTTON_LEFT)
|
||||
{
|
||||
|
@ -17960,6 +17977,17 @@ nk_input_has_mouse_click(const struct nk_input *i, enum nk_buttons id)
|
|||
NK_API nk_bool
|
||||
nk_input_has_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
|
||||
struct nk_rect b)
|
||||
{
|
||||
const struct nk_mouse_button *btn;
|
||||
if (!i) return nk_false;
|
||||
btn = &i->mouse.buttons[id];
|
||||
if (!NK_INBOX(btn->clicked_pos.x,btn->clicked_pos.y,b.x,b.y,b.w,b.h))
|
||||
return nk_false;
|
||||
return nk_true;
|
||||
}
|
||||
NK_API nk_bool
|
||||
nk_input_has_mouse_click_in_button_rect(const struct nk_input *i, enum nk_buttons id,
|
||||
struct nk_rect b)
|
||||
{
|
||||
const struct nk_mouse_button *btn;
|
||||
if (!i) return nk_false;
|
||||
|
@ -23792,7 +23820,7 @@ nk_button_behavior(nk_flags *state, struct nk_rect r,
|
|||
*state = NK_WIDGET_STATE_HOVERED;
|
||||
if (nk_input_is_mouse_down(i, NK_BUTTON_LEFT))
|
||||
*state = NK_WIDGET_STATE_ACTIVE;
|
||||
if (nk_input_has_mouse_click_in_rect(i, NK_BUTTON_LEFT, r)) {
|
||||
if (nk_input_has_mouse_click_in_button_rect(i, NK_BUTTON_LEFT, r)) {
|
||||
ret = (behavior != NK_BUTTON_DEFAULT) ?
|
||||
nk_input_is_mouse_down(i, NK_BUTTON_LEFT):
|
||||
#ifdef NK_BUTTON_TRIGGER_ON_RELEASE
|
||||
|
@ -26172,7 +26200,7 @@ nk_textedit_text(struct nk_text_edit *state, const char *text, int total_len)
|
|||
text+text_len, 1))
|
||||
{
|
||||
nk_textedit_makeundo_insert(state, state->cursor, 1);
|
||||
++state->cursor;
|
||||
state->cursor = NK_MIN(state->cursor + 1, state->string.len);
|
||||
state->has_preferred_x = 0;
|
||||
}
|
||||
}
|
||||
|
@ -29666,7 +29694,14 @@ nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
|
|||
/// - [y]: Minor version with non-breaking API and library changes
|
||||
/// - [z]: Patch version with no direct changes to the API
|
||||
///
|
||||
/// - 2022/04/19 (4.9.8) - Added nk_rule_horizontal() widget
|
||||
/// - 2022/12/23 (4.10.6) - Fix incorrect glyph index in nk_font_bake()
|
||||
/// - 2022/12/17 (4.10.5) - Fix nk_font_bake_pack() using TTC font offset incorrectly
|
||||
/// - 2022/10/24 (4.10.4) - Fix nk_str_{append,insert}_str_utf8 always returning 0
|
||||
/// - 2022/09/03 (4.10.3) - Renamed the `null` texture variable to `tex_null`
|
||||
/// - 2022/08/01 (4.10.2) - Fix Apple Silicon with incorrect NK_SITE_TYPE and NK_POINTER_TYPE
|
||||
/// - 2022/08/01 (4.10.1) - Fix cursor jumping back to beginning of text when typing more than
|
||||
/// nk_edit_xxx limit
|
||||
/// - 2022/05/27 (4.10.0) - Add nk_input_has_mouse_click_in_button_rect() to fix window move bug
|
||||
/// - 2022/04/18 (4.9.7) - Change button behavior when NK_BUTTON_TRIGGER_ON_RELEASE is defined to
|
||||
/// only trigger when the mouse position was inside the same button on down
|
||||
/// - 2022/02/03 (4.9.6) - Allow overriding the NK_INV_SQRT function, similar to NK_SIN and NK_COS
|
||||
|
|
|
@ -7,6 +7,14 @@
|
|||
/// - [y]: Minor version with non-breaking API and library changes
|
||||
/// - [z]: Patch version with no direct changes to the API
|
||||
///
|
||||
/// - 2022/12/23 (4.10.6) - Fix incorrect glyph index in nk_font_bake()
|
||||
/// - 2022/12/17 (4.10.5) - Fix nk_font_bake_pack() using TTC font offset incorrectly
|
||||
/// - 2022/10/24 (4.10.4) - Fix nk_str_{append,insert}_str_utf8 always returning 0
|
||||
/// - 2022/09/03 (4.10.3) - Renamed the `null` texture variable to `tex_null`
|
||||
/// - 2022/08/01 (4.10.2) - Fix Apple Silicon with incorrect NK_SITE_TYPE and NK_POINTER_TYPE
|
||||
/// - 2022/08/01 (4.10.1) - Fix cursor jumping back to beginning of text when typing more than
|
||||
/// nk_edit_xxx limit
|
||||
/// - 2022/05/27 (4.10.0) - Add nk_input_has_mouse_click_in_button_rect() to fix window move bug
|
||||
/// - 2022/04/19 (4.9.8) - Added nk_rule_horizontal() widget
|
||||
/// - 2022/04/18 (4.9.7) - Change button behavior when NK_BUTTON_TRIGGER_ON_RELEASE is defined to
|
||||
/// only trigger when the mouse position was inside the same button on down
|
||||
|
|
|
@ -47,6 +47,7 @@
|
|||
/// - No global or hidden state
|
||||
/// - Customizable library modules (you can compile and use only what you need)
|
||||
/// - Optional font baker and vertex buffer output
|
||||
/// - [Code available on github](https://github.com/Immediate-Mode-UI/Nuklear/)
|
||||
///
|
||||
/// ## Features
|
||||
/// - Absolutely no platform dependent code
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
File Packer:
|
||||
------------
|
||||
- [Click to generate nuklear.h](http://apoorvaj.io/single-header-packer.html?macro=NK&pre=https://raw.githubusercontent.com/vurtun/nuklear/master/src/HEADER&pub=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear.h&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_internal.h&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_math.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_util.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_color.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_utf8.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_buffer.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_string.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_draw.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_vertex.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_font.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_input.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_style.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_context.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_pool.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_page_element.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_table.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_panel.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_window.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_popup.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_contextual.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_menu.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_layout.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_tree.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_group.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_list_view.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_widget.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_text.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_image.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_button.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_toggle.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_selectable.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_slider.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_progress.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_scrollbar.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_text_editor.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_edit.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_property.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_chart.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_color_picker.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_combo.c&priv=https://raw.githubusercontent.com/vurtun/nuklear/master/src/nuklear_tooltip.c&post=https://raw.githubusercontent.com/vurtun/nuklear/master/src/LICENSE&post=https://raw.githubusercontent.com/vurtun/nuklear/master/src/CHANGELOG&post=https://raw.githubusercontent.com/vurtun/nuklear/master/src/CREDITS)
|
||||
- On Linux/Mac just run ./paq.sh > ../nuklear.h
|
||||
- On Windows just run paq.bat
|
||||
|
|
560
src/nuklear.h
560
src/nuklear.h
|
@ -151,7 +151,7 @@ extern "C" {
|
|||
#elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
|
||||
#define NK_SIZE_TYPE unsigned __int32
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
#if defined(__x86_64__) || defined(__ppc64__)
|
||||
#if defined(__x86_64__) || defined(__ppc64__) || defined(__aarch64__)
|
||||
#define NK_SIZE_TYPE unsigned long
|
||||
#else
|
||||
#define NK_SIZE_TYPE unsigned int
|
||||
|
@ -166,7 +166,7 @@ extern "C" {
|
|||
#elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
|
||||
#define NK_POINTER_TYPE unsigned __int32
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
#if defined(__x86_64__) || defined(__ppc64__)
|
||||
#if defined(__x86_64__) || defined(__ppc64__) || defined(__aarch64__)
|
||||
#define NK_POINTER_TYPE unsigned long
|
||||
#else
|
||||
#define NK_POINTER_TYPE unsigned int
|
||||
|
@ -906,7 +906,7 @@ NK_API void nk_input_end(struct nk_context*);
|
|||
/// cfg.curve_segment_count = 22;
|
||||
/// cfg.arc_segment_count = 22;
|
||||
/// cfg.global_alpha = 1.0f;
|
||||
/// cfg.null = dev->null;
|
||||
/// cfg.tex_null = dev->tex_null;
|
||||
/// //
|
||||
/// // setup buffers and convert
|
||||
/// struct nk_buffer cmds, verts, idx;
|
||||
|
@ -956,7 +956,7 @@ struct nk_convert_config {
|
|||
unsigned circle_segment_count; /* number of segments used for circles: default to 22 */
|
||||
unsigned arc_segment_count; /* number of segments used for arcs: default to 22 */
|
||||
unsigned curve_segment_count; /* number of segments used for curves: default to 22 */
|
||||
struct nk_draw_null_texture null; /* handle to texture with a white pixel for shape drawing */
|
||||
struct nk_draw_null_texture tex_null; /* handle to texture with a white pixel for shape drawing */
|
||||
const struct nk_draw_vertex_layout_element *vertex_layout; /* describes the vertex output format and packing */
|
||||
nk_size vertex_size; /* sizeof one vertex for vertex packing */
|
||||
nk_size vertex_alignment; /* vertex alignment: Can be obtained by NK_ALIGNOF */
|
||||
|
@ -1945,7 +1945,7 @@ NK_API void nk_rule_horizontal(struct nk_context *ctx, struct nk_color color, nk
|
|||
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
|
||||
/// if (nk_begin_xxx(...) {
|
||||
/// // two rows with height: 30 composed of two widgets with width 60 and 40
|
||||
/// const float size[] = {60,40};
|
||||
/// const float ratio[] = {60,40};
|
||||
/// nk_layout_row(ctx, NK_STATIC, 30, 2, ratio);
|
||||
/// nk_widget(...);
|
||||
/// nk_widget(...);
|
||||
|
@ -3589,149 +3589,155 @@ NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune
|
|||
* FONT
|
||||
*
|
||||
* ===============================================================*/
|
||||
/* Font handling in this library was designed to be quite customizable and lets
|
||||
you decide what you want to use and what you want to provide. There are three
|
||||
different ways to use the font atlas. The first two will use your font
|
||||
handling scheme and only requires essential data to run nuklear. The next
|
||||
slightly more advanced features is font handling with vertex buffer output.
|
||||
Finally the most complex API wise is using nuklear's font baking API.
|
||||
|
||||
1.) Using your own implementation without vertex buffer output
|
||||
--------------------------------------------------------------
|
||||
So first up the easiest way to do font handling is by just providing a
|
||||
`nk_user_font` struct which only requires the height in pixel of the used
|
||||
font and a callback to calculate the width of a string. This way of handling
|
||||
fonts is best fitted for using the normal draw shape command API where you
|
||||
do all the text drawing yourself and the library does not require any kind
|
||||
of deeper knowledge about which font handling mechanism you use.
|
||||
IMPORTANT: the `nk_user_font` pointer provided to nuklear has to persist
|
||||
over the complete life time! I know this sucks but it is currently the only
|
||||
way to switch between fonts.
|
||||
|
||||
float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
|
||||
{
|
||||
your_font_type *type = handle.ptr;
|
||||
float text_width = ...;
|
||||
return text_width;
|
||||
}
|
||||
|
||||
struct nk_user_font font;
|
||||
font.userdata.ptr = &your_font_class_or_struct;
|
||||
font.height = your_font_height;
|
||||
font.width = your_text_width_calculation;
|
||||
|
||||
struct nk_context ctx;
|
||||
nk_init_default(&ctx, &font);
|
||||
|
||||
2.) Using your own implementation with vertex buffer output
|
||||
--------------------------------------------------------------
|
||||
While the first approach works fine if you don't want to use the optional
|
||||
vertex buffer output it is not enough if you do. To get font handling working
|
||||
for these cases you have to provide two additional parameters inside the
|
||||
`nk_user_font`. First a texture atlas handle used to draw text as subimages
|
||||
of a bigger font atlas texture and a callback to query a character's glyph
|
||||
information (offset, size, ...). So it is still possible to provide your own
|
||||
font and use the vertex buffer output.
|
||||
|
||||
float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
|
||||
{
|
||||
your_font_type *type = handle.ptr;
|
||||
float text_width = ...;
|
||||
return text_width;
|
||||
}
|
||||
void query_your_font_glyph(nk_handle handle, float font_height, struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
|
||||
{
|
||||
your_font_type *type = handle.ptr;
|
||||
glyph.width = ...;
|
||||
glyph.height = ...;
|
||||
glyph.xadvance = ...;
|
||||
glyph.uv[0].x = ...;
|
||||
glyph.uv[0].y = ...;
|
||||
glyph.uv[1].x = ...;
|
||||
glyph.uv[1].y = ...;
|
||||
glyph.offset.x = ...;
|
||||
glyph.offset.y = ...;
|
||||
}
|
||||
|
||||
struct nk_user_font font;
|
||||
font.userdata.ptr = &your_font_class_or_struct;
|
||||
font.height = your_font_height;
|
||||
font.width = your_text_width_calculation;
|
||||
font.query = query_your_font_glyph;
|
||||
font.texture.id = your_font_texture;
|
||||
|
||||
struct nk_context ctx;
|
||||
nk_init_default(&ctx, &font);
|
||||
|
||||
3.) Nuklear font baker
|
||||
------------------------------------
|
||||
The final approach if you do not have a font handling functionality or don't
|
||||
want to use it in this library is by using the optional font baker.
|
||||
The font baker APIs can be used to create a font plus font atlas texture
|
||||
and can be used with or without the vertex buffer output.
|
||||
|
||||
It still uses the `nk_user_font` struct and the two different approaches
|
||||
previously stated still work. The font baker is not located inside
|
||||
`nk_context` like all other systems since it can be understood as more of
|
||||
an extension to nuklear and does not really depend on any `nk_context` state.
|
||||
|
||||
Font baker need to be initialized first by one of the nk_font_atlas_init_xxx
|
||||
functions. If you don't care about memory just call the default version
|
||||
`nk_font_atlas_init_default` which will allocate all memory from the standard library.
|
||||
If you want to control memory allocation but you don't care if the allocated
|
||||
memory is temporary and therefore can be freed directly after the baking process
|
||||
is over or permanent you can call `nk_font_atlas_init`.
|
||||
|
||||
After successfully initializing the font baker you can add Truetype(.ttf) fonts from
|
||||
different sources like memory or from file by calling one of the `nk_font_atlas_add_xxx`.
|
||||
functions. Adding font will permanently store each font, font config and ttf memory block(!)
|
||||
inside the font atlas and allows to reuse the font atlas. If you don't want to reuse
|
||||
the font baker by for example adding additional fonts you can call
|
||||
`nk_font_atlas_cleanup` after the baking process is over (after calling nk_font_atlas_end).
|
||||
|
||||
As soon as you added all fonts you wanted you can now start the baking process
|
||||
for every selected glyph to image by calling `nk_font_atlas_bake`.
|
||||
The baking process returns image memory, width and height which can be used to
|
||||
either create your own image object or upload it to any graphics library.
|
||||
No matter which case you finally have to call `nk_font_atlas_end` which
|
||||
will free all temporary memory including the font atlas image so make sure
|
||||
you created our texture beforehand. `nk_font_atlas_end` requires a handle
|
||||
to your font texture or object and optionally fills a `struct nk_draw_null_texture`
|
||||
which can be used for the optional vertex output. If you don't want it just
|
||||
set the argument to `NULL`.
|
||||
|
||||
At this point you are done and if you don't want to reuse the font atlas you
|
||||
can call `nk_font_atlas_cleanup` to free all truetype blobs and configuration
|
||||
memory. Finally if you don't use the font atlas and any of it's fonts anymore
|
||||
you need to call `nk_font_atlas_clear` to free all memory still being used.
|
||||
|
||||
struct nk_font_atlas atlas;
|
||||
nk_font_atlas_init_default(&atlas);
|
||||
nk_font_atlas_begin(&atlas);
|
||||
nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, 0);
|
||||
nk_font *font2 = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font2.ttf", 16, 0);
|
||||
const void* img = nk_font_atlas_bake(&atlas, &img_width, &img_height, NK_FONT_ATLAS_RGBA32);
|
||||
nk_font_atlas_end(&atlas, nk_handle_id(texture), 0);
|
||||
|
||||
struct nk_context ctx;
|
||||
nk_init_default(&ctx, &font->handle);
|
||||
while (1) {
|
||||
|
||||
}
|
||||
nk_font_atlas_clear(&atlas);
|
||||
|
||||
The font baker API is probably the most complex API inside this library and
|
||||
I would suggest reading some of my examples `example/` to get a grip on how
|
||||
to use the font atlas. There are a number of details I left out. For example
|
||||
how to merge fonts, configure a font with `nk_font_config` to use other languages,
|
||||
use another texture coordinate format and a lot more:
|
||||
|
||||
struct nk_font_config cfg = nk_font_config(font_pixel_height);
|
||||
cfg.merge_mode = nk_false or nk_true;
|
||||
cfg.range = nk_font_korean_glyph_ranges();
|
||||
cfg.coord_type = NK_COORD_PIXEL;
|
||||
nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, &cfg);
|
||||
|
||||
/*/// ### Font
|
||||
/// Font handling in this library was designed to be quite customizable and lets
|
||||
/// you decide what you want to use and what you want to provide. There are three
|
||||
/// different ways to use the font atlas. The first two will use your font
|
||||
/// handling scheme and only requires essential data to run nuklear. The next
|
||||
/// slightly more advanced features is font handling with vertex buffer output.
|
||||
/// Finally the most complex API wise is using nuklear's font baking API.
|
||||
//
|
||||
/// #### Using your own implementation without vertex buffer output
|
||||
///
|
||||
/// So first up the easiest way to do font handling is by just providing a
|
||||
/// `nk_user_font` struct which only requires the height in pixel of the used
|
||||
/// font and a callback to calculate the width of a string. This way of handling
|
||||
/// fonts is best fitted for using the normal draw shape command API where you
|
||||
/// do all the text drawing yourself and the library does not require any kind
|
||||
/// of deeper knowledge about which font handling mechanism you use.
|
||||
/// IMPORTANT: the `nk_user_font` pointer provided to nuklear has to persist
|
||||
/// over the complete life time! I know this sucks but it is currently the only
|
||||
/// way to switch between fonts.
|
||||
///
|
||||
/// ```c
|
||||
/// float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
|
||||
/// {
|
||||
/// your_font_type *type = handle.ptr;
|
||||
/// float text_width = ...;
|
||||
/// return text_width;
|
||||
/// }
|
||||
///
|
||||
/// struct nk_user_font font;
|
||||
/// font.userdata.ptr = &your_font_class_or_struct;
|
||||
/// font.height = your_font_height;
|
||||
/// font.width = your_text_width_calculation;
|
||||
///
|
||||
/// struct nk_context ctx;
|
||||
/// nk_init_default(&ctx, &font);
|
||||
/// ```
|
||||
/// #### Using your own implementation with vertex buffer output
|
||||
///
|
||||
/// While the first approach works fine if you don't want to use the optional
|
||||
/// vertex buffer output it is not enough if you do. To get font handling working
|
||||
/// for these cases you have to provide two additional parameters inside the
|
||||
/// `nk_user_font`. First a texture atlas handle used to draw text as subimages
|
||||
/// of a bigger font atlas texture and a callback to query a character's glyph
|
||||
/// information (offset, size, ...). So it is still possible to provide your own
|
||||
/// font and use the vertex buffer output.
|
||||
///
|
||||
/// ```c
|
||||
/// float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
|
||||
/// {
|
||||
/// your_font_type *type = handle.ptr;
|
||||
/// float text_width = ...;
|
||||
/// return text_width;
|
||||
/// }
|
||||
/// void query_your_font_glyph(nk_handle handle, float font_height, struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
|
||||
/// {
|
||||
/// your_font_type *type = handle.ptr;
|
||||
/// glyph.width = ...;
|
||||
/// glyph.height = ...;
|
||||
/// glyph.xadvance = ...;
|
||||
/// glyph.uv[0].x = ...;
|
||||
/// glyph.uv[0].y = ...;
|
||||
/// glyph.uv[1].x = ...;
|
||||
/// glyph.uv[1].y = ...;
|
||||
/// glyph.offset.x = ...;
|
||||
/// glyph.offset.y = ...;
|
||||
/// }
|
||||
///
|
||||
/// struct nk_user_font font;
|
||||
/// font.userdata.ptr = &your_font_class_or_struct;
|
||||
/// font.height = your_font_height;
|
||||
/// font.width = your_text_width_calculation;
|
||||
/// font.query = query_your_font_glyph;
|
||||
/// font.texture.id = your_font_texture;
|
||||
///
|
||||
/// struct nk_context ctx;
|
||||
/// nk_init_default(&ctx, &font);
|
||||
/// ```
|
||||
///
|
||||
/// #### Nuklear font baker
|
||||
///
|
||||
/// The final approach if you do not have a font handling functionality or don't
|
||||
/// want to use it in this library is by using the optional font baker.
|
||||
/// The font baker APIs can be used to create a font plus font atlas texture
|
||||
/// and can be used with or without the vertex buffer output.
|
||||
///
|
||||
/// It still uses the `nk_user_font` struct and the two different approaches
|
||||
/// previously stated still work. The font baker is not located inside
|
||||
/// `nk_context` like all other systems since it can be understood as more of
|
||||
/// an extension to nuklear and does not really depend on any `nk_context` state.
|
||||
///
|
||||
/// Font baker need to be initialized first by one of the nk_font_atlas_init_xxx
|
||||
/// functions. If you don't care about memory just call the default version
|
||||
/// `nk_font_atlas_init_default` which will allocate all memory from the standard library.
|
||||
/// If you want to control memory allocation but you don't care if the allocated
|
||||
/// memory is temporary and therefore can be freed directly after the baking process
|
||||
/// is over or permanent you can call `nk_font_atlas_init`.
|
||||
///
|
||||
/// After successfully initializing the font baker you can add Truetype(.ttf) fonts from
|
||||
/// different sources like memory or from file by calling one of the `nk_font_atlas_add_xxx`.
|
||||
/// functions. Adding font will permanently store each font, font config and ttf memory block(!)
|
||||
/// inside the font atlas and allows to reuse the font atlas. If you don't want to reuse
|
||||
/// the font baker by for example adding additional fonts you can call
|
||||
/// `nk_font_atlas_cleanup` after the baking process is over (after calling nk_font_atlas_end).
|
||||
///
|
||||
/// As soon as you added all fonts you wanted you can now start the baking process
|
||||
/// for every selected glyph to image by calling `nk_font_atlas_bake`.
|
||||
/// The baking process returns image memory, width and height which can be used to
|
||||
/// either create your own image object or upload it to any graphics library.
|
||||
/// No matter which case you finally have to call `nk_font_atlas_end` which
|
||||
/// will free all temporary memory including the font atlas image so make sure
|
||||
/// you created our texture beforehand. `nk_font_atlas_end` requires a handle
|
||||
/// to your font texture or object and optionally fills a `struct nk_draw_null_texture`
|
||||
/// which can be used for the optional vertex output. If you don't want it just
|
||||
/// set the argument to `NULL`.
|
||||
///
|
||||
/// At this point you are done and if you don't want to reuse the font atlas you
|
||||
/// can call `nk_font_atlas_cleanup` to free all truetype blobs and configuration
|
||||
/// memory. Finally if you don't use the font atlas and any of it's fonts anymore
|
||||
/// you need to call `nk_font_atlas_clear` to free all memory still being used.
|
||||
///
|
||||
/// ```c
|
||||
/// struct nk_font_atlas atlas;
|
||||
/// nk_font_atlas_init_default(&atlas);
|
||||
/// nk_font_atlas_begin(&atlas);
|
||||
/// nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, 0);
|
||||
/// nk_font *font2 = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font2.ttf", 16, 0);
|
||||
/// const void* img = nk_font_atlas_bake(&atlas, &img_width, &img_height, NK_FONT_ATLAS_RGBA32);
|
||||
/// nk_font_atlas_end(&atlas, nk_handle_id(texture), 0);
|
||||
///
|
||||
/// struct nk_context ctx;
|
||||
/// nk_init_default(&ctx, &font->handle);
|
||||
/// while (1) {
|
||||
///
|
||||
/// }
|
||||
/// nk_font_atlas_clear(&atlas);
|
||||
/// ```
|
||||
/// The font baker API is probably the most complex API inside this library and
|
||||
/// I would suggest reading some of my examples `example/` to get a grip on how
|
||||
/// to use the font atlas. There are a number of details I left out. For example
|
||||
/// how to merge fonts, configure a font with `nk_font_config` to use other languages,
|
||||
/// use another texture coordinate format and a lot more:
|
||||
///
|
||||
/// ```c
|
||||
/// struct nk_font_config cfg = nk_font_config(font_pixel_height);
|
||||
/// cfg.merge_mode = nk_false or nk_true;
|
||||
/// cfg.range = nk_font_korean_glyph_ranges();
|
||||
/// cfg.coord_type = NK_COORD_PIXEL;
|
||||
/// nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, &cfg);
|
||||
/// ```
|
||||
*/
|
||||
struct nk_user_font_glyph;
|
||||
typedef float(*nk_text_width_f)(nk_handle, float h, const char*, int len);
|
||||
|
@ -3902,33 +3908,34 @@ NK_API void nk_font_atlas_clear(struct nk_font_atlas*);
|
|||
* MEMORY BUFFER
|
||||
*
|
||||
* ===============================================================*/
|
||||
/* A basic (double)-buffer with linear allocation and resetting as only
|
||||
freeing policy. The buffer's main purpose is to control all memory management
|
||||
inside the GUI toolkit and still leave memory control as much as possible in
|
||||
the hand of the user while also making sure the library is easy to use if
|
||||
not as much control is needed.
|
||||
In general all memory inside this library can be provided from the user in
|
||||
three different ways.
|
||||
|
||||
The first way and the one providing most control is by just passing a fixed
|
||||
size memory block. In this case all control lies in the hand of the user
|
||||
since he can exactly control where the memory comes from and how much memory
|
||||
the library should consume. Of course using the fixed size API removes the
|
||||
ability to automatically resize a buffer if not enough memory is provided so
|
||||
you have to take over the resizing. While being a fixed sized buffer sounds
|
||||
quite limiting, it is very effective in this library since the actual memory
|
||||
consumption is quite stable and has a fixed upper bound for a lot of cases.
|
||||
|
||||
If you don't want to think about how much memory the library should allocate
|
||||
at all time or have a very dynamic UI with unpredictable memory consumption
|
||||
habits but still want control over memory allocation you can use the dynamic
|
||||
allocator based API. The allocator consists of two callbacks for allocating
|
||||
and freeing memory and optional userdata so you can plugin your own allocator.
|
||||
|
||||
The final and easiest way can be used by defining
|
||||
NK_INCLUDE_DEFAULT_ALLOCATOR which uses the standard library memory
|
||||
allocation functions malloc and free and takes over complete control over
|
||||
memory in this library.
|
||||
/*/// ### Memory Buffer
|
||||
/// A basic (double)-buffer with linear allocation and resetting as only
|
||||
/// freeing policy. The buffer's main purpose is to control all memory management
|
||||
/// inside the GUI toolkit and still leave memory control as much as possible in
|
||||
/// the hand of the user while also making sure the library is easy to use if
|
||||
/// not as much control is needed.
|
||||
/// In general all memory inside this library can be provided from the user in
|
||||
/// three different ways.
|
||||
///
|
||||
/// The first way and the one providing most control is by just passing a fixed
|
||||
/// size memory block. In this case all control lies in the hand of the user
|
||||
/// since he can exactly control where the memory comes from and how much memory
|
||||
/// the library should consume. Of course using the fixed size API removes the
|
||||
/// ability to automatically resize a buffer if not enough memory is provided so
|
||||
/// you have to take over the resizing. While being a fixed sized buffer sounds
|
||||
/// quite limiting, it is very effective in this library since the actual memory
|
||||
/// consumption is quite stable and has a fixed upper bound for a lot of cases.
|
||||
///
|
||||
/// If you don't want to think about how much memory the library should allocate
|
||||
/// at all time or have a very dynamic UI with unpredictable memory consumption
|
||||
/// habits but still want control over memory allocation you can use the dynamic
|
||||
/// allocator based API. The allocator consists of two callbacks for allocating
|
||||
/// and freeing memory and optional userdata so you can plugin your own allocator.
|
||||
///
|
||||
/// The final and easiest way can be used by defining
|
||||
/// NK_INCLUDE_DEFAULT_ALLOCATOR which uses the standard library memory
|
||||
/// allocation functions malloc and free and takes over complete control over
|
||||
/// memory in this library.
|
||||
*/
|
||||
struct nk_memory_status {
|
||||
void *memory;
|
||||
|
@ -4053,28 +4060,29 @@ NK_API int nk_str_len_char(struct nk_str*);
|
|||
* TEXT EDITOR
|
||||
*
|
||||
* ===============================================================*/
|
||||
/* Editing text in this library is handled by either `nk_edit_string` or
|
||||
* `nk_edit_buffer`. But like almost everything in this library there are multiple
|
||||
* ways of doing it and a balance between control and ease of use with memory
|
||||
* as well as functionality controlled by flags.
|
||||
*
|
||||
* This library generally allows three different levels of memory control:
|
||||
* First of is the most basic way of just providing a simple char array with
|
||||
* string length. This method is probably the easiest way of handling simple
|
||||
* user text input. Main upside is complete control over memory while the biggest
|
||||
* downside in comparison with the other two approaches is missing undo/redo.
|
||||
*
|
||||
* For UIs that require undo/redo the second way was created. It is based on
|
||||
* a fixed size nk_text_edit struct, which has an internal undo/redo stack.
|
||||
* This is mainly useful if you want something more like a text editor but don't want
|
||||
* to have a dynamically growing buffer.
|
||||
*
|
||||
* The final way is using a dynamically growing nk_text_edit struct, which
|
||||
* has both a default version if you don't care where memory comes from and an
|
||||
* allocator version if you do. While the text editor is quite powerful for its
|
||||
* complexity I would not recommend editing gigabytes of data with it.
|
||||
* It is rather designed for uses cases which make sense for a GUI library not for
|
||||
* an full blown text editor.
|
||||
/*/// ### Text Editor
|
||||
/// Editing text in this library is handled by either `nk_edit_string` or
|
||||
/// `nk_edit_buffer`. But like almost everything in this library there are multiple
|
||||
/// ways of doing it and a balance between control and ease of use with memory
|
||||
/// as well as functionality controlled by flags.
|
||||
///
|
||||
/// This library generally allows three different levels of memory control:
|
||||
/// First of is the most basic way of just providing a simple char array with
|
||||
/// string length. This method is probably the easiest way of handling simple
|
||||
/// user text input. Main upside is complete control over memory while the biggest
|
||||
/// downside in comparison with the other two approaches is missing undo/redo.
|
||||
///
|
||||
/// For UIs that require undo/redo the second way was created. It is based on
|
||||
/// a fixed size nk_text_edit struct, which has an internal undo/redo stack.
|
||||
/// This is mainly useful if you want something more like a text editor but don't want
|
||||
/// to have a dynamically growing buffer.
|
||||
///
|
||||
/// The final way is using a dynamically growing nk_text_edit struct, which
|
||||
/// has both a default version if you don't care where memory comes from and an
|
||||
/// allocator version if you do. While the text editor is quite powerful for its
|
||||
/// complexity I would not recommend editing gigabytes of data with it.
|
||||
/// It is rather designed for uses cases which make sense for a GUI library not for
|
||||
/// an full blown text editor.
|
||||
*/
|
||||
#ifndef NK_TEXTEDIT_UNDOSTATECOUNT
|
||||
#define NK_TEXTEDIT_UNDOSTATECOUNT 99
|
||||
|
@ -4168,49 +4176,52 @@ NK_API void nk_textedit_redo(struct nk_text_edit*);
|
|||
* DRAWING
|
||||
*
|
||||
* ===============================================================*/
|
||||
/* This library was designed to be render backend agnostic so it does
|
||||
not draw anything to screen. Instead all drawn shapes, widgets
|
||||
are made of, are buffered into memory and make up a command queue.
|
||||
Each frame therefore fills the command buffer with draw commands
|
||||
that then need to be executed by the user and his own render backend.
|
||||
After that the command buffer needs to be cleared and a new frame can be
|
||||
started. It is probably important to note that the command buffer is the main
|
||||
drawing API and the optional vertex buffer API only takes this format and
|
||||
converts it into a hardware accessible format.
|
||||
|
||||
To use the command queue to draw your own widgets you can access the
|
||||
command buffer of each window by calling `nk_window_get_canvas` after
|
||||
previously having called `nk_begin`:
|
||||
|
||||
void draw_red_rectangle_widget(struct nk_context *ctx)
|
||||
{
|
||||
struct nk_command_buffer *canvas;
|
||||
struct nk_input *input = &ctx->input;
|
||||
canvas = nk_window_get_canvas(ctx);
|
||||
|
||||
struct nk_rect space;
|
||||
enum nk_widget_layout_states state;
|
||||
state = nk_widget(&space, ctx);
|
||||
if (!state) return;
|
||||
|
||||
if (state != NK_WIDGET_ROM)
|
||||
update_your_widget_by_user_input(...);
|
||||
nk_fill_rect(canvas, space, 0, nk_rgb(255,0,0));
|
||||
}
|
||||
|
||||
if (nk_begin(...)) {
|
||||
nk_layout_row_dynamic(ctx, 25, 1);
|
||||
draw_red_rectangle_widget(ctx);
|
||||
}
|
||||
nk_end(..)
|
||||
|
||||
Important to know if you want to create your own widgets is the `nk_widget`
|
||||
call. It allocates space on the panel reserved for this widget to be used,
|
||||
but also returns the state of the widget space. If your widget is not seen and does
|
||||
not have to be updated it is '0' and you can just return. If it only has
|
||||
to be drawn the state will be `NK_WIDGET_ROM` otherwise you can do both
|
||||
update and draw your widget. The reason for separating is to only draw and
|
||||
update what is actually necessary which is crucial for performance.
|
||||
/*/// ### Drawing
|
||||
/// This library was designed to be render backend agnostic so it does
|
||||
/// not draw anything to screen. Instead all drawn shapes, widgets
|
||||
/// are made of, are buffered into memory and make up a command queue.
|
||||
/// Each frame therefore fills the command buffer with draw commands
|
||||
/// that then need to be executed by the user and his own render backend.
|
||||
/// After that the command buffer needs to be cleared and a new frame can be
|
||||
/// started. It is probably important to note that the command buffer is the main
|
||||
/// drawing API and the optional vertex buffer API only takes this format and
|
||||
/// converts it into a hardware accessible format.
|
||||
///
|
||||
/// To use the command queue to draw your own widgets you can access the
|
||||
/// command buffer of each window by calling `nk_window_get_canvas` after
|
||||
/// previously having called `nk_begin`:
|
||||
///
|
||||
/// ```c
|
||||
/// void draw_red_rectangle_widget(struct nk_context *ctx)
|
||||
/// {
|
||||
/// struct nk_command_buffer *canvas;
|
||||
/// struct nk_input *input = &ctx->input;
|
||||
/// canvas = nk_window_get_canvas(ctx);
|
||||
///
|
||||
/// struct nk_rect space;
|
||||
/// enum nk_widget_layout_states state;
|
||||
/// state = nk_widget(&space, ctx);
|
||||
/// if (!state) return;
|
||||
///
|
||||
/// if (state != NK_WIDGET_ROM)
|
||||
/// update_your_widget_by_user_input(...);
|
||||
/// nk_fill_rect(canvas, space, 0, nk_rgb(255,0,0));
|
||||
/// }
|
||||
///
|
||||
/// if (nk_begin(...)) {
|
||||
/// nk_layout_row_dynamic(ctx, 25, 1);
|
||||
/// draw_red_rectangle_widget(ctx);
|
||||
/// }
|
||||
/// nk_end(..)
|
||||
///
|
||||
/// ```
|
||||
/// Important to know if you want to create your own widgets is the `nk_widget`
|
||||
/// call. It allocates space on the panel reserved for this widget to be used,
|
||||
/// but also returns the state of the widget space. If your widget is not seen and does
|
||||
/// not have to be updated it is '0' and you can just return. If it only has
|
||||
/// to be drawn the state will be `NK_WIDGET_ROM` otherwise you can do both
|
||||
/// update and draw your widget. The reason for separating is to only draw and
|
||||
/// update what is actually necessary which is crucial for performance.
|
||||
*/
|
||||
enum nk_command_type {
|
||||
NK_COMMAND_NOP,
|
||||
|
@ -4474,6 +4485,7 @@ struct nk_input {
|
|||
|
||||
NK_API nk_bool nk_input_has_mouse_click(const struct nk_input*, enum nk_buttons);
|
||||
NK_API nk_bool nk_input_has_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
|
||||
NK_API nk_bool nk_input_has_mouse_click_in_button_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
|
||||
NK_API nk_bool nk_input_has_mouse_click_down_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect, nk_bool down);
|
||||
NK_API nk_bool nk_input_is_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
|
||||
NK_API nk_bool nk_input_is_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id, struct nk_rect b, nk_bool down);
|
||||
|
@ -4494,18 +4506,19 @@ NK_API nk_bool nk_input_is_key_down(const struct nk_input*, enum nk_keys);
|
|||
*
|
||||
* ===============================================================*/
|
||||
#ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
|
||||
/* The optional vertex buffer draw list provides a 2D drawing context
|
||||
with antialiasing functionality which takes basic filled or outlined shapes
|
||||
or a path and outputs vertexes, elements and draw commands.
|
||||
The actual draw list API is not required to be used directly while using this
|
||||
library since converting the default library draw command output is done by
|
||||
just calling `nk_convert` but I decided to still make this library accessible
|
||||
since it can be useful.
|
||||
|
||||
The draw list is based on a path buffering and polygon and polyline
|
||||
rendering API which allows a lot of ways to draw 2D content to screen.
|
||||
In fact it is probably more powerful than needed but allows even more crazy
|
||||
things than this library provides by default.
|
||||
/* ### Draw List
|
||||
/// The optional vertex buffer draw list provides a 2D drawing context
|
||||
/// with antialiasing functionality which takes basic filled or outlined shapes
|
||||
/// or a path and outputs vertexes, elements and draw commands.
|
||||
/// The actual draw list API is not required to be used directly while using this
|
||||
/// library since converting the default library draw command output is done by
|
||||
/// just calling `nk_convert` but I decided to still make this library accessible
|
||||
/// since it can be useful.
|
||||
///
|
||||
/// The draw list is based on a path buffering and polygon and polyline
|
||||
/// rendering API which allows a lot of ways to draw 2D content to screen.
|
||||
/// In fact it is probably more powerful than needed but allows even more crazy
|
||||
/// things than this library provides by default.
|
||||
*/
|
||||
#ifdef NK_UINT_DRAW_INDEX
|
||||
typedef nk_uint nk_draw_index;
|
||||
|
@ -5283,27 +5296,28 @@ struct nk_window {
|
|||
/*==============================================================
|
||||
* STACK
|
||||
* =============================================================*/
|
||||
/* The style modifier stack can be used to temporarily change a
|
||||
* property inside `nk_style`. For example if you want a special
|
||||
* red button you can temporarily push the old button color onto a stack
|
||||
* draw the button with a red color and then you just pop the old color
|
||||
* back from the stack:
|
||||
*
|
||||
* nk_style_push_style_item(ctx, &ctx->style.button.normal, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
* nk_style_push_style_item(ctx, &ctx->style.button.hover, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
* nk_style_push_style_item(ctx, &ctx->style.button.active, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
* nk_style_push_vec2(ctx, &cx->style.button.padding, nk_vec2(2,2));
|
||||
*
|
||||
* nk_button(...);
|
||||
*
|
||||
* nk_style_pop_style_item(ctx);
|
||||
* nk_style_pop_style_item(ctx);
|
||||
* nk_style_pop_style_item(ctx);
|
||||
* nk_style_pop_vec2(ctx);
|
||||
*
|
||||
* Nuklear has a stack for style_items, float properties, vector properties,
|
||||
* flags, colors, fonts and for button_behavior. Each has it's own fixed size stack
|
||||
* which can be changed at compile time.
|
||||
/*/// ### Stack
|
||||
/// The style modifier stack can be used to temporarily change a
|
||||
/// property inside `nk_style`. For example if you want a special
|
||||
/// red button you can temporarily push the old button color onto a stack
|
||||
/// draw the button with a red color and then you just pop the old color
|
||||
/// back from the stack:
|
||||
///
|
||||
/// nk_style_push_style_item(ctx, &ctx->style.button.normal, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
/// nk_style_push_style_item(ctx, &ctx->style.button.hover, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
/// nk_style_push_style_item(ctx, &ctx->style.button.active, nk_style_item_color(nk_rgb(255,0,0)));
|
||||
/// nk_style_push_vec2(ctx, &cx->style.button.padding, nk_vec2(2,2));
|
||||
///
|
||||
/// nk_button(...);
|
||||
///
|
||||
/// nk_style_pop_style_item(ctx);
|
||||
/// nk_style_pop_style_item(ctx);
|
||||
/// nk_style_pop_style_item(ctx);
|
||||
/// nk_style_pop_vec2(ctx);
|
||||
///
|
||||
/// Nuklear has a stack for style_items, float properties, vector properties,
|
||||
/// flags, colors, fonts and for button_behavior. Each has it's own fixed size stack
|
||||
/// which can be changed at compile time.
|
||||
*/
|
||||
#ifndef NK_BUTTON_BEHAVIOR_STACK_SIZE
|
||||
#define NK_BUTTON_BEHAVIOR_STACK_SIZE 8
|
||||
|
|
|
@ -70,7 +70,7 @@ nk_button_behavior(nk_flags *state, struct nk_rect r,
|
|||
*state = NK_WIDGET_STATE_HOVERED;
|
||||
if (nk_input_is_mouse_down(i, NK_BUTTON_LEFT))
|
||||
*state = NK_WIDGET_STATE_ACTIVE;
|
||||
if (nk_input_has_mouse_click_in_rect(i, NK_BUTTON_LEFT, r)) {
|
||||
if (nk_input_has_mouse_click_in_button_rect(i, NK_BUTTON_LEFT, r)) {
|
||||
ret = (behavior != NK_BUTTON_DEFAULT) ?
|
||||
nk_input_is_mouse_down(i, NK_BUTTON_LEFT):
|
||||
#ifdef NK_BUTTON_TRIGGER_ON_RELEASE
|
||||
|
|
|
@ -85,7 +85,7 @@ nk_font_chinese_glyph_ranges(void)
|
|||
0x3000, 0x30FF,
|
||||
0x31F0, 0x31FF,
|
||||
0xFF00, 0xFFEF,
|
||||
0x4e00, 0x9FAF,
|
||||
0x4E00, 0x9FAF,
|
||||
0
|
||||
};
|
||||
return ranges;
|
||||
|
@ -194,7 +194,7 @@ nk_font_bake_pack(struct nk_font_baker *baker,
|
|||
struct stbtt_fontinfo *font_info = &baker->build[i++].info;
|
||||
font_info->userdata = alloc;
|
||||
|
||||
if (!stbtt_InitFont(font_info, (const unsigned char*)it->ttf_blob, 0))
|
||||
if (!stbtt_InitFont(font_info, (const unsigned char*)it->ttf_blob, stbtt_GetFontOffsetForIndex((const unsigned char*)it->ttf_blob, 0)))
|
||||
return nk_false;
|
||||
} while ((it = it->n) != config_iter);
|
||||
}
|
||||
|
@ -354,7 +354,6 @@ nk_font_bake(struct nk_font_baker *baker, void *image_memory, int width, int hei
|
|||
|
||||
/* query glyph bounds from stb_truetype */
|
||||
const stbtt_packedchar *pc = &range->chardata_for_range[char_idx];
|
||||
if (!pc->x0 && !pc->x1 && !pc->y0 && !pc->y1) continue;
|
||||
codepoint = (nk_rune)(range->first_unicode_codepoint_in_range + char_idx);
|
||||
stbtt_GetPackedQuad(range->chardata_for_range, (int)width,
|
||||
(int)height, char_idx, &dummy_x, &dummy_y, &q, 0);
|
||||
|
@ -1276,20 +1275,20 @@ failed:
|
|||
}
|
||||
NK_API void
|
||||
nk_font_atlas_end(struct nk_font_atlas *atlas, nk_handle texture,
|
||||
struct nk_draw_null_texture *null)
|
||||
struct nk_draw_null_texture *tex_null)
|
||||
{
|
||||
int i = 0;
|
||||
struct nk_font *font_iter;
|
||||
NK_ASSERT(atlas);
|
||||
if (!atlas) {
|
||||
if (!null) return;
|
||||
null->texture = texture;
|
||||
null->uv = nk_vec2(0.5f,0.5f);
|
||||
if (!tex_null) return;
|
||||
tex_null->texture = texture;
|
||||
tex_null->uv = nk_vec2(0.5f,0.5f);
|
||||
}
|
||||
if (null) {
|
||||
null->texture = texture;
|
||||
null->uv.x = (atlas->custom.x + 0.5f)/(float)atlas->tex_width;
|
||||
null->uv.y = (atlas->custom.y + 0.5f)/(float)atlas->tex_height;
|
||||
if (tex_null) {
|
||||
tex_null->texture = texture;
|
||||
tex_null->uv.x = (atlas->custom.x + 0.5f)/(float)atlas->tex_width;
|
||||
tex_null->uv.y = (atlas->custom.y + 0.5f)/(float)atlas->tex_height;
|
||||
}
|
||||
for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
|
||||
font_iter->texture = texture;
|
||||
|
|
|
@ -83,6 +83,10 @@ nk_input_button(struct nk_context *ctx, enum nk_buttons id, int x, int y, nk_boo
|
|||
btn->clicked_pos.y = (float)y;
|
||||
btn->down = down;
|
||||
btn->clicked++;
|
||||
|
||||
/* Fix Click-Drag for touch events. */
|
||||
in->mouse.delta.x = 0;
|
||||
in->mouse.delta.y = 0;
|
||||
#ifdef NK_BUTTON_TRIGGER_ON_RELEASE
|
||||
if (down == 1 && id == NK_BUTTON_LEFT)
|
||||
{
|
||||
|
@ -146,6 +150,17 @@ nk_input_has_mouse_click(const struct nk_input *i, enum nk_buttons id)
|
|||
NK_API nk_bool
|
||||
nk_input_has_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
|
||||
struct nk_rect b)
|
||||
{
|
||||
const struct nk_mouse_button *btn;
|
||||
if (!i) return nk_false;
|
||||
btn = &i->mouse.buttons[id];
|
||||
if (!NK_INBOX(btn->clicked_pos.x,btn->clicked_pos.y,b.x,b.y,b.w,b.h))
|
||||
return nk_false;
|
||||
return nk_true;
|
||||
}
|
||||
NK_API nk_bool
|
||||
nk_input_has_mouse_click_in_button_rect(const struct nk_input *i, enum nk_buttons id,
|
||||
struct nk_rect b)
|
||||
{
|
||||
const struct nk_mouse_button *btn;
|
||||
if (!i) return nk_false;
|
||||
|
|
|
@ -6,32 +6,33 @@
|
|||
* MATH
|
||||
*
|
||||
* ===============================================================*/
|
||||
/* Since nuklear is supposed to work on all systems providing floating point
|
||||
math without any dependencies I also had to implement my own math functions
|
||||
for sqrt, sin and cos. Since the actual highly accurate implementations for
|
||||
the standard library functions are quite complex and I do not need high
|
||||
precision for my use cases I use approximations.
|
||||
|
||||
Sqrt
|
||||
----
|
||||
For square root nuklear uses the famous fast inverse square root:
|
||||
https://en.wikipedia.org/wiki/Fast_inverse_square_root with
|
||||
slightly tweaked magic constant. While on today's hardware it is
|
||||
probably not faster it is still fast and accurate enough for
|
||||
nuklear's use cases. IMPORTANT: this requires float format IEEE 754
|
||||
|
||||
Sine/Cosine
|
||||
-----------
|
||||
All constants inside both function are generated Remez's minimax
|
||||
approximations for value range 0...2*PI. The reason why I decided to
|
||||
approximate exactly that range is that nuklear only needs sine and
|
||||
cosine to generate circles which only requires that exact range.
|
||||
In addition I used Remez instead of Taylor for additional precision:
|
||||
www.lolengine.net/blog/2011/12/21/better-function-approximations.
|
||||
|
||||
The tool I used to generate constants for both sine and cosine
|
||||
(it can actually approximate a lot more functions) can be
|
||||
found here: www.lolengine.net/wiki/oss/lolremez
|
||||
/*/// ### Math
|
||||
/// Since nuklear is supposed to work on all systems providing floating point
|
||||
/// math without any dependencies I also had to implement my own math functions
|
||||
/// for sqrt, sin and cos. Since the actual highly accurate implementations for
|
||||
/// the standard library functions are quite complex and I do not need high
|
||||
/// precision for my use cases I use approximations.
|
||||
///
|
||||
/// Sqrt
|
||||
/// ----
|
||||
/// For square root nuklear uses the famous fast inverse square root:
|
||||
/// https://en.wikipedia.org/wiki/Fast_inverse_square_root with
|
||||
/// slightly tweaked magic constant. While on today's hardware it is
|
||||
/// probably not faster it is still fast and accurate enough for
|
||||
/// nuklear's use cases. IMPORTANT: this requires float format IEEE 754
|
||||
///
|
||||
/// Sine/Cosine
|
||||
/// -----------
|
||||
/// All constants inside both function are generated Remez's minimax
|
||||
/// approximations for value range 0...2*PI. The reason why I decided to
|
||||
/// approximate exactly that range is that nuklear only needs sine and
|
||||
/// cosine to generate circles which only requires that exact range.
|
||||
/// In addition I used Remez instead of Taylor for additional precision:
|
||||
/// www.lolengine.net/blog/2011/12/21/better-function-approximations.
|
||||
///
|
||||
/// The tool I used to generate constants for both sine and cosine
|
||||
/// (it can actually approximate a lot more functions) can be
|
||||
/// found here: www.lolengine.net/wiki/oss/lolremez
|
||||
*/
|
||||
#ifndef NK_INV_SQRT
|
||||
#define NK_INV_SQRT nk_inv_sqrt
|
||||
|
|
|
@ -64,7 +64,6 @@ nk_str_append_text_utf8(struct nk_str *str, const char *text, int len)
|
|||
NK_API int
|
||||
nk_str_append_str_utf8(struct nk_str *str, const char *text)
|
||||
{
|
||||
int runes = 0;
|
||||
int byte_len = 0;
|
||||
int num_runes = 0;
|
||||
int glyph_len = 0;
|
||||
|
@ -78,7 +77,7 @@ nk_str_append_str_utf8(struct nk_str *str, const char *text)
|
|||
num_runes++;
|
||||
}
|
||||
nk_str_append_text_char(str, text, byte_len);
|
||||
return runes;
|
||||
return num_runes;
|
||||
}
|
||||
NK_API int
|
||||
nk_str_append_text_runes(struct nk_str *str, const nk_rune *text, int len)
|
||||
|
@ -193,7 +192,6 @@ nk_str_insert_text_utf8(struct nk_str *str, int pos, const char *text, int len)
|
|||
NK_API int
|
||||
nk_str_insert_str_utf8(struct nk_str *str, int pos, const char *text)
|
||||
{
|
||||
int runes = 0;
|
||||
int byte_len = 0;
|
||||
int num_runes = 0;
|
||||
int glyph_len = 0;
|
||||
|
@ -207,7 +205,7 @@ nk_str_insert_str_utf8(struct nk_str *str, int pos, const char *text)
|
|||
num_runes++;
|
||||
}
|
||||
nk_str_insert_at_rune(str, pos, text, byte_len);
|
||||
return runes;
|
||||
return num_runes;
|
||||
}
|
||||
NK_API int
|
||||
nk_str_insert_text_runes(struct nk_str *str, int pos, const nk_rune *runes, int len)
|
||||
|
|
|
@ -394,7 +394,7 @@ nk_textedit_text(struct nk_text_edit *state, const char *text, int total_len)
|
|||
text+text_len, 1))
|
||||
{
|
||||
nk_textedit_makeundo_insert(state, state->cursor, 1);
|
||||
++state->cursor;
|
||||
state->cursor = NK_MIN(state->cursor + 1, state->string.len);
|
||||
state->has_preferred_x = 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -177,7 +177,7 @@ nk_draw_list_add_clip(struct nk_draw_list *list, struct nk_rect rect)
|
|||
NK_ASSERT(list);
|
||||
if (!list) return;
|
||||
if (!list->cmd_count) {
|
||||
nk_draw_list_push_command(list, rect, list->config.null.texture);
|
||||
nk_draw_list_push_command(list, rect, list->config.tex_null.texture);
|
||||
} else {
|
||||
struct nk_draw_command *prev = nk_draw_list_command_last(list);
|
||||
if (prev->elem_count == 0)
|
||||
|
@ -532,7 +532,7 @@ nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *p
|
|||
|
||||
/* fill vertices */
|
||||
for (i = 0; i < points_count; ++i) {
|
||||
const struct nk_vec2 uv = list->config.null.uv;
|
||||
const struct nk_vec2 uv = list->config.tex_null.uv;
|
||||
vtx = nk_draw_vertex(vtx, &list->config, points[i], uv, col);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+0], uv, col_trans);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+1], uv, col_trans);
|
||||
|
@ -597,7 +597,7 @@ nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *p
|
|||
|
||||
/* add vertices */
|
||||
for (i = 0; i < points_count; ++i) {
|
||||
const struct nk_vec2 uv = list->config.null.uv;
|
||||
const struct nk_vec2 uv = list->config.tex_null.uv;
|
||||
vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+0], uv, col_trans);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+1], uv, col);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+2], uv, col);
|
||||
|
@ -618,7 +618,7 @@ nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *p
|
|||
|
||||
for (i1 = 0; i1 < count; ++i1) {
|
||||
float dx, dy;
|
||||
const struct nk_vec2 uv = list->config.null.uv;
|
||||
const struct nk_vec2 uv = list->config.tex_null.uv;
|
||||
const nk_size i2 = ((i1+1) == points_count) ? 0 : i1 + 1;
|
||||
const struct nk_vec2 p1 = points[i1];
|
||||
const struct nk_vec2 p2 = points[i2];
|
||||
|
@ -728,7 +728,7 @@ nk_draw_list_fill_poly_convex(struct nk_draw_list *list,
|
|||
|
||||
/* add vertices + indexes */
|
||||
for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
|
||||
const struct nk_vec2 uv = list->config.null.uv;
|
||||
const struct nk_vec2 uv = list->config.tex_null.uv;
|
||||
struct nk_vec2 n0 = normals[i0];
|
||||
struct nk_vec2 n1 = normals[i1];
|
||||
struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(n0, n1), 0.5f);
|
||||
|
@ -765,7 +765,7 @@ nk_draw_list_fill_poly_convex(struct nk_draw_list *list,
|
|||
|
||||
if (!vtx || !ids) return;
|
||||
for (i = 0; i < vtx_count; ++i)
|
||||
vtx = nk_draw_vertex(vtx, &list->config, points[i], list->config.null.uv, col);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, points[i], list->config.tex_null.uv, col);
|
||||
for (i = 2; i < points_count; ++i) {
|
||||
ids[0] = (nk_draw_index)index;
|
||||
ids[1] = (nk_draw_index)(index+ i - 1);
|
||||
|
@ -794,8 +794,8 @@ nk_draw_list_path_line_to(struct nk_draw_list *list, struct nk_vec2 pos)
|
|||
nk_draw_list_add_clip(list, nk_null_rect);
|
||||
|
||||
cmd = nk_draw_list_command_last(list);
|
||||
if (cmd && cmd->texture.ptr != list->config.null.texture.ptr)
|
||||
nk_draw_list_push_image(list, list->config.null.texture);
|
||||
if (cmd && cmd->texture.ptr != list->config.tex_null.texture.ptr)
|
||||
nk_draw_list_push_image(list, list->config.tex_null.texture);
|
||||
|
||||
points = nk_draw_list_alloc_path(list, 1);
|
||||
if (!points) return;
|
||||
|
@ -997,7 +997,7 @@ nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rec
|
|||
NK_ASSERT(list);
|
||||
if (!list) return;
|
||||
|
||||
nk_draw_list_push_image(list, list->config.null.texture);
|
||||
nk_draw_list_push_image(list, list->config.tex_null.texture);
|
||||
index = (nk_draw_index)list->vertex_count;
|
||||
vtx = nk_draw_list_alloc_vertices(list, 4);
|
||||
idx = nk_draw_list_alloc_elements(list, 6);
|
||||
|
@ -1007,10 +1007,10 @@ nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rec
|
|||
idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
|
||||
idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
|
||||
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y), list->config.null.uv, col_left);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y), list->config.null.uv, col_top);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y + rect.h), list->config.null.uv, col_right);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y + rect.h), list->config.null.uv, col_bottom);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y), list->config.tex_null.uv, col_left);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y), list->config.tex_null.uv, col_top);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y + rect.h), list->config.tex_null.uv, col_right);
|
||||
vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y + rect.h), list->config.tex_null.uv, col_bottom);
|
||||
}
|
||||
NK_API void
|
||||
nk_draw_list_fill_triangle(struct nk_draw_list *list, struct nk_vec2 a,
|
||||
|
|
Loading…
Reference in New Issue