using System;
using Unity.Simulation;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace UnityEngine.Perception.GroundTruth
{
///
/// Abstract class for defining custom annotation and metric generation to be run by .
/// Instances of CameraLabeler on will be invoked each frame the camera is
/// set to capture data (see ).
///
[Serializable]
public abstract class CameraLabeler
{
///
/// Whether the CameraLabeler should be set up and called each frame.
///
public bool enabled = true;
internal bool isInitialized { get; private set; }
///
/// Labelers should set this in their setup to define if they support realtime
/// visualization of their data.
///
protected abstract bool supportsVisualization
{
get;
}
///
/// Retrieve a handle to the visualization canvas . This is the specific canvas that all visualization
/// labelers should be added to. The canvas has helper functions to create many common visualization components.
///
public VisualizationCanvas visualizationCanvas { get; private set; }
///
/// The control panel that is attached to the visualization canvas. The common location to add interactive controls.
///
public ControlPanel controlPanel => visualizationCanvas != null ? visualizationCanvas.controlPanel : null;
///
/// The heads up display (HUD) panel. Generally used to add stats to the display.
///
public HUDPanel hudPanel => visualizationCanvas != null ? visualizationCanvas.hudPanel : null;
///
/// The that contains this labeler.
///
protected PerceptionCamera perceptionCamera { get; private set; }
///
/// The SensorHandle for the that contains this labeler. Use this to report
/// annotations and metrics.
///
protected SensorHandle sensorHandle { get; private set; }
///
/// Called just before the first call to or . Implement this
/// to initialize state.
///
protected virtual void Setup() { }
///
/// Called immediately after . Implement this to initialize labeler's visualization
/// capability if one exists .
///
/// The target control panel for the labeler's visualization component
protected virtual void PopulateVisualizationPanel(ControlPanel panel) { }
///
/// Called when the labeler's visualization capability is turned on or off.
///
/// The current enabled state of the visualizer
protected virtual void OnVisualizerEnabledChanged(bool enabled) {}
///
/// Called during the Update each frame the the labeler is enabled and is true.
///
protected virtual void OnUpdate() {}
///
/// Called just before the camera renders each frame the the labeler is enabled and is true.
///
protected virtual void OnBeginRendering() {}
///
/// Called when the Labeler is about to be destroyed or removed from the PerceptionCamera. Use this to clean up to state.
///
protected virtual void Cleanup() {}
internal void InternalSetup() => Setup();
internal void InternalPopulateVisualizationPanel(GameObject panel) => PopulateVisualizationPanel(controlPanel);
internal bool InternalVisualizationEnabled
{
get => visualizationEnabled;
set => visualizationEnabled = value;
}
internal void InternalOnUpdate() => OnUpdate();
internal void InternalOnBeginRendering() => OnBeginRendering();
internal void InternalCleanup() => Cleanup();
private bool m_ShowVisualizations = false;
///
/// Turns on/off the labeler's realtime visualization capability. If a labeler does not support realtime
/// visualization () or visualization is not enabled on the PerceptionCamera
/// this will not function.
///
protected bool visualizationEnabled
{
get
{
return supportsVisualization && m_ShowVisualizations;
}
set
{
if (!supportsVisualization) return;
if (visualizationCanvas == null) return;
if (value != m_ShowVisualizations)
{
m_ShowVisualizations = value;
OnVisualizerEnabledChanged(m_ShowVisualizations);
}
}
}
internal void Init(PerceptionCamera newPerceptionCamera, VisualizationCanvas visualizationCanvas)
{
try
{
this.perceptionCamera = newPerceptionCamera;
sensorHandle = newPerceptionCamera.SensorHandle;
this.visualizationCanvas = visualizationCanvas;
Setup();
isInitialized = true;
if (supportsVisualization && visualizationCanvas != null)
{
m_ShowVisualizations = true;
InitVisualizationUI();
}
}
catch (Exception)
{
this.enabled = false;
throw;
}
}
private void InitVisualizationUI()
{
PopulateVisualizationPanel(controlPanel);
}
}
}