Example #1
0
        public void OnInputFieldEditExit(string newText)
        {
            // LogSystem.Log("OnInputFieldEditExit(string newText) with text: " + newText);

            textComp.OnLeaveEditMode();
            textComp.SetText(newText);
            inputField.Remove();
            inputField = null;

            TextEditCallback persistTextCallback = textEditCallback.transform.GetComponent(typeof(RuntimeTextEdit.TextEditCallback)) as TextEditCallback;

            if (persistTextCallback != null)
            {
                persistTextCallback.PersistText(newText, textComp.GetID());
            }
            else
            {
                LogSystem.LogWithHighlight("Searched for implementation of interface TextEditCallback on GameObject (" + textEditCallback.name + ") but failed to find it. Could not call PersistText(...). Set inspector property TextEditCallback in RuntimeTextEdit.", this);
            }

            onLeaveEditMode.Invoke();

            this.isInEditMode = false;
            RuntimeTextEdit.oneInstanceInEditMode = false;
            manager.DeactivateMe(this);
        }
Example #2
0
 public TextBuffer(int sz = 2048)
 {
     if ((sz % 8) != 0)
     {
         throw new Exception("Must be multiple of 8");
     }
     Size    = sz;
     Pointer = Marshal.AllocHGlobal(sz);
     Clear();
     Callback = HandleTextEditCallback;
 }
Example #3
0
        public unsafe void Draw(string title, byte[] mem_data, int mem_size, int base_display_addr = 0)
        {
            if (!ImGui.BeginWindow(title))
            {
                ImGui.EndWindow();
                return;
            }

            float line_height      = ImGuiNative.igGetTextLineHeight();
            int   line_total_count = (mem_size + Rows - 1) / Rows;

            ImGuiNative.igSetNextWindowContentSize(new Vector2(0.0f, line_total_count * line_height));
            ImGui.BeginChild("##scrolling", new Vector2(0, -ImGuiNative.igGetFrameHeightWithSpacing()), false, 0);

            ImGui.PushStyleVar(StyleVar.FramePadding, new Vector2(0, 0));
            ImGui.PushStyleVar(StyleVar.ItemSpacing, new Vector2(0, 0));

            int addr_digits_count = 0;

            for (int n = base_display_addr + mem_size - 1; n > 0; n >>= 4)
            {
                addr_digits_count++;
            }

            float glyph_width = ImGui.GetTextSize("F").X;
            float cell_width  = glyph_width * 3; // "FF " we include trailing space in the width to easily catch clicks everywhere

            var clipper            = new ImGuiListClipper(line_total_count, line_height);
            int visible_start_addr = clipper.DisplayStart * Rows;
            int visible_end_addr   = clipper.DisplayEnd * Rows;

            bool data_next = false;

            if (!AllowEdits || DataEditingAddr >= mem_size)
            {
                DataEditingAddr = -1;
            }

            int data_editing_addr_backup = DataEditingAddr;

            if (DataEditingAddr != -1)
            {
                if (ImGui.IsKeyPressed(ImGui.GetKeyIndex(GuiKey.UpArrow)) && DataEditingAddr >= Rows)
                {
                    DataEditingAddr -= Rows; DataEditingTakeFocus = true;
                }
                else if (ImGui.IsKeyPressed(ImGui.GetKeyIndex(GuiKey.DownArrow)) && DataEditingAddr < mem_size - Rows)
                {
                    DataEditingAddr += Rows; DataEditingTakeFocus = true;
                }
                else if (ImGui.IsKeyPressed(ImGui.GetKeyIndex(GuiKey.LeftArrow)) && DataEditingAddr > 0)
                {
                    DataEditingAddr -= 1; DataEditingTakeFocus = true;
                }
                else if (ImGui.IsKeyPressed(ImGui.GetKeyIndex(GuiKey.RightArrow)) && DataEditingAddr < mem_size - 1)
                {
                    DataEditingAddr += 1; DataEditingTakeFocus = true;
                }
            }
            if ((DataEditingAddr / Rows) != (data_editing_addr_backup / Rows))
            {
                // Track cursor movements
                float scroll_offset  = ((DataEditingAddr / Rows) - (data_editing_addr_backup / Rows)) * line_height;
                bool  scroll_desired = (scroll_offset < 0.0f && DataEditingAddr < visible_start_addr + Rows * 2) || (scroll_offset > 0.0f && DataEditingAddr > visible_end_addr - Rows * 2);
                if (scroll_desired)
                {
                    ImGuiNative.igSetScrollY(ImGuiNative.igGetScrollY() + scroll_offset);
                }
            }

            for (int line_i = clipper.DisplayStart; line_i < clipper.DisplayEnd; line_i++) // display only visible items
            {
                int addr = line_i * Rows;
                ImGui.Text(FixedHex(base_display_addr + addr, addr_digits_count) + ": ");
                ImGui.SameLine();

                // Draw Hexadecimal
                float line_start_x = ImGuiNative.igGetCursorPosX();
                for (int n = 0; n < Rows && addr < mem_size; n++, addr++)
                {
                    ImGui.SameLine(line_start_x + cell_width * n);

                    if (DataEditingAddr == addr)
                    {
                        // Display text input on current byte
                        ImGui.PushID(addr);

                        // FIXME: We should have a way to retrieve the text edit cursor position more easily in the API, this is rather tedious.
                        TextEditCallback callback = (data) =>
                        {
                            int *p_cursor_pos = (int *)data->UserData;
                            if (!data->HasSelection())
                            {
                                *p_cursor_pos = data->CursorPos;
                            }
                            return(0);
                        };
                        int  cursor_pos = -1;
                        bool data_write = false;
                        if (DataEditingTakeFocus)
                        {
                            ImGui.SetKeyboardFocusHere();
                            ReplaceChars(DataInput, FixedHex(mem_data[addr], 2));
                            ReplaceChars(AddrInput, FixedHex(base_display_addr + addr, addr_digits_count));
                        }
                        ImGui.PushItemWidth(ImGui.GetTextSize("FF").X);

                        var flags = InputTextFlags.CharsHexadecimal | InputTextFlags.EnterReturnsTrue | InputTextFlags.AutoSelectAll | InputTextFlags.NoHorizontalScroll | InputTextFlags.AlwaysInsertMode | InputTextFlags.CallbackAlways;
                        if (ImGui.InputText("##data", DataInput, 32, flags, callback, new IntPtr(&cursor_pos)))
                        {
                            data_write = data_next = true;
                        }
                        else if (!DataEditingTakeFocus && !ImGui.IsLastItemActive())
                        {
                            DataEditingAddr = -1;
                        }

                        DataEditingTakeFocus = false;
                        ImGui.PopItemWidth();
                        if (cursor_pos >= 2)
                        {
                            data_write = data_next = true;
                        }
                        if (data_write)
                        {
                            int data;
                            if (TryHexParse(DataInput, out data))
                            {
                                mem_data[addr] = (byte)data;
                            }
                        }
                        ImGui.PopID();
                    }
                    else
                    {
                        ImGui.Text(FixedHex(mem_data[addr], 2));
                        if (AllowEdits && ImGui.IsItemHovered(HoveredFlags.Default) && ImGui.IsMouseClicked(0))
                        {
                            DataEditingTakeFocus = true;
                            DataEditingAddr      = addr;
                        }
                    }
                }

                ImGui.SameLine(line_start_x + cell_width * Rows + glyph_width * 2);
                //separator line drawing replaced by printing a pipe char

                // Draw ASCII values
                addr = line_i * Rows;
                var asciiVal = new System.Text.StringBuilder(2 + Rows);
                asciiVal.Append("| ");
                for (int n = 0; n < Rows && addr < mem_size; n++, addr++)
                {
                    int c = mem_data[addr];
                    asciiVal.Append((c >= 32 && c < 128) ? Convert.ToChar(c) : '.');
                }
                ImGui.TextUnformatted(asciiVal.ToString());  //use unformatted, so string can contain the '%' character
            }
            //clipper.End();  //not implemented
            ImGui.PopStyleVar(2);

            ImGui.EndChild();

            if (data_next && DataEditingAddr < mem_size)
            {
                DataEditingAddr      = DataEditingAddr + 1;
                DataEditingTakeFocus = true;
            }

            ImGui.Separator();

            ImGuiNative.igAlignTextToFramePadding();
            ImGui.PushItemWidth(50);
            ImGuiNative.igPushAllowKeyboardFocus(false);
            int rows_backup = Rows;

            if (ImGui.DragInt("##rows", ref Rows, 0.2f, 4, 32, "%.0f rows"))
            {
                if (Rows <= 0)
                {
                    Rows = 4;
                }
                Vector2 new_window_size = ImGui.GetWindowSize();
                new_window_size.X += (Rows - rows_backup) * (cell_width + glyph_width);
                ImGui.SetWindowSize(new_window_size);
            }
            ImGuiNative.igPopAllowKeyboardFocus();
            ImGui.PopItemWidth();
            ImGui.SameLine();
            ImGui.Text(string.Format(" Range {0}..{1} ", FixedHex(base_display_addr, addr_digits_count),
                                     FixedHex(base_display_addr + mem_size - 1, addr_digits_count)));
            ImGui.SameLine();
            ImGui.PushItemWidth(70);
            if (ImGui.InputText("##addr", AddrInput, 32, InputTextFlags.CharsHexadecimal | InputTextFlags.EnterReturnsTrue, null))
            {
                int goto_addr;
                if (TryHexParse(AddrInput, out goto_addr))
                {
                    goto_addr -= base_display_addr;
                    if (goto_addr >= 0 && goto_addr < mem_size)
                    {
                        ImGui.BeginChild("##scrolling");
                        ImGuiNative.igSetScrollFromPosY(ImGui.GetCursorStartPos().Y + (goto_addr / Rows) * ImGuiNative.igGetTextLineHeight());
                        ImGui.EndChild();
                        DataEditingAddr      = goto_addr;
                        DataEditingTakeFocus = true;
                    }
                }
            }
            ImGui.PopItemWidth();

            ImGui.EndWindow();
        }
 public static extern bool igInputTextMultiline(string label, IntPtr buffer, uint buf_size, Vector2 size, InputTextFlags flags, TextEditCallback callback, void *user_data);
Example #5
0
 public static unsafe void InputTextMultiline(string label, IntPtr textBuffer, uint bufferSize, Vector2 size, InputTextFlags flags, TextEditCallback callback, IntPtr userData)
 {
     ImGuiNative.igInputTextMultiline(label, textBuffer, bufferSize, size, flags, callback, userData.ToPointer());
 }
Example #6
0
 public static unsafe bool InputText(string label, IntPtr textBuffer, uint bufferSize, InputTextFlags flags, TextEditCallback textEditCallback, IntPtr userData)
 {
     return(ImGuiNative.igInputText(label, textBuffer, bufferSize, flags, textEditCallback, userData.ToPointer()));
 }
Example #7
0
 public static unsafe bool InputText(string label, IntPtr textBuffer, uint bufferSize, InputTextFlags flags, TextEditCallback textEditCallback)
 {
     return(InputText(label, textBuffer, bufferSize, flags, textEditCallback, IntPtr.Zero));
 }
Example #8
0
 public static unsafe bool InputText(string label, byte[] textBuffer, uint bufferSize, InputTextFlags flags, TextEditCallback textEditCallback, IntPtr userData)
 {
     Debug.Assert(bufferSize <= textBuffer.Length);
     fixed(byte *ptrBuf = textBuffer)
     {
         return(InputText(label, new IntPtr(ptrBuf), bufferSize, flags, textEditCallback, userData));
     }
 }
Example #9
0
 public static unsafe void InputTextMultiline(string label, IntPtr textBuffer, uint bufferSize, Vector2 size, InputTextFlags flags, TextEditCallback callback, IntPtr userData)
 {
     ImGuiNative.igInputTextMultiline(label, textBuffer, bufferSize, size, flags, callback, userData.ToPointer());
 }
Example #10
0
 public static unsafe bool InputText(string label, IntPtr textBuffer, uint bufferSize, InputTextFlags flags, TextEditCallback textEditCallback, IntPtr userData)
 {
     return ImGuiNative.igInputText(label, textBuffer, bufferSize, flags, textEditCallback, userData.ToPointer());
 }
Example #11
0
 public static unsafe bool InputText(string label, IntPtr textBuffer, uint bufferSize, InputTextFlags flags, TextEditCallback textEditCallback)
 {
     return InputText(label, textBuffer, bufferSize, flags, textEditCallback, IntPtr.Zero);
 }
Example #12
0
 public static extern bool igInputTextMultiline(string label, IntPtr buffer, uint buf_size, Vector2 size, InputTextFlags flags, TextEditCallback callback, void* user_data);
Example #13
0
 public TextBuffer()
 {
     Pointer = Marshal.AllocHGlobal(2048);
     Clear();
     Callback = HandleTextEditCallback;
 }
Example #14
0
 public static unsafe void InputTextMultiline(string label, IntPtr textBuffer, uint bufferSize, Vector2 size, InputTextFlags flags, TextEditCallback callback)
 {
     ImGuiNative.igInputTextMultiline(utf8String(label), textBuffer, bufferSize, size, flags, callback, null);
 }
        public static bool InputText(string label, ref string text, uint maxLength = 1024, InputTextFlags flags = InputTextFlags.Default, TextEditCallback callback = null)
        {
            bool state;

            byte[] textArray = new byte[maxLength];
            Encoding.Default.GetBytes(text, 0, text.Length, textArray, 0);
            byte[] labelArray = Encoding.UTF8.GetBytes(label);
            unsafe
            {
                fixed(byte *txtPtr = textArray)
                {
                    state = ImGuiNative.igInputText(labelArray, new IntPtr(txtPtr), maxLength, flags, callback,
                                                    IntPtr.Zero.ToPointer());
                }
            }

            text = System.Text.Encoding.Default.GetString(textArray).TrimEnd('\0');
            return(state);
        }