CameraPreview.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. namespace bs.Editor
  2. {
  3. /** @addtogroup Scene-Editor
  4. * @{
  5. */
  6. /// <summary>
  7. /// Shows a pinnable preview of a <see cref="Camera"/> on the scene editor window.
  8. /// </summary>
  9. internal class CameraPreview
  10. {
  11. public bool IsPinned { get; private set; } = false;
  12. public Camera Camera { get; }
  13. private GUIPanel previewPanel;
  14. // Render texture
  15. private GUIPanel renderTexturePanel;
  16. private GUIRenderTexture renderTextureGUI;
  17. // Controls
  18. private GUIPanel controlsPanel;
  19. private GUILabel cameraNameLabel;
  20. private GUIButton pinButton;
  21. public CameraPreview(Camera camera, GUIPanel previewsPanel)
  22. {
  23. Camera = camera;
  24. previewPanel = previewsPanel.AddPanel();
  25. // Render texture GUI
  26. renderTexturePanel = previewPanel.AddPanel();
  27. renderTextureGUI = new GUIRenderTexture(null);
  28. renderTexturePanel.AddElement(renderTextureGUI);
  29. // Control GUI
  30. controlsPanel = previewPanel.AddPanel(-1);
  31. GUILayoutX controlsLayout = controlsPanel.AddLayoutX();
  32. controlsLayout.SetHeight(16);
  33. cameraNameLabel = new GUILabel(string.Empty);
  34. pinButton = new GUIButton(string.Empty);
  35. pinButton.SetWidth(16);
  36. pinButton.SetHeight(16);
  37. pinButton.OnClick += () =>
  38. {
  39. IsPinned = !IsPinned;
  40. UpdatePinButton();
  41. };
  42. controlsLayout.AddElement(cameraNameLabel);
  43. controlsLayout.AddFlexibleSpace();
  44. controlsLayout.AddElement(pinButton);
  45. UpdatePinButton();
  46. }
  47. /// <summary>
  48. /// Sets the content of the pin button according to its current state.
  49. /// </summary>
  50. private void UpdatePinButton()
  51. {
  52. pinButton.SetContent(new GUIContent(IsPinned ? "-" : "+", IsPinned ? "Unpin" : "Pin"));
  53. }
  54. /// <summary>
  55. /// Shows a preview of the specified camera with the specified bounds.
  56. /// </summary>
  57. /// <param name="camera">The camera to preview</param>
  58. /// <param name="bounds">The bounds of the preview</param>
  59. public void ShowPreview(Camera camera, Rect2I bounds)
  60. {
  61. previewPanel.Bounds = bounds;
  62. cameraNameLabel.SetContent(camera.SceneObject?.Name);
  63. renderTextureGUI.SetWidth(bounds.width);
  64. renderTextureGUI.SetHeight(bounds.height);
  65. var cameraRenderTexture = (RenderTexture)camera.Viewport.Target;
  66. if (cameraRenderTexture != null)
  67. {
  68. var renderTexture = new RenderTexture(cameraRenderTexture.ColorSurface);
  69. renderTextureGUI.RenderTexture = renderTexture;
  70. }
  71. else
  72. {
  73. // TODO: We cannot preview cameras that don't have a render target because we need preview support for
  74. // setting a temporary render target for preview purposes
  75. }
  76. }
  77. /// <summary>
  78. /// Destroys the GUI elements representing the preview box.
  79. /// </summary>
  80. public void Destroy()
  81. {
  82. previewPanel?.Destroy();
  83. }
  84. }
  85. /** @} */
  86. }