Optimize terminal text display

This commit is contained in:
Jens Pitkänen 2020-08-08 03:57:37 +03:00
parent c44ffe090a
commit 1317c59e94
2 changed files with 24 additions and 5 deletions

View File

@ -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:

View File

@ -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<string, string> Helps = new Dictionary<string, string>();
private List<string> Messages = new List<string>();
private string Text = "";
private List<string> PreviousRuns = new List<string>();
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();
}
}
}