namespace bs.Editor
{
/** @addtogroup Scene-Editor
* @{
*/
///
/// Shows a pinnable preview of a on the scene editor window.
///
internal class CameraPreview
{
public bool IsPinned { get; private set; } = false;
public Camera Camera { get; }
private GUIPanel previewPanel;
// Render texture
private GUIPanel renderTexturePanel;
private GUIRenderTexture renderTextureGUI;
// Controls
private GUIPanel controlsPanel;
private GUILabel cameraNameLabel;
private GUIButton pinButton;
public CameraPreview(Camera camera, GUIPanel previewsPanel)
{
Camera = camera;
previewPanel = previewsPanel.AddPanel();
// Render texture GUI
renderTexturePanel = previewPanel.AddPanel();
renderTextureGUI = new GUIRenderTexture(null);
renderTexturePanel.AddElement(renderTextureGUI);
// Control GUI
controlsPanel = previewPanel.AddPanel(-1);
GUILayoutX controlsLayout = controlsPanel.AddLayoutX();
controlsLayout.SetHeight(16);
cameraNameLabel = new GUILabel(string.Empty);
pinButton = new GUIButton(string.Empty);
pinButton.SetWidth(16);
pinButton.SetHeight(16);
pinButton.OnClick += () =>
{
IsPinned = !IsPinned;
UpdatePinButton();
};
controlsLayout.AddElement(cameraNameLabel);
controlsLayout.AddFlexibleSpace();
controlsLayout.AddElement(pinButton);
UpdatePinButton();
}
///
/// Sets the content of the pin button according to its current state.
///
private void UpdatePinButton()
{
pinButton.SetContent(new GUIContent(IsPinned ? "-" : "+", IsPinned ? "Unpin" : "Pin"));
}
///
/// Shows a preview of the specified camera with the specified bounds.
///
/// The camera to preview
/// The bounds of the preview
public void ShowPreview(Camera camera, Rect2I bounds)
{
previewPanel.Bounds = bounds;
cameraNameLabel.SetContent(camera.SceneObject?.Name);
renderTextureGUI.SetWidth(bounds.width);
renderTextureGUI.SetHeight(bounds.height);
var cameraRenderTexture = (RenderTexture)camera.Viewport.Target;
if (cameraRenderTexture != null)
{
var renderTexture = new RenderTexture(cameraRenderTexture.ColorSurface);
renderTextureGUI.RenderTexture = renderTexture;
}
else
{
// TODO: We cannot preview cameras that don't have a render target because we need preview support for
// setting a temporary render target for preview purposes
}
}
///
/// Destroys the GUI elements representing the preview box.
///
public void Destroy()
{
previewPanel?.Destroy();
}
}
/** @} */
}