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.º 2
0
        public static void nk_textedit_key(nk_text_edit state, int key, bool shift_mod, nk_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.HasPreferredX = (byte)(0);
                break;

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

            case NK_KEY_TEXT_SELECT_ALL:
                nk_textedit_select_all(state);
                state.HasPreferredX = (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)
                {
                    nk_textedit_clamp(state);
                    nk_textedit_prep_selection_at_cursor(state);
                    if ((state.SelectionEnd) > (0))
                    {
                        --state.SelectionEnd;
                    }
                    state.Cursor        = (int)(state.SelectionEnd);
                    state.HasPreferredX = (byte)(0);
                }
                else
                {
                    if (((state).SelectionStart != (state).SelectionEnd))
                    {
                        nk_textedit_move_to_first(state);
                    }
                    else if ((state.Cursor) > (0))
                    {
                        --state.Cursor;
                    }
                    state.HasPreferredX = (byte)(0);
                }
                break;

            case NK_KEY_RIGHT:
                if (shift_mod)
                {
                    nk_textedit_prep_selection_at_cursor(state);
                    ++state.SelectionEnd;
                    nk_textedit_clamp(state);
                    state.Cursor        = (int)(state.SelectionEnd);
                    state.HasPreferredX = (byte)(0);
                }
                else
                {
                    if (((state).SelectionStart != (state).SelectionEnd))
                    {
                        nk_textedit_move_to_last(state);
                    }
                    else
                    {
                        ++state.Cursor;
                    }
                    nk_textedit_clamp(state);
                    state.HasPreferredX = (byte)(0);
                }
                break;

            case NK_KEY_TEXT_WORD_LEFT:
                if (shift_mod)
                {
                    if (!((state).SelectionStart != (state).SelectionEnd))
                    {
                        nk_textedit_prep_selection_at_cursor(state);
                    }
                    state.Cursor       = (int)(nk_textedit_move_to_word_previous(state));
                    state.SelectionEnd = (int)(state.Cursor);
                    nk_textedit_clamp(state);
                }
                else
                {
                    if (((state).SelectionStart != (state).SelectionEnd))
                    {
                        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)
                {
                    if (!((state).SelectionStart != (state).SelectionEnd))
                    {
                        nk_textedit_prep_selection_at_cursor(state);
                    }
                    state.Cursor       = (int)(nk_textedit_move_to_word_next(state));
                    state.SelectionEnd = (int)(state.Cursor);
                    nk_textedit_clamp(state);
                }
                else
                {
                    if (((state).SelectionStart != (state).SelectionEnd))
                    {
                        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;
                bool sel = shift_mod;
                if ((state.SingleLine) != 0)
                {
                    key = (int)(NK_KEY_RIGHT);
                    goto retry;
                }
                if (sel)
                {
                    nk_textedit_prep_selection_at_cursor(state);
                }
                else if (((state).SelectionStart != (state).SelectionEnd))
                {
                    nk_textedit_move_to_last(state);
                }
                nk_textedit_clamp(state);
                nk_textedit_find_charpos(&find, state, (int)(state.Cursor), (int)(state.SingleLine), font, (float)(row_height));
                if ((find.length) != 0)
                {
                    float x;
                    float goal_x = (float)((state.HasPreferredX) != 0 ? state.PreferredX : 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.HasPreferredX = (byte)(1);
                    state.PreferredX    = (float)(goal_x);
                    if (sel)
                    {
                        state.SelectionEnd = (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;
                bool sel = shift_mod;
                if ((state.SingleLine) != 0)
                {
                    key = (int)(NK_KEY_LEFT);
                    goto retry;
                }
                if (sel)
                {
                    nk_textedit_prep_selection_at_cursor(state);
                }
                else if (((state).SelectionStart != (state).SelectionEnd))
                {
                    nk_textedit_move_to_first(state);
                }
                nk_textedit_clamp(state);
                nk_textedit_find_charpos(&find, state, (int)(state.Cursor), (int)(state.SingleLine), font, (float)(row_height));
                if (find.prev_first != find.first_char)
                {
                    float x;
                    float goal_x = (float)((state.HasPreferredX) != 0 ? state.PreferredX : 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.HasPreferredX = (byte)(1);
                    state.PreferredX    = (float)(goal_x);
                    if (sel)
                    {
                        state.SelectionEnd = (int)(state.Cursor);
                    }
                }
            }
            break;

            case NK_KEY_DEL:
                if ((state.Mode) == (NK_TEXT_EDIT_MODE_VIEW))
                {
                    break;
                }
                if (((state).SelectionStart != (state).SelectionEnd))
                {
                    nk_textedit_delete_selection(state);
                }
                else
                {
                    int n = (int)(state.Text.len);
                    if ((state.Cursor) < (n))
                    {
                        nk_textedit_delete(state, (int)(state.Cursor), (int)(1));
                    }
                }
                state.HasPreferredX = (byte)(0);
                break;

            case NK_KEY_BACKSPACE:
                if ((state.Mode) == (NK_TEXT_EDIT_MODE_VIEW))
                {
                    break;
                }
                if (((state).SelectionStart != (state).SelectionEnd))
                {
                    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.HasPreferredX = (byte)(0);
                break;

            case NK_KEY_TEXT_START:
                if (shift_mod)
                {
                    nk_textedit_prep_selection_at_cursor(state);
                    state.Cursor        = (int)(state.SelectionEnd = (int)(0));
                    state.HasPreferredX = (byte)(0);
                }
                else
                {
                    state.Cursor        = (int)(state.SelectionStart = (int)(state.SelectionEnd = (int)(0)));
                    state.HasPreferredX = (byte)(0);
                }
                break;

            case NK_KEY_TEXT_END:
                if (shift_mod)
                {
                    nk_textedit_prep_selection_at_cursor(state);
                    state.Cursor        = (int)(state.SelectionEnd = (int)(state.Text.len));
                    state.HasPreferredX = (byte)(0);
                }
                else
                {
                    state.Cursor         = (int)(state.Text.len);
                    state.SelectionStart = (int)(state.SelectionEnd = (int)(0));
                    state.HasPreferredX  = (byte)(0);
                }
                break;

            case NK_KEY_TEXT_LINE_START:
            {
                if (shift_mod)
                {
                    nk_text_find find = new nk_text_find();
                    nk_textedit_clamp(state);
                    nk_textedit_prep_selection_at_cursor(state);
                    if (((state.Text.len) != 0) && ((state.Cursor) == (state.Text.len)))
                    {
                        --state.Cursor;
                    }
                    nk_textedit_find_charpos(&find, state, (int)(state.Cursor), (int)(state.SingleLine), font, (float)(row_height));
                    state.Cursor        = (int)(state.SelectionEnd = (int)(find.first_char));
                    state.HasPreferredX = (byte)(0);
                }
                else
                {
                    nk_text_find find = new nk_text_find();
                    if (((state.Text.len) != 0) && ((state.Cursor) == (state.Text.len)))
                    {
                        --state.Cursor;
                    }
                    nk_textedit_clamp(state);
                    nk_textedit_move_to_first(state);
                    nk_textedit_find_charpos(&find, state, (int)(state.Cursor), (int)(state.SingleLine), font, (float)(row_height));
                    state.Cursor        = (int)(find.first_char);
                    state.HasPreferredX = (byte)(0);
                }
            }
            break;

            case NK_KEY_TEXT_LINE_END:
            {
                if (shift_mod)
                {
                    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.SingleLine), font, (float)(row_height));
                    state.HasPreferredX = (byte)(0);
                    state.Cursor        = (int)(find.first_char + find.length);
                    if (((find.length) > (0)) && ((state.Text[(int)(state.Cursor - 1)]) == ('\n')))
                    {
                        --state.Cursor;
                    }
                    state.SelectionEnd = (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.SingleLine), font, (float)(row_height));
                    state.HasPreferredX = (byte)(0);
                    state.Cursor        = (int)(find.first_char + find.length);
                    if (((find.length) > (0)) && ((state.Text[(int)(state.Cursor - 1)]) == ('\n')))
                    {
                        --state.Cursor;
                    }
                }
            }
            break;
            }
        }