diff --git a/src/CSConsole/ConsoleController.cs b/src/CSConsole/ConsoleController.cs index 014865d..3e246cf 100644 --- a/src/CSConsole/ConsoleController.cs +++ b/src/CSConsole/ConsoleController.cs @@ -86,7 +86,10 @@ public ConsoleController() } _panel.OnInputChanged += OnInputChanged; - _panel.InputScroller.OnScroll += OnInputScrolled; + if (_panel.InputScroller != null) + { + _panel.InputScroller.OnScroll += OnInputScrolled; + } _panel.OnCompileClicked += Evaluate; _panel.OnResetClicked += ResetConsole; _panel.OnDropdownChanged += SelectedDropDown; @@ -454,7 +457,7 @@ private void UpdateCaret(out bool caretMoved) } // If caret moved, ensure caret is visible in the viewport - if (caretMoved) + if (caretMoved && !_panel.UsesSafeInputFallback) { UICharInfo charInfo = Input.TextGenerator.characters[lastCaretPosition]; float charTop = charInfo.cursorPos.y; @@ -573,6 +576,12 @@ private void HighlightVisibleInput(out bool inStringOrComment) return; } + if (_panel.UsesSafeInputFallback) + { + HighlightCompleteInput(out inStringOrComment); + return; + } + // Calculate the visible lines int topLine = -1; @@ -654,6 +663,32 @@ private void HighlightVisibleInput(out bool inStringOrComment) _panel.LineNumberText.text = sb.ToString(); } + private void HighlightCompleteInput(out bool inStringOrComment) + { + _panel.HighlightText.text = _lexer.BuildHighlightedString(Input.Text, 0, Input.Text.Length - 1, 0, lastCaretPosition, out inStringOrComment); + + StringBuilder sb = new(); + int realLine = 1; + sb.Append(realLine).Append('\n'); + + for (int i = 0; i < Input.Text.Length; i++) + { + char c = Input.Text[i]; + if (c == '\r' || c == '\n') + { + if (c == '\r' && i + 1 < Input.Text.Length && Input.Text[i + 1] == '\n') + { + i++; + } + + realLine++; + sb.Append(realLine).Append('\n'); + } + } + + _panel.LineNumberText.text = sb.ToString(); + } + #endregion diff --git a/src/Hooks/HookCreator.cs b/src/Hooks/HookCreator.cs index 17abccd..2d6912a 100644 --- a/src/Hooks/HookCreator.cs +++ b/src/Hooks/HookCreator.cs @@ -37,7 +37,8 @@ public class HookCreator : ICellPoolDataSource public static GameObject EditorRoot { get; private set; } public static Text EditingHookLabel { get; private set; } public static InputFieldScroller EditorInputScroller { get; private set; } - public static InputFieldRef EditorInput => EditorInputScroller.InputField; + public static InputFieldRef EditorInputFallback { get; private set; } + public static InputFieldRef EditorInput => EditorInputScroller != null ? EditorInputScroller.InputField : EditorInputFallback; public static Text EditorInputText { get; private set; } public static Text EditorHighlightText { get; private set; } @@ -299,8 +300,15 @@ public void ConstructEditor(GameObject parent) editorDoneButton.OnClick += EditorInputCancel; int fontSize = 16; - GameObject inputObj = UIFactory.CreateScrollInputField(EditorRoot, "EditorInput", "", out InputFieldScroller inputScroller, fontSize); - EditorInputScroller = inputScroller; + if (ExplorerCore.IsUnity6000OrNewer) + { + ConstructSafeEditorInput(fontSize); + } + else + { + ConstructScrollEditorInput(fontSize); + } + EditorInput.OnValueChanged += OnEditorInputChanged; EditorInputText = EditorInput.Component.textComponent; @@ -329,5 +337,28 @@ public void ConstructEditor(GameObject parent) EditorInput.PlaceholderText.font = UniversalUI.ConsoleFont; EditorHighlightText.font = UniversalUI.ConsoleFont; } + + private static void ConstructScrollEditorInput(int fontSize) + { + UIFactory.CreateScrollInputField(EditorRoot, "EditorInput", "", out InputFieldScroller inputScroller, fontSize); + EditorInputScroller = inputScroller; + } + + private static void ConstructSafeEditorInput(int fontSize) + { + ExplorerCore.Log("Unity 6000 detected, using safe Hook Editor input fallback."); + + EditorInputFallback = UIFactory.CreateInputField(EditorRoot, "EditorInput", ""); + UIFactory.SetLayoutElement(EditorInputFallback.Component.gameObject, minWidth: 100, minHeight: 30, flexibleWidth: 5000, flexibleHeight: 5000); + + EditorInputFallback.Component.lineType = InputField.LineType.MultiLineNewline; + EditorInputFallback.Component.targetGraphic.color = new Color(0.12f, 0.12f, 0.12f); + EditorInputFallback.Component.textComponent.alignment = TextAnchor.UpperLeft; + EditorInputFallback.Component.textComponent.fontSize = fontSize; + EditorInputFallback.Component.textComponent.horizontalOverflow = HorizontalWrapMode.Wrap; + EditorInputFallback.PlaceholderText.alignment = TextAnchor.UpperLeft; + EditorInputFallback.PlaceholderText.fontSize = fontSize; + EditorInputFallback.PlaceholderText.horizontalOverflow = HorizontalWrapMode.Wrap; + } } } diff --git a/src/UI/Panels/CSConsolePanel.cs b/src/UI/Panels/CSConsolePanel.cs index 1ae30bd..ae6359b 100644 --- a/src/UI/Panels/CSConsolePanel.cs +++ b/src/UI/Panels/CSConsolePanel.cs @@ -17,7 +17,9 @@ public class CSConsolePanel : UEPanel public override Vector2 DefaultAnchorMax => new(0.85f, 0.925f); public InputFieldScroller InputScroller { get; private set; } - public InputFieldRef Input => InputScroller.InputField; + public InputFieldRef InputFallback { get; private set; } + public InputFieldRef Input => InputScroller != null ? InputScroller.InputField : InputFallback; + public bool UsesSafeInputFallback => InputScroller == null && InputFallback != null; public Text InputText { get; private set; } public Text HighlightText { get; private set; } public Text LineNumberText { get; private set; } @@ -130,6 +132,7 @@ protected override void ConstructPanelContent() linesRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 50); linesHolder.AddComponent().color = new Color(0.05f, 0.05f, 0.05f); UIFactory.SetLayoutGroup(linesHolder, true, true, true, true); + UIFactory.SetLayoutElement(linesHolder, minWidth: 50, flexibleWidth: 0, flexibleHeight: 9999); LineNumberText = UIFactory.CreateLabel(linesHolder, "LineNumbers", "1", TextAnchor.UpperCenter, Color.grey, fontSize: 16); LineNumberText.font = UniversalUI.ConsoleFont; @@ -138,11 +141,25 @@ protected override void ConstructPanelContent() int fontSize = 16; + if (ExplorerCore.IsUnity6000OrNewer) + { + ConstructSafeInputField(inputArea, fontSize); + } + else + { + ConstructScrollInputField(inputArea, linesRect, fontSize); + } + + ConfigureInputVisuals(fontSize); + + RuntimeHelper.StartCoroutine(DelayedLayoutSetup()); + } + + private void ConstructScrollInputField(GameObject inputArea, RectTransform linesRect, int fontSize) + { GameObject inputObj = UIFactory.CreateScrollInputField(inputArea, "ConsoleInput", ConsoleController.STARTUP_TEXT, out InputFieldScroller inputScroller, fontSize); InputScroller = inputScroller; - ConsoleController.DefaultInputFieldAlpha = Input.Component.selectionColor.a; - Input.OnValueChanged += InvokeOnValueChanged; // move line number text with input field linesRect.transform.SetParent(inputObj.transform.Find("Viewport"), false); @@ -153,6 +170,29 @@ void SetLinesPosition() linesRect.anchoredPosition = new Vector2(linesRect.anchoredPosition.x, inputScroller.ContentRect.anchoredPosition.y); //SetInputLayout(); } + } + + private void ConstructSafeInputField(GameObject inputArea, int fontSize) + { + ExplorerCore.Log("Unity 6000 detected, using safe C# Console input fallback."); + + InputFallback = UIFactory.CreateInputField(inputArea, "ConsoleInput", ConsoleController.STARTUP_TEXT); + UIFactory.SetLayoutElement(InputFallback.Component.gameObject, minWidth: 100, minHeight: 30, flexibleWidth: 5000, flexibleHeight: 5000); + + InputFallback.Component.lineType = InputField.LineType.MultiLineNewline; + InputFallback.Component.targetGraphic.color = new Color(0.12f, 0.12f, 0.12f); + InputFallback.Component.textComponent.alignment = TextAnchor.UpperLeft; + InputFallback.Component.textComponent.fontSize = fontSize; + InputFallback.Component.textComponent.horizontalOverflow = HorizontalWrapMode.Wrap; + InputFallback.PlaceholderText.alignment = TextAnchor.UpperLeft; + InputFallback.PlaceholderText.fontSize = fontSize; + InputFallback.PlaceholderText.horizontalOverflow = HorizontalWrapMode.Wrap; + } + + private void ConfigureInputVisuals(int fontSize) + { + ConsoleController.DefaultInputFieldAlpha = Input.Component.selectionColor.a; + Input.OnValueChanged += InvokeOnValueChanged; InputText = Input.Component.textComponent; InputText.supportRichText = false; @@ -179,8 +219,6 @@ void SetLinesPosition() InputText.font = UniversalUI.ConsoleFont; Input.PlaceholderText.font = UniversalUI.ConsoleFont; HighlightText.font = UniversalUI.ConsoleFont; - - RuntimeHelper.StartCoroutine(DelayedLayoutSetup()); } private IEnumerator DelayedLayoutSetup() @@ -191,6 +229,11 @@ private IEnumerator DelayedLayoutSetup() public void SetInputLayout() { + if (UsesSafeInputFallback) + { + return; + } + Input.Transform.offsetMin = new Vector2(52, Input.Transform.offsetMin.y); Input.Transform.offsetMax = new Vector2(2, Input.Transform.offsetMax.y); } diff --git a/src/UI/Widgets/EvaluateWidget/GenericConstructorWidget.cs b/src/UI/Widgets/EvaluateWidget/GenericConstructorWidget.cs index 66bb900..cf0999e 100644 --- a/src/UI/Widgets/EvaluateWidget/GenericConstructorWidget.cs +++ b/src/UI/Widgets/EvaluateWidget/GenericConstructorWidget.cs @@ -117,8 +117,30 @@ internal void ConstructUI(GameObject parent) Title = UIFactory.CreateLabel(UIRoot, "Title", LocalizationManager.GetText("generic_arguments", "Generic Arguments"), TextAnchor.MiddleCenter); UIFactory.SetLayoutElement(Title.gameObject, minHeight: 25, flexibleWidth: 9999); - GameObject scrollview = UIFactory.CreateScrollView(UIRoot, "GenericArgsScrollView", out ArgsHolder, out _, new(0.1f, 0.1f, 0.1f)); - UIFactory.SetLayoutElement(scrollview, flexibleWidth: 9999, flexibleHeight: 9999); + if (ExplorerCore.IsUnity6000OrNewer) + { + ExplorerCore.Log("Unity 6000 detected, using safe Generic Arguments container fallback."); + + ArgsHolder = UIFactory.CreateVerticalGroup( + UIRoot, + "GenericArgsContent", + true, + false, + true, + true, + 0, + new Vector4(5, 5, 5, 5), + new(0.1f, 0.1f, 0.1f), + TextAnchor.UpperLeft); + + UIFactory.SetLayoutElement(ArgsHolder, flexibleWidth: 9999, flexibleHeight: 9999); + } + else + { + GameObject scrollview = UIFactory.CreateScrollView(UIRoot, "GenericArgsScrollView", out ArgsHolder, out _, new(0.1f, 0.1f, 0.1f)); + UIFactory.SetLayoutElement(scrollview, flexibleWidth: 9999, flexibleHeight: 9999); + } + UIFactory.SetLayoutGroup(ArgsHolder, padTop: 5, padLeft: 5, padBottom: 5, padRight: 5); } }