using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// For the ZED 2D Object Detection sample. Handles the canvas elements that are moved and resized
/// to represent objects detected in 2D.
/// This was designed specifically for the 2D Bounding Box prefab.
///
public class BBox2DHandler : MonoBehaviour
{
///
/// Text object that displays the ID and distance values of the object.
///
[Tooltip("Text object that displays the ID and distance values of the object.")]
public Text infoText;
///
/// Outline component around the Image component that surrounds the Text component.
///
[Tooltip("Outline component around the Image component that surrounds the Text component.")]
public Outline boxOutline;
///
/// All images in the prefab that should be colored when SetColor() is called.
///
[Tooltip("All images in the prefab that should be colored when SetColor() is called.")]
public List imagesToColor = new List();
///
/// RawImage object on the prefab to be used to display the object's mask, if enabled.
///
[Space(5)]
[Tooltip("RawImage object on the prefab to be used to display the object's mask, if enabled.")]
public RawImage maskImage;
///
/// If true, infoText will display the ID value of the object, assuming it's been set.
///
[Space(5)]
[Tooltip("If true, infoText will display the ID value of the object, assuming it's been set.")]
public bool showID = true;
///
/// If true, infoText will display the distance value of the object from its capturing camera, assuming it's been set.
///
[Tooltip("If true, infoText will display the ID value of the object, assuming it's been set.")]
public bool showDistance = true;
///
/// ID of the object that this instance is currently representing.
///
public int currentID { get; private set; }
///
/// Distance from this object to the ZED camera that detected it.
///
public float currentDistance { get; private set; }
private void OnEnable()
{
if(maskImage) //Disable the mask image at first, so you don't see it if we're not applying a mask.
{
maskImage.gameObject.SetActive(false);
}
}
///
/// Changes the color of all relevant elements in the prefab (box, text, etc.) to the color provided.
/// Ignores input alpha and uses existing alpha of prefab's components.
///
public void SetColor(Color col)
{
foreach (Image img in imagesToColor)
{
float oldimgalpha = img.color.a;
img.color = new Color(col.r, col.g, col.b, oldimgalpha);
}
if (infoText)
{
float oldtextalpha = infoText.color.a;
infoText.color = new Color(col.r, col.g, col.b, oldtextalpha);
}
if(boxOutline)
{
float oldtextalpha = boxOutline.effectColor.a;
boxOutline.effectColor = new Color(col.r, col.g, col.b, oldtextalpha);
}
if(maskImage)
{
float oldmaskalpha = maskImage.color.a;
maskImage.color = new Color(col.r, col.g, col.b, oldmaskalpha);
}
}
///
/// Sets the ID value that will be displayed on the box's label.
/// Usually set when the box first starts representing a detected object.
///
public void SetID(int id)
{
currentID = id;
UpdateText(currentID, currentDistance);
}
///
/// Sets the distance value that will be displayed on the box's label.
/// Designed to indicate the distance from the camera that saw the object.
/// Value is expected in meters. Should be updated with each new detection.
///
public void SetDistance(float dist)
{
currentDistance = dist;
UpdateText(currentID, currentDistance);
}
public void SetMaskImage(Texture mask)
{
if(maskImage)
{
maskImage.gameObject.SetActive(true);
maskImage.texture = mask;
}
}
///
/// Updates the text in the label based on the given ID and distance values.
///
private void UpdateText(int id, float dist)
{
string newtext = "";
if (showID) newtext += "ID: " + id.ToString();
if (showID && showDistance) newtext += "\r\n";
if (showDistance) newtext += dist.ToString("F2") + "m";
if (infoText) infoText.text = newtext;
}
private void OnDestroy()
{
if(maskImage) //Makes sure textures left over on the masks are cleaned up.
{
Destroy(maskImage.texture);
}
}
}