using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace UnityEngine.Perception.GroundTruth
{
///
/// The control panel for labeler visualizers. This panel resides in the upper right hand corner of
/// the display. Any UI element can be added to the panel, but it contains helper methods to quickly
/// create some of the most common control panel display elements: toggles and sliders.
///
public class ControlPanel : MonoBehaviour
{
HashSet m_Controls = new HashSet();
///
/// Retrieves a list of the current controls in the control panel.
///
public IEnumerable controls => m_Controls;
///
/// Adds a new UI control to the control panel. If the control cannot be added and the method will
/// return false. Also, all UI elements must have a LayoutElement component and if they do not,
/// this method will reject the new element, and return false.
///
/// The control that is to be added to the control panel
/// True if the control could be added, false if it there was a problem adding it
public bool AddNewControl(GameObject uiControl)
{
if (uiControl.GetComponent() == null)
{
Debug.LogError("Control panel UI control must have a rect transform component.");
return false;
}
if (uiControl.GetComponent() == null)
{
Debug.LogError("Control panel UI control must contain a layout element component.");
return false;
}
if (m_Controls.Add(uiControl))
{
uiControl.transform.SetParent(this.transform, false);
return true;
}
return false;
}
///
/// Removes the passed in component from the control panel. Returns
/// false if the element does not exist. Returns true on a successful removal.
/// The caller is responsible for destroying the control.
///
/// The control that needs to be removed from the panel
/// True if the control could be removed, false if there was an issue removing the control
public bool RemoveControl(GameObject uiControl)
{
if (m_Controls.Remove(uiControl))
{
uiControl.transform.SetParent(null);
return true;
}
return false;
}
///
/// Creates a new toggle control with passed in name. The passed in listener will be
/// called on toggle clicks. If anything goes wrong this method will return null.
/// Returns the control panel element upon a successful add.
///
/// The name of the toggle
/// The callback action that will be triggered when the toggle's state changes
/// The created toggle
public GameObject AddToggleControl(string name, UnityAction listener)
{
if (listener == null)
{
Debug.LogWarning("Adding toggle to control panel without a listener, nothing will respond to user's clicks");
}
var toggle = GameObject.Instantiate(Resources.Load("GenericToggle"));
toggle.transform.SetParent(this.transform, false);
toggle.GetComponentInChildren().text = name;
toggle.GetComponent().onValueChanged.AddListener(listener);
m_Controls.Add(toggle);
return toggle;
}
///
/// Creates a new slider control with the passed in name and default value. The slider's value runs from 0 to 1.
/// The passed in listener will be called on slider changes. If anything goes wrong this method will return null.
/// Returns the control panel element upon a successful add.
///
/// The name of the slider control
/// The default value of the slider, between 0 and 1
/// The callback action that will be triggered when the slider's value changes
/// The created slider
public GameObject AddSliderControl(string name, float defaultValue, UnityAction listener)
{
if (listener == null)
{
Debug.LogWarning("Adding slider to control panel without a listener, nothing will respond to user's interactions");
}
var gameObject = GameObject.Instantiate(Resources.Load("GenericSlider"));
gameObject.transform.SetParent(this.transform, false);
gameObject.GetComponentInChildren().text = name;
var slider = gameObject.GetComponentInChildren();
slider.value = defaultValue;
slider.onValueChanged.AddListener(listener);
m_Controls.Add(gameObject);
return gameObject;
}
}
}