Refactor TaskButtonManager and TaskTimer to streamline timer command handling

This commit is contained in:
tom.hempel
2025-10-21 18:27:33 +02:00
parent 8274e78d1b
commit fb002a6096
2 changed files with 53 additions and 129 deletions

View File

@ -15,10 +15,16 @@ public class TaskButtonManager : MonoBehaviour
private Thread udpListenerThread;
private bool isListening = false;
private string pendingSceneName = null;
private string pendingTimerCommand = null;
private readonly object sceneLock = new object();
private readonly object timerLock = new object();
private TaskTimer taskTimer;
private void Start()
{
// Find TaskTimer in the scene
taskTimer = FindObjectOfType<TaskTimer>();
if (enableUDPControl)
{
StartUDPListener();
@ -37,6 +43,23 @@ public class TaskButtonManager : MonoBehaviour
}
}
// Check for pending timer commands (must be done on main thread)
lock (timerLock)
{
if (!string.IsNullOrEmpty(pendingTimerCommand))
{
if (taskTimer != null)
{
taskTimer.ProcessTimerCommand(pendingTimerCommand);
}
else
{
Debug.LogWarning("TaskTimer not found in scene. Cannot process timer command.");
}
pendingTimerCommand = null;
}
}
// VR Tasks (Keys 1-4)
if (Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1))
LoadSceneByName("VR-Task1");
@ -108,20 +131,39 @@ public class TaskButtonManager : MonoBehaviour
if (string.IsNullOrEmpty(message))
return;
// Parse command format: SWITCH:SceneName
// Parse command format: COMMAND:Parameter
string[] parts = message.Split(':');
if (parts.Length == 2 && parts[0].Trim().ToUpper() == "SWITCH")
if (parts.Length >= 2)
{
string sceneName = parts[1].Trim();
lock (sceneLock)
string commandType = parts[0].Trim().ToUpper();
string parameter = parts[1].Trim();
if (commandType == "SWITCH")
{
pendingSceneName = sceneName;
// Scene switching command
lock (sceneLock)
{
pendingSceneName = parameter;
}
Debug.Log($"Scheduled scene switch to: {parameter}");
}
else if (commandType == "TIMER")
{
// Timer control command
lock (timerLock)
{
pendingTimerCommand = parameter;
}
Debug.Log($"Scheduled timer command: {parameter}");
}
else
{
Debug.LogWarning($"Unknown UDP command type: {commandType}. Expected: SWITCH or TIMER");
}
Debug.Log($"Scheduled scene switch to: {sceneName}");
}
else
{
Debug.LogWarning($"Invalid UDP command format: {message}. Expected format: SWITCH:SceneName");
Debug.LogWarning($"Invalid UDP command format: {message}. Expected format: COMMAND:Parameter (e.g., SWITCH:SceneName or TIMER:START)");
}
}