From 1317c59e94ffe942db5a3e7b6d6e2367f05346bb Mon Sep 17 00:00:00 2001 From: Jens Pitkanen Date: Sat, 8 Aug 2020 03:57:37 +0300 Subject: [PATCH] Optimize terminal text display --- Assets/Prefabs/UI/TerminalRoot.prefab | 4 +++- Assets/Scripts/Interface/Terminal.cs | 25 +++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Assets/Prefabs/UI/TerminalRoot.prefab b/Assets/Prefabs/UI/TerminalRoot.prefab index f25013f..fad5929 100644 --- a/Assets/Prefabs/UI/TerminalRoot.prefab +++ b/Assets/Prefabs/UI/TerminalRoot.prefab @@ -53,6 +53,8 @@ MonoBehaviour: InputField: {fileID: 7286298882252502165} TextField: {fileID: 4807139487522286296} Player: {fileID: 0} + LinesVisibleAtOnce: 32 + TerminalScrollbar: {fileID: 4580324269108703239} --- !u!1 &170678579363757794 GameObject: m_ObjectHideFlags: 0 @@ -398,7 +400,7 @@ MonoBehaviour: m_TargetGraphic: {fileID: 2282722458285268294} m_HandleRect: {fileID: 4556893897637480247} m_Direction: 2 - m_Value: 0 + m_Value: 1 m_Size: 1 m_NumberOfSteps: 0 m_OnValueChanged: diff --git a/Assets/Scripts/Interface/Terminal.cs b/Assets/Scripts/Interface/Terminal.cs index c7d0e10..1e5d5ea 100644 --- a/Assets/Scripts/Interface/Terminal.cs +++ b/Assets/Scripts/Interface/Terminal.cs @@ -1,9 +1,10 @@ using System.Collections.Generic; using System; -using System.Collections; +using System.Text; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.EventSystems; +using UnityEngine.UI; using NeonTea.Quakeball.Players; using TMPro; @@ -14,6 +15,9 @@ namespace NeonTea.Quakeball.Interface { public TMP_InputField InputField; public TMP_Text TextField; public LocalPlayer Player; + [Tooltip("Used to optimize the amount of text rendered. 0 for no optimization.")] + public float LinesVisibleAtOnce; + public Scrollbar TerminalScrollbar; private InputAction ToggleTerminalAction; private InputAction SubmitTerminal; @@ -24,7 +28,6 @@ namespace NeonTea.Quakeball.Interface { private Dictionary Helps = new Dictionary(); private List Messages = new List(); - private string Text = ""; private List PreviousRuns = new List(); private int CurrentScroll = -1; private bool JustScrolled = false; @@ -101,8 +104,22 @@ namespace NeonTea.Quakeball.Interface { InputField.readOnly = false; } if (IsOpen) { - Text = String.Join("\n", Messages); - TextField.text = Text; + if (LinesVisibleAtOnce == 0) { + TextField.text = String.Join("\n", Messages); + } else { + float VisibleLinesCenterIndex = (1 - TerminalScrollbar.value) * Messages.Count; + int StartIndex = Mathf.Max(0, (int)(VisibleLinesCenterIndex - LinesVisibleAtOnce / 2)); + int EndIndex = Mathf.Min(Messages.Count - 1, (int)(VisibleLinesCenterIndex + LinesVisibleAtOnce / 2)); + StringBuilder OptimizedText = new StringBuilder(); + for (int i = 0; i < Messages.Count; i++) { + if (i < StartIndex || i > EndIndex) { + OptimizedText.AppendLine(); + } else { + OptimizedText.AppendLine(Messages[i]); + } + } + TextField.text = OptimizedText.ToString(); + } } }