Ejemplo n.º 1
0
        private unsafe int TextEditCallback(ImGuiInputTextCallbackData *dataPtr)
        {
            ImGuiInputTextCallbackDataPtr data = dataPtr;

            switch (data.EventFlag)
            {
            case ImGuiInputTextFlags.CallbackAlways:
                if (m_suggestionPosition >= 0 && data.BufTextLen != m_suggestionKeepLength)
                {
                    InvalidateSuggestions();
                }
                break;

            case ImGuiInputTextFlags.CallbackCompletion:
                ShowSuggestion(data);
                break;

            case ImGuiInputTextFlags.CallbackHistory:
                if ((data.EventKey == ImGuiKey.UpArrow || data.EventKey == ImGuiKey.DownArrow) && m_commandHistory.Count > 0)
                {
                    SelectHistory(data.EventKey == ImGuiKey.UpArrow);
                    data.DeleteChars(0, data.BufTextLen);
                    data.InsertChars(0, m_commandHistory[m_historyPosition]);
                }
                break;
            }

            return(0);
        }
Ejemplo n.º 2
0
        private void ShowSuggestion(ImGuiInputTextCallbackDataPtr data)
        {
            byte[] buffer = new byte[data.BufSize];
            Marshal.Copy(data.Buf, buffer, 0, data.BufSize);
            string encodedString  = Encoding.UTF8.GetString(buffer);
            int    nullTerminator = encodedString.IndexOf('\0');

            encodedString = encodedString.Substring(0, nullTerminator);

            if (!string.IsNullOrWhiteSpace(encodedString))
            {
                // If we are not having any suggestions currently get new ones
                if (m_suggestionPosition < 0)
                {
                    List <string> fullSuggestions = new List <string>();
                    CConfigManager.Instance.GetConsoleSuggestions(encodedString, ref fullSuggestions, ref m_currentSuggestions);
                    foreach (string suggestion in fullSuggestions)
                    {
                        m_logHistory.Add("[Suggestion]" + suggestion);
                    }

                    if (m_currentSuggestions.Count > 0)
                    {
                        m_suggestionPosition = 0;
                    }
                    else
                    {
                        m_logHistory.Add("No suggestions for: " + encodedString);
                    }
                }
                else
                {
                    m_suggestionPosition++;
                    if (m_suggestionPosition >= m_currentSuggestions.Count)
                    {
                        m_suggestionPosition = 0;
                    }
                }

                if (m_suggestionPosition >= 0)
                {
                    data.DeleteChars(0, data.BufTextLen);
                    data.InsertChars(0, m_currentSuggestions[m_suggestionPosition]);
                    m_suggestionKeepLength = data.BufTextLen;
                }
            }
        }
    unsafe void Layout()
    {
        if (!CursorDefault.consoleIsOpen)
        {
            //ImGui.ShowDemoWindow();
            return;
        }

        ImGui.SetNextWindowSize(new Vector2(Screen.width, 200));
        ImGui.SetNextWindowPos(new Vector2(0, 0));
        if (ImGui.Begin("window", ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.NoSavedSettings))
        {
            ImGui.Separator();

            if (ImGui.BeginMenuBar())
            {
                ImGui.Checkbox("Errors", ref showErrors);
                ImGui.Checkbox("Warnings", ref showWarnings);
                ImGui.Checkbox("Info", ref showInfo);

                ImGui.Spacing();

                ImGui.Checkbox("Timestamps", ref timestamps);

                ImGui.EndMenuBar();
            }

            ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(0, 0));
            ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, Vector2.one);
            if (ImGui.BeginChildFrame(1, ImGui.GetWindowSize() - new Vector2(0, 60)))
            {
                uint idx = 0;
                foreach (var log in inst.logs)
                {
                    string str = timestamps ? log.logStr : log.logStrNoTimestamp;
                    ImGui.PushStyleVar(ImGuiStyleVar.ItemInnerSpacing, Vector2.zero);

                    TimeSpan diff  = DateTime.Now - log.time;
                    float    alpha = 1 - Mathf.Clamp01((float)diff.TotalSeconds - (float)CmdFadeTime.TotalSeconds);

                    Vector4 col = Vector4.one;
                    if (log.logType == LogType.Assert || log.logType == LogType.Exception)
                    {
                        col = new Vector4(.6f, 0, 0, 1);
                    }
                    if (log.logType == LogType.Error)
                    {
                        col = new Vector4(1, 0, 0, 1);
                    }
                    if (log.logType == LogType.Warning)
                    {
                        col = new Vector4(1, 1, .3f, 1);
                    }
                    if (log.logType == LogType.Log)
                    {
                        col = new Vector4(1, 1, 1, 1);
                    }
                    ImGui.PushStyleColor(ImGuiCol.Button, new Vector4(1, 1, 1, 0.1f * alpha));
                    ImGui.PushStyleColor(ImGuiCol.Text, col);
                    Vector2 size = new Vector2(ImGui.GetWindowWidth(), ImGui.CalcTextSize(str).y + 3);
                    ImGui.PushStyleVar(ImGuiStyleVar.ButtonTextAlign, new Vector2(0f, 0.5f));
                    bool show = ((log.logType == LogType.Assert || log.logType == LogType.Exception) && showErrors) ||
                                (log.logType == LogType.Error && showErrors) ||
                                (log.logType == LogType.Warning && showWarnings) ||
                                (log.logType == LogType.Log && showInfo);
                    if (show)
                    {
                        if (ImGui.Button(str, size))
                        {
                            log.isOpen = !log.isOpen;
                        }
                        if (ImGui.BeginPopupContextItem($"expand_log_{idx}"))
                        {
                            ImGui.Checkbox("Stack trace", ref log.isOpen);
                            if (ImGui.Selectable("Copy..."))
                            {
                                if (log.isOpen)
                                {
                                    ImGui.SetClipboardText($"{str}\n{log.stackTrace}");
                                }
                                else
                                {
                                    ImGui.SetClipboardText(str);
                                }
                                inst.PushStatus("Copied!");
                            }

                            ImGui.EndPopup();
                        }
                        if (log.isOpen)
                        {
                            ImGui.Text(log.stackTrace);
                        }
                    }
                    ImGui.PopStyleColor();
                    ImGui.PopStyleColor();
                    ImGui.PopStyleVar();
                    ImGui.PopStyleVar();

                    idx++;
                }

                inst.scrollToBottom = ImGui.GetScrollY() >= ImGui.GetScrollMaxY();

                if (inst.scrollToBottom)
                {
                    ImGui.SetScrollHereY(1.0f);
                }

                ImGui.EndChildFrame();
            }
            ImGui.PopStyleVar();
            ImGui.PopStyleVar();

            ImGui.Spacing();

            ImGuiInputTextCallback cb = (ImGuiInputTextCallbackData * data) =>
            {
                ImGuiInputTextCallbackDataPtr ptr = new ImGuiInputTextCallbackDataPtr(data);
                if (desiredConsoleInput != null)
                {
                    ptr.DeleteChars(0, ptr.BufTextLen);
                    ptr.InsertChars(0, desiredConsoleInput);
                    desiredConsoleInput = null;
                }
                return(ptr.BufTextLen);
            };
            if (ImGui.InputTextWithHint("", ">", ref consoleInput, 150, ImGuiInputTextFlags.EnterReturnsTrue | ImGuiInputTextFlags.CallbackAlways, cb))
            {
                bool didRun = ComReg.RunCom(consoleInput);
                //if (history.Count == 0 || history[history.Count - 1] != consoleInput)
                if (didRun)
                {
                    history.Add(consoleInput);
                }
                consoleInput    = "";
                historyLogIndex = -1;
                ImGui.SetKeyboardFocusHere();
            }

            if (FocusText)
            {
                ImGui.SetKeyboardFocusHere(-1);
                FocusText = false;
            }
            if (consoleInput.Contains("`")) //hack
            {
                consoleInput = consoleInput.Replace("`", "");
            }
            ImGui.SameLine();
            inst.DrawStatusImgui();

            ImGui.End();
        }
    }