Fixed problem with display module text input field

This commit is contained in:
Christen
2026-03-10 17:02:07 -04:00
parent 5119422966
commit a8679c35a9
3 changed files with 44 additions and 60 deletions

View File

@@ -15,10 +15,6 @@ public class ControlPanel : MonoBehaviour
private DisplayModule _display; private DisplayModule _display;
private SpeakerModule _speaker; private SpeakerModule _speaker;
private string _savedDCInput = "";
private int _savedDirectionIndex = 0;
private string _savedDisplayInput = "";
private Text _caption; // child "DegreesCaption" private Text _caption; // child "DegreesCaption"
private Text _buttonText; // child "RotateButton/Text (TMP)" private Text _buttonText; // child "RotateButton/Text (TMP)"
private GameObject _directionCaptionGO; private GameObject _directionCaptionGO;
@@ -29,6 +25,20 @@ public class ControlPanel : MonoBehaviour
private Button _playButton; private Button _playButton;
private Text _playButtonText; private Text _playButtonText;
private void OnEnable()
{
if (_mode == Mode.Speaker)
{
SetUploadVisible(true);
SetPlayVisible(true);
}
else
{
SetUploadVisible(false);
SetPlayVisible(false);
}
}
private void Awake() private void Awake()
{ {
CacheUIFromHierarchy(); CacheUIFromHierarchy();
@@ -42,26 +52,10 @@ public class ControlPanel : MonoBehaviour
button.onClick.RemoveAllListeners(); button.onClick.RemoveAllListeners();
button.onClick.AddListener(OnMainButtonClicked); 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) public void Initialize(ModuleBase module)
{ {
CacheCurrentInput();
_dc = module as DCMotorModule; _dc = module as DCMotorModule;
_display = module as DisplayModule; _display = module as DisplayModule;
_speaker = module as SpeakerModule; _speaker = module as SpeakerModule;
@@ -75,7 +69,6 @@ public class ControlPanel : MonoBehaviour
public void HidePanel() public void HidePanel()
{ {
CacheCurrentInput();
gameObject.SetActive(false); gameObject.SetActive(false);
_mode = Mode.None; _mode = Mode.None;
_dc = null; _dc = null;
@@ -102,7 +95,6 @@ public class ControlPanel : MonoBehaviour
private void SwitchMode(Mode newMode) private void SwitchMode(Mode newMode)
{ {
CacheCurrentInput();
_mode = newMode; _mode = newMode;
if (_mode == Mode.DC) if (_mode == Mode.DC)
@@ -118,11 +110,11 @@ public class ControlPanel : MonoBehaviour
if (inputField != null) if (inputField != null)
{ {
inputField.readOnly = false; inputField.readOnly = false;
inputField.SetTextWithoutNotify(_savedDCInput); inputField.SetTextWithoutNotify("");
} }
if (directionDropdown != null) if (directionDropdown != null)
directionDropdown.SetValueWithoutNotify(_savedDirectionIndex); directionDropdown.SetValueWithoutNotify(0);
} }
else if (_mode == Mode.Display) else if (_mode == Mode.Display)
{ {
@@ -137,10 +129,7 @@ public class ControlPanel : MonoBehaviour
if (inputField != null) if (inputField != null)
{ {
inputField.readOnly = false; inputField.readOnly = false;
string toShow = _savedDisplayInput; inputField.SetTextWithoutNotify("");
if (_display != null && !string.IsNullOrEmpty(_display.displayText))
toShow = _display.displayText;
inputField.SetTextWithoutNotify(toShow);
} }
} }
else if (_mode == Mode.Speaker) else if (_mode == Mode.Speaker)
@@ -179,21 +168,6 @@ public class ControlPanel : MonoBehaviour
inputField.gameObject.SetActive(visible); 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() private void OnMainButtonClicked()
{ {
if (_mode == Mode.DC) if (_mode == Mode.DC)

View File

@@ -4,7 +4,7 @@ using UnityEngine;
public class DistanceSensorModule : ModuleBase public class DistanceSensorModule : ModuleBase
{ {
// Keep a fixed-size array (no GC alloc every frame) // 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 moduleType => "Distance";
public override string moduleName => "Distance Sensor Module"; public override string moduleName => "Distance Sensor Module";
@@ -15,28 +15,26 @@ public class DistanceSensorModule : ModuleBase
void Update() void Update()
{ {
// Always show something immediately
infoLines[0] = $"ModuleID raw: '{moduleID}'";
// Poll at a fixed rate (avoids frame spam) // Poll at a fixed rate (avoids frame spam)
if (Time.unscaledTime < nextPollTime) return; if (Time.unscaledTime < nextPollTime) return;
nextPollTime = Time.unscaledTime + (1f / Mathf.Max(1f, pollHz)); nextPollTime = Time.unscaledTime + (1f / Mathf.Max(1f, pollHz));
if (!int.TryParse(moduleID, out var id)) if (!int.TryParse(moduleID, out var id))
{ {
infoLines[1] = "ERROR: moduleID is not an int"; infoLines[0] = "ERROR: moduleID is not an int";
return; return;
} }
try try
{ {
double distance = ControlLibrary.get_distance_control(id); double distance = ControlLibrary.get_distance_control(id);
infoLines[1] = $"Distance: {distance:F0} mm"; infoLines[0] = $"Distance: {distance:F0} mm";
} }
catch (Exception e) catch (Exception e)
{ {
infoLines[1] = $"EXCEPTION: {e.GetType().Name}: {e.Message}"; // infoLines[1] = $"EXCEPTION: {e.GetType().Name}: {e.Message}";
Debug.LogException(e); // Debug.LogException(e);
infoLines[0] = "Error: Could not detect any distance";
} }
} }

View File

@@ -21,6 +21,7 @@ public class LiveViewModulePanel : MonoBehaviour
[Range(0f, 0.5f)] [Range(0f, 0.5f)]
public float cornerRadius = 0.12f; public float cornerRadius = 0.12f;
private ModuleBase _lastSelected = null;
private bool _sliderDragging; private bool _sliderDragging;
private ControlPanel panel; private ControlPanel panel;
private GameObject sensorTextContainer; private GameObject sensorTextContainer;
@@ -64,12 +65,15 @@ public class LiveViewModulePanel : MonoBehaviour
{ {
var selected = ModuleSelector.SelectedModule; var selected = ModuleSelector.SelectedModule;
// Hide sensor text by default unless a sensor branch shows it
SetSensorTextActive(false);
if (selected == null) if (selected == null)
{ {
_lastSelected = null;
SetModuleInfo("No module selected"); SetModuleInfo("No module selected");
SetServoSectionActive(false); SetServoSectionActive(false);
SetModuleControlSectionActive(false); SetModuleControlSectionActive(false);
SetSensorTextActive(false);
return; return;
} }
@@ -81,15 +85,16 @@ public class LiveViewModulePanel : MonoBehaviour
var servo = selected as ServoMotorModule; var servo = selected as ServoMotorModule;
var dc = selected as DCMotorModule; var dc = selected as DCMotorModule;
var display = selected as DisplayModule; var display = selected as DisplayModule;
var speaker = selected as SpeakerModule;
var distance = selected as DistanceSensorModule; var distance = selected as DistanceSensorModule;
var imu = selected as IMUSensorModule; var imu = selected as IMUSensorModule;
var speaker = selected as SpeakerModule;
if (servo != null) if (servo != null)
{ {
_lastSelected = selected;
SetServoSectionActive(true); SetServoSectionActive(true);
SetModuleControlSectionActive(false); SetModuleControlSectionActive(false);
SetSensorTextActive(false);
if (servoAngleSlider != null && !_sliderDragging) if (servoAngleSlider != null && !_sliderDragging)
servoAngleSlider.SetValueWithoutNotify(servo.currentAngle); servoAngleSlider.SetValueWithoutNotify(servo.currentAngle);
@@ -99,16 +104,20 @@ public class LiveViewModulePanel : MonoBehaviour
if (distance != null) if (distance != null)
{ {
_lastSelected = selected;
SetServoSectionActive(false); SetServoSectionActive(false);
SetModuleControlSectionActive(false); SetModuleControlSectionActive(false);
SetSensorTextActive(true); SetSensorTextActive(true);
SetSensorLines(distance.GetInfoLines()); // implement like IMU or build here SetSensorLines(distance.GetInfoLines());
return; return;
} }
if (imu != null) if (imu != null)
{ {
_lastSelected = selected;
SetServoSectionActive(false); SetServoSectionActive(false);
SetModuleControlSectionActive(false); SetModuleControlSectionActive(false);
@@ -117,20 +126,24 @@ public class LiveViewModulePanel : MonoBehaviour
return; return;
} }
// DC or Display both use the same control section
if (dc != null || display != null || speaker != null) if (dc != null || display != null || speaker != null)
{ {
SetServoSectionActive(false); SetServoSectionActive(false);
SetModuleControlSectionActive(true); SetModuleControlSectionActive(true);
panel?.Initialize(selected); if (selected != _lastSelected)
{
_lastSelected = selected;
panel?.Initialize(selected);
}
return; return;
} }
// Other types // Other module types
_lastSelected = selected;
SetServoSectionActive(false); SetServoSectionActive(false);
SetModuleControlSectionActive(false); SetModuleControlSectionActive(false);
SetSensorTextActive(false);
} }
void SetModuleControlSectionActive(bool active) void SetModuleControlSectionActive(bool active)
@@ -199,7 +212,6 @@ public class LiveViewModulePanel : MonoBehaviour
var txt = go.AddComponent<Text>(); var txt = go.AddComponent<Text>();
txt.font = moduleInfoText.font; txt.font = moduleInfoText.font;
txt.fontStyle = FontStyle.Bold;
txt.fontSize = 22; txt.fontSize = 22;
txt.color = Color.white; txt.color = Color.white;
txt.alignment = TextAnchor.UpperLeft; txt.alignment = TextAnchor.UpperLeft;