Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nk_input_is_mouse_moved() #632

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 37 additions & 30 deletions nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -3849,7 +3849,7 @@ NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune
/// 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
Expand All @@ -3872,12 +3872,12 @@ NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune
/// 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
Expand Down Expand Up @@ -3906,44 +3906,44 @@ NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune
/// 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
Expand All @@ -3954,12 +3954,12 @@ NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune
/// 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);
Expand All @@ -3968,11 +3968,11 @@ NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune
/// 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);
/// ```
Expand Down Expand Up @@ -4167,7 +4167,7 @@ NK_API void nk_font_atlas_clear(struct nk_font_atlas*);
/// 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
Expand All @@ -4176,13 +4176,13 @@ NK_API void nk_font_atlas_clear(struct nk_font_atlas*);
/// 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
Expand Down Expand Up @@ -4437,34 +4437,34 @@ NK_API void nk_textedit_redo(struct nk_text_edit*);
/// 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,
Expand Down Expand Up @@ -4747,6 +4747,7 @@ NK_API nk_bool nk_input_mouse_clicked(const struct nk_input*, enum nk_buttons, s
NK_API nk_bool nk_input_is_mouse_down(const struct nk_input*, enum nk_buttons);
NK_API nk_bool nk_input_is_mouse_pressed(const struct nk_input*, enum nk_buttons);
NK_API nk_bool nk_input_is_mouse_released(const struct nk_input*, enum nk_buttons);
NK_API nk_bool nk_input_is_mouse_moved(const struct nk_input*);
NK_API nk_bool nk_input_is_key_pressed(const struct nk_input*, enum nk_keys);
NK_API nk_bool nk_input_is_key_released(const struct nk_input*, enum nk_keys);
NK_API nk_bool nk_input_is_key_down(const struct nk_input*, enum nk_keys);
Expand All @@ -4765,7 +4766,7 @@ NK_API nk_bool nk_input_is_key_down(const struct nk_input*, enum nk_keys);
/// 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
Expand Down Expand Up @@ -5581,19 +5582,19 @@ struct nk_window {
/// 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.
Expand Down Expand Up @@ -18162,6 +18163,12 @@ nk_input_is_mouse_released(const struct nk_input *i, enum nk_buttons id)
return (!i->mouse.buttons[id].down && i->mouse.buttons[id].clicked);
}
NK_API nk_bool
nk_input_is_mouse_moved(const struct nk_input *i)
{
if (!i) return nk_false;
return i->mouse.delta.x != 0 || i->mouse.delta.y != 0;
}
NK_API nk_bool
nk_input_is_key_pressed(const struct nk_input *i, enum nk_keys key)
{
const struct nk_key *k;
Expand Down Expand Up @@ -20080,7 +20087,7 @@ nk_panel_end(struct nk_context *ctx)

/* hide scroll if no user input */
if (window->flags & NK_WINDOW_SCROLL_AUTO_HIDE) {
int has_input = ctx->input.mouse.delta.x != 0 || ctx->input.mouse.delta.y != 0 || ctx->input.mouse.scroll_delta.y != 0;
int has_input = nk_input_is_mouse_moved(&ctx->input) || ctx->input.mouse.scroll_delta.y != 0;
int is_window_hovered = nk_window_is_hovered(ctx);
int any_item_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
if ((!has_input && is_window_hovered) || (!is_window_hovered && !any_item_active))
Expand Down Expand Up @@ -27475,7 +27482,7 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
in->mouse.buttons[NK_BUTTON_LEFT].clicked) {
nk_textedit_click(edit, mouse_x, mouse_y, font, row_height);
} else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
(in->mouse.delta.x != 0.0f || in->mouse.delta.y != 0.0f)) {
nk_input_is_mouse_moved(in)) {
nk_textedit_drag(edit, mouse_x, mouse_y, font, row_height);
cursor_follow = nk_true;
} else if (is_hovered && in->mouse.buttons[NK_BUTTON_RIGHT].clicked &&
Expand Down
Loading