Merge branch 'master' into xcb-cairo

This commit is contained in:
Rob Loach 2024-04-17 15:19:53 -04:00 committed by GitHub
commit 3a7f458499
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
116 changed files with 12956 additions and 3845 deletions

View File

@ -10,5 +10,5 @@ indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
[clib.json]
[**.{json,yml}]
indent_size = 2

View File

@ -6,16 +6,44 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: apt-update
run: sudo apt-get update -qq
- name: apt get glfw
run: sudo apt-get install -y --no-install-recommends libglfw3 libglfw3-dev libglew-dev
- name: build opengl2
- name: apt get demo-libs
run: sudo apt-get install -y --no-install-recommends liballegro5-dev liballegro-image5-dev liballegro-ttf5-dev libglfw3 libglfw3-dev libglew-dev libsdl2-dev libwayland-dev libx11-dev libxft-dev wayland-protocols
- name: build allegro5
run: make -C demo/allegro5
- name: build glfw_opengl2
run: make -C demo/glfw_opengl2
- name: build opengl3
- name: build glfw_opengl3
run: make -C demo/glfw_opengl3
- name: build glfw_opengl4
run: make -C demo/glfw_opengl4
# - name: build glfw_vulkan
# run: make -C demo/glfw_vulkan
- name: build sdl_opengl2
run: make -C demo/sdl_opengl2
- name: build sdl_opengl3
run: make -C demo/sdl_opengl3
- name: build sdl_opengles2
run: make -C demo/sdl_opengles2
- name: build sdl_renderer
run: make -C demo/sdl_renderer
- name: build sdl_rawfb
run: make -C demo/rawfb/sdl
- name: build wayland_rawfb
run: make -C demo/rawfb/wayland
- name: build x11
run: make -C demo/x11
- name: build x11_opengl2
run: make -C demo/x11_opengl2
- name: build x11_opengl3
run: make -C demo/x11_opengl3
- name: build x11_rawfb
run: make -C demo/rawfb/x11
- name: build x11_xft
run: make -C demo/x11_xft
- name: build example
run: make -C example

View File

@ -9,8 +9,9 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: butlerlogic/action-autotag@stable
with:
- uses: actions/checkout@v4
- uses: butlerlogic/action-autotag@1.1.2
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
with:
root: clib.json

1
.gitignore vendored
View File

@ -5,6 +5,7 @@ example/bin/*
docs/xml
docs/build
docs/src
doc/doc*
*.tmp
*.swo
*.swp

0
.gitmodules vendored
View File

View File

@ -1,6 +1,6 @@
{
"name": "nuklear",
"version": "4.10.1",
"version": "4.12.0",
"repo": "Immediate-Mode-UI/Nuklear",
"description": "A small ANSI C gui toolkit",
"keywords": ["gl", "ui", "toolkit"],

View File

@ -1,6 +1,6 @@
# Allegro v5 nuklear backend
This backend provides support for [Allegro version 5](http://liballeg.org/). It works on on all supported platforms with an OpenGL backend, including iOS and Android.
This backend provides support for [Allegro version 5](https://liballeg.github.io). It works on on all supported platforms with an OpenGL backend, including iOS and Android.
Touch support is provided by handling the first touch (ignoring any extra simultaneous touches) and emitting nuklear mouse events. nuklear will handle only the first touch like a single left-mouse click. Dragging the touch screen emits mouse-move events.

View File

@ -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)
{

View File

@ -46,6 +46,7 @@ NK_API void nk_allegro5_font_set_font(NkAllegro5Font *font);
* ===============================================================
*/
#ifdef NK_ALLEGRO5_IMPLEMENTATION
#include <stdio.h>
#ifndef NK_ALLEGRO5_TEXT_MAX
#define NK_ALLEGRO5_TEXT_MAX 256
@ -101,7 +102,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 +112,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 +474,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);
}

File diff suppressed because it is too large Load Diff

View File

@ -2,21 +2,19 @@ static int
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_bool show_menu = 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;
/* widget flags */
static nk_bool disable_widgets = nk_false;
/* popups */
static enum nk_style_header_align header_align = NK_HEADER_RIGHT;
static int show_app_about = nk_false;
static nk_bool show_app_about = nk_false;
#ifdef INCLUDE_STYLE
/* styles */
static const char* themes[] = {"Black", "White", "Red", "Blue", "Dark"};
static int current_theme = 0;
#endif
@ -24,14 +22,11 @@ overview(struct nk_context *ctx)
/* 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)
{
@ -39,7 +34,7 @@ overview(struct nk_context *ctx)
enum menu_states {MENU_DEFAULT, MENU_WINDOWS};
static nk_size mprog = 60;
static int mslider = 10;
static int mcheck = nk_true;
static nk_bool mcheck = nk_true;
nk_menubar_begin(ctx);
/* menu #1 */
@ -49,7 +44,7 @@ overview(struct nk_context *ctx)
{
static size_t prog = 40;
static int slider = 10;
static int check = nk_true;
static nk_bool check = nk_true;
nk_layout_row_dynamic(ctx, 25, 1);
if (nk_menu_item_label(ctx, "Hide", NK_TEXT_LEFT))
show_menu = nk_false;
@ -149,22 +144,30 @@ 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_checkbox_label(ctx, "Disable widgets", &disable_widgets);
nk_tree_pop(ctx);
}
if (disable_widgets)
nk_widget_disable_begin(ctx);
if (nk_tree_push(ctx, NK_TREE_TAB, "Widgets", NK_MINIMIZED))
{
enum options {A,B,C};
static int checkbox;
static int option;
static nk_bool checkbox_left_text_left;
static nk_bool checkbox_centered_text_right;
static nk_bool checkbox_right_text_right;
static nk_bool checkbox_right_text_left;
static int option_left;
static int option_right;
if (nk_tree_push(ctx, NK_TREE_NODE, "Text", NK_MINIMIZED))
{
/* Text Widgets */
@ -208,6 +211,7 @@ overview(struct nk_context *ctx)
nk_layout_row_static(ctx, 30, 100, 2);
nk_button_symbol_label(ctx, NK_SYMBOL_TRIANGLE_LEFT, "prev", NK_TEXT_RIGHT);
nk_button_symbol_label(ctx, NK_SYMBOL_TRIANGLE_RIGHT, "next", NK_TEXT_LEFT);
nk_tree_pop(ctx);
}
@ -229,13 +233,21 @@ overview(struct nk_context *ctx)
static int range_int_max = 4096;
static const float ratio[] = {120, 150};
nk_layout_row_static(ctx, 30, 100, 1);
nk_checkbox_label(ctx, "Checkbox", &checkbox);
nk_layout_row_dynamic(ctx, 0, 1);
nk_checkbox_label(ctx, "CheckLeft TextLeft", &checkbox_left_text_left);
nk_checkbox_label_align(ctx, "CheckCenter TextRight", &checkbox_centered_text_right, NK_WIDGET_ALIGN_CENTERED | NK_WIDGET_ALIGN_MIDDLE, NK_TEXT_RIGHT);
nk_checkbox_label_align(ctx, "CheckRight TextRight", &checkbox_right_text_right, NK_WIDGET_LEFT, NK_TEXT_RIGHT);
nk_checkbox_label_align(ctx, "CheckRight TextLeft", &checkbox_right_text_left, NK_WIDGET_RIGHT, NK_TEXT_LEFT);
nk_layout_row_static(ctx, 30, 80, 3);
option = nk_option_label(ctx, "optionA", option == A) ? A : option;
option = nk_option_label(ctx, "optionB", option == B) ? B : option;
option = nk_option_label(ctx, "optionC", option == C) ? C : option;
option_left = nk_option_label(ctx, "optionA", option_left == A) ? A : option_left;
option_left = nk_option_label(ctx, "optionB", option_left == B) ? B : option_left;
option_left = nk_option_label(ctx, "optionC", option_left == C) ? C : option_left;
nk_layout_row_static(ctx, 30, 80, 3);
option_right = nk_option_label_align(ctx, "optionA", option_right == A, NK_WIDGET_RIGHT, NK_TEXT_RIGHT) ? A : option_right;
option_right = nk_option_label_align(ctx, "optionB", option_right == B, NK_WIDGET_RIGHT, NK_TEXT_RIGHT) ? B : option_right;
option_right = nk_option_label_align(ctx, "optionC", option_right == C, NK_WIDGET_RIGHT, NK_TEXT_RIGHT) ? C : option_right;
nk_layout_row(ctx, NK_STATIC, 30, 2, ratio);
nk_labelf(ctx, NK_TEXT_LEFT, "Slider int");
@ -270,35 +282,28 @@ overview(struct nk_context *ctx)
if (nk_tree_push(ctx, NK_TREE_NODE, "Inactive", NK_MINIMIZED))
{
static int inactive = 1;
static nk_bool inactive = 1;
nk_layout_row_dynamic(ctx, 30, 1);
nk_checkbox_label(ctx, "Inactive", &inactive);
nk_layout_row_static(ctx, 30, 80, 1);
if (inactive) {
struct nk_style_button button;
button = ctx->style.button;
ctx->style.button.normal = nk_style_item_color(nk_rgb(40,40,40));
ctx->style.button.hover = nk_style_item_color(nk_rgb(40,40,40));
ctx->style.button.active = nk_style_item_color(nk_rgb(40,40,40));
ctx->style.button.border_color = nk_rgb(60,60,60);
ctx->style.button.text_background = nk_rgb(60,60,60);
ctx->style.button.text_normal = nk_rgb(60,60,60);
ctx->style.button.text_hover = nk_rgb(60,60,60);
ctx->style.button.text_active = nk_rgb(60,60,60);
nk_button_label(ctx, "button");
ctx->style.button = button;
} else if (nk_button_label(ctx, "button"))
nk_widget_disable_begin(ctx);
}
if (nk_button_label(ctx, "button"))
fprintf(stdout, "button pressed\n");
nk_widget_disable_end(ctx);
nk_tree_pop(ctx);
}
if (nk_tree_push(ctx, NK_TREE_NODE, "Selectable", NK_MINIMIZED))
{
if (nk_tree_push(ctx, NK_TREE_NODE, "List", NK_MINIMIZED))
{
static int selected[4] = {nk_false, nk_false, nk_true, nk_false};
static nk_bool selected[4] = {nk_false, nk_false, nk_true, nk_false};
nk_layout_row_static(ctx, 18, 100, 1);
nk_selectable_label(ctx, "Selectable", NK_TEXT_LEFT, &selected[0]);
nk_selectable_label(ctx, "Selectable", NK_TEXT_LEFT, &selected[1]);
@ -310,7 +315,7 @@ overview(struct nk_context *ctx)
if (nk_tree_push(ctx, NK_TREE_NODE, "Grid", NK_MINIMIZED))
{
int i;
static int selected[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
static nk_bool selected[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
nk_layout_row_static(ctx, 50, 50, 4);
for (i = 0; i < 16; ++i) {
if (nk_selectable_label(ctx, "Z", NK_TEXT_CENTERED, &selected[i])) {
@ -357,7 +362,7 @@ overview(struct nk_context *ctx)
*/
static float chart_selection = 8.0f;
static int current_weapon = 0;
static int check_values[5];
static nk_bool check_values[5];
static float position[3];
static struct nk_color combo_color = {130, 50, 50, 255};
static struct nk_colorf combo_color2 = {0.509f, 0.705f, 0.2f, 1.0f};
@ -618,6 +623,18 @@ overview(struct nk_context *ctx)
}
nk_tree_pop(ctx);
}
if (nk_tree_push(ctx, NK_TREE_NODE, "Horizontal Rule", NK_MINIMIZED))
{
nk_layout_row_dynamic(ctx, 12, 1);
nk_label(ctx, "Use this to subdivide spaces visually", NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 4, 1);
nk_rule_horizontal(ctx, nk_white, nk_true);
nk_layout_row_dynamic(ctx, 75, 1);
nk_label_wrap(ctx, "Best used in 'Card'-like layouts, with a bigger title font on top. Takes on the size of the previous layout definition. Rounding optional.");
nk_tree_pop(ctx);
}
nk_tree_pop(ctx);
}
@ -636,6 +653,7 @@ overview(struct nk_context *ctx)
float id = 0;
static int col_index = -1;
static int line_index = -1;
static nk_bool show_markers = nk_true;
float step = (2*3.141592654f) / 32;
int i;
@ -644,7 +662,10 @@ overview(struct nk_context *ctx)
/* line chart */
id = 0;
index = -1;
nk_layout_row_dynamic(ctx, 15, 1);
nk_checkbox_label(ctx, "Show markers", &show_markers);
nk_layout_row_dynamic(ctx, 100, 1);
ctx->style.chart.show_markers = show_markers;
if (nk_chart_begin(ctx, NK_CHART_LINES, 32, -1.0f, 1.0f)) {
for (i = 0; i < 32; ++i) {
nk_flags res = nk_chart_push(ctx, (float)cos(id));
@ -717,8 +738,8 @@ overview(struct nk_context *ctx)
if (nk_tree_push(ctx, NK_TREE_TAB, "Popup", NK_MINIMIZED))
{
static struct nk_color color = {255,0,0, 255};
static int select[4];
static int popup_active;
static nk_bool select[4];
static nk_bool popup_active;
const struct nk_input *in = &ctx->input;
struct nk_rect bounds;
@ -887,9 +908,9 @@ overview(struct nk_context *ctx)
if (nk_tree_push(ctx, NK_TREE_NODE, "Group", NK_MINIMIZED))
{
static int group_titlebar = nk_false;
static int group_border = nk_true;
static int group_no_scrollbar = nk_false;
static nk_bool group_titlebar = nk_false;
static nk_bool group_border = nk_true;
static nk_bool group_no_scrollbar = nk_false;
static int group_width = 320;
static int group_height = 200;
@ -915,7 +936,7 @@ overview(struct nk_context *ctx)
nk_layout_row_static(ctx, (float)group_height, group_width, 2);
if (nk_group_begin(ctx, "Group", group_flags)) {
int i = 0;
static int selected[16];
static nk_bool selected[16];
nk_layout_row_static(ctx, 18, 100, 1);
for (i = 0; i < 16; ++i)
nk_selectable_label(ctx, (selected[i]) ? "Selected": "Unselected", NK_TEXT_CENTERED, &selected[i]);
@ -925,11 +946,12 @@ overview(struct nk_context *ctx)
}
if (nk_tree_push(ctx, NK_TREE_NODE, "Tree", NK_MINIMIZED))
{
static int root_selected = 0;
int sel = root_selected;
static nk_bool root_selected = 0;
nk_bool sel = root_selected;
if (nk_tree_element_push(ctx, NK_TREE_NODE, "Root", NK_MINIMIZED, &sel)) {
static int selected[8];
int i = 0, node_select = selected[0];
static nk_bool selected[8];
int i = 0;
nk_bool node_select = selected[0];
if (sel != root_selected) {
root_selected = sel;
for (i = 0; i < 8; ++i)
@ -937,7 +959,7 @@ overview(struct nk_context *ctx)
}
if (nk_tree_element_push(ctx, NK_TREE_NODE, "Node", NK_MINIMIZED, &node_select)) {
int j = 0;
static int sel_nodes[4];
static nk_bool sel_nodes[4];
if (node_select != selected[0]) {
selected[0] = node_select;
for (i = 0; i < 4; ++i)
@ -1065,7 +1087,7 @@ overview(struct nk_context *ctx)
nk_layout_space_begin(ctx, NK_STATIC, 500, 64);
nk_layout_space_push(ctx, nk_rect(0,0,150,500));
if (nk_group_begin(ctx, "Group_left", NK_WINDOW_BORDER)) {
static int selected[32];
static nk_bool selected[32];
nk_layout_row_static(ctx, 18, 100, 1);
for (i = 0; i < 32; ++i)
nk_selectable_label(ctx, (selected[i]) ? "Selected": "Unselected", NK_TEXT_CENTERED, &selected[i]);
@ -1098,7 +1120,7 @@ overview(struct nk_context *ctx)
nk_layout_space_push(ctx, nk_rect(320,0,150,150));
if (nk_group_begin(ctx, "Group_right_top", NK_WINDOW_BORDER)) {
static int selected[4];
static nk_bool selected[4];
nk_layout_row_static(ctx, 18, 100, 1);
for (i = 0; i < 4; ++i)
nk_selectable_label(ctx, (selected[i]) ? "Selected": "Unselected", NK_TEXT_CENTERED, &selected[i]);
@ -1107,7 +1129,7 @@ overview(struct nk_context *ctx)
nk_layout_space_push(ctx, nk_rect(320,160,150,150));
if (nk_group_begin(ctx, "Group_right_center", NK_WINDOW_BORDER)) {
static int selected[4];
static nk_bool selected[4];
nk_layout_row_static(ctx, 18, 100, 1);
for (i = 0; i < 4; ++i)
nk_selectable_label(ctx, (selected[i]) ? "Selected": "Unselected", NK_TEXT_CENTERED, &selected[i]);
@ -1116,7 +1138,7 @@ overview(struct nk_context *ctx)
nk_layout_space_push(ctx, nk_rect(320,320,150,150));
if (nk_group_begin(ctx, "Group_right_bottom", NK_WINDOW_BORDER)) {
static int selected[4];
static nk_bool selected[4];
nk_layout_row_static(ctx, 18, 100, 1);
for (i = 0; i < 4; ++i)
nk_selectable_label(ctx, (selected[i]) ? "Selected": "Unselected", NK_TEXT_CENTERED, &selected[i]);
@ -1305,8 +1327,9 @@ overview(struct nk_context *ctx)
}
nk_tree_pop(ctx);
}
if (disable_widgets)
nk_widget_disable_end(ctx);
}
nk_end(ctx);
return !nk_window_is_closed(ctx, "Overview");
}

View File

@ -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;

View File

@ -41,6 +41,7 @@ NK_API void nk_d3d11_shutdown(void);
#define COBJMACROS
#include <d3d11.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <float.h>
@ -61,7 +62,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 +138,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;
@ -373,7 +374,7 @@ nk_d3d11_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
(void)usr;
if (IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL))
{
HGLOBAL mem = GetClipboardData(CF_UNICODETEXT);
HGLOBAL mem = GetClipboardData(CF_UNICODETEXT);
if (mem)
{
SIZE_T size = GlobalSize(mem) - 1;
@ -393,7 +394,7 @@ nk_d3d11_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
free(utf8);
}
}
GlobalUnlock(mem);
GlobalUnlock(mem);
}
}
}
@ -419,7 +420,7 @@ nk_d3d11_clipboard_copy(nk_handle usr, const char *text, int len)
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
wstr[wsize] = 0;
GlobalUnlock(mem);
SetClipboardData(CF_UNICODETEXT, mem);
SetClipboardData(CF_UNICODETEXT, mem);
}
}
}
@ -603,7 +604,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);
}

View File

@ -191,7 +191,7 @@ WindowProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
}
int main(void)
{
{
struct nk_context *ctx;
struct nk_colorf bg;
@ -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);
@ -278,7 +278,7 @@ int main(void)
/* GUI */
ctx = nk_d3d12_init(device, WINDOW_WIDTH, WINDOW_HEIGHT, MAX_VERTEX_BUFFER, MAX_INDEX_BUFFER, USER_TEXTURES);
/* 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 */
{
@ -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;

View File

@ -2,7 +2,7 @@
* Nuklear - 1.32.0 - public domain
* no warrenty implied; use at your own risk.
* authored from 2015-2016 by Micha Mettke
*
*
* D3D12 backend created by Ludwig Fuechsl (2022)
*/
/*
@ -30,7 +30,7 @@ NK_API struct nk_context *nk_d3d12_init(ID3D12Device *device, int width, int hei
NK_API void nk_d3d12_font_stash_begin(struct nk_font_atlas **atlas);
/*
* USAGE:
* - Call this function after a call to nk_d3d12_font_stash_begin(...) when all fonts have been loaded and configured.
* - Call this function after a call to nk_d3d12_font_stash_begin(...) when all fonts have been loaded and configured.
* - This function will place commands on the supplied ID3D12GraphicsCommandList.
* - This function will allocate temporary data that is required until the command list has finish executing. The temporary data can be free by calling nk_d3d12_font_stash_cleanup(...)
*/
@ -88,6 +88,7 @@ NK_API void nk_d3d12_shutdown(void);
#define COBJMACROS
#include <d3d12.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <float.h>
@ -96,7 +97,7 @@ NK_API void nk_d3d12_shutdown(void);
#include "nuklear_d3d12_vertex_shader.h"
#include "nuklear_d3d12_pixel_shader.h"
struct nk_d3d12_vertex
struct nk_d3d12_vertex
{
float position[2];
float uv[2];
@ -109,7 +110,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 +199,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);
@ -505,7 +506,7 @@ nk_d3d12_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
(void)usr;
if (IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL))
{
HGLOBAL mem = GetClipboardData(CF_UNICODETEXT);
HGLOBAL mem = GetClipboardData(CF_UNICODETEXT);
if (mem)
{
SIZE_T size = GlobalSize(mem) - 1;
@ -525,7 +526,7 @@ nk_d3d12_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
free(utf8);
}
}
GlobalUnlock(mem);
GlobalUnlock(mem);
}
}
}
@ -551,7 +552,7 @@ nk_d3d12_clipboard_copy(nk_handle usr, const char *text, int len)
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
wstr[wsize] = 0;
GlobalUnlock(mem);
SetClipboardData(CF_UNICODETEXT, mem);
SetClipboardData(CF_UNICODETEXT, mem);
}
}
}
@ -566,7 +567,7 @@ nk_d3d12_init(ID3D12Device *device, int width, int height, unsigned int max_vert
D3D12_CONSTANT_BUFFER_VIEW_DESC cbv;
D3D12_CPU_DESCRIPTOR_HANDLE cbv_handle;
/* Do plain object / ref copys */
/* Do plain object / ref copys */
d3d12.max_vertex_buffer = max_vertex_buffer;
d3d12.max_index_buffer = max_index_buffer;
d3d12.max_user_textures = max_user_textures;
@ -679,7 +680,7 @@ nk_d3d12_init(ID3D12Device *device, int width, int height, unsigned int max_vert
/* Get address of first handle (CPU and GPU) */
ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(d3d12.desc_heap, &d3d12.cpu_descriptor_handle);
ID3D12DescriptorHeap_GetGPUDescriptorHandleForHeapStart(d3d12.desc_heap, &d3d12.gpu_descriptor_handle);
/* Get addresses of vertex & index buffers */
d3d12.gpu_vertex_buffer_address = ID3D12Resource_GetGPUVirtualAddress(d3d12.vertex_buffer);
d3d12.gpu_index_buffer_address = ID3D12Resource_GetGPUVirtualAddress(d3d12.index_buffer);
@ -863,14 +864,14 @@ 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)
nk_style_set_font(&d3d12.ctx, &d3d12.atlas.default_font->handle);
}
NK_API
NK_API
void nk_d3d12_font_stash_cleanup()
{
if(d3d12.font_upload_buffer)
@ -880,7 +881,7 @@ void nk_d3d12_font_stash_cleanup()
}
}
NK_API
NK_API
nk_bool nk_d3d12_set_user_texture(unsigned int index, ID3D12Resource* texture, const D3D12_SHADER_RESOURCE_VIEW_DESC* description, nk_handle* handle_out)
{
nk_bool result = nk_false;
@ -919,7 +920,7 @@ void nk_d3d12_shutdown(void)
ID3D12Resource_Release(d3d12.const_buffer);
ID3D12Resource_Release(d3d12.index_buffer);
ID3D12Resource_Release(d3d12.vertex_buffer);
if(d3d12.font_texture)
if(d3d12.font_texture)
ID3D12Resource_Release(d3d12.font_texture);
if(d3d12.font_upload_buffer)
ID3D12Resource_Release(d3d12.font_upload_buffer);

View File

@ -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;

View File

@ -41,6 +41,7 @@ NK_API void nk_d3d9_shutdown(void);
#define COBJMACROS
#include <d3d9.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
@ -58,7 +59,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 +151,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 +249,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
@ -468,7 +469,7 @@ nk_d3d9_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
}
}
GlobalUnlock(mem);
GlobalUnlock(mem);
CloseClipboard();
}
@ -491,7 +492,7 @@ nk_d3d9_clipboard_copy(nk_handle usr, const char *text, int len)
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
wstr[wsize] = 0;
GlobalUnlock(mem);
SetClipboardData(CF_UNICODETEXT, mem);
SetClipboardData(CF_UNICODETEXT, mem);
}
}
}

View File

@ -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)

View File

@ -38,7 +38,7 @@ NK_API void nk_gdi_set_font(GdiFont *font);
* ===============================================================
*/
#ifdef NK_GDI_IMPLEMENTATION
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
@ -524,7 +524,8 @@ nk_gdi_draw_text(HDC dc, short x, short y, unsigned short w, unsigned short h,
wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
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));

View File

@ -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

View File

@ -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;
}

View File

@ -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 <string.h>
#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

View File

@ -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

View File

@ -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)

View File

@ -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) {
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();
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;
}
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))
{
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 (!OpenClipboard(NULL))
return;
wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
if (!wsize) {
SetClipboardData(CF_UNICODETEXT, mem);
}
}
}
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;
}
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
wstr[wsize] = 0;
GlobalUnlock(mem);
if (!SetClipboardData(CF_UNICODETEXT, mem))
GlobalFree(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);

View File

@ -85,7 +85,7 @@ int main(void)
/* Platform */
static GLFWwindow *win;
int width = 0, height = 0;
/* GUI */
struct nk_context *ctx;
struct nk_colorf bg;
@ -121,14 +121,20 @@ 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;
#ifdef INCLUDE_FILE_BROWSER
/* icons */
glEnable(GL_TEXTURE_2D);
@ -219,7 +225,7 @@ int main(void)
glfwSwapBuffers(win);
}
#ifdef INCLUDE_FILE_BROWSER
#ifdef INCLUDE_FILE_BROWSER
glDeleteTextures(1,(const GLuint*)&media.icons.home.handle.id);
glDeleteTextures(1,(const GLuint*)&media.icons.directory.handle.id);
glDeleteTextures(1,(const GLuint*)&media.icons.computer.handle.id);
@ -232,8 +238,8 @@ int main(void)
glDeleteTextures(1,(const GLuint*)&media.icons.movie_file.handle.id);
file_browser_free(&browser);
#endif
#endif
nk_glfw3_shutdown();
glfwTerminate();
return 0;

View File

@ -40,6 +40,8 @@ NK_API void nk_gflw3_scroll_callback(GLFWwindow *win, double xof
* ===============================================================
*/
#ifdef NK_GLFW_GL2_IMPLEMENTATION
#include <string.h>
#include <stdlib.h>
#ifndef NK_GLFW_TEXT_MAX
#define NK_GLFW_TEXT_MAX 256
@ -53,7 +55,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 +142,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 +290,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);
}

View File

@ -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;

View File

@ -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;
@ -78,6 +78,9 @@ NK_API void nk_glfw3_mouse_button_callback(GLFWwindow *win, int
* ===============================================================
*/
#ifdef NK_GLFW_GL3_IMPLEMENTATION
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#ifndef NK_GLFW_DOUBLE_CLICK_LO
#define NK_GLFW_DOUBLE_CLICK_LO 0.02
@ -240,7 +243,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 +269,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 +295,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 +406,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);
}

View File

@ -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.

View File

@ -50,6 +50,9 @@ NK_API void nk_glfw3_destroy_texture(int tex_index);
*/
#ifdef NK_GLFW_GL4_IMPLEMENTATION
#undef NK_GLFW_GL4_IMPLEMENTATION
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#ifndef NK_GLFW_TEXT_MAX
#define NK_GLFW_TEXT_MAX 256
@ -72,7 +75,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 +410,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 +556,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);
}

View File

@ -0,0 +1,4 @@
---
BasedOnStyle: LLVM
# same as .editorconfig
IndentWidth: 4

3
demo/glfw_vulkan/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
src/nuklearshaders/*.spv
src/nuklear_glfw_vulkan.h
shaders/*.spv

29
demo/glfw_vulkan/Makefile Normal file
View File

@ -0,0 +1,29 @@
# Install
BIN = demo
# Flags
CFLAGS += -std=c89 -Wall -Wextra -pedantic -O2
SRC = main.c
OBJ = $(SRC:.c=.o)
ifeq ($(OS),Windows_NT)
BIN := $(BIN).exe
LIBS = -lglfw3 -lvulkan -lm
else
UNAME_S := $(shell uname -s)
GLFW3 := $(shell pkg-config --libs glfw3)
LIBS = $(GLFW3) -lvulkan -lm
endif
$(BIN): shaders/demo.vert.spv shaders/demo.frag.spv
@mkdir -p bin
rm -f bin/$(BIN) $(OBJS)
$(CC) $(SRC) $(CFLAGS) -o bin/$(BIN) $(LIBS)
shaders/demo.vert.spv: shaders/demo.vert
glslc --target-env=vulkan shaders/demo.vert -o shaders/demo.vert.spv
shaders/demo.frag.spv: shaders/demo.frag
glslc --target-env=vulkan shaders/demo.frag -o shaders/demo.frag.spv

View File

@ -0,0 +1,52 @@
# nuklear glfw vulkan
## Theory of operation
The nuklear glfw vulkan integration creates an independent graphics pipeline that will render the nuklear UI to separate render targets.
The application is responsible to fully manage these render targets. So it must ensure they are properly sized (and resized when requested).
Furthermore it is assumed that you will have a swap chain in place and the number of nuklear overlay images and number of swap chain images match.
This is how you can integrate it in your application:
```
/*
Setup: overlay_images have been created and their number match with the number
of the swap_chain_images of your application. The overlay_images in this
example have the same format as your swap_chain images (optional)
*/
struct nk_context *ctx = nk_glfw3_init(
demo.win, demo.device, demo.physical_device, demo.indices.graphics,
demo.overlay_image_views, demo.swap_chain_images_len,
demo.swap_chain_image_format, NK_GLFW3_INSTALL_CALLBACKS,
MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
[...]
/*
in your draw loop draw you can then render to the overlay image at
`image_index`
your own application can then wait for the semaphore and produce
the swap_chain_image at `image_index`
this should simply sample from the overlay_image (see example)
*/
nk_semaphore semaphore =
nk_glfw3_render(demo.graphics_queue, image_index,
demo.image_available, NK_ANTI_ALIASING_ON);
if (!render(&demo, &bg, nk_semaphore, image_index)) {
fprintf(stderr, "render failed\n");
return false;
}
```
You must call `nk_glfw3_resize` whenever the size of the overlay_images resize.
## Using images
Images can be used by providing a VkImageView as an nk_image_ptr to nuklear:
```
img = nk_image_ptr(demo.demo_texture_image_view);
```
Note that they must have SHADER_READ_OPTIMAL layout
It is currently not possible to specify how they are being sampled. The nuklear glfw vulkan integration uses a fixed sampler that does linear filtering.

2229
demo/glfw_vulkan/main.c Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,12 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(binding = 0) uniform sampler2D overlay;
layout(location = 0) in vec2 inUV;
layout(location = 0) out vec4 outColor;
void main() {
outColor = texture(overlay, inUV);
}

View File

@ -0,0 +1,10 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout (location = 0) out vec2 outUV;
void main()
{
outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
gl_Position = vec4(outUV * 2.0f + -1.0f, 0.0f, 1.0f);
}

View File

@ -0,0 +1,11 @@
create_shader_inlined_header: nuklearshaders/nuklear.vert.spv nuklearshaders/nuklear.frag.spv
awk -v st='// NUKLEAR_SHADERS_START' -v et='// NUKLEAR_SHADERS_END' -v repl="$$(xxd -i nuklearshaders/nuklear.vert.spv && xxd -i nuklearshaders/nuklear.frag.spv)" '$$0 == st{del=1} $$0 == et{$$0 = repl; del=0} !del' nuklear_glfw_vulkan.in.h > nuklear_glfw_vulkan.h
nuklearshaders/nuklear.vert.spv: nuklearshaders/nuklear.vert
glslc --target-env=vulkan nuklearshaders/nuklear.vert -o nuklearshaders/nuklear.vert.spv
nuklearshaders/nuklear.frag.spv: nuklearshaders/nuklear.frag
glslc --target-env=vulkan nuklearshaders/nuklear.frag -o nuklearshaders/nuklear.frag.spv
clean:
rm nuklearshaders/nuklear.vert.spv nuklearshaders/nuklear.frag.spv nuklear_glfw_vulkan.h

View File

@ -0,0 +1,5 @@
Contrary to OpenGL Vulkan needs precompiled shaders in the SPIR-V format which makes it a bit more difficult to inline the shadercode.
After executing `make` the result should be a self contained `nuklear_glfw_vulkan.h`. Copy the result file to the parent directory and the "release" should be done.
You will need to have `xxd`, `glslc` and `awk` installed for this.

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,13 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(binding = 0, set = 1) uniform sampler2D currentTexture;
layout(location = 0) in vec4 fragColor;
layout(location = 1) in vec2 fragUv;
layout(location = 0) out vec4 outColor;
void main() {
vec4 texColor = texture(currentTexture, fragUv);
outColor = fragColor * texColor;
}

View File

@ -0,0 +1,23 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
out gl_PerVertex {
vec4 gl_Position;
};
layout(binding = 0) uniform UniformBufferObject {
mat4 projection;
} ubo;
layout(location = 0) in vec2 position;
layout(location = 1) in vec2 uv;
layout(location = 2) in uvec4 color;
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec2 fragUv;
void main() {
gl_Position = ubo.projection * vec4(position, 0.0, 1.0);
gl_Position.y = -gl_Position.y;
fragColor = vec4(color[0]/255.0, color[1]/255.0, color[2]/255.0, color[3]/255.0);
fragUv = uv;
}

View File

@ -55,6 +55,10 @@ NK_API void nk_rawfb_resize_fb(struct rawfb_context *rawfb, voi
* ===============================================================
*/
#ifdef NK_RAWFB_IMPLEMENTATION
#include <string.h>
#include <stdlib.h>
#include <assert.h>
struct rawfb_image {
void *pixels;
int w, h, pitch;
@ -83,21 +87,21 @@ nk_rawfb_color2int(const struct nk_color c, rawfb_pl pl)
switch (pl) {
case PIXEL_LAYOUT_RGBX_8888:
res |= c.r << 24;
res |= c.g << 16;
res |= c.b << 8;
res |= c.a;
break;
res |= c.r << 24;
res |= c.g << 16;
res |= c.b << 8;
res |= c.a;
break;
case PIXEL_LAYOUT_XRGB_8888:
res |= c.a << 24;
res |= c.r << 16;
res |= c.g << 8;
res |= c.b;
break;
res |= c.a << 24;
res |= c.r << 16;
res |= c.g << 8;
res |= c.b;
break;
default:
perror("nk_rawfb_color2int(): Unsupported pixel layout.\n");
break;
perror("nk_rawfb_color2int(): Unsupported pixel layout.\n");
break;
}
return (res);
}
@ -109,21 +113,21 @@ nk_rawfb_int2color(const unsigned int i, rawfb_pl pl)
switch (pl) {
case PIXEL_LAYOUT_RGBX_8888:
col.r = (i >> 24) & 0xff;
col.g = (i >> 16) & 0xff;
col.b = (i >> 8) & 0xff;
col.a = i & 0xff;
break;
col.r = (i >> 24) & 0xff;
col.g = (i >> 16) & 0xff;
col.b = (i >> 8) & 0xff;
col.a = i & 0xff;
break;
case PIXEL_LAYOUT_XRGB_8888:
col.a = (i >> 24) & 0xff;
col.r = (i >> 16) & 0xff;
col.g = (i >> 8) & 0xff;
col.b = i & 0xff;
break;
col.a = (i >> 24) & 0xff;
col.r = (i >> 16) & 0xff;
col.g = (i >> 8) & 0xff;
col.b = i & 0xff;
break;
default:
perror("nk_rawfb_int2color(): Unsupported pixel layout.\n");
break;
perror("nk_rawfb_int2color(): Unsupported pixel layout.\n");
break;
}
return col;
}
@ -180,12 +184,12 @@ nk_rawfb_img_setpixel(const struct rawfb_image *img,
NK_ASSERT(img);
if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
ptr = (unsigned char *)img->pixels + (img->pitch * y0);
pixel = (unsigned int *)ptr;
pixel = (unsigned int *)ptr;
if (img->format == NK_FONT_ATLAS_ALPHA8) {
ptr[x0] = col.a;
} else {
pixel[x0] = c;
pixel[x0] = c;
}
}
}
@ -204,8 +208,8 @@ nk_rawfb_img_getpixel(const struct rawfb_image *img, const int x0, const int y0)
col.a = ptr[x0];
col.b = col.g = col.r = 0xff;
} else {
pixel = ((unsigned int *)ptr)[x0];
col = nk_rawfb_int2color(pixel, img->pl);
pixel = ((unsigned int *)ptr)[x0];
col = nk_rawfb_int2color(pixel, img->pl);
}
} return col;
}
@ -594,7 +598,7 @@ nk_rawfb_draw_rect_multi_color(const struct rawfb_context *rawfb,
edge_buf = malloc(((2*w) + (2*h)) * sizeof(struct nk_color));
if (edge_buf == NULL)
return;
return;
edge_t = edge_buf;
edge_b = edge_buf + w;
@ -604,51 +608,51 @@ nk_rawfb_draw_rect_multi_color(const struct rawfb_context *rawfb,
/* Top and bottom edge gradients */
for (i=0; i<w; i++)
{
edge_t[i].r = (((((float)tr.r - tl.r)/(w-1))*i) + 0.5) + tl.r;
edge_t[i].g = (((((float)tr.g - tl.g)/(w-1))*i) + 0.5) + tl.g;
edge_t[i].b = (((((float)tr.b - tl.b)/(w-1))*i) + 0.5) + tl.b;
edge_t[i].a = (((((float)tr.a - tl.a)/(w-1))*i) + 0.5) + tl.a;
edge_t[i].r = (((((float)tr.r - tl.r)/(w-1))*i) + 0.5) + tl.r;
edge_t[i].g = (((((float)tr.g - tl.g)/(w-1))*i) + 0.5) + tl.g;
edge_t[i].b = (((((float)tr.b - tl.b)/(w-1))*i) + 0.5) + tl.b;
edge_t[i].a = (((((float)tr.a - tl.a)/(w-1))*i) + 0.5) + tl.a;
edge_b[i].r = (((((float)br.r - bl.r)/(w-1))*i) + 0.5) + bl.r;
edge_b[i].g = (((((float)br.g - bl.g)/(w-1))*i) + 0.5) + bl.g;
edge_b[i].b = (((((float)br.b - bl.b)/(w-1))*i) + 0.5) + bl.b;
edge_b[i].a = (((((float)br.a - bl.a)/(w-1))*i) + 0.5) + bl.a;
edge_b[i].r = (((((float)br.r - bl.r)/(w-1))*i) + 0.5) + bl.r;
edge_b[i].g = (((((float)br.g - bl.g)/(w-1))*i) + 0.5) + bl.g;
edge_b[i].b = (((((float)br.b - bl.b)/(w-1))*i) + 0.5) + bl.b;
edge_b[i].a = (((((float)br.a - bl.a)/(w-1))*i) + 0.5) + bl.a;
}
/* Left and right edge gradients */
for (i=0; i<h; i++)
{
edge_l[i].r = (((((float)bl.r - tl.r)/(h-1))*i) + 0.5) + tl.r;
edge_l[i].g = (((((float)bl.g - tl.g)/(h-1))*i) + 0.5) + tl.g;
edge_l[i].b = (((((float)bl.b - tl.b)/(h-1))*i) + 0.5) + tl.b;
edge_l[i].a = (((((float)bl.a - tl.a)/(h-1))*i) + 0.5) + tl.a;
edge_l[i].r = (((((float)bl.r - tl.r)/(h-1))*i) + 0.5) + tl.r;
edge_l[i].g = (((((float)bl.g - tl.g)/(h-1))*i) + 0.5) + tl.g;
edge_l[i].b = (((((float)bl.b - tl.b)/(h-1))*i) + 0.5) + tl.b;
edge_l[i].a = (((((float)bl.a - tl.a)/(h-1))*i) + 0.5) + tl.a;
edge_r[i].r = (((((float)br.r - tr.r)/(h-1))*i) + 0.5) + tr.r;
edge_r[i].g = (((((float)br.g - tr.g)/(h-1))*i) + 0.5) + tr.g;
edge_r[i].b = (((((float)br.b - tr.b)/(h-1))*i) + 0.5) + tr.b;
edge_r[i].a = (((((float)br.a - tr.a)/(h-1))*i) + 0.5) + tr.a;
edge_r[i].r = (((((float)br.r - tr.r)/(h-1))*i) + 0.5) + tr.r;
edge_r[i].g = (((((float)br.g - tr.g)/(h-1))*i) + 0.5) + tr.g;
edge_r[i].b = (((((float)br.b - tr.b)/(h-1))*i) + 0.5) + tr.b;
edge_r[i].a = (((((float)br.a - tr.a)/(h-1))*i) + 0.5) + tr.a;
}
for (i=0; i<h; i++) {
for (j=0; j<w; j++) {
if (i==0) {
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_t[j]);
} else if (i==h-1) {
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_b[j]);
} else {
if (j==0) {
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_l[i]);
} else if (j==w-1) {
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_r[i]);
} else {
pixel.r = (((((float)edge_r[i].r - edge_l[i].r)/(w-1))*j) + 0.5) + edge_l[i].r;
pixel.g = (((((float)edge_r[i].g - edge_l[i].g)/(w-1))*j) + 0.5) + edge_l[i].g;
pixel.b = (((((float)edge_r[i].b - edge_l[i].b)/(w-1))*j) + 0.5) + edge_l[i].b;
pixel.a = (((((float)edge_r[i].a - edge_l[i].a)/(w-1))*j) + 0.5) + edge_l[i].a;
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, pixel);
}
}
}
for (j=0; j<w; j++) {
if (i==0) {
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_t[j]);
} else if (i==h-1) {
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_b[j]);
} else {
if (j==0) {
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_l[i]);
} else if (j==w-1) {
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_r[i]);
} else {
pixel.r = (((((float)edge_r[i].r - edge_l[i].r)/(w-1))*j) + 0.5) + edge_l[i].r;
pixel.g = (((((float)edge_r[i].g - edge_l[i].g)/(w-1))*j) + 0.5) + edge_l[i].g;
pixel.b = (((((float)edge_r[i].b - edge_l[i].b)/(w-1))*j) + 0.5) + edge_l[i].b;
pixel.a = (((((float)edge_r[i].a - edge_l[i].a)/(w-1))*j) + 0.5) + edge_l[i].a;
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, pixel);
}
}
}
}
free(edge_buf);
@ -833,31 +837,30 @@ nk_rawfb_init(void *fb, void *tex_mem, const unsigned int w, const unsigned int
rawfb->font_tex.w = rawfb->font_tex.h = 0;
rawfb->fb.pixels = fb;
rawfb->fb.w= w;
rawfb->fb.w = w;
rawfb->fb.h = h;
rawfb->fb.pl = pl;
if (pl == PIXEL_LAYOUT_RGBX_8888 || pl == PIXEL_LAYOUT_XRGB_8888) {
rawfb->fb.format = NK_FONT_ATLAS_RGBA32;
rawfb->fb.pitch = pitch;
}
else {
perror("nk_rawfb_init(): Unsupported pixel layout.\n");
free(rawfb);
return NULL;
rawfb->fb.format = NK_FONT_ATLAS_RGBA32;
rawfb->fb.pitch = pitch;
} else {
perror("nk_rawfb_init(): Unsupported pixel layout.\n");
free(rawfb);
return NULL;
}
if (0 == nk_init_default(&rawfb->ctx, 0)) {
free(rawfb);
return NULL;
free(rawfb);
return NULL;
}
nk_font_atlas_init_default(&rawfb->atlas);
nk_font_atlas_begin(&rawfb->atlas);
tex = nk_font_atlas_bake(&rawfb->atlas, &rawfb->font_tex.w, &rawfb->font_tex.h, rawfb->font_tex.format);
if (!tex) {
free(rawfb);
return NULL;
free(rawfb);
return NULL;
}
switch(rawfb->font_tex.format) {
@ -875,6 +878,7 @@ nk_rawfb_init(void *fb, void *tex_mem, const unsigned int w, const unsigned int
nk_style_set_font(&rawfb->ctx, &rawfb->atlas.default_font->handle);
nk_style_load_all_cursors(&rawfb->ctx, rawfb->atlas.cursors);
nk_rawfb_scissor(rawfb, 0, 0, rawfb->fb.w, rawfb->fb.h);
return rawfb;
}
@ -901,12 +905,12 @@ nk_rawfb_stretch_image(const struct rawfb_image *dst,
continue;
}
col = nk_rawfb_img_getpixel(src, (int)xoff, (int) yoff);
if (col.r || col.g || col.b)
{
col.r = fg->r;
col.g = fg->g;
col.b = fg->b;
}
if (col.r || col.g || col.b)
{
col.r = fg->r;
col.g = fg->g;
col.b = fg->b;
}
nk_rawfb_img_blendpixel(dst, i + (int)(dst_rect->x + 0.5f), j + (int)(dst_rect->y + 0.5f), col);
xoff += xinc;
}
@ -982,8 +986,8 @@ nk_rawfb_draw_text(const struct rawfb_context *rawfb,
dst_rect.x = x + g.offset.x + rect.x;
dst_rect.y = g.offset.y + rect.y;
dst_rect.w = ceilf(g.width);
dst_rect.h = ceilf(g.height);
dst_rect.w = ceil(g.width);
dst_rect.h = ceil(g.height);
/* Use software rescaling to blit glyph from font_text to framebuffer */
nk_rawfb_stretch_image(&rawfb->fb, &rawfb->font_tex, &dst_rect, &src_rect, &rawfb->scissors, &fg);
@ -1020,9 +1024,9 @@ NK_API void
nk_rawfb_shutdown(struct rawfb_context *rawfb)
{
if (rawfb) {
nk_free(&rawfb->ctx);
memset(rawfb, 0, sizeof(struct rawfb_context));
free(rawfb);
nk_free(&rawfb->ctx);
memset(rawfb, 0, sizeof(struct rawfb_context));
free(rawfb);
}
}
@ -1032,7 +1036,7 @@ nk_rawfb_resize_fb(struct rawfb_context *rawfb,
const unsigned int w,
const unsigned int h,
const unsigned int pitch,
const rawfb_pl pl)
const rawfb_pl pl)
{
rawfb->fb.w = w;
rawfb->fb.h = h;
@ -1113,9 +1117,9 @@ nk_rawfb_render(const struct rawfb_context *rawfb,
q->end, 22, q->line_thickness, q->color);
} break;
case NK_COMMAND_RECT_MULTI_COLOR: {
const struct nk_command_rect_multi_color *q = (const struct nk_command_rect_multi_color *)cmd;
nk_rawfb_draw_rect_multi_color(rawfb, q->x, q->y, q->w, q->h, q->left, q->top, q->right, q->bottom);
} break;
const struct nk_command_rect_multi_color *q = (const struct nk_command_rect_multi_color *)cmd;
nk_rawfb_draw_rect_multi_color(rawfb, q->x, q->y, q->w, q->h, q->left, q->top, q->right, q->bottom);
} break;
case NK_COMMAND_IMAGE: {
const struct nk_command_image *q = (const struct nk_command_image *)cmd;
nk_rawfb_drawimage(rawfb, q->x, q->y, q->w, q->h, &q->img, &q->col);

View File

@ -2,7 +2,7 @@ CFLAGS=`sdl2-config --cflags --libs` -std=c89 -Wall -Wextra -pedantic -Wno-unu
.PHONY: clean
demo: main.c sdl2surface_rawfb.h
demo: main.c nuklear_sdl_rawfb.h
$(CC) -o demo *.c $(CFLAGS) -lrt -lm
clean:

View File

@ -18,9 +18,9 @@
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_SOFTWARE_FONT
#include "../../nuklear.h"
#define NK_SDLSURFACE_IMPLEMENTATION
#include "sdl2surface_rawfb.h"
#include "../../../nuklear.h"
#define NK_RAWFB_IMPLEMENTATION
#include "nuklear_sdl_rawfb.h"
/* ===============================================================
*
@ -45,19 +45,19 @@
#endif
#ifdef INCLUDE_STYLE
#include "../../demo/common/style.c"
#include "../../common/style.c"
#endif
#ifdef INCLUDE_CALCULATOR
#include "../../demo/common/calculator.c"
#include "../../common/calculator.c"
#endif
#ifdef INCLUDE_CANVAS
#include "../../demo/common/canvas.c"
#include "../../common/canvas.c"
#endif
#ifdef INCLUDE_OVERVIEW
#include "../../demo/common/overview.c"
#include "../../common/overview.c"
#endif
#ifdef INCLUDE_NODE_EDITOR
#include "../../demo/common/node_editor.c"
#include "../../common/node_editor.c"
#endif
static int translate_sdl_key(struct SDL_Keysym const *k)
@ -131,7 +131,7 @@ int main(int argc, char **argv)
struct nk_color clear = {0,100,0,255};
struct nk_vec2 vec;
struct nk_rect bounds = {40,40,0,0};
struct sdlsurface_context *context;
struct rawfb_context *context;
SDL_DisplayMode dm;
SDL_Window *window;
@ -165,7 +165,7 @@ int main(int argc, char **argv)
surface = SDL_CreateRGBSurfaceWithFormat(0, dm.w-200, dm.h-200, 32, SDL_PIXELFORMAT_ARGB8888);
context = nk_sdlsurface_init(surface, 13.0f);
context = nk_rawfb_init(surface, 13.0f);
while(1)
@ -240,7 +240,7 @@ int main(int argc, char **argv)
#endif
/* ----------------------------------------- */
nk_sdlsurface_render(context, clear, 1);
nk_rawfb_render(context, clear, 1);
@ -252,7 +252,7 @@ int main(int argc, char **argv)
}
nk_sdlsurface_shutdown(context);
nk_rawfb_shutdown(context);
SDL_FreeSurface(surface);
SDL_DestroyRenderer(renderer);

View File

@ -30,15 +30,19 @@
*/
/* Adapted from nulear_rawfb.h for use with SDL_Surface by Martijn Versteegh*/
#ifndef NK_SDLSURFACE_H_
#define NK_SDLSURFACE_H_
#ifndef NK_RAWFB_H_
#define NK_RAWFB_H_
#include <SDL.h>
#include <SDL_surface.h>
struct sdlsurface_context *nk_sdlsurface_init(SDL_Surface *fb, float fontSize);
void nk_sdlsurface_render(const struct sdlsurface_context *sdlsurface, const struct nk_color clear, const unsigned char enable_clear);
void nk_sdlsurface_shutdown(struct sdlsurface_context *sdlsurface);
struct rawfb_context;
/* All functions are thread-safe */
NK_API struct rawfb_context *nk_rawfb_init(SDL_Surface *fb, float fontSize);
NK_API void nk_rawfb_render(const struct rawfb_context *rawfb, const struct nk_color clear, const unsigned char enable_clear);
NK_API void nk_rawfb_shutdown(struct rawfb_context *rawfb);
#endif
/*
@ -48,12 +52,20 @@ void nk_sdlsurface_shutdown(struct sdlsurface_context *sdlsurfa
*
* ===============================================================
*/
#ifdef NK_SDLSURFACE_IMPLEMENTATION
struct sdlsurface_context {
#ifdef NK_RAWFB_IMPLEMENTATION
#include <string.h>
#include <stdlib.h>
#include <assert.h>
struct rawfb_image {
SDL_Surface *surf;
int w, h;
};
struct rawfb_context {
struct nk_context ctx;
struct nk_rect scissors;
struct SDL_Surface *fb;
struct SDL_Surface *font_tex;
struct rawfb_image fb;
struct rawfb_image font_tex;
struct nk_font_atlas atlas;
};
@ -65,13 +77,13 @@ struct sdlsurface_context {
#endif
static unsigned int
nk_sdlsurface_color2int(const struct nk_color c, const SDL_PixelFormat *format)
nk_rawfb_color2int(const struct nk_color c, const SDL_PixelFormat *format)
{
return SDL_MapRGBA(format, c.r, c.g, c.b, c.a);
}
static struct nk_color
nk_sdlsurface_int2color(const unsigned int i, const SDL_PixelFormat *format)
nk_rawfb_int2color(const unsigned int i, const SDL_PixelFormat *format)
{
struct nk_color col = {0,0,0,0};
@ -81,20 +93,20 @@ nk_sdlsurface_int2color(const unsigned int i, const SDL_PixelFormat *format)
}
static void
nk_sdlsurface_ctx_setpixel(const struct sdlsurface_context *sdlsurface,
nk_rawfb_ctx_setpixel(const struct rawfb_context *rawfb,
const short x0, const short y0, const struct nk_color col)
{
unsigned int c = nk_sdlsurface_color2int(col, sdlsurface->fb->format);
unsigned char *pixels = sdlsurface->fb->pixels;
unsigned int c = nk_rawfb_color2int(col, rawfb->fb.surf->format);
unsigned char *pixels = rawfb->fb.surf->pixels;
pixels += y0 * sdlsurface->fb->pitch;
pixels += y0 * rawfb->fb.surf->pitch;
if (y0 < sdlsurface->scissors.h && y0 >= sdlsurface->scissors.y &&
x0 >= sdlsurface->scissors.x && x0 < sdlsurface->scissors.w) {
if (y0 < rawfb->scissors.h && y0 >= rawfb->scissors.y &&
x0 >= rawfb->scissors.x && x0 < rawfb->scissors.w) {
if (sdlsurface->fb->format->BytesPerPixel == 4) {
if (rawfb->fb.surf->format->BytesPerPixel == 4) {
*((Uint32 *)pixels + x0) = c;
} else if (sdlsurface->fb->format->BytesPerPixel == 2) {
} else if (rawfb->fb.surf->format->BytesPerPixel == 2) {
*((Uint16 *)pixels + x0) = c;
} else {
*((Uint8 *)pixels + x0) = c;
@ -103,7 +115,7 @@ nk_sdlsurface_ctx_setpixel(const struct sdlsurface_context *sdlsurface,
}
static void
nk_sdlsurface_line_horizontal(const struct sdlsurface_context *sdlsurface,
nk_rawfb_line_horizontal(const struct rawfb_context *rawfb,
const short x0, const short y, const short x1, const struct nk_color col)
{
/* This function is called the most. Try to optimize it a bit...
@ -111,21 +123,21 @@ nk_sdlsurface_line_horizontal(const struct sdlsurface_context *sdlsurface,
* The caller has to make sure it does no exceed bounds. */
unsigned int i, n;
unsigned char c[16 * 4];
unsigned char *pixels = sdlsurface->fb->pixels;
unsigned int bpp = sdlsurface->fb->format->BytesPerPixel;
unsigned char *pixels = rawfb->fb.surf->pixels;
unsigned int bpp = rawfb->fb.surf->format->BytesPerPixel;
pixels += (y * sdlsurface->fb->pitch) + (x0 * bpp);
pixels += (y * rawfb->fb.surf->pitch) + (x0 * bpp);
n = (x1 - x0) * bpp;
if (bpp == 4) {
for (i = 0; i < sizeof(c) / bpp; i++)
((Uint32 *)c)[i] = nk_sdlsurface_color2int(col, sdlsurface->fb->format);
((Uint32 *)c)[i] = nk_rawfb_color2int(col, rawfb->fb.surf->format);
} else if (bpp == 2) {
for (i = 0; i < sizeof(c) / bpp; i++)
((Uint16 *)c)[i] = nk_sdlsurface_color2int(col, sdlsurface->fb->format);
((Uint16 *)c)[i] = nk_rawfb_color2int(col, rawfb->fb.surf->format);
} else {
for (i = 0; i < sizeof(c) / bpp; i++)
((Uint8 *)c)[i] = nk_sdlsurface_color2int(col, sdlsurface->fb->format);
((Uint8 *)c)[i] = nk_rawfb_color2int(col, rawfb->fb.surf->format);
}
while (n > sizeof(c)) {
@ -136,20 +148,20 @@ nk_sdlsurface_line_horizontal(const struct sdlsurface_context *sdlsurface,
}
static void
nk_sdlsurface_img_setpixel(const struct SDL_Surface *img,
nk_rawfb_img_setpixel(const struct rawfb_image *img,
const int x0, const int y0, const struct nk_color col)
{
unsigned int c = nk_sdlsurface_color2int(col, img->format);
unsigned int c = nk_rawfb_color2int(col, img->surf->format);
unsigned char *ptr;
NK_ASSERT(img);
if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
ptr = (unsigned char *)img->pixels + (img->pitch * y0);
ptr = (unsigned char *)img->surf->pixels + (img->surf->pitch * y0);
if (img->format == NK_FONT_ATLAS_ALPHA8) {
if (img->surf->format == NK_FONT_ATLAS_ALPHA8) {
ptr[x0] = col.a;
} else if (img->format->BytesPerPixel == 4) {
} else if (img->surf->format->BytesPerPixel == 4) {
((Uint32 *)ptr)[x0] = c;
} else if (img->format->BytesPerPixel == 2) {
} else if (img->surf->format->BytesPerPixel == 2) {
((Uint16 *)ptr)[x0] = c;
} else {
((Uint8 *)ptr)[x0] = c;
@ -158,32 +170,32 @@ nk_sdlsurface_img_setpixel(const struct SDL_Surface *img,
}
static struct nk_color
nk_sdlsurface_img_getpixel(const struct SDL_Surface *img, const int x0, const int y0)
nk_rawfb_img_getpixel(const struct rawfb_image *img, const int x0, const int y0)
{
struct nk_color col = {0, 0, 0, 0};
unsigned char *ptr;
unsigned int pixel;
NK_ASSERT(img);
if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
ptr = (unsigned char *)img->pixels + (img->pitch * y0);
ptr = (unsigned char *)img->surf->pixels + (img->surf->pitch * y0);
if (img->format == NK_FONT_ATLAS_ALPHA8) {
if (img->surf->format == NK_FONT_ATLAS_ALPHA8) {
col.a = ptr[x0];
col.b = col.g = col.r = 0xff;
} else if (img->format->BytesPerPixel == 4) {
} else if (img->surf->format->BytesPerPixel == 4) {
pixel = ((Uint32 *)ptr)[x0];
col = nk_sdlsurface_int2color(pixel, img->format);
} else if (img->format->BytesPerPixel == 2) {
col = nk_rawfb_int2color(pixel, img->surf->format);
} else if (img->surf->format->BytesPerPixel == 2) {
pixel = ((Uint16 *)ptr)[x0];
col = nk_sdlsurface_int2color(pixel, img->format);
col = nk_rawfb_int2color(pixel, img->surf->format);
} else {
pixel = ((Uint8 *)ptr)[x0];
col = nk_sdlsurface_int2color(pixel, img->format);
col = nk_rawfb_int2color(pixel, img->surf->format);
}
} return col;
}
static void
nk_sdlsurface_img_blendpixel(const struct SDL_Surface *img,
nk_rawfb_img_blendpixel(const struct rawfb_image *img,
const int x0, const int y0, struct nk_color col)
{
struct nk_color col2;
@ -192,28 +204,28 @@ nk_sdlsurface_img_blendpixel(const struct SDL_Surface *img,
return;
inv_a = 0xff - col.a;
col2 = nk_sdlsurface_img_getpixel(img, x0, y0);
col2 = nk_rawfb_img_getpixel(img, x0, y0);
col.r = (col.r * col.a + col2.r * inv_a) >> 8;
col.g = (col.g * col.a + col2.g * inv_a) >> 8;
col.b = (col.b * col.a + col2.b * inv_a) >> 8;
nk_sdlsurface_img_setpixel(img, x0, y0, col);
nk_rawfb_img_setpixel(img, x0, y0, col);
}
static void
nk_sdlsurface_scissor(struct sdlsurface_context *sdlsurface,
nk_rawfb_scissor(struct rawfb_context *rawfb,
const float x,
const float y,
const float w,
const float h)
{
sdlsurface->scissors.x = MIN(MAX(x, 0), sdlsurface->fb->w);
sdlsurface->scissors.y = MIN(MAX(y, 0), sdlsurface->fb->h);
sdlsurface->scissors.w = MIN(MAX(w + x, 0), sdlsurface->fb->w);
sdlsurface->scissors.h = MIN(MAX(h + y, 0), sdlsurface->fb->h);
rawfb->scissors.x = MIN(MAX(x, 0), rawfb->fb.w);
rawfb->scissors.y = MIN(MAX(y, 0), rawfb->fb.h);
rawfb->scissors.w = MIN(MAX(w + x, 0), rawfb->fb.w);
rawfb->scissors.h = MIN(MAX(h + y, 0), rawfb->fb.h);
}
static void
nk_sdlsurface_stroke_line(const struct sdlsurface_context *sdlsurface,
nk_rawfb_stroke_line(const struct rawfb_context *rawfb,
short x0, short y0, short x1, short y1,
const unsigned int line_thickness, const struct nk_color col)
{
@ -227,7 +239,7 @@ nk_sdlsurface_stroke_line(const struct sdlsurface_context *sdlsurface,
/* fast path */
if (dy == 0) {
if (dx == 0 || y0 >= sdlsurface->scissors.h || y0 < sdlsurface->scissors.y)
if (dx == 0 || y0 >= rawfb->scissors.h || y0 < rawfb->scissors.y)
return;
if (dx < 0) {
@ -236,11 +248,11 @@ nk_sdlsurface_stroke_line(const struct sdlsurface_context *sdlsurface,
x1 = x0;
x0 = tmp;
}
x1 = MIN(sdlsurface->scissors.w, x1);
x0 = MIN(sdlsurface->scissors.w, x0);
x1 = MAX(sdlsurface->scissors.x, x1);
x0 = MAX(sdlsurface->scissors.x, x0);
nk_sdlsurface_line_horizontal(sdlsurface, x0, y0, x1, col);
x1 = MIN(rawfb->scissors.w, x1);
x0 = MIN(rawfb->scissors.w, x0);
x1 = MAX(rawfb->scissors.x, x1);
x0 = MAX(rawfb->scissors.x, x0);
nk_rawfb_line_horizontal(rawfb, x0, y0, x1, col);
return;
}
if (dy < 0) {
@ -256,7 +268,7 @@ nk_sdlsurface_stroke_line(const struct sdlsurface_context *sdlsurface,
dy <<= 1;
dx <<= 1;
nk_sdlsurface_ctx_setpixel(sdlsurface, x0, y0, col);
nk_rawfb_ctx_setpixel(rawfb, x0, y0, col);
if (dx > dy) {
int fraction = dy - (dx >> 1);
while (x0 != x1) {
@ -266,7 +278,7 @@ nk_sdlsurface_stroke_line(const struct sdlsurface_context *sdlsurface,
}
x0 += stepx;
fraction += dy;
nk_sdlsurface_ctx_setpixel(sdlsurface, x0, y0, col);
nk_rawfb_ctx_setpixel(rawfb, x0, y0, col);
}
} else {
int fraction = dx - (dy >> 1);
@ -277,13 +289,13 @@ nk_sdlsurface_stroke_line(const struct sdlsurface_context *sdlsurface,
}
y0 += stepy;
fraction += dx;
nk_sdlsurface_ctx_setpixel(sdlsurface, x0, y0, col);
nk_rawfb_ctx_setpixel(rawfb, x0, y0, col);
}
}
}
static void
nk_sdlsurface_fill_polygon(const struct sdlsurface_context *sdlsurface,
nk_rawfb_fill_polygon(const struct rawfb_context *rawfb,
const struct nk_vec2i *pnts, int count, const struct nk_color col)
{
int i = 0;
@ -338,7 +350,7 @@ nk_sdlsurface_fill_polygon(const struct sdlsurface_context *sdlsurface,
if (nodeX[i+0] < left) nodeX[i+0] = left ;
if (nodeX[i+1] > right) nodeX[i+1] = right;
for (pixelX = nodeX[i]; pixelX < nodeX[i + 1]; pixelX++)
nk_sdlsurface_ctx_setpixel(sdlsurface, pixelX, pixelY, col);
nk_rawfb_ctx_setpixel(rawfb, pixelX, pixelY, col);
}
}
}
@ -346,7 +358,7 @@ nk_sdlsurface_fill_polygon(const struct sdlsurface_context *sdlsurface,
}
static void
nk_sdlsurface_stroke_arc(const struct sdlsurface_context *sdlsurface,
nk_rawfb_stroke_arc(const struct rawfb_context *rawfb,
short x0, short y0, short w, short h, const short s,
const short line_thickness, const struct nk_color col)
{
@ -369,13 +381,13 @@ nk_sdlsurface_stroke_arc(const struct sdlsurface_context *sdlsurface,
/* First half */
for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
if (s == 180)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 + y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 + y, col);
else if (s == 270)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 + y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 + y, col);
else if (s == 0)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 - y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 - y, col);
else if (s == 90)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 - y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 - y, col);
if (sigma >= 0) {
sigma += fa2 * (1 - y);
y--;
@ -385,13 +397,13 @@ nk_sdlsurface_stroke_arc(const struct sdlsurface_context *sdlsurface,
/* Second half */
for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
if (s == 180)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 + y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 + y, col);
else if (s == 270)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 + y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 + y, col);
else if (s == 0)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 - y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 - y, col);
else if (s == 90)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 - y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 - y, col);
if (sigma >= 0) {
sigma += fb2 * (1 - x);
x--;
@ -400,7 +412,7 @@ nk_sdlsurface_stroke_arc(const struct sdlsurface_context *sdlsurface,
}
static void
nk_sdlsurface_fill_arc(const struct sdlsurface_context *sdlsurface, short x0, short y0,
nk_rawfb_fill_arc(const struct rawfb_context *rawfb, short x0, short y0,
short w, short h, const short s, const struct nk_color col)
{
/* Bresenham's ellipses - modified to fill one quarter */
@ -435,7 +447,7 @@ nk_sdlsurface_fill_arc(const struct sdlsurface_context *sdlsurface, short x0, sh
} else if (s == 90) {
pnts[1].x = x0 - x; pnts[1].y = y0 - y;
}
nk_sdlsurface_fill_polygon(sdlsurface, pnts, 3, col);
nk_rawfb_fill_polygon(rawfb, pnts, 3, col);
pnts[2] = pnts[1];
if (sigma >= 0) {
sigma += fa2 * (1 - y);
@ -454,7 +466,7 @@ nk_sdlsurface_fill_arc(const struct sdlsurface_context *sdlsurface, short x0, sh
} else if (s == 90) {
pnts[1].x = x0 - x; pnts[1].y = y0 - y;
}
nk_sdlsurface_fill_polygon(sdlsurface, pnts, 3, col);
nk_rawfb_fill_polygon(rawfb, pnts, 3, col);
pnts[2] = pnts[1];
if (sigma >= 0) {
sigma += fb2 * (1 - x);
@ -464,46 +476,46 @@ nk_sdlsurface_fill_arc(const struct sdlsurface_context *sdlsurface, short x0, sh
}
static void
nk_sdlsurface_stroke_rect(const struct sdlsurface_context *sdlsurface,
nk_rawfb_stroke_rect(const struct rawfb_context *rawfb,
const short x, const short y, const short w, const short h,
const short r, const short line_thickness, const struct nk_color col)
{
if (r == 0) {
nk_sdlsurface_stroke_line(sdlsurface, x, y, x + w, y, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x, y + h, x + w, y + h, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x, y, x, y + h, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x + w, y, x + w, y + h, line_thickness, col);
nk_rawfb_stroke_line(rawfb, x, y, x + w, y, line_thickness, col);
nk_rawfb_stroke_line(rawfb, x, y + h, x + w, y + h, line_thickness, col);
nk_rawfb_stroke_line(rawfb, x, y, x, y + h, line_thickness, col);
nk_rawfb_stroke_line(rawfb, x + w, y, x + w, y + h, line_thickness, col);
} else {
const short xc = x + r;
const short yc = y + r;
const short wc = (short)(w - 2 * r);
const short hc = (short)(h - 2 * r);
nk_sdlsurface_stroke_line(sdlsurface, xc, y, xc + wc, y, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x + w, yc, x + w, yc + hc, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, xc, y + h, xc + wc, y + h, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x, yc, x, yc + hc, line_thickness, col);
nk_rawfb_stroke_line(rawfb, xc, y, xc + wc, y, line_thickness, col);
nk_rawfb_stroke_line(rawfb, x + w, yc, x + w, yc + hc, line_thickness, col);
nk_rawfb_stroke_line(rawfb, xc, y + h, xc + wc, y + h, line_thickness, col);
nk_rawfb_stroke_line(rawfb, x, yc, x, yc + hc, line_thickness, col);
nk_sdlsurface_stroke_arc(sdlsurface, xc + wc - r, y,
nk_rawfb_stroke_arc(rawfb, xc + wc - r, y,
(unsigned)r*2, (unsigned)r*2, 0 , line_thickness, col);
nk_sdlsurface_stroke_arc(sdlsurface, x, y,
nk_rawfb_stroke_arc(rawfb, x, y,
(unsigned)r*2, (unsigned)r*2, 90 , line_thickness, col);
nk_sdlsurface_stroke_arc(sdlsurface, x, yc + hc - r,
nk_rawfb_stroke_arc(rawfb, x, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 270 , line_thickness, col);
nk_sdlsurface_stroke_arc(sdlsurface, xc + wc - r, yc + hc - r,
nk_rawfb_stroke_arc(rawfb, xc + wc - r, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 180 , line_thickness, col);
}
}
static void
nk_sdlsurface_fill_rect(const struct sdlsurface_context *sdlsurface,
nk_rawfb_fill_rect(const struct rawfb_context *rawfb,
const short x, const short y, const short w, const short h,
const short r, const struct nk_color col)
{
int i;
if (r == 0) {
for (i = 0; i < h; i++)
nk_sdlsurface_stroke_line(sdlsurface, x, y + i, x + w, y + i, 1, col);
nk_rawfb_stroke_line(rawfb, x, y + i, x + w, y + i, 1, col);
} else {
const short xc = x + r;
const short yc = y + r;
@ -539,21 +551,21 @@ nk_sdlsurface_fill_rect(const struct sdlsurface_context *sdlsurface,
pnts[11].x = x;
pnts[11].y = yc + hc;
nk_sdlsurface_fill_polygon(sdlsurface, pnts, 12, col);
nk_rawfb_fill_polygon(rawfb, pnts, 12, col);
nk_sdlsurface_fill_arc(sdlsurface, xc + wc - r, y,
nk_rawfb_fill_arc(rawfb, xc + wc - r, y,
(unsigned)r*2, (unsigned)r*2, 0 , col);
nk_sdlsurface_fill_arc(sdlsurface, x, y,
nk_rawfb_fill_arc(rawfb, x, y,
(unsigned)r*2, (unsigned)r*2, 90 , col);
nk_sdlsurface_fill_arc(sdlsurface, x, yc + hc - r,
nk_rawfb_fill_arc(rawfb, x, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 270 , col);
nk_sdlsurface_fill_arc(sdlsurface, xc + wc - r, yc + hc - r,
nk_rawfb_fill_arc(rawfb, xc + wc - r, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 180 , col);
}
}
NK_API void
nk_sdlsurface_draw_rect_multi_color(const struct sdlsurface_context *sdlsurface,
nk_rawfb_draw_rect_multi_color(const struct rawfb_context *rawfb,
const short x, const short y, const short w, const short h, struct nk_color tl,
struct nk_color tr, struct nk_color br, struct nk_color bl)
{
@ -567,7 +579,7 @@ nk_sdlsurface_draw_rect_multi_color(const struct sdlsurface_context *sdlsurface,
edge_buf = malloc(((2*w) + (2*h)) * sizeof(struct nk_color));
if (edge_buf == NULL)
return;
return;
edge_t = edge_buf;
edge_b = edge_buf + w;
@ -577,58 +589,58 @@ nk_sdlsurface_draw_rect_multi_color(const struct sdlsurface_context *sdlsurface,
/* Top and bottom edge gradients */
for (i=0; i<w; i++)
{
edge_t[i].r = (((((float)tr.r - tl.r)/(w-1))*i) + 0.5) + tl.r;
edge_t[i].g = (((((float)tr.g - tl.g)/(w-1))*i) + 0.5) + tl.g;
edge_t[i].b = (((((float)tr.b - tl.b)/(w-1))*i) + 0.5) + tl.b;
edge_t[i].a = (((((float)tr.a - tl.a)/(w-1))*i) + 0.5) + tl.a;
edge_t[i].r = (((((float)tr.r - tl.r)/(w-1))*i) + 0.5) + tl.r;
edge_t[i].g = (((((float)tr.g - tl.g)/(w-1))*i) + 0.5) + tl.g;
edge_t[i].b = (((((float)tr.b - tl.b)/(w-1))*i) + 0.5) + tl.b;
edge_t[i].a = (((((float)tr.a - tl.a)/(w-1))*i) + 0.5) + tl.a;
edge_b[i].r = (((((float)br.r - bl.r)/(w-1))*i) + 0.5) + bl.r;
edge_b[i].g = (((((float)br.g - bl.g)/(w-1))*i) + 0.5) + bl.g;
edge_b[i].b = (((((float)br.b - bl.b)/(w-1))*i) + 0.5) + bl.b;
edge_b[i].a = (((((float)br.a - bl.a)/(w-1))*i) + 0.5) + bl.a;
edge_b[i].r = (((((float)br.r - bl.r)/(w-1))*i) + 0.5) + bl.r;
edge_b[i].g = (((((float)br.g - bl.g)/(w-1))*i) + 0.5) + bl.g;
edge_b[i].b = (((((float)br.b - bl.b)/(w-1))*i) + 0.5) + bl.b;
edge_b[i].a = (((((float)br.a - bl.a)/(w-1))*i) + 0.5) + bl.a;
}
/* Left and right edge gradients */
for (i=0; i<h; i++)
{
edge_l[i].r = (((((float)bl.r - tl.r)/(h-1))*i) + 0.5) + tl.r;
edge_l[i].g = (((((float)bl.g - tl.g)/(h-1))*i) + 0.5) + tl.g;
edge_l[i].b = (((((float)bl.b - tl.b)/(h-1))*i) + 0.5) + tl.b;
edge_l[i].a = (((((float)bl.a - tl.a)/(h-1))*i) + 0.5) + tl.a;
edge_l[i].r = (((((float)bl.r - tl.r)/(h-1))*i) + 0.5) + tl.r;
edge_l[i].g = (((((float)bl.g - tl.g)/(h-1))*i) + 0.5) + tl.g;
edge_l[i].b = (((((float)bl.b - tl.b)/(h-1))*i) + 0.5) + tl.b;
edge_l[i].a = (((((float)bl.a - tl.a)/(h-1))*i) + 0.5) + tl.a;
edge_r[i].r = (((((float)br.r - tr.r)/(h-1))*i) + 0.5) + tr.r;
edge_r[i].g = (((((float)br.g - tr.g)/(h-1))*i) + 0.5) + tr.g;
edge_r[i].b = (((((float)br.b - tr.b)/(h-1))*i) + 0.5) + tr.b;
edge_r[i].a = (((((float)br.a - tr.a)/(h-1))*i) + 0.5) + tr.a;
edge_r[i].r = (((((float)br.r - tr.r)/(h-1))*i) + 0.5) + tr.r;
edge_r[i].g = (((((float)br.g - tr.g)/(h-1))*i) + 0.5) + tr.g;
edge_r[i].b = (((((float)br.b - tr.b)/(h-1))*i) + 0.5) + tr.b;
edge_r[i].a = (((((float)br.a - tr.a)/(h-1))*i) + 0.5) + tr.a;
}
for (i=0; i<h; i++) {
for (j=0; j<w; j++) {
if (i==0) {
nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, edge_t[j]);
} else if (i==h-1) {
nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, edge_b[j]);
} else {
if (j==0) {
nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, edge_l[i]);
} else if (j==w-1) {
nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, edge_r[i]);
} else {
pixel.r = (((((float)edge_r[i].r - edge_l[i].r)/(w-1))*j) + 0.5) + edge_l[i].r;
pixel.g = (((((float)edge_r[i].g - edge_l[i].g)/(w-1))*j) + 0.5) + edge_l[i].g;
pixel.b = (((((float)edge_r[i].b - edge_l[i].b)/(w-1))*j) + 0.5) + edge_l[i].b;
pixel.a = (((((float)edge_r[i].a - edge_l[i].a)/(w-1))*j) + 0.5) + edge_l[i].a;
nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, pixel);
for (j=0; j<w; j++) {
if (i==0) {
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_t[j]);
} else if (i==h-1) {
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_b[j]);
} else {
if (j==0) {
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_l[i]);
} else if (j==w-1) {
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_r[i]);
} else {
pixel.r = (((((float)edge_r[i].r - edge_l[i].r)/(w-1))*j) + 0.5) + edge_l[i].r;
pixel.g = (((((float)edge_r[i].g - edge_l[i].g)/(w-1))*j) + 0.5) + edge_l[i].g;
pixel.b = (((((float)edge_r[i].b - edge_l[i].b)/(w-1))*j) + 0.5) + edge_l[i].b;
pixel.a = (((((float)edge_r[i].a - edge_l[i].a)/(w-1))*j) + 0.5) + edge_l[i].a;
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, pixel);
}
}
}
}
}
}
free(edge_buf);
}
static void
nk_sdlsurface_fill_triangle(const struct sdlsurface_context *sdlsurface,
nk_rawfb_fill_triangle(const struct rawfb_context *rawfb,
const short x0, const short y0, const short x1, const short y1,
const short x2, const short y2, const struct nk_color col)
{
@ -639,46 +651,46 @@ nk_sdlsurface_fill_triangle(const struct sdlsurface_context *sdlsurface,
pnts[1].y = y1;
pnts[2].x = x2;
pnts[2].y = y2;
nk_sdlsurface_fill_polygon(sdlsurface, pnts, 3, col);
nk_rawfb_fill_polygon(rawfb, pnts, 3, col);
}
static void
nk_sdlsurface_stroke_triangle(const struct sdlsurface_context *sdlsurface,
nk_rawfb_stroke_triangle(const struct rawfb_context *rawfb,
const short x0, const short y0, const short x1, const short y1,
const short x2, const short y2, const unsigned short line_thickness,
const struct nk_color col)
{
nk_sdlsurface_stroke_line(sdlsurface, x0, y0, x1, y1, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x1, y1, x2, y2, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x2, y2, x0, y0, line_thickness, col);
nk_rawfb_stroke_line(rawfb, x0, y0, x1, y1, line_thickness, col);
nk_rawfb_stroke_line(rawfb, x1, y1, x2, y2, line_thickness, col);
nk_rawfb_stroke_line(rawfb, x2, y2, x0, y0, line_thickness, col);
}
static void
nk_sdlsurface_stroke_polygon(const struct sdlsurface_context *sdlsurface,
nk_rawfb_stroke_polygon(const struct rawfb_context *rawfb,
const struct nk_vec2i *pnts, const int count,
const unsigned short line_thickness, const struct nk_color col)
{
int i;
for (i = 1; i < count; ++i)
nk_sdlsurface_stroke_line(sdlsurface, pnts[i-1].x, pnts[i-1].y, pnts[i].x,
nk_rawfb_stroke_line(rawfb, pnts[i-1].x, pnts[i-1].y, pnts[i].x,
pnts[i].y, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, pnts[count-1].x, pnts[count-1].y,
nk_rawfb_stroke_line(rawfb, pnts[count-1].x, pnts[count-1].y,
pnts[0].x, pnts[0].y, line_thickness, col);
}
static void
nk_sdlsurface_stroke_polyline(const struct sdlsurface_context *sdlsurface,
nk_rawfb_stroke_polyline(const struct rawfb_context *rawfb,
const struct nk_vec2i *pnts, const int count,
const unsigned short line_thickness, const struct nk_color col)
{
int i;
for (i = 0; i < count-1; ++i)
nk_sdlsurface_stroke_line(sdlsurface, pnts[i].x, pnts[i].y,
nk_rawfb_stroke_line(rawfb, pnts[i].x, pnts[i].y,
pnts[i+1].x, pnts[i+1].y, line_thickness, col);
}
static void
nk_sdlsurface_fill_circle(const struct sdlsurface_context *sdlsurface,
nk_rawfb_fill_circle(const struct rawfb_context *rawfb,
short x0, short y0, short w, short h, const struct nk_color col)
{
/* Bresenham's ellipses */
@ -695,8 +707,8 @@ nk_sdlsurface_fill_circle(const struct sdlsurface_context *sdlsurface,
/* First half */
for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
nk_sdlsurface_stroke_line(sdlsurface, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
nk_sdlsurface_stroke_line(sdlsurface, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
nk_rawfb_stroke_line(rawfb, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
nk_rawfb_stroke_line(rawfb, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
if (sigma >= 0) {
sigma += fa2 * (1 - y);
y--;
@ -704,8 +716,8 @@ nk_sdlsurface_fill_circle(const struct sdlsurface_context *sdlsurface,
}
/* Second half */
for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
nk_sdlsurface_stroke_line(sdlsurface, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
nk_sdlsurface_stroke_line(sdlsurface, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
nk_rawfb_stroke_line(rawfb, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
nk_rawfb_stroke_line(rawfb, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
if (sigma >= 0) {
sigma += fb2 * (1 - x);
x--;
@ -714,7 +726,7 @@ nk_sdlsurface_fill_circle(const struct sdlsurface_context *sdlsurface,
}
static void
nk_sdlsurface_stroke_circle(const struct sdlsurface_context *sdlsurface,
nk_rawfb_stroke_circle(const struct rawfb_context *rawfb,
short x0, short y0, short w, short h, const short line_thickness,
const struct nk_color col)
{
@ -734,10 +746,10 @@ nk_sdlsurface_stroke_circle(const struct sdlsurface_context *sdlsurface,
/* First half */
for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 + y, col);
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 + y, col);
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 - y, col);
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 - y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 + y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 + y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 - y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 - y, col);
if (sigma >= 0) {
sigma += fa2 * (1 - y);
y--;
@ -745,10 +757,10 @@ nk_sdlsurface_stroke_circle(const struct sdlsurface_context *sdlsurface,
}
/* Second half */
for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 + y, col);
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 + y, col);
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 - y, col);
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 - y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 + y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 + y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 - y, col);
nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 - y, col);
if (sigma >= 0) {
sigma += fb2 * (1 - x);
x--;
@ -757,7 +769,7 @@ nk_sdlsurface_stroke_circle(const struct sdlsurface_context *sdlsurface,
}
static void
nk_sdlsurface_stroke_curve(const struct sdlsurface_context *sdlsurface,
nk_rawfb_stroke_curve(const struct rawfb_context *rawfb,
const struct nk_vec2i p1, const struct nk_vec2i p2,
const struct nk_vec2i p3, const struct nk_vec2i p4,
const unsigned int num_segments, const unsigned short line_thickness,
@ -778,66 +790,66 @@ nk_sdlsurface_stroke_curve(const struct sdlsurface_context *sdlsurface,
float w4 = t * t *t;
float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
nk_sdlsurface_stroke_line(sdlsurface, last.x, last.y,
nk_rawfb_stroke_line(rawfb, last.x, last.y,
(short)x, (short)y, line_thickness,col);
last.x = (short)x; last.y = (short)y;
}
}
static void
nk_sdlsurface_clear(const struct sdlsurface_context *sdlsurface, const struct nk_color col)
nk_rawfb_clear(const struct rawfb_context *rawfb, const struct nk_color col)
{
nk_sdlsurface_fill_rect(sdlsurface, 0, 0, sdlsurface->fb->w, sdlsurface->fb->h, 0, col);
nk_rawfb_fill_rect(rawfb, 0, 0, rawfb->fb.w, rawfb->fb.h, 0, col);
}
struct sdlsurface_context*
nk_sdlsurface_init(SDL_Surface *fb, float fontSize)
NK_API struct rawfb_context*
nk_rawfb_init(SDL_Surface *fb, float fontSize)
{
const void *tex;
int texh, texw;
struct sdlsurface_context *sdlsurface;
struct rawfb_context *rawfb;
assert((fb->format->format == SDL_PIXELFORMAT_ARGB8888)
|| (fb->format->format == SDL_PIXELFORMAT_RGBA8888));
sdlsurface = malloc(sizeof(struct sdlsurface_context));
if (!sdlsurface)
rawfb = malloc(sizeof(struct rawfb_context));
if (!rawfb)
return NULL;
memset(sdlsurface, 0, sizeof(struct sdlsurface_context));
memset(rawfb, 0, sizeof(struct rawfb_context));
sdlsurface->fb = fb;
rawfb->fb.surf = fb;
rawfb->fb.w = fb->w;
rawfb->fb.h = fb->h;
if (0 == nk_init_default(&sdlsurface->ctx, 0)) {
free(sdlsurface);
return NULL;
if (0 == nk_init_default(&rawfb->ctx, 0)) {
free(rawfb);
return NULL;
}
nk_font_atlas_init_default(&sdlsurface->atlas);
nk_font_atlas_begin(&sdlsurface->atlas);
sdlsurface->atlas.default_font = nk_font_atlas_add_default(&sdlsurface->atlas, fontSize, 0);
tex = nk_font_atlas_bake(&sdlsurface->atlas, &texw, &texh, NK_FONT_ATLAS_RGBA32);
nk_font_atlas_init_default(&rawfb->atlas);
nk_font_atlas_begin(&rawfb->atlas);
rawfb->atlas.default_font = nk_font_atlas_add_default(&rawfb->atlas, fontSize, 0);
tex = nk_font_atlas_bake(&rawfb->atlas, &rawfb->font_tex.w, &rawfb->font_tex.h, NK_FONT_ATLAS_RGBA32);
if (!tex) {
free(sdlsurface);
return NULL;
free(rawfb);
return NULL;
}
sdlsurface->font_tex = SDL_CreateRGBSurface(0, texw, texh, 32, 0xff, 0xff00, 0xff0000, 0xff000000);
rawfb->font_tex.surf = SDL_CreateRGBSurface(0, rawfb->font_tex.w, rawfb->font_tex.h, 32, 0xff, 0xff00, 0xff0000, 0xff000000);
memcpy(sdlsurface->font_tex->pixels, tex, texw * texh * 4);
memcpy(rawfb->font_tex.surf->pixels, tex, rawfb->font_tex.w * rawfb->font_tex.h * 4);
nk_font_atlas_end(&rawfb->atlas, nk_handle_ptr(NULL), NULL);
if (rawfb->atlas.default_font)
nk_style_set_font(&rawfb->ctx, &rawfb->atlas.default_font->handle);
nk_style_load_all_cursors(&rawfb->ctx, rawfb->atlas.cursors);
nk_rawfb_scissor(rawfb, 0, 0, rawfb->fb.w, rawfb->fb.h);
nk_font_atlas_end(&sdlsurface->atlas, nk_handle_ptr(NULL), NULL);
if (sdlsurface->atlas.default_font)
nk_style_set_font(&sdlsurface->ctx, &sdlsurface->atlas.default_font->handle);
nk_style_load_all_cursors(&sdlsurface->ctx, sdlsurface->atlas.cursors);
nk_sdlsurface_scissor(sdlsurface, 0, 0, sdlsurface->fb->w, sdlsurface->fb->h);
return sdlsurface;
return rawfb;
}
static void
nk_sdlsurface_stretch_image(const struct SDL_Surface *dst,
const struct SDL_Surface *src, const struct nk_rect *dst_rect,
nk_rawfb_stretch_image(const struct rawfb_image *dst,
const struct rawfb_image *src, const struct nk_rect *dst_rect,
const struct nk_rect *src_rect, const struct nk_rect *dst_scissors,
const struct nk_color *fg)
{
@ -857,14 +869,14 @@ nk_sdlsurface_stretch_image(const struct SDL_Surface *dst,
if (j + (int)(dst_rect->y + 0.5f) < dst_scissors->y || j + (int)(dst_rect->y + 0.5f) >= dst_scissors->h)
continue;
}
col = nk_sdlsurface_img_getpixel(src, (int)xoff, (int) yoff);
col = nk_rawfb_img_getpixel(src, (int)xoff, (int) yoff);
if (col.r || col.g || col.b)
{
col.r = fg->r;
col.g = fg->g;
col.b = fg->b;
}
nk_sdlsurface_img_blendpixel(dst, i + (int)(dst_rect->x + 0.5f), j + (int)(dst_rect->y + 0.5f), col);
nk_rawfb_img_blendpixel(dst, i + (int)(dst_rect->x + 0.5f), j + (int)(dst_rect->y + 0.5f), col);
xoff += xinc;
}
xoff = src_rect->x;
@ -873,7 +885,7 @@ nk_sdlsurface_stretch_image(const struct SDL_Surface *dst,
}
static void
nk_sdlsurface_font_query_font_glyph(nk_handle handle, const float height,
nk_rawfb_font_query_font_glyph(nk_handle handle, const float height,
struct nk_user_font_glyph *glyph, const nk_rune codepoint,
const nk_rune next_codepoint)
{
@ -900,7 +912,7 @@ nk_sdlsurface_font_query_font_glyph(nk_handle handle, const float height,
}
NK_API void
nk_sdlsurface_draw_text(const struct sdlsurface_context *sdlsurface,
nk_rawfb_draw_text(const struct rawfb_context *rawfb,
const struct nk_user_font *font, const struct nk_rect rect,
const char *text, const int len, const float font_height,
const struct nk_color fg)
@ -927,15 +939,15 @@ nk_sdlsurface_draw_text(const struct sdlsurface_context *sdlsurface,
/* query currently drawn glyph information */
next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
nk_sdlsurface_font_query_font_glyph(font->userdata, font_height, &g, unicode,
nk_rawfb_font_query_font_glyph(font->userdata, font_height, &g, unicode,
(next == NK_UTF_INVALID) ? '\0' : next);
/* calculate and draw glyph drawing rectangle and image */
char_width = g.xadvance;
src_rect.x = g.uv[0].x * sdlsurface->font_tex->w;
src_rect.y = g.uv[0].y * sdlsurface->font_tex->h;
src_rect.w = g.uv[1].x * sdlsurface->font_tex->w - g.uv[0].x * sdlsurface->font_tex->w;
src_rect.h = g.uv[1].y * sdlsurface->font_tex->h - g.uv[0].y * sdlsurface->font_tex->h;
src_rect.x = g.uv[0].x * rawfb->font_tex.w;
src_rect.y = g.uv[0].y * rawfb->font_tex.h;
src_rect.w = g.uv[1].x * rawfb->font_tex.w - g.uv[0].x * rawfb->font_tex.w;
src_rect.h = g.uv[1].y * rawfb->font_tex.h - g.uv[0].y * rawfb->font_tex.h;
dst_rect.x = x + g.offset.x + rect.x;
dst_rect.y = g.offset.y + rect.y;
@ -943,7 +955,7 @@ nk_sdlsurface_draw_text(const struct sdlsurface_context *sdlsurface,
dst_rect.h = ceil(g.height);
/* Use software rescaling to blit glyph from font_text to framebuffer */
nk_sdlsurface_stretch_image(sdlsurface->fb, sdlsurface->font_tex, &dst_rect, &src_rect, &sdlsurface->scissors, &fg);
nk_rawfb_stretch_image(&rawfb->fb, &rawfb->font_tex, &dst_rect, &src_rect, &rawfb->scissors, &fg);
/* offset next glyph */
text_len += glyph_len;
@ -954,7 +966,7 @@ nk_sdlsurface_draw_text(const struct sdlsurface_context *sdlsurface,
}
NK_API void
nk_sdlsurface_drawimage(const struct sdlsurface_context *sdlsurface,
nk_rawfb_drawimage(const struct rawfb_context *rawfb,
const int x, const int y, const int w, const int h,
const struct nk_image *img, const struct nk_color *col)
{
@ -970,99 +982,99 @@ nk_sdlsurface_drawimage(const struct sdlsurface_context *sdlsurface,
dst_rect.y = y;
dst_rect.w = w;
dst_rect.h = h;
nk_sdlsurface_stretch_image(sdlsurface->fb, sdlsurface->font_tex, &dst_rect, &src_rect, &sdlsurface->scissors, col);
nk_rawfb_stretch_image(&rawfb->fb, &rawfb->font_tex, &dst_rect, &src_rect, &rawfb->scissors, col);
}
NK_API void
nk_sdlsurface_shutdown(struct sdlsurface_context *sdlsurface)
nk_rawfb_shutdown(struct rawfb_context *rawfb)
{
if (sdlsurface) {
SDL_FreeSurface(sdlsurface->font_tex);
nk_free(&sdlsurface->ctx);
memset(sdlsurface, 0, sizeof(struct sdlsurface_context));
free(sdlsurface);
if (rawfb) {
SDL_FreeSurface(rawfb->font_tex.surf);
nk_free(&rawfb->ctx);
memset(rawfb, 0, sizeof(struct rawfb_context));
free(rawfb);
}
}
NK_API void
nk_sdlsurface_render(const struct sdlsurface_context *sdlsurface,
nk_rawfb_render(const struct rawfb_context *rawfb,
const struct nk_color clear,
const unsigned char enable_clear)
{
const struct nk_command *cmd;
if (enable_clear)
nk_sdlsurface_clear(sdlsurface, clear);
nk_rawfb_clear(rawfb, clear);
nk_foreach(cmd, (struct nk_context*)&sdlsurface->ctx) {
nk_foreach(cmd, (struct nk_context*)&rawfb->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_sdlsurface_scissor((struct sdlsurface_context *)sdlsurface, s->x, s->y, s->w, s->h);
nk_rawfb_scissor((struct rawfb_context *)rawfb, 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_sdlsurface_stroke_line(sdlsurface, l->begin.x, l->begin.y, l->end.x,
nk_rawfb_stroke_line(rawfb, 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_sdlsurface_stroke_rect(sdlsurface, r->x, r->y, r->w, r->h,
nk_rawfb_stroke_rect(rawfb, 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_sdlsurface_fill_rect(sdlsurface, r->x, r->y, r->w, r->h,
nk_rawfb_fill_rect(rawfb, 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_sdlsurface_stroke_circle(sdlsurface, c->x, c->y, c->w, c->h, c->line_thickness, c->color);
nk_rawfb_stroke_circle(rawfb, 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_sdlsurface_fill_circle(sdlsurface, c->x, c->y, c->w, c->h, c->color);
nk_rawfb_fill_circle(rawfb, 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_sdlsurface_stroke_triangle(sdlsurface, t->a.x, t->a.y, t->b.x, t->b.y,
nk_rawfb_stroke_triangle(rawfb, 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_sdlsurface_fill_triangle(sdlsurface, t->a.x, t->a.y, t->b.x, t->b.y,
nk_rawfb_fill_triangle(rawfb, 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_sdlsurface_stroke_polygon(sdlsurface, p->points, p->point_count, p->line_thickness,p->color);
nk_rawfb_stroke_polygon(rawfb, 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_sdlsurface_fill_polygon(sdlsurface, p->points, p->point_count, p->color);
nk_rawfb_fill_polygon(rawfb, 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_sdlsurface_stroke_polyline(sdlsurface, p->points, p->point_count, p->line_thickness, p->color);
nk_rawfb_stroke_polyline(rawfb, 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_sdlsurface_draw_text(sdlsurface, t->font, nk_rect(t->x, t->y, t->w, t->h),
nk_rawfb_draw_text(rawfb, t->font, nk_rect(t->x, t->y, t->w, t->h),
t->string, t->length, t->height, t->foreground);
} break;
case NK_COMMAND_CURVE: {
const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
nk_sdlsurface_stroke_curve(sdlsurface, q->begin, q->ctrl[0], q->ctrl[1],
nk_rawfb_stroke_curve(rawfb, q->begin, q->ctrl[0], q->ctrl[1],
q->end, 22, q->line_thickness, q->color);
} break;
case NK_COMMAND_RECT_MULTI_COLOR: {
const struct nk_command_rect_multi_color *q = (const struct nk_command_rect_multi_color *)cmd;
nk_sdlsurface_draw_rect_multi_color(sdlsurface, q->x, q->y, q->w, q->h, q->left, q->top, q->right, q->bottom);
} break;
const struct nk_command_rect_multi_color *q = (const struct nk_command_rect_multi_color *)cmd;
nk_rawfb_draw_rect_multi_color(rawfb, q->x, q->y, q->w, q->h, q->left, q->top, q->right, q->bottom);
} break;
case NK_COMMAND_IMAGE: {
const struct nk_command_image *q = (const struct nk_command_image *)cmd;
nk_sdlsurface_drawimage(sdlsurface, q->x, q->y, q->w, q->h, &q->img, &q->col);
nk_rawfb_drawimage(rawfb, q->x, q->y, q->w, q->h, &q->img, &q->col);
} break;
case NK_COMMAND_ARC: {
assert(0 && "NK_COMMAND_ARC not implemented\n");
@ -1072,8 +1084,7 @@ nk_sdlsurface_render(const struct sdlsurface_context *sdlsurface,
} break;
default: break;
}
}
nk_clear((struct nk_context*)&sdlsurface->ctx);
} nk_clear((struct nk_context*)&rawfb->ctx);
}
#endif

View File

@ -4,11 +4,11 @@
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_IMPLEMENTATION
#define NK_RAWFB_IMPLEMENTATION
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_SOFTWARE_FONT
#include <wayland-client.h>
#include <stdlib.h>
#include <fcntl.h>
@ -20,11 +20,34 @@
#include <time.h>
#include <sys/time.h>
#include "../../nuklear.h"
#include "../../../nuklear.h"
#include "xdg-shell.h"
#include "nuklear_raw_wayland.h"
#include "../nuklear_rawfb.h"
struct nk_wayland {
/*wayland vars*/
struct wl_display *display;
struct wl_compositor *compositor;
struct xdg_wm_base *xdg_wm_base;
struct wl_shm *wl_shm;
struct wl_seat* seat;
struct wl_callback *frame_callback;
struct wl_surface *surface;
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
struct wl_buffer *front_buffer;
int32_t *data;
int mouse_pointer_x;
int mouse_pointer_y;
uint8_t tex_scratch[512 * 512];
struct rawfb_context *rawfb;
};
#define WIDTH 800
#define HEIGHT 600
#define DTIME 20
@ -51,19 +74,19 @@
#endif
#ifdef INCLUDE_STYLE
#include "../../demo/common/style.c"
#include "../../common/style.c"
#endif
#ifdef INCLUDE_CALCULATOR
#include "../../demo/common/calculator.c"
#include "../../common/calculator.c"
#endif
#ifdef INCLUDE_CANVAS
#include "../../demo/common/canvas.c"
#include "../../common/canvas.c"
#endif
#ifdef INCLUDE_OVERVIEW
#include "../../demo/common/overview.c"
#include "../../common/overview.c"
#endif
#ifdef INCLUDE_NODE_EDITOR
#include "../../demo/common/node_editor.c"
#include "../../common/node_editor.c"
#endif
@ -113,7 +136,7 @@ static const struct wl_output_listener nk_wayland_output_listener =
//-------------------------------------------------------------------- endof WAYLAND OUTPUT INTERFACE
//WAYLAND POINTER INTERFACE (mouse/touchpad)
static void nk_wayland_pointer_enter (void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y)
static void nk_wayland_pointer_enter (void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y)
{
NK_UNUSED(data);
NK_UNUSED(pointer);
@ -123,7 +146,7 @@ static void nk_wayland_pointer_enter (void *data, struct wl_pointer *pointer, ui
NK_UNUSED(surface_y);
}
static void nk_wayland_pointer_leave (void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface)
static void nk_wayland_pointer_leave (void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface)
{
NK_UNUSED(data);
NK_UNUSED(pointer);
@ -131,7 +154,7 @@ static void nk_wayland_pointer_leave (void *data, struct wl_pointer *pointer, ui
NK_UNUSED(surface);
}
static void nk_wayland_pointer_motion (void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t x, wl_fixed_t y)
static void nk_wayland_pointer_motion (void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t x, wl_fixed_t y)
{
struct nk_wayland* win = (struct nk_wayland*)data;
@ -140,11 +163,11 @@ static void nk_wayland_pointer_motion (void *data, struct wl_pointer *pointer, u
win->mouse_pointer_x = wl_fixed_to_int(x);
win->mouse_pointer_y = wl_fixed_to_int(y);
nk_input_motion(&(win->ctx), win->mouse_pointer_x, win->mouse_pointer_y);
nk_input_motion(&(win->rawfb->ctx), win->mouse_pointer_x, win->mouse_pointer_y);
}
static void nk_wayland_pointer_button (void *data, struct wl_pointer *pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
static void nk_wayland_pointer_button (void *data, struct wl_pointer *pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
{
struct nk_wayland* win = (struct nk_wayland*)data;
@ -155,15 +178,15 @@ static void nk_wayland_pointer_button (void *data, struct wl_pointer *pointer, u
if (button == 272){ //left mouse button
if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
// printf("nk_input_button x=%d, y=%d press: 1 \n", win->mouse_pointer_x, win->mouse_pointer_y);
nk_input_button(&(win->ctx), NK_BUTTON_LEFT, win->mouse_pointer_x, win->mouse_pointer_y, 1);
nk_input_button(&(win->rawfb->ctx), NK_BUTTON_LEFT, win->mouse_pointer_x, win->mouse_pointer_y, 1);
} else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
nk_input_button(&(win->ctx), NK_BUTTON_LEFT, win->mouse_pointer_x, win->mouse_pointer_y, 0);
nk_input_button(&(win->rawfb->ctx), NK_BUTTON_LEFT, win->mouse_pointer_x, win->mouse_pointer_y, 0);
}
}
}
static void nk_wayland_pointer_axis (void *data, struct wl_pointer *pointer, uint32_t time, uint32_t axis, wl_fixed_t value)
static void nk_wayland_pointer_axis (void *data, struct wl_pointer *pointer, uint32_t time, uint32_t axis, wl_fixed_t value)
{
NK_UNUSED(data);
NK_UNUSED(pointer);
@ -172,12 +195,12 @@ static void nk_wayland_pointer_axis (void *data, struct wl_pointer *pointer, uin
NK_UNUSED(value);
}
static struct wl_pointer_listener nk_wayland_pointer_listener =
static struct wl_pointer_listener nk_wayland_pointer_listener =
{
&nk_wayland_pointer_enter,
&nk_wayland_pointer_leave,
&nk_wayland_pointer_motion,
&nk_wayland_pointer_button,
&nk_wayland_pointer_enter,
&nk_wayland_pointer_leave,
&nk_wayland_pointer_motion,
&nk_wayland_pointer_button,
&nk_wayland_pointer_axis,
NULL,
NULL,
@ -187,7 +210,7 @@ static struct wl_pointer_listener nk_wayland_pointer_listener =
//-------------------------------------------------------------------- endof WAYLAND POINTER INTERFACE
//WAYLAND KEYBOARD INTERFACE
static void nk_wayland_keyboard_keymap (void *data, struct wl_keyboard *keyboard, uint32_t format, int32_t fd, uint32_t size)
static void nk_wayland_keyboard_keymap (void *data, struct wl_keyboard *keyboard, uint32_t format, int32_t fd, uint32_t size)
{
NK_UNUSED(data);
NK_UNUSED(keyboard);
@ -196,7 +219,7 @@ static void nk_wayland_keyboard_keymap (void *data, struct wl_keyboard *keyboard
NK_UNUSED(size);
}
static void nk_wayland_keyboard_enter (void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
static void nk_wayland_keyboard_enter (void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
{
NK_UNUSED(data);
NK_UNUSED(keyboard);
@ -205,7 +228,7 @@ static void nk_wayland_keyboard_enter (void *data, struct wl_keyboard *keyboard,
NK_UNUSED(keys);
}
static void nk_wayland_keyboard_leave (void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface)
static void nk_wayland_keyboard_leave (void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface)
{
NK_UNUSED(data);
NK_UNUSED(keyboard);
@ -213,7 +236,7 @@ static void nk_wayland_keyboard_leave (void *data, struct wl_keyboard *keyboard,
NK_UNUSED(surface);
}
static void nk_wayland_keyboard_key (void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
static void nk_wayland_keyboard_key (void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
{
NK_UNUSED(data);
NK_UNUSED(keyboard);
@ -234,22 +257,22 @@ static void nk_wayland_keyboard_modifiers (void *data, struct wl_keyboard *keybo
NK_UNUSED(group);
}
static struct wl_keyboard_listener nk_wayland_keyboard_listener =
static struct wl_keyboard_listener nk_wayland_keyboard_listener =
{
&nk_wayland_keyboard_keymap,
&nk_wayland_keyboard_enter,
&nk_wayland_keyboard_leave,
&nk_wayland_keyboard_key,
&nk_wayland_keyboard_keymap,
&nk_wayland_keyboard_enter,
&nk_wayland_keyboard_leave,
&nk_wayland_keyboard_key,
&nk_wayland_keyboard_modifiers,
NULL
};
//-------------------------------------------------------------------- endof WAYLAND KEYBOARD INTERFACE
//WAYLAND SEAT INTERFACE
static void seat_capabilities (void *data, struct wl_seat *seat, uint32_t capabilities)
static void seat_capabilities (void *data, struct wl_seat *seat, uint32_t capabilities)
{
struct nk_wayland* win = (struct nk_wayland*)data;
if (capabilities & WL_SEAT_CAPABILITY_POINTER) {
struct wl_pointer *pointer = wl_seat_get_pointer (seat);
wl_pointer_add_listener (pointer, &nk_wayland_pointer_listener, win);
@ -314,7 +337,7 @@ static struct xdg_toplevel_listener nk_wayland_xdg_toplevel_listener =
// WAYLAND REGISTRY INTERFACE
static void nk_wayland_registry_add_object (void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
static void nk_wayland_registry_add_object (void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
{
struct nk_wayland* win = (struct nk_wayland*)data;
@ -323,76 +346,38 @@ static void nk_wayland_registry_add_object (void *data, struct wl_registry *regi
//printf("looking for %s interface \n", interface);
if (!strcmp(interface,"wl_compositor")) {
win->compositor = wl_registry_bind (registry, name, &wl_compositor_interface, 1);
} else if (!strcmp(interface,"xdg_wm_base")) {
win->xdg_wm_base = wl_registry_bind (registry, name, &xdg_wm_base_interface, 1);
xdg_wm_base_add_listener (win->xdg_wm_base, &nk_wayland_xdg_wm_base_listener, win);
} else if (!strcmp(interface,"wl_shm")) {
win->wl_shm = wl_registry_bind (registry, name, &wl_shm_interface, 1);
} else if (!strcmp(interface,"wl_seat")) {
win->seat = wl_registry_bind (registry, name, &wl_seat_interface, 1);
wl_seat_add_listener (win->seat, &seat_listener, win);
} else if (!strcmp(interface, "wl_output")) {
struct wl_output *wl_output = wl_registry_bind(registry, name, &wl_output_interface, 1);
wl_output_add_listener(wl_output, &nk_wayland_output_listener, NULL);
}
}
static void nk_wayland_registry_remove_object (void *data, struct wl_registry *registry, uint32_t name)
static void nk_wayland_registry_remove_object (void *data, struct wl_registry *registry, uint32_t name)
{
NK_UNUSED(data);
NK_UNUSED(registry);
NK_UNUSED(name);
}
static struct wl_registry_listener nk_wayland_registry_listener =
static struct wl_registry_listener nk_wayland_registry_listener =
{
&nk_wayland_registry_add_object,
&nk_wayland_registry_add_object,
&nk_wayland_registry_remove_object
};
//------------------------------------------------------------------------------------------------ endof WAYLAND REGISTRY INTERFACE
static void nk_wayland_init(struct nk_wayland* win)
{
const void *tex;
win->font_tex.pixels = win->tex_scratch;
win->font_tex.format = NK_FONT_ATLAS_ALPHA8;
win->font_tex.w = win->font_tex.h = 0;
if (0 == nk_init_default(&(win->ctx), 0)) {
return;
}
nk_font_atlas_init_default(&(win->atlas));
nk_font_atlas_begin(&(win->atlas));
tex = nk_font_atlas_bake(&(win->atlas), &(win->font_tex.w), &(win->font_tex.h), win->font_tex.format);
if (!tex) {
return;
}
switch(win->font_tex.format) {
case NK_FONT_ATLAS_ALPHA8:
win->font_tex.pitch = win->font_tex.w * 1;
break;
case NK_FONT_ATLAS_RGBA32:
win->font_tex.pitch = win->font_tex.w * 4;
break;
};
/* Store the font texture in tex scratch memory */
memcpy(win->font_tex.pixels, tex, win->font_tex.pitch * win->font_tex.h);
nk_font_atlas_end(&(win->atlas), nk_handle_ptr(NULL), NULL);
if (win->atlas.default_font)
nk_style_set_font(&(win->ctx), &(win->atlas.default_font->handle));
nk_style_load_all_cursors(&(win->ctx), win->atlas.cursors);
nk_wayland_scissor(win, 0, 0, win->width, win->height);
}
static void nk_wayland_deinit(struct nk_wayland *win)
static void nk_wayland_deinit(struct nk_wayland *win)
{
xdg_toplevel_destroy (win->xdg_toplevel);
xdg_surface_destroy (win->xdg_surface);
@ -421,16 +406,16 @@ static void nk_wayland_surf_clear(struct nk_wayland* win)
{
int x, y;
int pix_idx;
for (y = 0; y < win->height; y++){
for (x = 0; x < win->width; x++){
pix_idx = y * win->width + x;
for (y = 0; y < HEIGHT; y++){
for (x = 0; x < WIDTH; x++){
pix_idx = y * WIDTH + x;
win->data[pix_idx] = 0xFF000000;
}
}
}
//This causes the screen to refresh
//This causes the screen to refresh
static const struct wl_callback_listener frame_listener;
static void redraw(void *data, struct wl_callback *callback, uint32_t time)
@ -442,15 +427,15 @@ static void redraw(void *data, struct wl_callback *callback, uint32_t time)
NK_UNUSED(time);
wl_callback_destroy(win->frame_callback);
wl_surface_damage(win->surface, 0, 0, WIDTH, HEIGHT);
wl_surface_damage(win->surface, 0, 0, WIDTH, HEIGHT);
win->frame_callback = wl_surface_frame(win->surface);
wl_surface_attach(win->surface, win->front_buffer, 0, 0);
wl_callback_add_listener(win->frame_callback, &frame_listener, win);
wl_surface_commit(win->surface);
}
@ -458,30 +443,27 @@ static const struct wl_callback_listener frame_listener = {
redraw
};
int main ()
int main ()
{
long dt;
long started;
struct nk_wayland nk_wayland_ctx;
struct wl_registry *registry;
int running = 1;
//1. Initialize display
nk_wayland_ctx.display = wl_display_connect (NULL);
if (nk_wayland_ctx.display == NULL) {
printf("no wayland display found. do you have wayland composer running? \n");
return -1;
}
registry = wl_display_get_registry (nk_wayland_ctx.display);
wl_registry_add_listener (registry, &nk_wayland_registry_listener, &nk_wayland_ctx);
wl_display_roundtrip (nk_wayland_ctx.display);
//2. Create Window
nk_wayland_ctx.width = WIDTH;
nk_wayland_ctx.height = HEIGHT;
//2. Create Window
nk_wayland_ctx.surface = wl_compositor_create_surface (nk_wayland_ctx.compositor);
nk_wayland_ctx.xdg_surface = xdg_wm_base_get_xdg_surface(nk_wayland_ctx.xdg_wm_base, nk_wayland_ctx.surface);
@ -508,70 +490,70 @@ int main ()
wl_display_roundtrip (nk_wayland_ctx.display);
//3. Clear window and start rendering loop
//3. Clear window and start rendering loop
nk_wayland_surf_clear(&nk_wayland_ctx);
wl_surface_attach (nk_wayland_ctx.surface, nk_wayland_ctx.front_buffer, 0, 0);
wl_surface_commit (nk_wayland_ctx.surface);
nk_wayland_init(&nk_wayland_ctx);
nk_rawfb_init(nk_wayland_ctx.data, nk_wayland_ctx.tex_scratch, WIDTH, HEIGHT, WIDTH*4, PIXEL_LAYOUT_XRGB_8888);
//4. rendering UI
while (running) {
started = timestamp();
// GUI
if (nk_begin(&(nk_wayland_ctx.ctx), "Demo", nk_rect(50, 50, 200, 200),
started = timestamp();
// GUI
if (nk_begin(&(nk_wayland_ctx.rawfb->ctx), "Demo", nk_rect(50, 50, 200, 200),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) {
enum {EASY, HARD};
static int op = EASY;
static int property = 20;
nk_layout_row_static(&(nk_wayland_ctx.ctx), 30, 80, 1);
if (nk_button_label(&(nk_wayland_ctx.ctx), "button")){
nk_layout_row_static(&(nk_wayland_ctx.rawfb->ctx), 30, 80, 1);
if (nk_button_label(&(nk_wayland_ctx.rawfb->ctx), "button")){
printf("button pressed\n");
}
nk_layout_row_dynamic(&(nk_wayland_ctx.ctx), 30, 2);
if (nk_option_label(&(nk_wayland_ctx.ctx), "easy", op == EASY)) op = EASY;
if (nk_option_label(&(nk_wayland_ctx.ctx), "hard", op == HARD)) op = HARD;
nk_layout_row_dynamic(&(nk_wayland_ctx.ctx), 25, 1);
nk_property_int(&(nk_wayland_ctx.ctx), "Compression:", 0, &property, 100, 10, 1);
nk_layout_row_dynamic(&(nk_wayland_ctx.rawfb->ctx), 30, 2);
if (nk_option_label(&(nk_wayland_ctx.rawfb->ctx), "easy", op == EASY)) op = EASY;
if (nk_option_label(&(nk_wayland_ctx.rawfb->ctx), "hard", op == HARD)) op = HARD;
nk_layout_row_dynamic(&(nk_wayland_ctx.rawfb->ctx), 25, 1);
nk_property_int(&(nk_wayland_ctx.rawfb->ctx), "Compression:", 0, &property, 100, 10, 1);
}
nk_end(&(nk_wayland_ctx.ctx));
if (nk_window_is_closed(&(nk_wayland_ctx.ctx), "Demo")) break;
nk_end(&(nk_wayland_ctx.rawfb->ctx));
if (nk_window_is_closed(&(nk_wayland_ctx.rawfb->ctx), "Demo")) break;
/* -------------- EXAMPLES ---------------- */
#ifdef INCLUDE_CALCULATOR
calculator(&(nk_wayland_ctx.ctx));
calculator(&(nk_wayland_ctx.rawfb->ctx));
#endif
#ifdef INCLUDE_CANVAS
canvas(&(nk_wayland_ctx.ctx));
canvas(&(nk_wayland_ctx.rawfb->ctx));
#endif
#ifdef INCLUDE_OVERVIEW
overview(&(nk_wayland_ctx.ctx));
overview(&(nk_wayland_ctx.rawfb->ctx));
#endif
#ifdef INCLUDE_NODE_EDITOR
node_editor(&(nk_wayland_ctx.ctx));
node_editor(&(nk_wayland_ctx.rawfb->ctx));
#endif
/* ----------------------------------------- */
// Draw framebuffer
nk_wayland_render(&nk_wayland_ctx, nk_rgb(30,30,30), 1);
// Draw framebuffer
nk_rawfb_render(nk_wayland_ctx.rawfb, nk_rgb(30,30,30), 1);
//handle wayland stuff (send display to FB & get inputs)
nk_input_begin(&(nk_wayland_ctx.ctx));
nk_input_begin(&(nk_wayland_ctx.rawfb->ctx));
wl_display_dispatch(nk_wayland_ctx.display);
nk_input_end(&(nk_wayland_ctx.ctx));
// Timing
nk_input_end(&(nk_wayland_ctx.rawfb->ctx));
// Timing
dt = timestamp() - started;
if (dt < DTIME)
sleep_for(DTIME - dt);
}
nk_wayland_deinit (&nk_wayland_ctx);
wl_display_disconnect (nk_wayland_ctx.display);
return 0;

View File

@ -46,8 +46,8 @@
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_SOFTWARE_FONT
#include "../../nuklear.h"
#include "nuklear_rawfb.h"
#include "../../../nuklear.h"
#include "../nuklear_rawfb.h"
#include "nuklear_xlib.h"
#define DTIME 20
@ -126,19 +126,19 @@ sleep_for(long t)
#endif
#ifdef INCLUDE_STYLE
#include "../../demo/common/style.c"
#include "../../common/style.c"
#endif
#ifdef INCLUDE_CALCULATOR
#include "../../demo/common/calculator.c"
#include "../../common/calculator.c"
#endif
#ifdef INCLUDE_CANVAS
#include "../../demo/common/canvas.c"
#include "../../common/canvas.c"
#endif
#ifdef INCLUDE_OVERVIEW
#include "../../demo/common/overview.c"
#include "../../common/overview.c"
#endif
#ifdef INCLUDE_NODE_EDITOR
#include "../../demo/common/node_editor.c"
#include "../../common/node_editor.c"
#endif
/* ===============================================================
@ -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) {

View File

@ -49,6 +49,9 @@ NK_API void nk_xlib_shutdown(void);
* ===============================================================
*/
#ifdef NK_XLIBSHM_IMPLEMENTATION
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>

View File

@ -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;
@ -133,6 +139,7 @@ main(int argc, char *argv[])
if (evt.type == SDL_QUIT) goto cleanup;
nk_sdl_handle_event(&evt);
}
nk_sdl_handle_grab(); /* optional grabbing behavior */
nk_input_end(ctx);
/* GUI */

View File

@ -30,10 +30,12 @@ NK_API void nk_sdl_shutdown(void);
* ===============================================================
*/
#ifdef NK_SDL_GL2_IMPLEMENTATION
#include <string.h>
#include <stdlib.h>
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 +122,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,27 +228,32 @@ 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);
}
NK_API void
nk_sdl_handle_grab(void)
{
struct nk_context *ctx = &sdl.ctx;
if (ctx->input.mouse.grab) {
SDL_SetRelativeMouseMode(SDL_TRUE);
} else if (ctx->input.mouse.ungrab) {
/* better support for older SDL by setting mode first; causes an extra mouse motion event */
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y);
} else if (ctx->input.mouse.grabbed) {
ctx->input.mouse.pos.x = ctx->input.mouse.prev.x;
ctx->input.mouse.pos.y = ctx->input.mouse.prev.y;
}
}
NK_API int
nk_sdl_handle_event(SDL_Event *evt)
{
struct nk_context *ctx = &sdl.ctx;
/* optional grabbing behavior */
if (ctx->input.mouse.grab) {
SDL_SetRelativeMouseMode(SDL_TRUE);
ctx->input.mouse.grab = 0;
} else if (ctx->input.mouse.ungrab) {
int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, x, y);
ctx->input.mouse.ungrab = 0;
}
switch(evt->type)
{
case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */

View File

@ -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;
@ -143,7 +149,9 @@ int main(int argc, char *argv[])
while (SDL_PollEvent(&evt)) {
if (evt.type == SDL_QUIT) goto cleanup;
nk_sdl_handle_event(&evt);
} nk_input_end(ctx);
}
nk_sdl_handle_grab(); /* optional grabbing behavior */
nk_input_end(ctx);
/* GUI */
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),

View File

@ -36,11 +36,13 @@ NK_API void nk_sdl_device_create(void);
*/
#ifdef NK_SDL_GL3_IMPLEMENTATION
#include <stdlib.h>
#include <assert.h>
#include <string.h>
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 +249,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,28 +336,33 @@ 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);
}
NK_API void
nk_sdl_handle_grab(void)
{
struct nk_context *ctx = &sdl.ctx;
if (ctx->input.mouse.grab) {
SDL_SetRelativeMouseMode(SDL_TRUE);
} else if (ctx->input.mouse.ungrab) {
/* better support for older SDL by setting mode first; causes an extra mouse motion event */
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y);
} else if (ctx->input.mouse.grabbed) {
ctx->input.mouse.pos.x = ctx->input.mouse.prev.x;
ctx->input.mouse.pos.y = ctx->input.mouse.prev.y;
}
}
NK_API int
nk_sdl_handle_event(SDL_Event *evt)
{
struct nk_context *ctx = &sdl.ctx;
/* optional grabbing behavior */
if (ctx->input.mouse.grab) {
SDL_SetRelativeMouseMode(SDL_TRUE);
ctx->input.mouse.grab = 0;
} else if (ctx->input.mouse.ungrab) {
int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, x, y);
ctx->input.mouse.ungrab = 0;
}
switch(evt->type)
{
case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */

View File

@ -92,6 +92,7 @@ MainLoop(void* loopArg){
if (evt.type == SDL_QUIT) running = nk_false;
nk_sdl_handle_event(&evt);
}
nk_sdl_handle_grab(); /* optional grabbing behavior */
nk_input_end(ctx);
@ -209,10 +210,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>

View File

@ -38,12 +38,13 @@ NK_API void nk_sdl_device_create(void);
* ===============================================================
*/
#ifdef NK_SDL_GLES2_IMPLEMENTATION
#include <stdlib.h>
#include <assert.h>
#include <string.h>
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;
@ -103,7 +104,7 @@ nk_sdl_device_create(void)
"}\n";
struct nk_sdl_device *dev = &sdl.ogl;
nk_buffer_init_default(&dev->cmds);
dev->prog = glCreateProgram();
dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER);
@ -133,7 +134,7 @@ nk_sdl_device_create(void)
dev->vp = offsetof(struct nk_sdl_vertex, position);
dev->vt = offsetof(struct nk_sdl_vertex, uv);
dev->vc = offsetof(struct nk_sdl_vertex, col);
/* Allocate buffers */
glGenBuffers(1, &dev->vbo);
glGenBuffers(1, &dev->ebo);
@ -214,7 +215,7 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b
/* Bind buffers */
glBindBuffer(GL_ARRAY_BUFFER, dev->vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo);
{
/* buffer setup */
glEnableVertexAttribArray((GLuint)dev->attrib_pos);
@ -245,7 +246,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,28 +336,33 @@ 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);
}
NK_API void
nk_sdl_handle_grab(void)
{
struct nk_context *ctx = &sdl.ctx;
if (ctx->input.mouse.grab) {
SDL_SetRelativeMouseMode(SDL_TRUE);
} else if (ctx->input.mouse.ungrab) {
/* better support for older SDL by setting mode first; causes an extra mouse motion event */
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y);
} else if (ctx->input.mouse.grabbed) {
ctx->input.mouse.pos.x = ctx->input.mouse.prev.x;
ctx->input.mouse.pos.y = ctx->input.mouse.prev.y;
}
}
NK_API int
nk_sdl_handle_event(SDL_Event *evt)
{
struct nk_context *ctx = &sdl.ctx;
/* optional grabbing behavior */
if (ctx->input.mouse.grab) {
SDL_SetRelativeMouseMode(SDL_TRUE);
ctx->input.mouse.grab = 0;
} else if (ctx->input.mouse.ungrab) {
int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, x, y);
ctx->input.mouse.ungrab = 0;
}
switch(evt->type)
{
case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */

View File

@ -155,10 +155,16 @@ main(int argc, char *argv[])
}
#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;
@ -171,6 +177,7 @@ main(int argc, char *argv[])
if (evt.type == SDL_QUIT) goto cleanup;
nk_sdl_handle_event(&evt);
}
nk_sdl_handle_grab(); /* optional grabbing behavior */
nk_input_end(ctx);
/* GUI */

View File

@ -39,12 +39,12 @@ NK_API void nk_sdl_shutdown(void);
* ===============================================================
*/
#ifdef NK_SDL_RENDERER_IMPLEMENTATION
#include <strings.h>
#include <string.h>
#include <stdlib.h>
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,27 +259,32 @@ 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);
}
NK_API void
nk_sdl_handle_grab(void)
{
struct nk_context *ctx = &sdl.ctx;
if (ctx->input.mouse.grab) {
SDL_SetRelativeMouseMode(SDL_TRUE);
} else if (ctx->input.mouse.ungrab) {
/* better support for older SDL by setting mode first; causes an extra mouse motion event */
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y);
} else if (ctx->input.mouse.grabbed) {
ctx->input.mouse.pos.x = ctx->input.mouse.prev.x;
ctx->input.mouse.pos.y = ctx->input.mouse.prev.y;
}
}
NK_API int
nk_sdl_handle_event(SDL_Event *evt)
{
struct nk_context *ctx = &sdl.ctx;
/* optional grabbing behavior */
if (ctx->input.mouse.grab) {
SDL_SetRelativeMouseMode(SDL_TRUE);
ctx->input.mouse.grab = 0;
} else if (ctx->input.mouse.ungrab) {
int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, x, y);
ctx->input.mouse.ungrab = 0;
}
switch(evt->type)
{
case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */

View File

@ -6,4 +6,4 @@ This backend provides support for [SFML 2.4](http://www.sfml-dev.org). It will w
You have to edit the Makefile provided so that you can build the demo. Edit the SFML_DIR variable to point to your SFML root folder. This will be the folder to which SFML was installed and contains the lib and include folders.
On Linux there is an extra step. You need to install the the udev development files.
On Linux there is an extra step. You need to install the udev development files.

View File

@ -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;

View File

@ -31,10 +31,12 @@ NK_API void nk_sfml_shutdown(void);
* ===============================================================
*/
#ifdef NK_SFML_GL2_IMPLEMENTATION
#include <cstdlib>
#include <cstring>
struct nk_sfml_device {
struct nk_buffer cmds;
struct nk_draw_null_texture null;
struct nk_draw_null_texture tex_null;
GLuint font_tex;
};
@ -59,7 +61,7 @@ nk_sfml_device_upload_atlas(const void* image, int width, int height)
glBindTexture(GL_TEXTURE_2D, dev->font_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, image);
}
@ -118,7 +120,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 +247,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);
}

View File

@ -8,4 +8,4 @@ This backend uses Glad to handle OpenGL extensions. You can download the Glad fi
Once SFML and Glad have been installed on your system you have to edit the Makefile provided so that you can build the demo. There are two variables that need to be edited: SFML_DIR and GLAD_DIR. Make these point to your SFML root folder and Glad root folder respectively.
On Linux there is an extra step. You need to install the the udev development files.
On Linux there is an extra step. You need to install the udev development files.

View File

@ -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;

View File

@ -38,12 +38,13 @@ NK_API void nk_sfml_device_destroy(void);
* ===============================================================
*/
#ifdef NK_SFML_GL3_IMPLEMENTATION
#include <cstring>
#include <assert.h>
#include <string>
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 +249,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 +354,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);
}
@ -451,7 +452,7 @@ nk_sfml_handle_event(sf::Event* evt)
return 1;
} else if(evt->type == sf::Event::TextEntered) {
/* 8 ~ backspace */
if (evt->text.unicode != 8) {
if (evt->text.unicode != 8) {
nk_input_unicode(ctx, evt->text.unicode);
}
return 1;

View File

@ -1,865 +0,0 @@
#ifndef NK_RAW_WAYLAND_H_
#define NK_RAW_WAYLAND_H_
#define WIDTH 800
#define HEIGHT 600
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a,b) ((a) < (b) ? (b) : (a))
#endif
typedef enum wayland_pixel_layout {
PIXEL_LAYOUT_XRGB_8888,
PIXEL_LAYOUT_RGBX_8888,
} wayland_pl;
struct wayland_img {
void *pixels;
int w, h, pitch;
wayland_pl pl;
enum nk_font_atlas_format format;
};
struct nk_wayland{
/*wayland vars*/
struct wl_display *display;
struct wl_compositor *compositor;
struct xdg_wm_base *xdg_wm_base;
struct wl_shm *wl_shm;
struct wl_seat* seat;
struct wl_callback *frame_callback;
struct wl_surface *surface;
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
struct wl_buffer *front_buffer;
/*nuklear vars*/
struct nk_context ctx;
struct nk_rect scissors;
struct nk_font_atlas atlas;
struct wayland_img font_tex;
int32_t width, height;
int32_t *data;
int mouse_pointer_x;
int mouse_pointer_y;
uint8_t tex_scratch[512 * 512];
};
static uint32_t nk_color_to_xrgb8888(struct nk_color col)
{
return (col.a << 24) + (col.r << 16) + (col.g << 8) + col.b;
}
static struct nk_color nk_wayland_int2color(const unsigned int i, wayland_pl pl)
{
struct nk_color col = {0,0,0,0};
switch (pl) {
case PIXEL_LAYOUT_RGBX_8888:
col.r = (i >> 24) & 0xff;
col.g = (i >> 16) & 0xff;
col.b = (i >> 8) & 0xff;
col.a = i & 0xff;
break;
case PIXEL_LAYOUT_XRGB_8888:
col.a = (i >> 24) & 0xff;
col.r = (i >> 16) & 0xff;
col.g = (i >> 8) & 0xff;
col.b = i & 0xff;
break;
default:
perror("nk_rawfb_int2color(): Unsupported pixel layout.\n");
break;
}
return col;
}
static unsigned int nk_wayland_color2int(const struct nk_color c, wayland_pl pl)
{
unsigned int res = 0;
switch (pl) {
case PIXEL_LAYOUT_RGBX_8888:
res |= c.r << 24;
res |= c.g << 16;
res |= c.b << 8;
res |= c.a;
break;
case PIXEL_LAYOUT_XRGB_8888:
res |= c.a << 24;
res |= c.r << 16;
res |= c.g << 8;
res |= c.b;
break;
default:
perror("nk_rawfb_color2int(): Unsupported pixel layout.\n");
break;
}
return (res);
}
static void nk_wayland_ctx_setpixel(const struct nk_wayland* win,
const short x0, const short y0, const struct nk_color col)
{
uint32_t c = nk_color_to_xrgb8888(col);
uint32_t *pixels = (uint32_t *)win->data;
unsigned int *ptr;
pixels += (y0 * win->width);
ptr = (unsigned int *)pixels + x0;
if (y0 < win->scissors.h && y0 >= win->scissors.y && x0 >= win->scissors.x && x0 < win->scissors.w){
*ptr = c;
}else {
printf("out of bound! \n");
}
}
static void nk_wayland_fill_polygon(const struct nk_wayland* win, const struct nk_vec2i *pnts, int count, const struct nk_color col)
{
int i = 0;
//#define MAX_POINTS 64
int left = 10000, top = 10000, bottom = 0, right = 0;
int nodes, nodeX[1024], pixelX, pixelY, j, swap ;
if (count == 0) return;
if (count > 1024)
count = 1024;
/* Get polygon dimensions */
for (i = 0; i < count; i++) {
if (left > pnts[i].x)
left = pnts[i].x;
if (right < pnts[i].x)
right = pnts[i].x;
if (top > pnts[i].y)
top = pnts[i].y;
if (bottom < pnts[i].y)
bottom = pnts[i].y;
} bottom++; right++;
/* Polygon scanline algorithm released under public-domain by Darel Rex Finley, 2007 */
/* Loop through the rows of the image. */
for (pixelY = top; pixelY < bottom; pixelY ++) {
nodes = 0; /* Build a list of nodes. */
j = count - 1;
for (i = 0; i < count; i++) {
if (((pnts[i].y < pixelY) && (pnts[j].y >= pixelY)) ||
((pnts[j].y < pixelY) && (pnts[i].y >= pixelY))) {
nodeX[nodes++]= (int)((float)pnts[i].x
+ ((float)pixelY - (float)pnts[i].y) / ((float)pnts[j].y - (float)pnts[i].y)
* ((float)pnts[j].x - (float)pnts[i].x));
} j = i;
}
/* Sort the nodes, via a simple “Bubble” sort. */
i = 0;
while (i < nodes - 1) {
if (nodeX[i] > nodeX[i+1]) {
swap = nodeX[i];
nodeX[i] = nodeX[i+1];
nodeX[i+1] = swap;
if (i) i--;
} else i++;
}
/* Fill the pixels between node pairs. */
for (i = 0; i < nodes; i += 2) {
if (nodeX[i+0] >= right) break;
if (nodeX[i+1] > left) {
if (nodeX[i+0] < left) nodeX[i+0] = left ;
if (nodeX[i+1] > right) nodeX[i+1] = right;
for (pixelX = nodeX[i]; pixelX < nodeX[i + 1]; pixelX++)
nk_wayland_ctx_setpixel(win, pixelX, pixelY, col);
}
}
}
}
static void nk_wayland_fill_arc(const struct nk_wayland* win, short x0, short y0, short w, short h, const short s, const struct nk_color col)
{
/* Bresenham's ellipses - modified to fill one quarter */
const int a2 = (w * w) / 4;
const int b2 = (h * h) / 4;
const int fa2 = 4 * a2, fb2 = 4 * b2;
int x, y, sigma;
struct nk_vec2i pnts[3];
if (w < 1 || h < 1) return;
if (s != 0 && s != 90 && s != 180 && s != 270)
return;
/* Convert upper left to center */
h = (h + 1) / 2;
w = (w + 1) / 2;
x0 += w;
y0 += h;
pnts[0].x = x0;
pnts[0].y = y0;
pnts[2].x = x0;
pnts[2].y = y0;
/* First half */
for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
if (s == 180) {
pnts[1].x = x0 + x; pnts[1].y = y0 + y;
} else if (s == 270) {
pnts[1].x = x0 - x; pnts[1].y = y0 + y;
} else if (s == 0) {
pnts[1].x = x0 + x; pnts[1].y = y0 - y;
} else if (s == 90) {
pnts[1].x = x0 - x; pnts[1].y = y0 - y;
}
nk_wayland_fill_polygon(win, pnts, 3, col);
pnts[2] = pnts[1];
if (sigma >= 0) {
sigma += fa2 * (1 - y);
y--;
} sigma += b2 * ((4 * x) + 6);
}
/* Second half */
for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
if (s == 180) {
pnts[1].x = x0 + x; pnts[1].y = y0 + y;
} else if (s == 270) {
pnts[1].x = x0 - x; pnts[1].y = y0 + y;
} else if (s == 0) {
pnts[1].x = x0 + x; pnts[1].y = y0 - y;
} else if (s == 90) {
pnts[1].x = x0 - x; pnts[1].y = y0 - y;
}
nk_wayland_fill_polygon(win, pnts, 3, col);
pnts[2] = pnts[1];
if (sigma >= 0) {
sigma += fb2 * (1 - x);
x--;
} sigma += a2 * ((4 * y) + 6);
}
}
static void nk_wayland_img_setpixel(const struct wayland_img *img, const int x0, const int y0, const struct nk_color col)
{
unsigned int c = nk_wayland_color2int(col, img->pl);
unsigned char *ptr;
unsigned int *pixel;
NK_ASSERT(img);
if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
ptr = (unsigned char *)img->pixels + (img->pitch * y0);
pixel = (unsigned int *)ptr;
if (img->format == NK_FONT_ATLAS_ALPHA8) {
ptr[x0] = col.a;
} else {
pixel[x0] = c;
}
}
}
static struct nk_color nk_wayland_getpixel(const struct nk_wayland* win, const int x0, const int y0)
{
struct nk_color col = {0, 0, 0, 0};
uint32_t *ptr;
if (y0 < win->height && y0 >= 0 && x0 >= 0 && x0 < win->width) {
ptr = (uint32_t *)win->data + (y0 * win->width);
col = nk_wayland_int2color(*ptr, PIXEL_LAYOUT_XRGB_8888);
}
return col;
}
static struct nk_color nk_wayland_img_getpixel(const struct wayland_img *img, const int x0, const int y0)
{
struct nk_color col = {0, 0, 0, 0};
unsigned char *ptr;
unsigned int pixel;
NK_ASSERT(img);
if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
ptr = (unsigned char *)img->pixels + (img->pitch * y0);
if (img->format == NK_FONT_ATLAS_ALPHA8) {
col.a = ptr[x0];
col.b = col.g = col.r = 0xff;
} else {
pixel = ((unsigned int *)ptr)[x0];
col = nk_wayland_int2color(pixel, img->pl);
}
} return col;
}
static void nk_wayland_blendpixel(const struct nk_wayland* win, const int x0, const int y0, struct nk_color col)
{
struct nk_color col2;
unsigned char inv_a;
if (col.a == 0)
return;
inv_a = 0xff - col.a;
col2 = nk_wayland_getpixel(win, x0, y0);
col.r = (col.r * col.a + col2.r * inv_a) >> 8;
col.g = (col.g * col.a + col2.g * inv_a) >> 8;
col.b = (col.b * col.a + col2.b * inv_a) >> 8;
nk_wayland_ctx_setpixel(win, x0, y0, col);
}
static void nk_wayland_img_blendpixel(const struct wayland_img *img, const int x0, const int y0, struct nk_color col)
{
struct nk_color col2;
unsigned char inv_a;
if (col.a == 0)
return;
inv_a = 0xff - col.a;
col2 = nk_wayland_img_getpixel(img, x0, y0);
col.r = (col.r * col.a + col2.r * inv_a) >> 8;
col.g = (col.g * col.a + col2.g * inv_a) >> 8;
col.b = (col.b * col.a + col2.b * inv_a) >> 8;
nk_wayland_img_setpixel(img, x0, y0, col);
}
static void nk_wayland_line_horizontal(const struct nk_wayland* win, const short x0, const short y, const short x1, const struct nk_color col)
{
/* This function is called the most. Try to optimize it a bit...
* It does not check for scissors or image borders.
* The caller has to make sure it does no exceed bounds. */
unsigned int i, n;
unsigned int c[16];
unsigned char *pixels = (uint8_t*)win->data;
unsigned int *ptr;
pixels += (y * (win->width * 4));
ptr = (unsigned int *)pixels + x0;
n = x1 - x0;
for (i = 0; i < sizeof(c) / sizeof(c[0]); i++)
c[i] = nk_color_to_xrgb8888(col);
while (n > 16) {
memcpy((void *)ptr, c, sizeof(c));
n -= 16; ptr += 16;
} for (i = 0; i < n; i++)
ptr[i] = c[i];
}
static void nk_wayland_scissor(struct nk_wayland* win, const float x, const float y, const float w, const float h)
{
win->scissors.x = MIN(MAX(x, 0), WIDTH);
win->scissors.y = MIN(MAX(y, 0), HEIGHT);
win->scissors.w = MIN(MAX(w + x, 0), WIDTH);
win->scissors.h = MIN(MAX(h + y, 0), HEIGHT);
}
static void nk_wayland_stroke_line(const struct nk_wayland* win, short x0, short y0, short x1, short y1, const unsigned int line_thickness, const struct nk_color col)
{
short tmp;
int dy, dx, stepx, stepy;
NK_UNUSED(line_thickness);
dy = y1 - y0;
dx = x1 - x0;
//printf("\n\n\n\n");
// fast path
if (dy == 0) {
if (dx == 0 || y0 >= win->scissors.h || y0 < win->scissors.y){
return;
}
if (dx < 0) {
// swap x0 and x1
tmp = x1;
x1 = x0;
x0 = tmp;
}
x1 = MIN(win->scissors.w, x1);
x0 = MIN(win->scissors.w, x0);
x1 = MAX(win->scissors.x, x1);
x0 = MAX(win->scissors.x, x0);
nk_wayland_line_horizontal(win, x0, y0, x1, col);
return;
}
if (dy < 0) {
dy = -dy;
stepy = -1;
} else stepy = 1;
if (dx < 0) {
dx = -dx;
stepx = -1;
} else stepx = 1;
dy <<= 1;
dx <<= 1;
nk_wayland_ctx_setpixel(win, x0, y0, col);
if (dx > dy) {
int fraction = dy - (dx >> 1);
while (x0 != x1) {
if (fraction >= 0) {
y0 += stepy;
fraction -= dx;
}
x0 += stepx;
fraction += dy;
nk_wayland_ctx_setpixel(win, x0, y0, col);
}
} else {
int fraction = dx - (dy >> 1);
while (y0 != y1) {
if (fraction >= 0) {
x0 += stepx;
fraction -= dy;
}
y0 += stepy;
fraction += dx;
nk_wayland_ctx_setpixel(win, x0, y0, col);
}
}
}
static void
nk_wayland_fill_rect(const struct nk_wayland* win,
const short x, const short y, const short w, const short h,
const short r, const struct nk_color col)
{
int i;
if (r == 0) {
for (i = 0; i < h; i++)
nk_wayland_stroke_line(win, x, y + i, x + w, y + i, 1, col);
} else {
const short xc = x + r;
const short yc = y + r;
const short wc = (short)(w - 2 * r);
const short hc = (short)(h - 2 * r);
struct nk_vec2i pnts[12];
pnts[0].x = x;
pnts[0].y = yc;
pnts[1].x = xc;
pnts[1].y = yc;
pnts[2].x = xc;
pnts[2].y = y;
pnts[3].x = xc + wc;
pnts[3].y = y;
pnts[4].x = xc + wc;
pnts[4].y = yc;
pnts[5].x = x + w;
pnts[5].y = yc;
pnts[6].x = x + w;
pnts[6].y = yc + hc;
pnts[7].x = xc + wc;
pnts[7].y = yc + hc;
pnts[8].x = xc + wc;
pnts[8].y = y + h;
pnts[9].x = xc;
pnts[9].y = y + h;
pnts[10].x = xc;
pnts[10].y = yc + hc;
pnts[11].x = x;
pnts[11].y = yc + hc;
nk_wayland_fill_polygon(win, pnts, 12, col);
nk_wayland_fill_arc(win, xc + wc - r, y,
(unsigned)r*2, (unsigned)r*2, 0 , col);
nk_wayland_fill_arc(win, x, y,
(unsigned)r*2, (unsigned)r*2, 90 , col);
nk_wayland_fill_arc(win, x, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 270 , col);
nk_wayland_fill_arc(win, xc + wc - r, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 180 , col);
}
}
static void nk_wayland_stroke_arc(const struct nk_wayland* win,
short x0, short y0, short w, short h, const short s,
const short line_thickness, const struct nk_color col)
{
/* Bresenham's ellipses - modified to draw one quarter */
const int a2 = (w * w) / 4;
const int b2 = (h * h) / 4;
const int fa2 = 4 * a2, fb2 = 4 * b2;
int x, y, sigma;
NK_UNUSED(line_thickness);
if (s != 0 && s != 90 && s != 180 && s != 270) return;
if (w < 1 || h < 1) return;
/* Convert upper left to center */
h = (h + 1) / 2;
w = (w + 1) / 2;
x0 += w; y0 += h;
/* First half */
for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
if (s == 180)
nk_wayland_ctx_setpixel(win, x0 + x, y0 + y, col);
else if (s == 270)
nk_wayland_ctx_setpixel(win, x0 - x, y0 + y, col);
else if (s == 0)
nk_wayland_ctx_setpixel(win, x0 + x, y0 - y, col);
else if (s == 90)
nk_wayland_ctx_setpixel(win, x0 - x, y0 - y, col);
if (sigma >= 0) {
sigma += fa2 * (1 - y);
y--;
} sigma += b2 * ((4 * x) + 6);
}
/* Second half */
for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
if (s == 180)
nk_wayland_ctx_setpixel(win, x0 + x, y0 + y, col);
else if (s == 270)
nk_wayland_ctx_setpixel(win, x0 - x, y0 + y, col);
else if (s == 0)
nk_wayland_ctx_setpixel(win, x0 + x, y0 - y, col);
else if (s == 90)
nk_wayland_ctx_setpixel(win, x0 - x, y0 - y, col);
if (sigma >= 0) {
sigma += fb2 * (1 - x);
x--;
} sigma += a2 * ((4 * y) + 6);
}
}
static void nk_wayland_stroke_rect(const struct nk_wayland* win,
const short x, const short y, const short w, const short h,
const short r, const short line_thickness, const struct nk_color col)
{
if (r == 0) {
nk_wayland_stroke_line(win, x, y, x + w, y, line_thickness, col);
nk_wayland_stroke_line(win, x, y + h, x + w, y + h, line_thickness, col);
nk_wayland_stroke_line(win, x, y, x, y + h, line_thickness, col);
nk_wayland_stroke_line(win, x + w, y, x + w, y + h, line_thickness, col);
} else {
const short xc = x + r;
const short yc = y + r;
const short wc = (short)(w - 2 * r);
const short hc = (short)(h - 2 * r);
nk_wayland_stroke_line(win, xc, y, xc + wc, y, line_thickness, col);
nk_wayland_stroke_line(win, x + w, yc, x + w, yc + hc, line_thickness, col);
nk_wayland_stroke_line(win, xc, y + h, xc + wc, y + h, line_thickness, col);
nk_wayland_stroke_line(win, x, yc, x, yc + hc, line_thickness, col);
nk_wayland_stroke_arc(win, xc + wc - r, y,
(unsigned)r*2, (unsigned)r*2, 0 , line_thickness, col);
nk_wayland_stroke_arc(win, x, y,
(unsigned)r*2, (unsigned)r*2, 90 , line_thickness, col);
nk_wayland_stroke_arc(win, x, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 270 , line_thickness, col);
nk_wayland_stroke_arc(win, xc + wc - r, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 180 , line_thickness, col);
}
}
static void nk_wayland_fill_triangle(const struct nk_wayland *win,
const short x0, const short y0, const short x1, const short y1,
const short x2, const short y2, const struct nk_color col)
{
struct nk_vec2i pnts[3];
pnts[0].x = x0;
pnts[0].y = y0;
pnts[1].x = x1;
pnts[1].y = y1;
pnts[2].x = x2;
pnts[2].y = y2;
nk_wayland_fill_polygon(win, pnts, 3, col);
}
static void nk_wayland_clear(const struct nk_wayland *win, const struct nk_color col)
{
nk_wayland_fill_rect(win, 0, 0, win->width, win->height, 0, col);
}
static void nk_wayland_fill_circle(struct nk_wayland* win, short x0, short y0, short w, short h, const struct nk_color col)
{
/* Bresenham's ellipses */
const int a2 = (w * w) / 4;
const int b2 = (h * h) / 4;
const int fa2 = 4 * a2, fb2 = 4 * b2;
int x, y, sigma;
/* Convert upper left to center */
h = (h + 1) / 2;
w = (w + 1) / 2;
x0 += w;
y0 += h;
/* First half */
for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
nk_wayland_stroke_line(win, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
nk_wayland_stroke_line(win, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
if (sigma >= 0) {
sigma += fa2 * (1 - y);
y--;
} sigma += b2 * ((4 * x) + 6);
}
/* Second half */
for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
nk_wayland_stroke_line(win, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
nk_wayland_stroke_line(win, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
if (sigma >= 0) {
sigma += fb2 * (1 - x);
x--;
} sigma += a2 * ((4 * y) + 6);
}
}
/**
* Copy wayland_img into nk_wayland with scissor & stretch
*/
static void nk_wayland_copy_image(const struct nk_wayland *win, const struct wayland_img *src,
const struct nk_rect *dst_rect,
const struct nk_rect *src_rect,
const struct nk_rect *dst_scissors,
const struct nk_color *fg)
{
short i, j;
struct nk_color col;
float xinc = src_rect->w / dst_rect->w;
float yinc = src_rect->h / dst_rect->h;
float xoff = src_rect->x, yoff = src_rect->y;
// Simple nearest filtering rescaling
// TODO: use bilinear filter
for (j = 0; j < (short)dst_rect->h; j++) {
for (i = 0; i < (short)dst_rect->w; i++) {
if (dst_scissors) {
if (i + (int)(dst_rect->x + 0.5f) < dst_scissors->x || i + (int)(dst_rect->x + 0.5f) >= dst_scissors->w)
continue;
if (j + (int)(dst_rect->y + 0.5f) < dst_scissors->y || j + (int)(dst_rect->y + 0.5f) >= dst_scissors->h)
continue;
}
col = nk_wayland_img_getpixel(src, (int)xoff, (int) yoff);
if (col.r || col.g || col.b)
{
col.r = fg->r;
col.g = fg->g;
col.b = fg->b;
}
nk_wayland_blendpixel(win, i + (int)(dst_rect->x + 0.5f), j + (int)(dst_rect->y + 0.5f), col);
xoff += xinc;
}
xoff = src_rect->x;
yoff += yinc;
}
}
static void nk_wayland_font_query_font_glyph(nk_handle handle, const float height, struct nk_user_font_glyph *glyph, const nk_rune codepoint, const nk_rune next_codepoint)
{
float scale;
const struct nk_font_glyph *g;
struct nk_font *font;
NK_ASSERT(glyph);
NK_UNUSED(next_codepoint);
font = (struct nk_font*)handle.ptr;
NK_ASSERT(font);
NK_ASSERT(font->glyphs);
if (!font || !glyph)
return;
scale = height/font->info.height;
g = nk_font_find_glyph(font, codepoint);
glyph->width = (g->x1 - g->x0) * scale;
glyph->height = (g->y1 - g->y0) * scale;
glyph->offset = nk_vec2(g->x0 * scale, g->y0 * scale);
glyph->xadvance = (g->xadvance * scale);
glyph->uv[0] = nk_vec2(g->u0, g->v0);
glyph->uv[1] = nk_vec2(g->u1, g->v1);
}
void nk_wayland_draw_text(const struct nk_wayland *win, const struct nk_user_font *font, const struct nk_rect rect, const char *text, const int len, const float font_height, const struct nk_color fg)
{
float x = 0;
int text_len = 0;
nk_rune unicode = 0;
nk_rune next = 0;
int glyph_len = 0;
int next_glyph_len = 0;
struct nk_user_font_glyph g;
if (!len || !text) return;
x = 0;
glyph_len = nk_utf_decode(text, &unicode, len);
if (!glyph_len) return;
// draw every glyph image
while (text_len < len && glyph_len) {
struct nk_rect src_rect;
struct nk_rect dst_rect;
float char_width = 0;
if (unicode == NK_UTF_INVALID) break;
// query currently drawn glyph information
next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
nk_wayland_font_query_font_glyph(font->userdata, font_height, &g, unicode,
(next == NK_UTF_INVALID) ? '\0' : next);
//calculate and draw glyph drawing rectangle and image
char_width = g.xadvance;
src_rect.x = g.uv[0].x * win->font_tex.w;
src_rect.y = g.uv[0].y * win->font_tex.h;
src_rect.w = g.uv[1].x * win->font_tex.w - g.uv[0].x * win->font_tex.w;
src_rect.h = g.uv[1].y * win->font_tex.h - g.uv[0].y * win->font_tex.h;
dst_rect.x = x + g.offset.x + rect.x;
dst_rect.y = g.offset.y + rect.y;
dst_rect.w = ceilf(g.width);
dst_rect.h = ceilf(g.height);
// Use software rescaling to blit glyph from font_text to framebuffer
nk_wayland_copy_image(win, &(win->font_tex), &dst_rect, &src_rect, &(win->scissors), &fg);
// offset next glyph
text_len += glyph_len;
x += char_width;
glyph_len = next_glyph_len;
unicode = next;
}
}
static void nk_wayland_render(struct nk_wayland *win, const struct nk_color clear, const unsigned char enable_clear)
{
const struct nk_command *cmd;
const struct nk_command_text *tx;
const struct nk_command_scissor *s;
const struct nk_command_rect_filled *rf;
const struct nk_command_rect *r;
const struct nk_command_circle_filled *c;
const struct nk_command_triangle_filled *t;
const struct nk_command_line *l;
const struct nk_command_polygon_filled *p;
if (enable_clear)
nk_wayland_clear(win, clear);
nk_foreach(cmd, (struct nk_context*)&(win->ctx)) {
switch (cmd->type) {
case NK_COMMAND_NOP:
//printf("NK_COMMAND_NOP \n");
break;
case NK_COMMAND_SCISSOR:
s = (const struct nk_command_scissor*)cmd;
nk_wayland_scissor(win, s->x, s->y, s->w, s->h);
break;
case NK_COMMAND_LINE:
l = (const struct nk_command_line *)cmd;
nk_wayland_stroke_line(win, l->begin.x, l->begin.y, l->end.x, l->end.y, l->line_thickness, l->color);
break;
case NK_COMMAND_RECT:
r = (const struct nk_command_rect *)cmd;
nk_wayland_stroke_rect(win, r->x, r->y, r->w, r->h, (unsigned short)r->rounding, r->line_thickness, r->color);
break;
case NK_COMMAND_RECT_FILLED:
rf = (const struct nk_command_rect_filled *)cmd;
nk_wayland_fill_rect(win, rf->x, rf->y, rf->w, rf->h, (unsigned short)rf->rounding, rf->color);
break;
case NK_COMMAND_CIRCLE:
// printf("NK_COMMAND_CIRCLE \n");
//const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
//nk_rawfb_stroke_circle(rawfb, c->x, c->y, c->w, c->h, c->line_thickness, c->color);
break;
case NK_COMMAND_CIRCLE_FILLED:
c = (const struct nk_command_circle_filled *)cmd;
nk_wayland_fill_circle(win, c->x, c->y, c->w, c->h, c->color);
//const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
//nk_rawfb_fill_circle(rawfb, c->x, c->y, c->w, c->h, c->color);
break;
case NK_COMMAND_TRIANGLE:
//printf("NK_COMMAND_TRIANGLE \n");
//const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
//nk_rawfb_stroke_triangle(rawfb, 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:
t = (const struct nk_command_triangle_filled *)cmd;
nk_wayland_fill_triangle(win, 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:
// printf("NK_COMMAND_POLYGON \n");
//const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd;
//nk_rawfb_stroke_polygon(rawfb, p->points, p->point_count, p->line_thickness,p->color);
break;
case NK_COMMAND_POLYGON_FILLED:
// printf("NK_COMMAND_POLYGON_FILLED \n");
p = (const struct nk_command_polygon_filled *)cmd;
nk_wayland_fill_polygon(win, p->points, p->point_count, p->color);
break;
case NK_COMMAND_POLYLINE:
// printf("NK_COMMAND_POLYLINE \n");
//const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
//nk_rawfb_stroke_polyline(rawfb, p->points, p->point_count, p->line_thickness, p->color);
break;
case NK_COMMAND_TEXT:
tx = (const struct nk_command_text*)cmd;
nk_wayland_draw_text(win, tx->font, nk_rect(tx->x, tx->y, tx->w, tx->h), tx->string, tx->length, tx->height, tx->foreground);
break;
case NK_COMMAND_CURVE:
// printf("NK_COMMAND_CURVE \n");
//const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
//nk_rawfb_stroke_curve(rawfb, q->begin, q->ctrl[0], q->ctrl[1], q->end, 22, q->line_thickness, q->color);
break;
case NK_COMMAND_RECT_MULTI_COLOR:
// printf("NK_COMMAND_RECT_MULTI_COLOR \n");
//const struct nk_command_rect_multi_color *q = (const struct nk_command_rect_multi_color *)cmd;
//nk_rawfb_draw_rect_multi_color(rawfb, q->x, q->y, q->w, q->h, q->left, q->top, q->right, q->bottom);
break;
case NK_COMMAND_IMAGE:
//printf("NK_COMMAND_IMAGE \n");
// const struct nk_command_image *q = (const struct nk_command_image *)cmd;
// nk_rawfb_drawimage(rawfb, q->x, q->y, q->w, q->h, &q->img, &q->col);
break;
case NK_COMMAND_ARC:
printf("NK_COMMAND_ARC \n");
assert(0 && "NK_COMMAND_ARC not implemented\n");
break;
case NK_COMMAND_ARC_FILLED:
printf("NK_COMMAND_ARC \n");
assert(0 && "NK_COMMAND_ARC_FILLED not implemented\n");
break;
default:
printf("unhandled OP: %d \n", cmd->type);
break;
}
} nk_clear(&(win->ctx));
}
#endif

View File

@ -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)

View File

@ -44,6 +44,9 @@ NK_API void nk_xfont_del(Display *dpy, XFont *font);
* ===============================================================
*/
#ifdef NK_XLIB_IMPLEMENTATION
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
@ -357,6 +360,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 +411,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;
@ -413,10 +436,10 @@ nk_stbi_image_to_xsurf(unsigned char *data, int width, int height, int channels)
int bpl = channels;
long i, isize = width*height*channels;
XImageWithAlpha *aimage = (XImageWithAlpha*)calloc( 1, sizeof(XImageWithAlpha) );
int depth = DefaultDepth(surf->dpy, surf->screen);
int depth = DefaultDepth(surf->dpy, surf->screen);
if (data == NULL) return nk_image_id(0);
if (aimage == NULL) return nk_image_id(0);
switch (depth){
case 24:
bpl = 4;
@ -429,7 +452,7 @@ nk_stbi_image_to_xsurf(unsigned char *data, int width, int height, int channels)
bpl = 1;
break;
}
/* rgba to bgra */
if (channels >= 3){
for (i=0; i < isize; i += channels) {
@ -441,9 +464,9 @@ nk_stbi_image_to_xsurf(unsigned char *data, int width, int height, int channels)
}
if (channels == 4){
const unsigned alpha_treshold = 127;
const unsigned alpha_treshold = 127;
aimage->clipMask = XCreatePixmap(surf->dpy, surf->drawable, width, height, 1);
if( aimage->clipMask ){
aimage->clipMaskGC = XCreateGC(surf->dpy, aimage->clipMask, 0, 0);
XSetForeground(surf->dpy, aimage->clipMaskGC, BlackPixel(surf->dpy, surf->screen));
@ -460,13 +483,13 @@ nk_stbi_image_to_xsurf(unsigned char *data, int width, int height, int channels)
}
}
}
aimage->ximage = XCreateImage(surf->dpy,
CopyFromParent, depth,
ZPixmap, 0,
(char*)data,
width, height,
bpl*8, bpl * width);
aimage->ximage = XCreateImage(surf->dpy,
CopyFromParent, depth,
ZPixmap, 0,
(char*)data,
width, height,
bpl*8, bpl * width);
img = nk_image_ptr( (void*)aimage);
img.h = height;
img.w = width;
@ -503,7 +526,7 @@ nk_xsurf_draw_image(XSurface *surf, short x, short y, unsigned short w, unsigned
if (aimage){
if (aimage->clipMask){
XSetClipMask(surf->dpy, surf->gc, aimage->clipMask);
XSetClipOrigin(surf->dpy, surf->gc, x, y);
XSetClipOrigin(surf->dpy, surf->gc, x, y);
}
XPutImage(surf->dpy, surf->drawable, surf->gc, aimage->ximage, 0, 0, x, y, w, h);
XSetClipMask(surf->dpy, surf->gc, None);
@ -915,6 +938,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 +970,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 +983,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;
}

View File

@ -257,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;

View File

@ -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);
}

View File

@ -254,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;

View File

@ -32,6 +32,7 @@ NK_API void nk_x11_device_destroy(void);
* ===============================================================
*/
#ifdef NK_XLIB_GL3_IMPLEMENTATION
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -173,7 +174,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 +537,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 +593,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);
}

View File

@ -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)

View File

@ -48,6 +48,9 @@ NK_API void nk_xfont_del(Display *dpy, XFont *font);
* ===============================================================
*/
#ifdef NK_XLIB_IMPLEMENTATION
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
@ -389,6 +392,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 +443,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 +452,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 +1023,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 +1055,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 +1068,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;
}

View File

@ -13,12 +13,14 @@ LIBS =
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
LIBS =
LIBS =
else
LIBS =
endif
endif
$(BIN):
rm -f $(BIN) $(OBJS)
$(BIN): clean
$(CC) $(SRC) $(CFLAGS) -o $(BIN)
clean:
rm -f $(BIN) $(OBJS)

View File

@ -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;
@ -884,14 +886,14 @@ NK_WINDOW_NO_INPUT | Prevents window of scaling, moving or getting focu
#### nk_collapse_states
State | Description
----------------|-----------------------------------------------------------
__NK_MINIMIZED__| UI section is collased and not visible until maximized
__NK_MINIMIZED__| UI section is collapsed and not visible until maximized
__NK_MAXIMIZED__| UI section is extended and visible until minimized
<br /><br />
#### nk_begin
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
------------|-----------------------------------------------------------
@ -1265,7 +1267,7 @@ Parameter | Description
__ctx__ | Must point to an previously initialized `nk_context` struct
__name__ | Identifier of the window to either hide or show
__state__ | state with either visible or hidden to modify the window with
__cond__ | condition that has to be met to actually commit the visbility state change
__cond__ | condition that has to be met to actually commit the visibility state change
### Layouting
Layouting in general describes placing widget inside a window with position and size.
While in this particular implementation there are five different APIs for layouting
@ -1304,7 +1306,7 @@ functions should be fine.
if the owning window grows in width. So the number of columns dictates
the size of each widget dynamically by formula:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
widget_width = (window_width - padding - spacing) * (1/colum_count)
widget_width = (window_width - padding - spacing) * (1/column_count)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Just like all other layouting APIs if you define more widget than columns this
library will allocate a new row and keep all layouting parameters previously
@ -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.
- [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
[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
- [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
@ -2412,7 +2760,7 @@ X...XXXXXXXXXXXXX...X - "
dynamic and static widgets.
- 2016/12/31 (1.30.0) - Extended scrollbar offset from 16-bit to 32-bit.
- 2016/12/31 (1.29.2) - Fixed closing window bug of minimized windows.
- 2016/12/03 (1.29.1) - Fixed wrapped text with no seperator and C89 error.
- 2016/12/03 (1.29.1) - Fixed wrapped text with no separator and C89 error.
- 2016/12/03 (1.29.0) - Changed text wrapping to process words not characters.
- 2016/11/22 (1.28.6) - Fixed window minimized closing bug.
- 2016/11/19 (1.28.5) - Fixed abstract combo box closing behavior.
@ -2550,7 +2898,7 @@ X...XXXXXXXXXXXXX...X - "
precision.
- 2016/08/08 (1.07.2) - Fixed compiling error without define `NK_INCLUDE_FIXED_TYPE`.
- 2016/08/08 (1.07.1) - Fixed possible floating point error inside `nk_widget` leading
to wrong wiget width calculation which results in widgets falsely
to wrong widget width calculation which results in widgets falsely
becoming tagged as not inside window and cannot be accessed.
- 2016/08/08 (1.07.0) - Nuklear now differentiates between hiding a window (NK_WINDOW_HIDDEN) and
closing a window (NK_WINDOW_CLOSED). A window can be hidden/shown

View File

@ -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;
@ -78,24 +78,24 @@ die(const char *fmt, ...)
exit(EXIT_FAILURE);
}
static struct nk_image
icon_load(const char *filename)
{
int x,y,n;
GLuint tex;
unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
if (!data) die("[SDL]: failed to load image: %s", filename);
static struct nk_image
icon_load(const char *filename)
{
int x,y,n;
GLuint tex;
unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
if (!data) die("[SDL]: failed to load image: %s", filename);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
return nk_image_id((int)tex);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
return nk_image_id((int)tex);
}
*/
@ -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);

View File

@ -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 */

View File

@ -345,7 +345,8 @@ media_init(struct media *media)
static void
file_browser_reload_directory_content(struct file_browser *browser, const char *path)
{
strncpy(browser->directory, path, MAX_PATH_LEN);
const size_t path_len = nk_strlen(path) + 1;
NK_MEMCPY(browser->directory, path, MIN(path_len, MAX_PATH_LEN));
browser->directory[MAX_PATH_LEN - 1] = 0;
dir_free_list(browser->files, browser->file_count);
dir_free_list(browser->directories, browser->dir_count);
@ -527,7 +528,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 +723,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 +824,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 */

View File

@ -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 */

1579
nuklear.h

File diff suppressed because it is too large Load Diff

View File

@ -7,9 +7,18 @@
/// - [y]: Minor version with non-breaking API and library changes
/// - [z]: Patch version with no direct changes to the API
///
/// - 2024/03/07 (4.12.1) - Fix bitwise operations warnings in C++20
/// - 2023/11/26 (4.12.0) - Added an alignment option to checkboxes and radio buttons.
/// - 2023/10/11 (4.11.0) - Added nk_widget_disable_begin() and nk_widget_disable_end()
/// - 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
/// - 2022/02/03 (4.9.6) - Allow overriding the NK_INV_SQRT function, similar to NK_SIN and NK_COS
@ -145,7 +154,7 @@
/// dynamic and static widgets.
/// - 2016/12/31 (1.30.0) - Extended scrollbar offset from 16-bit to 32-bit.
/// - 2016/12/31 (1.29.2) - Fixed closing window bug of minimized windows.
/// - 2016/12/03 (1.29.1) - Fixed wrapped text with no seperator and C89 error.
/// - 2016/12/03 (1.29.1) - Fixed wrapped text with no separator and C89 error.
/// - 2016/12/03 (1.29.0) - Changed text wrapping to process words not characters.
/// - 2016/11/22 (1.28.6) - Fixed window minimized closing bug.
/// - 2016/11/19 (1.28.5) - Fixed abstract combo box closing behavior.
@ -283,7 +292,7 @@
/// precision.
/// - 2016/08/08 (1.07.2) - Fixed compiling error without define `NK_INCLUDE_FIXED_TYPE`.
/// - 2016/08/08 (1.07.1) - Fixed possible floating point error inside `nk_widget` leading
/// to wrong wiget width calculation which results in widgets falsely
/// to wrong widget width calculation which results in widgets falsely
/// becoming tagged as not inside window and cannot be accessed.
/// - 2016/08/08 (1.07.0) - Nuklear now differentiates between hiding a window (NK_WINDOW_HIDDEN) and
/// closing a window (NK_WINDOW_CLOSED). A window can be hidden/shown

View File

@ -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

View File

@ -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

View File

@ -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(__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(__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 */
@ -1241,7 +1241,7 @@ NK_API const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*
/// #### nk_collapse_states
/// State | Description
/// ----------------|-----------------------------------------------------------
/// __NK_MINIMIZED__| UI section is collased and not visible until maximized
/// __NK_MINIMIZED__| UI section is collapsed and not visible until maximized
/// __NK_MAXIMIZED__| UI section is extended and visible until minimized
/// <br /><br />
*/
@ -1779,9 +1779,22 @@ NK_API void nk_window_show(struct nk_context*, const char *name, enum nk_show_st
/// __ctx__ | Must point to an previously initialized `nk_context` struct
/// __name__ | Identifier of the window to either hide or show
/// __state__ | state with either visible or hidden to modify the window with
/// __cond__ | condition that has to be met to actually commit the visbility state change
/// __cond__ | condition that has to be met to actually commit the visibility state change
*/
NK_API void nk_window_show_if(struct nk_context*, const char *name, enum nk_show_states, int cond);
/*/// #### nk_window_show_if
/// Line for visual separation. Draws a line with thickness determined by the current row height.
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// void nk_rule_horizontal(struct nk_context *ctx, struct nk_color color, NK_BOOL rounding)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
///
/// Parameter | Description
/// ----------------|-------------------------------------------------------
/// __ctx__ | Must point to an previously initialized `nk_context` struct
/// __color__ | Color of the horizontal line
/// __rounding__ | Whether or not to make the line round
*/
NK_API void nk_rule_horizontal(struct nk_context *ctx, struct nk_color color, nk_bool rounding);
/* =============================================================================
*
* LAYOUT
@ -1834,7 +1847,7 @@ NK_API void nk_window_show_if(struct nk_context*, const char *name, enum nk_show
/// the size of each widget dynamically by formula:
///
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// widget_width = (window_width - padding - spacing) * (1/colum_count)
/// widget_width = (window_width - padding - spacing) * (1/column_count)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
///
/// Just like all other layouting APIs if you define more widget than columns this
@ -1932,7 +1945,7 @@ NK_API void nk_window_show_if(struct nk_context*, const char *name, enum nk_show
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~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(...);
@ -2055,6 +2068,21 @@ NK_API void nk_window_show_if(struct nk_context*, const char *name, enum nk_show
/// nk_layout_space_rect_to_screen | Converts rectangle from nk_layout_space coordinate space into screen space
/// nk_layout_space_rect_to_local | Converts rectangle from screen space into nk_layout_space coordinates
*/
enum nk_widget_align {
NK_WIDGET_ALIGN_LEFT = 0x01,
NK_WIDGET_ALIGN_CENTERED = 0x02,
NK_WIDGET_ALIGN_RIGHT = 0x04,
NK_WIDGET_ALIGN_TOP = 0x08,
NK_WIDGET_ALIGN_MIDDLE = 0x10,
NK_WIDGET_ALIGN_BOTTOM = 0x20
};
enum nk_widget_alignment {
NK_WIDGET_LEFT = NK_WIDGET_ALIGN_MIDDLE|NK_WIDGET_ALIGN_LEFT,
NK_WIDGET_CENTERED = NK_WIDGET_ALIGN_MIDDLE|NK_WIDGET_ALIGN_CENTERED,
NK_WIDGET_RIGHT = NK_WIDGET_ALIGN_MIDDLE|NK_WIDGET_ALIGN_RIGHT
};
/*/// #### nk_layout_set_min_row_height
/// Sets the currently used minimum row height.
/// !!! WARNING
@ -2854,7 +2882,8 @@ NK_API void nk_list_view_end(struct nk_list_view*);
enum nk_widget_layout_states {
NK_WIDGET_INVALID, /* The widget cannot be seen and is completely out of view */
NK_WIDGET_VALID, /* The widget is completely inside the window and can be updated and drawn */
NK_WIDGET_ROM /* The widget is partially visible and cannot be updated */
NK_WIDGET_ROM, /* The widget is partially visible and cannot be updated */
NK_WIDGET_DISABLED /* The widget is manually disabled and acts like NK_WIDGET_ROM */
};
enum nk_widget_states {
NK_WIDGET_STATE_MODIFIED = NK_FLAG(1),
@ -2877,6 +2906,8 @@ NK_API nk_bool nk_widget_is_hovered(struct nk_context*);
NK_API nk_bool nk_widget_is_mouse_clicked(struct nk_context*, enum nk_buttons);
NK_API nk_bool nk_widget_has_mouse_click_down(struct nk_context*, enum nk_buttons, nk_bool down);
NK_API void nk_spacing(struct nk_context*, int cols);
NK_API void nk_widget_disable_begin(struct nk_context* ctx);
NK_API void nk_widget_disable_end(struct nk_context* ctx);
/* =============================================================================
*
* TEXT
@ -2954,10 +2985,13 @@ NK_API nk_bool nk_button_pop_behavior(struct nk_context*);
* ============================================================================= */
NK_API nk_bool nk_check_label(struct nk_context*, const char*, nk_bool active);
NK_API nk_bool nk_check_text(struct nk_context*, const char*, int, nk_bool active);
NK_API nk_bool nk_check_text_align(struct nk_context*, const char*, int, nk_bool active, nk_flags widget_alignment, nk_flags text_alignment);
NK_API unsigned nk_check_flags_label(struct nk_context*, const char*, unsigned int flags, unsigned int value);
NK_API unsigned nk_check_flags_text(struct nk_context*, const char*, int, unsigned int flags, unsigned int value);
NK_API nk_bool nk_checkbox_label(struct nk_context*, const char*, nk_bool *active);
NK_API nk_bool nk_checkbox_label_align(struct nk_context *ctx, const char *label, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment);
NK_API nk_bool nk_checkbox_text(struct nk_context*, const char*, int, nk_bool *active);
NK_API nk_bool nk_checkbox_text_align(struct nk_context *ctx, const char *text, int len, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment);
NK_API nk_bool nk_checkbox_flags_label(struct nk_context*, const char*, unsigned int *flags, unsigned int value);
NK_API nk_bool nk_checkbox_flags_text(struct nk_context*, const char*, int, unsigned int *flags, unsigned int value);
/* =============================================================================
@ -2966,9 +3000,13 @@ NK_API nk_bool nk_checkbox_flags_text(struct nk_context*, const char*, int, unsi
*
* ============================================================================= */
NK_API nk_bool nk_radio_label(struct nk_context*, const char*, nk_bool *active);
NK_API nk_bool nk_radio_label_align(struct nk_context *ctx, const char *label, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment);
NK_API nk_bool nk_radio_text(struct nk_context*, const char*, int, nk_bool *active);
NK_API nk_bool nk_radio_text_align(struct nk_context *ctx, const char *text, int len, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment);
NK_API nk_bool nk_option_label(struct nk_context*, const char*, nk_bool active);
NK_API nk_bool nk_option_label_align(struct nk_context *ctx, const char *label, nk_bool active, nk_flags widget_alignment, nk_flags text_alignment);
NK_API nk_bool nk_option_text(struct nk_context*, const char*, int, nk_bool active);
NK_API nk_bool nk_option_text_align(struct nk_context *ctx, const char *text, int len, nk_bool is_active, nk_flags widget_alignment, nk_flags text_alignment);
/* =============================================================================
*
* SELECTABLE
@ -3371,6 +3409,9 @@ NK_API void nk_menu_end(struct nk_context*);
* STYLE
*
* ============================================================================= */
#define NK_WIDGET_DISABLED_FACTOR 0.5f
enum nk_style_colors {
NK_COLOR_TEXT,
NK_COLOR_WINDOW,
@ -3447,6 +3488,7 @@ NK_API struct nk_color nk_rgb_f(float r, float g, float b);
NK_API struct nk_color nk_rgb_fv(const float *rgb);
NK_API struct nk_color nk_rgb_cf(struct nk_colorf c);
NK_API struct nk_color nk_rgb_hex(const char *rgb);
NK_API struct nk_color nk_rgb_factor(struct nk_color col, const float factor);
NK_API struct nk_color nk_rgba(int r, int g, int b, int a);
NK_API struct nk_color nk_rgba_u32(nk_uint);
@ -3576,149 +3618,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);
@ -3889,33 +3937,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;
@ -4040,28 +4089,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
@ -4155,49 +4205,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,
@ -4482,18 +4535,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;
@ -4655,6 +4709,8 @@ struct nk_style_item {
struct nk_style_text {
struct nk_color color;
struct nk_vec2 padding;
float color_factor;
float disabled_factor;
};
struct nk_style_button {
@ -4663,6 +4719,7 @@ struct nk_style_button {
struct nk_style_item hover;
struct nk_style_item active;
struct nk_color border_color;
float color_factor_background;
/* text */
struct nk_color text_background;
@ -4670,6 +4727,7 @@ struct nk_style_button {
struct nk_color text_hover;
struct nk_color text_active;
nk_flags text_alignment;
float color_factor_text;
/* properties */
float border;
@ -4677,6 +4735,7 @@ struct nk_style_button {
struct nk_vec2 padding;
struct nk_vec2 image_padding;
struct nk_vec2 touch_padding;
float disabled_factor;
/* optional user callbacks */
nk_handle userdata;
@ -4707,6 +4766,8 @@ struct nk_style_toggle {
struct nk_vec2 touch_padding;
float spacing;
float border;
float color_factor;
float disabled_factor;
/* optional user callbacks */
nk_handle userdata;
@ -4742,6 +4803,8 @@ struct nk_style_selectable {
struct nk_vec2 padding;
struct nk_vec2 touch_padding;
struct nk_vec2 image_padding;
float color_factor;
float disabled_factor;
/* optional user callbacks */
nk_handle userdata;
@ -4774,6 +4837,8 @@ struct nk_style_slider {
struct nk_vec2 padding;
struct nk_vec2 spacing;
struct nk_vec2 cursor_size;
float color_factor;
float disabled_factor;
/* optional buttons */
int show_buttons;
@ -4807,6 +4872,8 @@ struct nk_style_progress {
float cursor_border;
float cursor_rounding;
struct nk_vec2 padding;
float color_factor;
float disabled_factor;
/* optional user callbacks */
nk_handle userdata;
@ -4833,6 +4900,8 @@ struct nk_style_scrollbar {
float border_cursor;
float rounding_cursor;
struct nk_vec2 padding;
float color_factor;
float disabled_factor;
/* optional buttons */
int show_buttons;
@ -4879,6 +4948,8 @@ struct nk_style_edit {
struct nk_vec2 scrollbar_size;
struct nk_vec2 padding;
float row_padding;
float color_factor;
float disabled_factor;
};
struct nk_style_property {
@ -4901,6 +4972,8 @@ struct nk_style_property {
float border;
float rounding;
struct nk_vec2 padding;
float color_factor;
float disabled_factor;
struct nk_style_edit edit;
struct nk_style_button inc_button;
@ -4923,6 +4996,9 @@ struct nk_style_chart {
float border;
float rounding;
struct nk_vec2 padding;
float color_factor;
float disabled_factor;
nk_bool show_markers;
};
struct nk_style_combo {
@ -4954,6 +5030,8 @@ struct nk_style_combo {
struct nk_vec2 content_padding;
struct nk_vec2 button_padding;
struct nk_vec2 spacing;
float color_factor;
float disabled_factor;
};
struct nk_style_tab {
@ -4976,6 +5054,8 @@ struct nk_style_tab {
float indent;
struct nk_vec2 padding;
struct nk_vec2 spacing;
float color_factor;
float disabled_factor;
};
enum nk_style_header_align {
@ -5109,6 +5189,7 @@ struct nk_chart_slot {
int count;
struct nk_vec2 last;
int index;
nk_bool show_markers;
};
struct nk_chart {
@ -5258,6 +5339,7 @@ struct nk_window {
struct nk_popup_state popup;
struct nk_edit_state edit;
unsigned int scrolled;
nk_bool widgets_disabled;
struct nk_table *tables;
unsigned int table_count;
@ -5271,27 +5353,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

View File

@ -98,16 +98,16 @@ nk_draw_button(struct nk_command_buffer *out,
background = &style->active;
else background = &style->normal;
switch(background->type) {
switch (background->type) {
case NK_STYLE_ITEM_IMAGE:
nk_draw_image(out, *bounds, &background->data.image, nk_white);
nk_draw_image(out, *bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor_background));
break;
case NK_STYLE_ITEM_NINE_SLICE:
nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_white);
nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor_background));
break;
case NK_STYLE_ITEM_COLOR:
nk_fill_rect(out, *bounds, style->rounding, background->data.color);
nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
nk_fill_rect(out, *bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor_background));
nk_stroke_rect(out, *bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor_background));
break;
}
return background;
@ -127,8 +127,8 @@ nk_do_button(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r,
/* calculate button content space */
content->x = r.x + style->padding.x + style->border + style->rounding;
content->y = r.y + style->padding.y + style->border + style->rounding;
content->w = r.w - (2 * style->padding.x + style->border + style->rounding*2);
content->h = r.h - (2 * style->padding.y + style->border + style->rounding*2);
content->w = r.w - (2 * (style->padding.x + style->border + style->rounding));
content->h = r.h - (2 * (style->padding.y + style->border + style->rounding));
/* execute button behavior */
bounds.x = r.x - style->touch_padding.x;
@ -157,6 +157,8 @@ nk_draw_button_text(struct nk_command_buffer *out,
text.text = style->text_active;
else text.text = style->text_normal;
text.text = nk_rgb_factor(text.text, style->color_factor_text);
text.padding = nk_vec2(0,0);
nk_widget_text(out, *content, txt, len, &text, text_alignment, font);
}
@ -204,6 +206,8 @@ nk_draw_button_symbol(struct nk_command_buffer *out,
else if (state & NK_WIDGET_STATE_ACTIVED)
sym = style->text_active;
else sym = style->text_normal;
sym = nk_rgb_factor(sym, style->color_factor_text);
nk_draw_symbol(out, type, *content, bg, sym, 1, font);
}
NK_LIB nk_bool
@ -235,7 +239,7 @@ nk_draw_button_image(struct nk_command_buffer *out,
nk_flags state, const struct nk_style_button *style, const struct nk_image *img)
{
nk_draw_button(out, bounds, state, style);
nk_draw_image(out, *content, img, nk_white);
nk_draw_image(out, *content, img, nk_rgb_factor(nk_white, style->color_factor_background));
}
NK_LIB nk_bool
nk_do_button_image(nk_flags *state,
@ -292,6 +296,8 @@ nk_draw_button_text_symbol(struct nk_command_buffer *out,
text.text = style->text_normal;
}
sym = nk_rgb_factor(sym, style->color_factor_text);
text.text = nk_rgb_factor(text.text, style->color_factor_text);
text.padding = nk_vec2(0,0);
nk_draw_symbol(out, type, *symbol, style->text_background, sym, 0, font);
nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
@ -349,9 +355,10 @@ nk_draw_button_text_image(struct nk_command_buffer *out,
text.text = style->text_active;
else text.text = style->text_normal;
text.padding = nk_vec2(0,0);
text.text = nk_rgb_factor(text.text, style->color_factor_text);
text.padding = nk_vec2(0, 0);
nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
nk_draw_image(out, *image, img, nk_white);
nk_draw_image(out, *image, img, nk_rgb_factor(nk_white, style->color_factor_background));
}
NK_LIB nk_bool
nk_do_button_text_image(nk_flags *state,
@ -456,7 +463,7 @@ nk_button_text_styled(struct nk_context *ctx,
state = nk_widget(&bounds, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
return nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
title, len, style->text_alignment, ctx->button_behavior,
style, in, ctx->style.font);
@ -501,7 +508,7 @@ nk_button_color(struct nk_context *ctx, struct nk_color color)
state = nk_widget(&bounds, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
button = ctx->style.button;
button.normal = nk_style_item_color(color);
@ -533,7 +540,7 @@ nk_button_symbol_styled(struct nk_context *ctx,
layout = win->layout;
state = nk_widget(&bounds, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
return nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, bounds,
symbol, ctx->button_behavior, style, in, ctx->style.font);
}
@ -566,7 +573,7 @@ nk_button_image_styled(struct nk_context *ctx, const struct nk_style_button *sty
state = nk_widget(&bounds, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
return nk_do_button_image(&ctx->last_widget_state, &win->buffer, bounds,
img, ctx->button_behavior, style, in);
}
@ -600,7 +607,7 @@ nk_button_symbol_text_styled(struct nk_context *ctx,
state = nk_widget(&bounds, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
return nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
symbol, text, len, align, ctx->button_behavior,
style, ctx->style.font, in);
@ -647,7 +654,7 @@ nk_button_image_text_styled(struct nk_context *ctx,
state = nk_widget(&bounds, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
return nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
bounds, img, text, len, align, ctx->button_behavior,
style, ctx->style.font, in);

View File

@ -48,26 +48,27 @@ nk_chart_begin_colored(struct nk_context *ctx, enum nk_chart_type type,
{struct nk_chart_slot *slot = &chart->slots[chart->slot++];
slot->type = type;
slot->count = count;
slot->color = color;
slot->color = nk_rgb_factor(color, style->color_factor);
slot->highlight = highlight;
slot->min = NK_MIN(min_value, max_value);
slot->max = NK_MAX(min_value, max_value);
slot->range = slot->max - slot->min;}
slot->range = slot->max - slot->min;
slot->show_markers = style->show_markers;}
/* draw chart background */
background = &style->background;
switch(background->type) {
case NK_STYLE_ITEM_IMAGE:
nk_draw_image(&win->buffer, bounds, &background->data.image, nk_white);
nk_draw_image(&win->buffer, bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
break;
case NK_STYLE_ITEM_NINE_SLICE:
nk_draw_nine_slice(&win->buffer, bounds, &background->data.slice, nk_white);
nk_draw_nine_slice(&win->buffer, bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
break;
case NK_STYLE_ITEM_COLOR:
nk_fill_rect(&win->buffer, bounds, style->rounding, style->border_color);
nk_fill_rect(&win->buffer, bounds, style->rounding, nk_rgb_factor(style->border_color, style->color_factor));
nk_fill_rect(&win->buffer, nk_shrink_rect(bounds, style->border),
style->rounding, style->background.data.color);
style->rounding, nk_rgb_factor(style->background.data.color, style->color_factor));
break;
}
return 1;
@ -84,6 +85,8 @@ nk_chart_add_slot_colored(struct nk_context *ctx, const enum nk_chart_type type,
struct nk_color color, struct nk_color highlight,
int count, float min_value, float max_value)
{
const struct nk_style_chart* style;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
@ -91,16 +94,19 @@ nk_chart_add_slot_colored(struct nk_context *ctx, const enum nk_chart_type type,
if (!ctx || !ctx->current || !ctx->current->layout) return;
if (ctx->current->layout->chart.slot >= NK_CHART_MAX_SLOT) return;
style = &ctx->style.chart;
/* add another slot into the graph */
{struct nk_chart *chart = &ctx->current->layout->chart;
struct nk_chart_slot *slot = &chart->slots[chart->slot++];
slot->type = type;
slot->count = count;
slot->color = color;
slot->color = nk_rgb_factor(color, style->color_factor);
slot->highlight = highlight;
slot->min = NK_MIN(min_value, max_value);
slot->max = NK_MAX(min_value, max_value);
slot->range = slot->max - slot->min;}
slot->range = slot->max - slot->min;
slot->show_markers = style->show_markers;}
}
NK_API void
nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type type,
@ -114,7 +120,7 @@ nk_chart_push_line(struct nk_context *ctx, struct nk_window *win,
struct nk_chart *g, float value, int slot)
{
struct nk_panel *layout = win->layout;
const struct nk_input *i = &ctx->input;
const struct nk_input *i = ctx->current->widgets_disabled ? 0 : &ctx->input;
struct nk_command_buffer *out = &win->buffer;
nk_flags ret = 0;
@ -140,14 +146,16 @@ nk_chart_push_line(struct nk_context *ctx, struct nk_window *win,
bounds.w = bounds.h = 4;
color = g->slots[slot].color;
if (!(layout->flags & NK_WINDOW_ROM) &&
if (!(layout->flags & NK_WINDOW_ROM) && i &&
NK_INBOX(i->mouse.pos.x,i->mouse.pos.y, g->slots[slot].last.x-3, g->slots[slot].last.y-3, 6, 6)){
ret = nk_input_is_mouse_hovering_rect(i, bounds) ? NK_CHART_HOVERING : 0;
ret |= (i->mouse.buttons[NK_BUTTON_LEFT].down &&
i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
color = g->slots[slot].highlight;
}
nk_fill_rect(out, bounds, 0, color);
if (g->slots[slot].show_markers) {
nk_fill_rect(out, bounds, 0, color);
}
g->slots[slot].index += 1;
return ret;
}
@ -171,7 +179,9 @@ nk_chart_push_line(struct nk_context *ctx, struct nk_window *win,
color = g->slots[slot].highlight;
}
}
nk_fill_rect(out, nk_rect(cur.x - 2, cur.y - 2, 4, 4), 0, color);
if (g->slots[slot].show_markers) {
nk_fill_rect(out, nk_rect(cur.x - 2, cur.y - 2, 4, 4), 0, color);
}
/* save current data point position */
g->slots[slot].last.x = cur.x;
@ -184,7 +194,7 @@ nk_chart_push_column(const struct nk_context *ctx, struct nk_window *win,
struct nk_chart *chart, float value, int slot)
{
struct nk_command_buffer *out = &win->buffer;
const struct nk_input *in = &ctx->input;
const struct nk_input *in = ctx->current->widgets_disabled ? 0 : &ctx->input;
struct nk_panel *layout = win->layout;
float ratio;
@ -214,7 +224,7 @@ nk_chart_push_column(const struct nk_context *ctx, struct nk_window *win,
item.x = item.x + ((float)chart->slots[slot].index);
/* user chart bar selection */
if (!(layout->flags & NK_WINDOW_ROM) &&
if (!(layout->flags & NK_WINDOW_ROM) && in &&
NK_INBOX(in->mouse.pos.x,in->mouse.pos.y,item.x,item.y,item.w,item.h)) {
ret = NK_CHART_HOVERING;
ret |= (!in->mouse.buttons[NK_BUTTON_LEFT].down &&

View File

@ -23,6 +23,16 @@ nk_parse_hex(const char *p, int length)
return i;
}
NK_API struct nk_color
nk_rgb_factor(struct nk_color col, const float factor)
{
if (factor == 1.0f)
return col;
col.r = (nk_byte)(col.r * factor);
col.g = (nk_byte)(col.g * factor);
col.b = (nk_byte)(col.b * factor);
return col;
}
NK_API struct nk_color
nk_rgba(int r, int g, int b, int a)
{
struct nk_color ret;

View File

@ -187,7 +187,7 @@ nk_color_pick(struct nk_context * ctx, struct nk_colorf *color,
layout = win->layout;
state = nk_widget(&bounds, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
return nk_do_color_picker(&ctx->last_widget_state, &win->buffer, color, fmt, bounds,
nk_vec2(0,0), in, config->font);
}

View File

@ -67,7 +67,7 @@ nk_combo_begin_text(struct nk_context *ctx, const char *selected, int len,
if (s == NK_WIDGET_INVALID)
return 0;
in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
is_clicked = nk_true;
@ -83,19 +83,21 @@ nk_combo_begin_text(struct nk_context *ctx, const char *selected, int len,
text.text = style->combo.label_normal;
}
text.text = nk_rgb_factor(text.text, style->combo.color_factor);
switch(background->type) {
case NK_STYLE_ITEM_IMAGE:
text.background = nk_rgba(0, 0, 0, 0);
nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
break;
case NK_STYLE_ITEM_NINE_SLICE:
text.background = nk_rgba(0, 0, 0, 0);
nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
break;
case NK_STYLE_ITEM_COLOR:
text.background = background->data.color;
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
break;
}
{
@ -175,7 +177,7 @@ nk_combo_begin_color(struct nk_context *ctx, struct nk_color color, struct nk_ve
if (s == NK_WIDGET_INVALID)
return 0;
in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
is_clicked = nk_true;
@ -188,14 +190,14 @@ nk_combo_begin_color(struct nk_context *ctx, struct nk_color color, struct nk_ve
switch(background->type) {
case NK_STYLE_ITEM_IMAGE:
nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
break;
case NK_STYLE_ITEM_NINE_SLICE:
nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
break;
case NK_STYLE_ITEM_COLOR:
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
break;
}
{
@ -233,7 +235,7 @@ nk_combo_begin_color(struct nk_context *ctx, struct nk_color color, struct nk_ve
bounds.w = (button.x - (style->combo.content_padding.x + style->combo.spacing.x)) - bounds.x;
else
bounds.w = header.w - 4 * style->combo.content_padding.x;
nk_fill_rect(&win->buffer, bounds, 0, color);
nk_fill_rect(&win->buffer, bounds, 0, nk_rgb_factor(color, style->combo.color_factor));
/* draw open/close button */
if (draw_button_symbol)
@ -268,7 +270,7 @@ nk_combo_begin_symbol(struct nk_context *ctx, enum nk_symbol_type symbol, struct
if (s == NK_WIDGET_INVALID)
return 0;
in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
is_clicked = nk_true;
@ -284,19 +286,21 @@ nk_combo_begin_symbol(struct nk_context *ctx, enum nk_symbol_type symbol, struct
symbol_color = style->combo.symbol_hover;
}
symbol_color = nk_rgb_factor(symbol_color, style->combo.color_factor);
switch(background->type) {
case NK_STYLE_ITEM_IMAGE:
sym_background = nk_rgba(0, 0, 0, 0);
nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
break;
case NK_STYLE_ITEM_NINE_SLICE:
sym_background = nk_rgba(0, 0, 0, 0);
nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
break;
case NK_STYLE_ITEM_COLOR:
sym_background = background->data.color;
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
break;
}
{
@ -362,7 +366,7 @@ nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len
s = nk_widget(&header, ctx);
if (!s) return 0;
in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
is_clicked = nk_true;
@ -381,19 +385,22 @@ nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len
text.text = style->combo.label_normal;
}
text.text = nk_rgb_factor(text.text, style->combo.color_factor);
symbol_color = nk_rgb_factor(symbol_color, style->combo.color_factor);
switch(background->type) {
case NK_STYLE_ITEM_IMAGE:
text.background = nk_rgba(0, 0, 0, 0);
nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
break;
case NK_STYLE_ITEM_NINE_SLICE:
text.background = nk_rgba(0, 0, 0, 0);
nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
break;
case NK_STYLE_ITEM_COLOR:
text.background = background->data.color;
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
break;
}
{
@ -464,7 +471,7 @@ nk_combo_begin_image(struct nk_context *ctx, struct nk_image img, struct nk_vec2
if (s == NK_WIDGET_INVALID)
return 0;
in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
is_clicked = nk_true;
@ -477,14 +484,14 @@ nk_combo_begin_image(struct nk_context *ctx, struct nk_image img, struct nk_vec2
switch (background->type) {
case NK_STYLE_ITEM_IMAGE:
nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
break;
case NK_STYLE_ITEM_NINE_SLICE:
nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
break;
case NK_STYLE_ITEM_COLOR:
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
break;
}
{
@ -522,7 +529,7 @@ nk_combo_begin_image(struct nk_context *ctx, struct nk_image img, struct nk_vec2
bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
else
bounds.w = header.w - 2 * style->combo.content_padding.x;
nk_draw_image(&win->buffer, bounds, &img, nk_white);
nk_draw_image(&win->buffer, bounds, &img, nk_rgb_factor(nk_white, style->combo.color_factor));
/* draw open/close button */
if (draw_button_symbol)
@ -556,7 +563,7 @@ nk_combo_begin_image_text(struct nk_context *ctx, const char *selected, int len,
s = nk_widget(&header, ctx);
if (!s) return 0;
in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
is_clicked = nk_true;
@ -572,19 +579,21 @@ nk_combo_begin_image_text(struct nk_context *ctx, const char *selected, int len,
text.text = style->combo.label_normal;
}
text.text = nk_rgb_factor(text.text, style->combo.color_factor);
switch(background->type) {
case NK_STYLE_ITEM_IMAGE:
text.background = nk_rgba(0, 0, 0, 0);
nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
break;
case NK_STYLE_ITEM_NINE_SLICE:
text.background = nk_rgba(0, 0, 0, 0);
nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
break;
case NK_STYLE_ITEM_COLOR:
text.background = background->data.color;
nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
break;
}
{
@ -623,7 +632,7 @@ nk_combo_begin_image_text(struct nk_context *ctx, const char *selected, int len,
image.y = header.y + style->combo.content_padding.y;
image.h = header.h - 2 * style->combo.content_padding.y;
image.w = image.h;
nk_draw_image(&win->buffer, image, &img, nk_white);
nk_draw_image(&win->buffer, image, &img, nk_rgb_factor(nk_white, style->combo.color_factor));
/* draw label */
text.padding = nk_vec2(0,0);

View File

@ -13,6 +13,7 @@ nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size,
struct nk_window *win;
struct nk_window *popup;
struct nk_rect body;
struct nk_input* in;
NK_STORAGE const struct nk_rect null_rect = {-1,-1,0,0};
int is_clicked = 0;
@ -33,35 +34,39 @@ nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size,
/* check if currently active contextual is active */
popup = win->popup.win;
is_open = (popup && win->popup.type == NK_PANEL_CONTEXTUAL);
is_clicked = nk_input_mouse_clicked(&ctx->input, NK_BUTTON_RIGHT, trigger_bounds);
if (win->popup.active_con && win->popup.con_count != win->popup.active_con)
return 0;
if (!is_open && win->popup.active_con)
win->popup.active_con = 0;
if ((!is_open && !is_clicked))
return 0;
in = win->widgets_disabled ? 0 : &ctx->input;
if (in) {
is_clicked = nk_input_mouse_clicked(in, NK_BUTTON_RIGHT, trigger_bounds);
if (win->popup.active_con && win->popup.con_count != win->popup.active_con)
return 0;
if (!is_open && win->popup.active_con)
win->popup.active_con = 0;
if ((!is_open && !is_clicked))
return 0;
/* calculate contextual position on click */
win->popup.active_con = win->popup.con_count;
if (is_clicked) {
body.x = ctx->input.mouse.pos.x;
body.y = ctx->input.mouse.pos.y;
} else {
body.x = popup->bounds.x;
body.y = popup->bounds.y;
}
body.w = size.x;
body.h = size.y;
/* calculate contextual position on click */
win->popup.active_con = win->popup.con_count;
if (is_clicked) {
body.x = in->mouse.pos.x;
body.y = in->mouse.pos.y;
} else {
body.x = popup->bounds.x;
body.y = popup->bounds.y;
}
/* start nonblocking contextual popup */
ret = nk_nonblock_begin(ctx, flags|NK_WINDOW_NO_SCROLLBAR, body,
body.w = size.x;
body.h = size.y;
/* start nonblocking contextual popup */
ret = nk_nonblock_begin(ctx, flags | NK_WINDOW_NO_SCROLLBAR, body,
null_rect, NK_PANEL_CONTEXTUAL);
if (ret) win->popup.type = NK_PANEL_CONTEXTUAL;
else {
win->popup.active_con = 0;
win->popup.type = NK_PANEL_NONE;
if (win->popup.win)
win->popup.win->flags = 0;
if (ret) win->popup.type = NK_PANEL_CONTEXTUAL;
else {
win->popup.active_con = 0;
win->popup.type = NK_PANEL_NONE;
if (win->popup.win)
win->popup.win->flags = 0;
}
}
return ret;
}
@ -193,7 +198,7 @@ nk_contextual_end(struct nk_context *ctx)
popup = ctx->current;
panel = popup->layout;
NK_ASSERT(popup->parent);
NK_ASSERT(panel->type & NK_PANEL_SET_POPUP);
NK_ASSERT((int)panel->type & (int)NK_PANEL_SET_POPUP);
if (panel->flags & NK_WINDOW_DYNAMIC) {
/* Close behavior
This is a bit of a hack solution since we do not know before we end our popup

View File

@ -94,6 +94,9 @@ nk_edit_draw_text(struct nk_command_buffer *out,
txt.background = background;
txt.text = foreground;
foreground = nk_rgb_factor(foreground, style->color_factor);
background = nk_rgb_factor(background, style->color_factor);
glyph_len = nk_utf_decode(text+text_len, &unicode, byte_len-text_len);
if (!glyph_len) return;
while ((text_len < byte_len) && glyph_len)
@ -331,14 +334,14 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
/* draw background frame */
switch(background->type) {
case NK_STYLE_ITEM_IMAGE:
nk_draw_image(out, bounds, &background->data.image, nk_white);
nk_draw_image(out, bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
break;
case NK_STYLE_ITEM_NINE_SLICE:
nk_draw_nine_slice(out, bounds, &background->data.slice, nk_white);
nk_draw_nine_slice(out, bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
break;
case NK_STYLE_ITEM_COLOR:
nk_fill_rect(out, bounds, style->rounding, background->data.color);
nk_stroke_rect(out, bounds, style->rounding, style->border, style->border_color);
nk_fill_rect(out, bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor));
nk_stroke_rect(out, bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor));
break;
}}
@ -547,6 +550,8 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
else
background_color = background->data.color;
cursor_color = nk_rgb_factor(cursor_color, style->color_factor);
cursor_text_color = nk_rgb_factor(cursor_text_color, style->color_factor);
if (edit->select_start == edit->select_end) {
/* no selection so just draw the complete text */
@ -654,6 +659,10 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
background_color = nk_rgba(0,0,0,0);
else
background_color = background->data.color;
background_color = nk_rgb_factor(background_color, style->color_factor);
text_color = nk_rgb_factor(text_color, style->color_factor);
nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
background_color, text_color, nk_false);
@ -773,6 +782,8 @@ nk_edit_buffer(struct nk_context *ctx, nk_flags flags,
style = &ctx->style;
state = nk_widget(&bounds, ctx);
if (!state) return state;
else if (state == NK_WIDGET_DISABLED)
flags |= NK_EDIT_READ_ONLY;
in = (win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
/* check if edit is currently hot item */

View File

@ -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;

View File

@ -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)
{

View File

@ -255,9 +255,9 @@ enum nk_toggle_type {
NK_TOGGLE_OPTION
};
NK_LIB nk_bool nk_toggle_behavior(const struct nk_input *in, struct nk_rect select, nk_flags *state, nk_bool active);
NK_LIB void nk_draw_checkbox(struct nk_command_buffer *out, nk_flags state, const struct nk_style_toggle *style, nk_bool active, const struct nk_rect *label, const struct nk_rect *selector, const struct nk_rect *cursors, const char *string, int len, const struct nk_user_font *font);
NK_LIB void nk_draw_option(struct nk_command_buffer *out, nk_flags state, const struct nk_style_toggle *style, nk_bool active, const struct nk_rect *label, const struct nk_rect *selector, const struct nk_rect *cursors, const char *string, int len, const struct nk_user_font *font);
NK_LIB nk_bool nk_do_toggle(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r, nk_bool *active, const char *str, int len, enum nk_toggle_type type, const struct nk_style_toggle *style, const struct nk_input *in, const struct nk_user_font *font);
NK_LIB void nk_draw_checkbox(struct nk_command_buffer *out, nk_flags state, const struct nk_style_toggle *style, nk_bool active, const struct nk_rect *label, const struct nk_rect *selector, const struct nk_rect *cursors, const char *string, int len, const struct nk_user_font *font, nk_flags text_alignment);
NK_LIB void nk_draw_option(struct nk_command_buffer *out, nk_flags state, const struct nk_style_toggle *style, nk_bool active, const struct nk_rect *label, const struct nk_rect *selector, const struct nk_rect *cursors, const char *string, int len, const struct nk_user_font *font, nk_flags text_alignment);
NK_LIB nk_bool nk_do_toggle(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r, nk_bool *active, const char *str, int len, enum nk_toggle_type type, const struct nk_style_toggle *style, const struct nk_input *in, const struct nk_user_font *font, nk_flags widget_alignment, nk_flags text_alignment);
/* progress */
NK_LIB nk_size nk_progress_behavior(nk_flags *state, struct nk_input *in, struct nk_rect r, struct nk_rect cursor, nk_size max, nk_size value, nk_bool modifiable);
@ -328,21 +328,36 @@ NK_LIB void nk_property(struct nk_context *ctx, const char *name, struct nk_prop
#ifdef NK_INCLUDE_FONT_BAKING
/**
* @def NK_NO_STB_RECT_PACK_IMPLEMENTATION
*
* When defined, will avoid enabling STB_RECT_PACK_IMPLEMENTATION for when stb_rect_pack.h is already implemented elsewhere.
*/
#ifndef NK_NO_STB_RECT_PACK_IMPLEMENTATION
#define STB_RECT_PACK_IMPLEMENTATION
#endif /* NK_NO_STB_RECT_PACK_IMPLEMENTATION */
/**
* @def NK_NO_STB_TRUETYPE_IMPLEMENTATION
*
* When defined, will avoid enabling STB_TRUETYPE_IMPLEMENTATION for when stb_truetype.h is already implemented elsewhere.
*/
#ifndef NK_NO_STB_TRUETYPE_IMPLEMENTATION
#define STB_TRUETYPE_IMPLEMENTATION
#endif /* NK_NO_STB_TRUETYPE_IMPLEMENTATION */
/* Allow consumer to define own STBTT_malloc/STBTT_free, and use the font atlas' allocator otherwise */
#ifndef STBTT_malloc
static void*
nk_stbtt_malloc(nk_size size, void *user_data) {
struct nk_allocator *alloc = (struct nk_allocator *) user_data;
return alloc->alloc(alloc->userdata, 0, size);
struct nk_allocator *alloc = (struct nk_allocator *) user_data;
return alloc->alloc(alloc->userdata, 0, size);
}
static void
nk_stbtt_free(void *ptr, void *user_data) {
struct nk_allocator *alloc = (struct nk_allocator *) user_data;
alloc->free(alloc->userdata, ptr);
struct nk_allocator *alloc = (struct nk_allocator *) user_data;
alloc->free(alloc->userdata, ptr);
}
#define STBTT_malloc(x,u) nk_stbtt_malloc(x,u)

View File

@ -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

View File

@ -128,7 +128,7 @@ nk_menu_begin_text(struct nk_context *ctx, const char *title, int len,
win = ctx->current;
state = nk_widget(&header, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || win->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, header,
title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
is_clicked = nk_true;
@ -158,7 +158,7 @@ nk_menu_begin_image(struct nk_context *ctx, const char *id, struct nk_image img,
win = ctx->current;
state = nk_widget(&header, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
if (nk_do_button_image(&ctx->last_widget_state, &win->buffer, header,
img, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in))
is_clicked = nk_true;
@ -183,7 +183,7 @@ nk_menu_begin_symbol(struct nk_context *ctx, const char *id,
win = ctx->current;
state = nk_widget(&header, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
if (nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, header,
sym, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
is_clicked = nk_true;
@ -208,7 +208,7 @@ nk_menu_begin_image_text(struct nk_context *ctx, const char *title, int len,
win = ctx->current;
state = nk_widget(&header, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
header, img, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
ctx->style.font, in))
@ -241,7 +241,7 @@ nk_menu_begin_symbol_text(struct nk_context *ctx, const char *title, int len,
state = nk_widget(&header, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer,
header, sym, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
ctx->style.font, in)) is_clicked = nk_true;

Some files were not shown because too many files have changed in this diff Show More