public static void nk_textedit_click(nk_text_edit state, float x, float y, nk_user_font font, float row_height)
 {
     state.cursor          = (int)(nk_textedit_locate_coord(state, (float)(x), (float)(y), font, (float)(row_height)));
     state.select_start    = (int)(state.cursor);
     state.select_end      = (int)(state.cursor);
     state.has_preferred_x = (byte)(0);
 }
        public static void nk_textedit_layout_row(nk_text_edit_row *r, nk_text_edit edit, int line_start_id, float row_height,
                                                  nk_user_font font)
        {
            int   glyphs = (int)(0);
            char *remaining;
            int   len = (int)(edit._string_.len);

            fixed(char *str2 = edit._string_.str)
            {
                char *end = str2 + len;

                char *  text = str2 + line_start_id;
                nk_vec2 size =
                    (nk_vec2)
                    (nk_text_calculate_text_bounds(font, text, (int)(end - text), (float)(row_height), &remaining, null, &glyphs,
                                                   (int)(NK_STOP_ON_NEW_LINE)));

                r->x0 = (float)(0.0f);
                r->x1 = (float)(size.x);
                r->baseline_y_delta = (float)(size.y);
                r->ymin             = (float)(0.0f);
                r->ymax             = (float)(size.y);
                r->num_chars        = (int)(glyphs);
            }
        }
        public static void nk_textedit_find_charpos(nk_text_find *find, nk_text_edit state, int n, int single_line,
                                                    nk_user_font font, float row_height)
        {
            nk_text_edit_row r = new nk_text_edit_row();
            int prev_start     = (int)(0);
            int z = (int)(state._string_.len);
            int i = (int)(0);
            int first;

            nk_zero(&r, (ulong)(sizeof(nk_text_edit_row)));
            if ((n) == (z))
            {
                nk_textedit_layout_row(&r, state, (int)(0), (float)(row_height), font);
                if ((single_line) != 0)
                {
                    find->first_char = (int)(0);
                    find->length     = (int)(z);
                }
                else
                {
                    while ((i) < (z))
                    {
                        prev_start = (int)(i);
                        i         += (int)(r.num_chars);
                        nk_textedit_layout_row(&r, state, (int)(i), (float)(row_height), font);
                    }
                    find->first_char = (int)(i);
                    find->length     = (int)(r.num_chars);
                }
                find->x          = (float)(r.x1);
                find->y          = (float)(r.ymin);
                find->height     = (float)(r.ymax - r.ymin);
                find->prev_first = (int)(prev_start);
                return;
            }

            find->y = (float)(0);
            for (;;)
            {
                nk_textedit_layout_row(&r, state, (int)(i), (float)(row_height), font);
                if ((n) < (i + r.num_chars))
                {
                    break;
                }
                prev_start = (int)(i);
                i         += (int)(r.num_chars);
                find->y   += (float)(r.baseline_y_delta);
            }
            find->first_char = (int)(first = (int)(i));
            find->length     = (int)(r.num_chars);
            find->height     = (float)(r.ymax - r.ymin);
            find->prev_first = (int)(prev_start);
            find->x          = (float)(r.x0);
            for (i = (int)(0); (first + i) < (n); ++i)
            {
                find->x += (float)(nk_textedit_get_width(state, (int)(first), (int)(i), font));
            }
        }
Beispiel #4
0
 public static int nk_init_fixed(nk_context ctx, void *memory, ulong size, nk_user_font font)
 {
     if (memory == null)
     {
         return(0);
     }
     nk_setup(ctx, font);
     return(1);
 }
        public static void nk_textedit_drag(nk_text_edit state, float x, float y, nk_user_font font, float row_height)
        {
            int p = (int)(nk_textedit_locate_coord(state, (float)(x), (float)(y), font, (float)(row_height)));

            if ((state.select_start) == (state.select_end))
            {
                state.select_start = (int)(state.cursor);
            }
            state.cursor = (int)(state.select_end = (int)(p));
        }
        public static int nk_textedit_locate_coord(nk_text_edit edit, float x, float y, nk_user_font font, float row_height)
        {
            nk_text_edit_row r = new nk_text_edit_row();
            int   n            = (int)(edit._string_.len);
            float base_y       = (float)(0);
            float prev_x;
            int   i = (int)(0);
            int   k;

            r.x0        = (float)(r.x1 = (float)(0));
            r.ymin      = (float)(r.ymax = (float)(0));
            r.num_chars = (int)(0);
            while ((i) < (n))
            {
                nk_textedit_layout_row(&r, edit, (int)(i), (float)(row_height), font);
                if (r.num_chars <= 0)
                {
                    return((int)(n));
                }
                if (((i) == (0)) && ((y) < (base_y + r.ymin)))
                {
                    return((int)(0));
                }
                if ((y) < (base_y + r.ymax))
                {
                    break;
                }
                i      += (int)(r.num_chars);
                base_y += (float)(r.baseline_y_delta);
            }
            if ((i) >= (n))
            {
                return((int)(n));
            }
            if ((x) < (r.x0))
            {
                return((int)(i));
            }
            if ((x) < (r.x1))
            {
                k      = (int)(i);
                prev_x = (float)(r.x0);
                for (i = (int)(0); (i) < (r.num_chars); ++i)
                {
                    float w = (float)(nk_textedit_get_width(edit, (int)(k), (int)(i), font));
                    if ((x) < (prev_x + w))
                    {
                        if ((x) < (prev_x + w / 2))
                        {
                            return((int)(k + i));
                        }
                        else
                        {
                            return((int)(k + i + 1));
                        }
                    }
                    prev_x += (float)(w);
                }
            }

            if ((edit._string_[i + r.num_chars - 1] == ('\n')))
            {
                return((int)(i + r.num_chars - 1));
            }
            else
            {
                return((int)(i + r.num_chars));
            }
        }
        public static float nk_textedit_get_width(nk_text_edit edit, int line_start, int char_id, nk_user_font font)
        {
            fixed(char *str2 = edit._string_.str)
            {
                char *str = str2 + line_start + char_id;

                return((float)(font.width((nk_handle)(font.userdata), (float)(font.height), str, 1)));
            }
        }
        public static void nk_textedit_key(nk_text_edit state, int key, int shift_mod, nk_user_font font, float row_height)
        {
retry:
            ;
            switch (key)
            {
            case NK_KEY_NONE:
            case NK_KEY_CTRL:
            case NK_KEY_ENTER:
            case NK_KEY_SHIFT:
            case NK_KEY_TAB:
            case NK_KEY_COPY:
            case NK_KEY_CUT:
            case NK_KEY_PASTE:
            case NK_KEY_MAX:
            default:
                break;

            case NK_KEY_TEXT_UNDO:
                nk_textedit_undo(state);
                state.has_preferred_x = (byte)(0);
                break;

            case NK_KEY_TEXT_REDO:
                nk_textedit_redo(state);
                state.has_preferred_x = (byte)(0);
                break;

            case NK_KEY_TEXT_SELECT_ALL:
                nk_textedit_select_all(state);
                state.has_preferred_x = (byte)(0);
                break;

            case NK_KEY_TEXT_INSERT_MODE:
                if ((state.mode) == (NK_TEXT_EDIT_MODE_VIEW))
                {
                    state.mode = (byte)(NK_TEXT_EDIT_MODE_INSERT);
                }
                break;

            case NK_KEY_TEXT_REPLACE_MODE:
                if ((state.mode) == (NK_TEXT_EDIT_MODE_VIEW))
                {
                    state.mode = (byte)(NK_TEXT_EDIT_MODE_REPLACE);
                }
                break;

            case NK_KEY_TEXT_RESET_MODE:
                if (((state.mode) == (NK_TEXT_EDIT_MODE_INSERT)) || ((state.mode) == (NK_TEXT_EDIT_MODE_REPLACE)))
                {
                    state.mode = (byte)(NK_TEXT_EDIT_MODE_VIEW);
                }
                break;

            case NK_KEY_LEFT:
                if ((shift_mod) != 0)
                {
                    nk_textedit_clamp(state);
                    nk_textedit_prep_selection_at_cursor(state);
                    if ((state.select_end) > (0))
                    {
                        --state.select_end;
                    }
                    state.cursor          = (int)(state.select_end);
                    state.has_preferred_x = (byte)(0);
                }
                else
                {
                    if (((state).select_start != (state).select_end))
                    {
                        nk_textedit_move_to_first(state);
                    }
                    else if ((state.cursor) > (0))
                    {
                        --state.cursor;
                    }
                    state.has_preferred_x = (byte)(0);
                }
                break;

            case NK_KEY_RIGHT:
                if ((shift_mod) != 0)
                {
                    nk_textedit_prep_selection_at_cursor(state);
                    ++state.select_end;
                    nk_textedit_clamp(state);
                    state.cursor          = (int)(state.select_end);
                    state.has_preferred_x = (byte)(0);
                }
                else
                {
                    if (((state).select_start != (state).select_end))
                    {
                        nk_textedit_move_to_last(state);
                    }
                    else
                    {
                        ++state.cursor;
                    }
                    nk_textedit_clamp(state);
                    state.has_preferred_x = (byte)(0);
                }
                break;

            case NK_KEY_TEXT_WORD_LEFT:
                if ((shift_mod) != 0)
                {
                    if (!((state).select_start != (state).select_end))
                    {
                        nk_textedit_prep_selection_at_cursor(state);
                    }
                    state.cursor     = (int)(nk_textedit_move_to_word_previous(state));
                    state.select_end = (int)(state.cursor);
                    nk_textedit_clamp(state);
                }
                else
                {
                    if (((state).select_start != (state).select_end))
                    {
                        nk_textedit_move_to_first(state);
                    }
                    else
                    {
                        state.cursor = (int)(nk_textedit_move_to_word_previous(state));
                        nk_textedit_clamp(state);
                    }
                }
                break;

            case NK_KEY_TEXT_WORD_RIGHT:
                if ((shift_mod) != 0)
                {
                    if (!((state).select_start != (state).select_end))
                    {
                        nk_textedit_prep_selection_at_cursor(state);
                    }
                    state.cursor     = (int)(nk_textedit_move_to_word_next(state));
                    state.select_end = (int)(state.cursor);
                    nk_textedit_clamp(state);
                }
                else
                {
                    if (((state).select_start != (state).select_end))
                    {
                        nk_textedit_move_to_last(state);
                    }
                    else
                    {
                        state.cursor = (int)(nk_textedit_move_to_word_next(state));
                        nk_textedit_clamp(state);
                    }
                }
                break;

            case NK_KEY_DOWN:
            {
                nk_text_find     find = new nk_text_find();
                nk_text_edit_row row  = new nk_text_edit_row();
                int i;
                int sel = (int)(shift_mod);
                if ((state.single_line) != 0)
                {
                    key = (int)(NK_KEY_RIGHT);
                    goto retry;
                }
                if ((sel) != 0)
                {
                    nk_textedit_prep_selection_at_cursor(state);
                }
                else if (((state).select_start != (state).select_end))
                {
                    nk_textedit_move_to_last(state);
                }
                nk_textedit_clamp(state);
                nk_textedit_find_charpos(&find, state, (int)(state.cursor), (int)(state.single_line), font, (float)(row_height));
                if ((find.length) != 0)
                {
                    float x;
                    float goal_x = (float)((state.has_preferred_x) != 0 ? state.preferred_x : find.x);
                    int   start  = (int)(find.first_char + find.length);
                    state.cursor = (int)(start);
                    nk_textedit_layout_row(&row, state, (int)(state.cursor), (float)(row_height), font);
                    x = (float)(row.x0);
                    for (i = (int)(0); ((i) < (row.num_chars)) && ((x) < (row.x1)); ++i)
                    {
                        float dx = (float)(nk_textedit_get_width(state, (int)(start), (int)(i), font));
                        x += (float)(dx);
                        if ((x) > (goal_x))
                        {
                            break;
                        }
                        ++state.cursor;
                    }
                    nk_textedit_clamp(state);
                    state.has_preferred_x = (byte)(1);
                    state.preferred_x     = (float)(goal_x);
                    if ((sel) != 0)
                    {
                        state.select_end = (int)(state.cursor);
                    }
                }
            }
            break;

            case NK_KEY_UP:
            {
                nk_text_find     find = new nk_text_find();
                nk_text_edit_row row  = new nk_text_edit_row();
                int i;
                int sel = (int)(shift_mod);
                if ((state.single_line) != 0)
                {
                    key = (int)(NK_KEY_LEFT);
                    goto retry;
                }
                if ((sel) != 0)
                {
                    nk_textedit_prep_selection_at_cursor(state);
                }
                else if (((state).select_start != (state).select_end))
                {
                    nk_textedit_move_to_first(state);
                }
                nk_textedit_clamp(state);
                nk_textedit_find_charpos(&find, state, (int)(state.cursor), (int)(state.single_line), font, (float)(row_height));
                if (find.prev_first != find.first_char)
                {
                    float x;
                    float goal_x = (float)((state.has_preferred_x) != 0 ? state.preferred_x : find.x);
                    state.cursor = (int)(find.prev_first);
                    nk_textedit_layout_row(&row, state, (int)(state.cursor), (float)(row_height), font);
                    x = (float)(row.x0);
                    for (i = (int)(0); ((i) < (row.num_chars)) && ((x) < (row.x1)); ++i)
                    {
                        float dx = (float)(nk_textedit_get_width(state, (int)(find.prev_first), (int)(i), font));
                        x += (float)(dx);
                        if ((x) > (goal_x))
                        {
                            break;
                        }
                        ++state.cursor;
                    }
                    nk_textedit_clamp(state);
                    state.has_preferred_x = (byte)(1);
                    state.preferred_x     = (float)(goal_x);
                    if ((sel) != 0)
                    {
                        state.select_end = (int)(state.cursor);
                    }
                }
            }
            break;

            case NK_KEY_DEL:
                if ((state.mode) == (NK_TEXT_EDIT_MODE_VIEW))
                {
                    break;
                }
                if (((state).select_start != (state).select_end))
                {
                    nk_textedit_delete_selection(state);
                }
                else
                {
                    int n = (int)(state._string_.len);
                    if ((state.cursor) < (n))
                    {
                        nk_textedit_delete(state, (int)(state.cursor), (int)(1));
                    }
                }
                state.has_preferred_x = (byte)(0);
                break;

            case NK_KEY_BACKSPACE:
                if ((state.mode) == (NK_TEXT_EDIT_MODE_VIEW))
                {
                    break;
                }
                if (((state).select_start != (state).select_end))
                {
                    nk_textedit_delete_selection(state);
                }
                else
                {
                    nk_textedit_clamp(state);
                    if ((state.cursor) > (0))
                    {
                        nk_textedit_delete(state, (int)(state.cursor - 1), (int)(1));
                        --state.cursor;
                    }
                }
                state.has_preferred_x = (byte)(0);
                break;

            case NK_KEY_TEXT_START:
                if ((shift_mod) != 0)
                {
                    nk_textedit_prep_selection_at_cursor(state);
                    state.cursor          = (int)(state.select_end = (int)(0));
                    state.has_preferred_x = (byte)(0);
                }
                else
                {
                    state.cursor          = (int)(state.select_start = (int)(state.select_end = (int)(0)));
                    state.has_preferred_x = (byte)(0);
                }
                break;

            case NK_KEY_TEXT_END:
                if ((shift_mod) != 0)
                {
                    nk_textedit_prep_selection_at_cursor(state);
                    state.cursor          = (int)(state.select_end = (int)(state._string_.len));
                    state.has_preferred_x = (byte)(0);
                }
                else
                {
                    state.cursor          = (int)(state._string_.len);
                    state.select_start    = (int)(state.select_end = (int)(0));
                    state.has_preferred_x = (byte)(0);
                }
                break;

            case NK_KEY_TEXT_LINE_START:
            {
                if ((shift_mod) != 0)
                {
                    nk_text_find find = new nk_text_find();
                    nk_textedit_clamp(state);
                    nk_textedit_prep_selection_at_cursor(state);
                    if (((state._string_.len) != 0) && ((state.cursor) == (state._string_.len)))
                    {
                        --state.cursor;
                    }
                    nk_textedit_find_charpos(&find, state, (int)(state.cursor), (int)(state.single_line), font, (float)(row_height));
                    state.cursor          = (int)(state.select_end = (int)(find.first_char));
                    state.has_preferred_x = (byte)(0);
                }
                else
                {
                    nk_text_find find = new nk_text_find();
                    if (((state._string_.len) != 0) && ((state.cursor) == (state._string_.len)))
                    {
                        --state.cursor;
                    }
                    nk_textedit_clamp(state);
                    nk_textedit_move_to_first(state);
                    nk_textedit_find_charpos(&find, state, (int)(state.cursor), (int)(state.single_line), font, (float)(row_height));
                    state.cursor          = (int)(find.first_char);
                    state.has_preferred_x = (byte)(0);
                }
            }
            break;

            case NK_KEY_TEXT_LINE_END:
            {
                if ((shift_mod) != 0)
                {
                    nk_text_find find = new nk_text_find();
                    nk_textedit_clamp(state);
                    nk_textedit_prep_selection_at_cursor(state);
                    nk_textedit_find_charpos(&find, state, (int)(state.cursor), (int)(state.single_line), font, (float)(row_height));
                    state.has_preferred_x = (byte)(0);
                    state.cursor          = (int)(find.first_char + find.length);
                    if (((find.length) > (0)) && ((state._string_[(int)(state.cursor - 1)]) == ('\n')))
                    {
                        --state.cursor;
                    }
                    state.select_end = (int)(state.cursor);
                }
                else
                {
                    nk_text_find find = new nk_text_find();
                    nk_textedit_clamp(state);
                    nk_textedit_move_to_first(state);
                    nk_textedit_find_charpos(&find, state, (int)(state.cursor), (int)(state.single_line), font, (float)(row_height));
                    state.has_preferred_x = (byte)(0);
                    state.cursor          = (int)(find.first_char + find.length);
                    if (((find.length) > (0)) && ((state._string_[(int)(state.cursor - 1)]) == ('\n')))
                    {
                        --state.cursor;
                    }
                }
            }
            break;
            }
        }
Beispiel #9
0
 public static int nk_init_default(nk_context ctx, nk_user_font font)
 {
     return(nk_init(ctx, font));
 }
Beispiel #10
0
 public static int nk_init(nk_context ctx, nk_user_font font)
 {
     nk_setup(ctx, font);
     return(1);
 }