diff --git a/Assets/ControlPanel.cs b/Assets/ControlPanel.cs index 0ed9671..62006dc 100644 --- a/Assets/ControlPanel.cs +++ b/Assets/ControlPanel.cs @@ -15,10 +15,6 @@ public class ControlPanel : MonoBehaviour private DisplayModule _display; private SpeakerModule _speaker; - private string _savedDCInput = ""; - private int _savedDirectionIndex = 0; - private string _savedDisplayInput = ""; - private Text _caption; // child "DegreesCaption" private Text _buttonText; // child "RotateButton/Text (TMP)" private GameObject _directionCaptionGO; @@ -29,6 +25,20 @@ public class ControlPanel : MonoBehaviour private Button _playButton; private Text _playButtonText; + private void OnEnable() + { + if (_mode == Mode.Speaker) + { + SetUploadVisible(true); + SetPlayVisible(true); + } + else + { + SetUploadVisible(false); + SetPlayVisible(false); + } + } + private void Awake() { CacheUIFromHierarchy(); @@ -42,26 +52,10 @@ public class ControlPanel : MonoBehaviour button.onClick.RemoveAllListeners(); button.onClick.AddListener(OnMainButtonClicked); } - - if (inputField != null) - { - inputField.onValueChanged.AddListener(_ => CacheCurrentInput()); - inputField.onEndEdit.AddListener(_ => CacheCurrentInput()); - } - - if (directionDropdown != null) - { - directionDropdown.onValueChanged.AddListener(v => - { - if (_mode == Mode.DC) _savedDirectionIndex = v; - }); - } } public void Initialize(ModuleBase module) { - CacheCurrentInput(); - _dc = module as DCMotorModule; _display = module as DisplayModule; _speaker = module as SpeakerModule; @@ -75,7 +69,6 @@ public class ControlPanel : MonoBehaviour public void HidePanel() { - CacheCurrentInput(); gameObject.SetActive(false); _mode = Mode.None; _dc = null; @@ -102,7 +95,6 @@ public class ControlPanel : MonoBehaviour private void SwitchMode(Mode newMode) { - CacheCurrentInput(); _mode = newMode; if (_mode == Mode.DC) @@ -118,11 +110,11 @@ public class ControlPanel : MonoBehaviour if (inputField != null) { inputField.readOnly = false; - inputField.SetTextWithoutNotify(_savedDCInput); + inputField.SetTextWithoutNotify(""); } if (directionDropdown != null) - directionDropdown.SetValueWithoutNotify(_savedDirectionIndex); + directionDropdown.SetValueWithoutNotify(0); } else if (_mode == Mode.Display) { @@ -137,10 +129,7 @@ public class ControlPanel : MonoBehaviour if (inputField != null) { inputField.readOnly = false; - string toShow = _savedDisplayInput; - if (_display != null && !string.IsNullOrEmpty(_display.displayText)) - toShow = _display.displayText; - inputField.SetTextWithoutNotify(toShow); + inputField.SetTextWithoutNotify(""); } } else if (_mode == Mode.Speaker) @@ -179,21 +168,6 @@ public class ControlPanel : MonoBehaviour inputField.gameObject.SetActive(visible); } - private void CacheCurrentInput() - { - if (inputField == null) return; - - if (_mode == Mode.DC) - { - _savedDCInput = inputField.text; - if (directionDropdown != null) _savedDirectionIndex = directionDropdown.value; - } - else if (_mode == Mode.Display) - { - _savedDisplayInput = inputField.text; - } - } - private void OnMainButtonClicked() { if (_mode == Mode.DC) diff --git a/Assets/DistanceSensorModule.cs b/Assets/DistanceSensorModule.cs index 44ad54c..1fe1296 100644 --- a/Assets/DistanceSensorModule.cs +++ b/Assets/DistanceSensorModule.cs @@ -4,7 +4,7 @@ using UnityEngine; public class DistanceSensorModule : ModuleBase { // Keep a fixed-size array (no GC alloc every frame) - public string[] infoLines = new string[2]; + public string[] infoLines = new string[1]; public override string moduleType => "Distance"; public override string moduleName => "Distance Sensor Module"; @@ -15,28 +15,26 @@ public class DistanceSensorModule : ModuleBase void Update() { - // Always show something immediately - infoLines[0] = $"ModuleID raw: '{moduleID}'"; - // Poll at a fixed rate (avoids frame spam) if (Time.unscaledTime < nextPollTime) return; nextPollTime = Time.unscaledTime + (1f / Mathf.Max(1f, pollHz)); if (!int.TryParse(moduleID, out var id)) { - infoLines[1] = "ERROR: moduleID is not an int"; + infoLines[0] = "ERROR: moduleID is not an int"; return; } try { double distance = ControlLibrary.get_distance_control(id); - infoLines[1] = $"Distance: {distance:F0} mm"; + infoLines[0] = $"Distance: {distance:F0} mm"; } catch (Exception e) { - infoLines[1] = $"EXCEPTION: {e.GetType().Name}: {e.Message}"; - Debug.LogException(e); + // infoLines[1] = $"EXCEPTION: {e.GetType().Name}: {e.Message}"; + // Debug.LogException(e); + infoLines[0] = "Error: Could not detect any distance"; } } diff --git a/Assets/LiveViewModulePanel.cs b/Assets/LiveViewModulePanel.cs index 6a4d4f9..f9b2cb3 100644 --- a/Assets/LiveViewModulePanel.cs +++ b/Assets/LiveViewModulePanel.cs @@ -21,6 +21,7 @@ public class LiveViewModulePanel : MonoBehaviour [Range(0f, 0.5f)] public float cornerRadius = 0.12f; + private ModuleBase _lastSelected = null; private bool _sliderDragging; private ControlPanel panel; private GameObject sensorTextContainer; @@ -64,12 +65,15 @@ public class LiveViewModulePanel : MonoBehaviour { var selected = ModuleSelector.SelectedModule; + // Hide sensor text by default unless a sensor branch shows it + SetSensorTextActive(false); + if (selected == null) { + _lastSelected = null; SetModuleInfo("No module selected"); SetServoSectionActive(false); SetModuleControlSectionActive(false); - SetSensorTextActive(false); return; } @@ -81,15 +85,16 @@ public class LiveViewModulePanel : MonoBehaviour var servo = selected as ServoMotorModule; var dc = selected as DCMotorModule; var display = selected as DisplayModule; + var speaker = selected as SpeakerModule; var distance = selected as DistanceSensorModule; var imu = selected as IMUSensorModule; - var speaker = selected as SpeakerModule; if (servo != null) { + _lastSelected = selected; + SetServoSectionActive(true); SetModuleControlSectionActive(false); - SetSensorTextActive(false); if (servoAngleSlider != null && !_sliderDragging) servoAngleSlider.SetValueWithoutNotify(servo.currentAngle); @@ -99,16 +104,20 @@ public class LiveViewModulePanel : MonoBehaviour if (distance != null) { + _lastSelected = selected; + SetServoSectionActive(false); SetModuleControlSectionActive(false); SetSensorTextActive(true); - SetSensorLines(distance.GetInfoLines()); // implement like IMU or build here + SetSensorLines(distance.GetInfoLines()); return; } if (imu != null) { + _lastSelected = selected; + SetServoSectionActive(false); SetModuleControlSectionActive(false); @@ -117,20 +126,24 @@ public class LiveViewModulePanel : MonoBehaviour return; } - // DC or Display both use the same control section if (dc != null || display != null || speaker != null) { SetServoSectionActive(false); SetModuleControlSectionActive(true); - panel?.Initialize(selected); + if (selected != _lastSelected) + { + _lastSelected = selected; + panel?.Initialize(selected); + } + return; } - // Other types + // Other module types + _lastSelected = selected; SetServoSectionActive(false); SetModuleControlSectionActive(false); - SetSensorTextActive(false); } void SetModuleControlSectionActive(bool active) @@ -199,7 +212,6 @@ public class LiveViewModulePanel : MonoBehaviour var txt = go.AddComponent(); txt.font = moduleInfoText.font; - txt.fontStyle = FontStyle.Bold; txt.fontSize = 22; txt.color = Color.white; txt.alignment = TextAnchor.UpperLeft;