private void processInput()
{
if (userInputRepeated && terminalTextArea && userInput)
{
// Clear previous entry if needed
foreach (var entry in newEntries)
{
if (entry != null)
{
Destroy(entry);
}
}
newEntries.Clear();
int inputIndex = userInput.transform.GetSiblingIndex();
// Create user command entry
GameObject userEntry = Instantiate(userInputRepeated, terminalTextArea.transform);
userEntry.transform.SetSiblingIndex(inputIndex);
TextMeshProUGUI userText = userEntry.GetComponentInChildren<TextMeshProUGUI>();
if (userText != null)
{
userText.text = "User: " + userInput.text;
}
// Create terminal reply entry
GameObject systemEntry = determineResponse.findCommand(userInput.text);
if (systemEntry==null)
{
systemEntry = Instantiate(userInputRepeated, terminalTextArea.transform);
TextMeshProUGUI systemText = systemEntry.GetComponentInChildren<TextMeshProUGUI>();
if (systemText != null)
{
systemText.text = "command not recognised";
systemText.color = Color.gray;
}
}
else
{
systemEntry = Instantiate(systemEntry, terminalTextArea.transform);
}
systemEntry.transform.SetSiblingIndex(inputIndex + 1); // Insert below the command
// Clear input and reactivate
userInput.text = "";
userInput.ActivateInputField();
// Rebuild layout
LayoutRebuilder.ForceRebuildLayoutImmediate(terminalTextArea.GetComponent<RectTransform>());
// Scroll to bottom
ScrollRect scrollRect = terminalTextArea.GetComponentInParent<ScrollRect>();
if (scrollRect != null)
{
Canvas.ForceUpdateCanvases();
scrollRect.verticalNormalizedPosition = 0f;
}
// Track both entries if needed
newEntries.Add(userEntry);
newEntries.Add(systemEntry);
}
}