Skip to content
This repository has been archived by the owner on Dec 2, 2019. It is now read-only.

Bug fixes and add feature: text edit wrap #819

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

zecruel
Copy link

@zecruel zecruel commented Mar 7, 2019

Bug fixes:

  • Weird behavior in text edit when using multibyte utf8 characters (mainly in paste operations). Apparently was a mistake in calculating the length of string.
  • Text rendering out of bondary rectangle in multiline text edit. In this case, clip area must be recalculated.

New widget feature:

  • Auto wrap text in multiline edit. I do not know if it's the most elegant and stable solution, but it's working fine.

Bug fixes and add new widget feature: text edit multiline auto wrap
@irtusb
Copy link

irtusb commented Mar 7, 2019

i wonder who uses vertical tab '\v' these days

@dumblob
Copy link
Contributor

dumblob commented Mar 12, 2019

I've looked at the source and the use of \v seems correct to me.

@zecruel could you please make the changes in source files (see the src/ directory), bump version (we're following Semver), and run amalgamation?

Add feature: Multiline edit widget with automatic text wrap. Fix some bugs too.
@@ -98,7 +98,7 @@ nk_edit_draw_text(struct nk_command_buffer *out,
if (!glyph_len) return;
while ((text_len < byte_len) && glyph_len)
{
if (unicode == '\n') {
if (unicode == '\n' || unicode == '\v') {
/* new line separator so draw previous line */
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For edit auto wrap feature. Consider a vertical tab (\v) as a line break too.

@@ -421,7 +429,7 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
selection_offset_end.x = row_size.x;
select_end_ptr = text + text_len;
}
if (unicode == '\n') {
if (unicode == '\n' || unicode == '\v') {
text_size.x = NK_MAX(text_size.x, line_width);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For edit auto wrap feature. Consider a vertical tab (\v) as a line break too.

@@ -1078,7 +1078,7 @@ nk_text_calculate_text_bounds(const struct nk_user_font *font,

*glyphs = 0;
while ((text_len < byte_len) && glyph_len) {
if (unicode == '\n') {
if (unicode == '\n' || unicode == '\v') {
text_size.x = NK_MAX(text_size.x, line_width);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For edit auto wrap feature. Consider a vertical tab (\v) as a line break too.

nk_textedit_makeundo_insert(state, state->cursor, glyphs);
state->cursor += len;
state->cursor += glyphs;
state->has_preferred_x = 0;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug fix: weird behavior on multibyte characters operations.

}
NK_API int
nk_str_insert_str_char(struct nk_str *str, int pos, const char *text)
{
return nk_str_insert_text_utf8(str, pos, text, nk_strlen(text));
return nk_str_insert_text_char(str, pos, text, nk_strlen(text));
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug fix: weird behavior on multibyte characters operations.

@@ -149,6 +149,7 @@ nk_nonblock_begin(struct nk_context *ctx,
root->flags |= NK_WINDOW_REMOVE_ROM;
root = root->parent;
}
win->popup.buf.active = is_active;
return is_active;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug fix: combo boxes crashes program (points to invalid memory) in draw rotine.

win->edit.active = nk_false;
} return ret_flags;
}
NK_API nk_flags
nk_edit_string_zero_terminated(struct nk_context *ctx, nk_flags flags,
char *buffer, int max, nk_plugin_filter filter)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add feature: Multiline edit box with automatic text wrap.

@@ -468,7 +476,7 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
/* vertical scroll */
if (cursor_pos.y < edit->scrollbar.y)
edit->scrollbar.y = NK_MAX(0.0f, cursor_pos.y - row_height);
if (cursor_pos.y >= edit->scrollbar.y + area.h)
if (cursor_pos.y >= edit->scrollbar.y + area.h - row_height)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug fix: text rendering out of clip box.

/* calculate clipping rectangle */
old_clip = out->clip;
nk_unify(&clip, &old_clip, area.x, area.y, area.x + area.w, area.y + area.h);
}
if (edit->active)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug fix: text rendering out of clip box.

@zecruel
Copy link
Author

zecruel commented Mar 14, 2019

I've looked at the source and the use of \v seems correct to me.

@zecruel could you please make the changes in source files (see the src/ directory), bump version (we're following Semver), and run amalgamation?

Done. Please verify if I made correctly.
I add some comments in pull request for better explanation.

else tolerance = byte_len - text_len; /* tolerance until the string end */
if (tolerance > 3){ /* need to break */
if (last_spc){ /* if has a good point for break, use it */
nk_str_insert_text_char(&edit->string, last_spc + 1, "\v", 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vurtun does something like this happen also on other places in the library?

IMHO we shall not allow this as we can't guarantee, that we'll put the string to it's original state once edited internally by Nuklear (because it's not distinguishable from a user made edition). It seems also not intuitive for the programmer to edit the string internally when the function itself is "just for visualisation" (this edition of "inserting \v" is not done by user).

I can though understand, that such internal editions do make the code simpler. For that purpose we could consider something like a string view providing this "metainformation" about internally inserted/deleted characters, but not editing the underlying string unless it's a user made edition.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants