public static void nk_textedit_click(nk_text_edit state, float x, float y, NkUserFont 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,
                                                  NkUserFont 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;
                NkVec2 size =
                    (NkVec2)
                    (nk_text_calculate_text_bounds(font, text, (int)(end - text), (float)(row_height), &remaining, null, &glyphs,
                                                   true));

                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,
                                                    NkUserFont 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));
            }
        }
        public static void nk_textedit_drag(nk_text_edit state, float x, float y, NkUserFont 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, NkUserFont 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, NkUserFont font)
        {
            fixed(char *str2 = edit._string_.Str)
            {
                char *str = str2 + line_start + char_id;

                return((float)(font.Width((NkHandle)(font.Userdata), (float)(font.Height), str, 1)));
            }
        }
        public static void nk_textedit_key(nk_text_edit state, NkKeys key, int shift_mod, NkUserFont font, float row_height)
        {
retry:
            ;
            switch (key)
            {
            case NkKeys.NONE:
            case NkKeys.CTRL:
            case NkKeys.ENTER:
            case NkKeys.SHIFT:
            case NkKeys.TAB:
            case NkKeys.COPY:
            case NkKeys.CUT:
            case NkKeys.PASTE:
            case NkKeys.MAX:
            default:
                break;

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

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

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

            case NkKeys.TEXT_INSERT_MODE:
                if ((state.mode) == (NkTextEditMode.VIEW))
                {
                    state.mode = (NkTextEditMode.INSERT);
                }
                break;

            case NkKeys.TEXT_REPLACE_MODE:
                if ((state.mode) == (NkTextEditMode.VIEW))
                {
                    state.mode = (NkTextEditMode.REPLACE);
                }
                break;

            case NkKeys.TEXT_RESET_MODE:
                if (((state.mode) == (NkTextEditMode.INSERT)) || ((state.mode) == (NkTextEditMode.REPLACE)))
                {
                    state.mode = (byte)(NkTextEditMode.VIEW);
                }
                break;

            case NkKeys.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 NkKeys.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 NkKeys.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 NkKeys.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 NkKeys.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 = (NkKeys.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 NkKeys.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 = (NkKeys.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 NkKeys.DEL:
                if ((state.mode) == (NkTextEditMode.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 NkKeys.BACKSPACE:
                if ((state.mode) == (NkTextEditMode.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 NkKeys.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 NkKeys.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 NkKeys.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 NkKeys.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;
            }
        }
Exemplo n.º 8
0
 public bool StylePushFont(NkUserFont font)
 {
     return(Nk.nk_style_push_font(_ctx, font) != 0);
 }
Exemplo n.º 9
0
 public void StyleSetFont(NkUserFont font)
 {
     Nk.nk_style_set_font(_ctx, font);
 }
Exemplo n.º 10
0
 public void Setup(NkUserFont font)
 {
     Nk.nk_setup(_ctx, font);
 }