| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using BansheeEngine;
- namespace BansheeEditor
- {
- internal sealed class SceneWindow : EditorWindow
- {
- private const int HeaderHeight = 20;
- private Camera camera;
- private SceneCamera cameraController;
- private RenderTexture2D renderTexture;
- private GUILayoutY mainLayout;
- private GUIRenderTexture renderTextureGUI;
- private SceneViewHandler sceneViewHandler;
- private GUIToggle viewButton;
- private GUIToggle moveButton;
- private GUIToggle rotateButton;
- private GUIToggle scaleButton;
- private GUIToggle localCoordButton;
- private GUIToggle worldCoordButton;
- private GUIToggle pivotButton;
- private GUIToggle centerButton;
- private GUIToggle moveSnapButton;
- private GUIFloatField moveSnapInput;
- private GUIToggle rotateSnapButton;
- private GUIFloatField rotateSnapInput;
- private int editorSettingsHash = int.MaxValue;
- public Camera GetCamera()
- {
- return camera;
- }
- internal SceneWindow()
- { }
- private void OnInitialize()
- {
- mainLayout = GUI.AddLayoutY();
- GUIToggleGroup handlesTG = new GUIToggleGroup();
- viewButton = new GUIToggle("V", handlesTG, EditorStyles.Button);
- moveButton = new GUIToggle("M", handlesTG, EditorStyles.Button);
- rotateButton = new GUIToggle("R", handlesTG, EditorStyles.Button);
- scaleButton = new GUIToggle("S", handlesTG, EditorStyles.Button);
- GUIToggleGroup coordModeTG = new GUIToggleGroup();
- localCoordButton = new GUIToggle("L", coordModeTG, EditorStyles.Button);
- worldCoordButton = new GUIToggle("W", coordModeTG, EditorStyles.Button);
- GUIToggleGroup pivotModeTG = new GUIToggleGroup();
- pivotButton = new GUIToggle("P", pivotModeTG, EditorStyles.Button);
- centerButton = new GUIToggle("C", pivotModeTG, EditorStyles.Button);
- moveSnapButton = new GUIToggle("MS", EditorStyles.Button);
- moveSnapInput = new GUIFloatField();
- rotateSnapButton = new GUIToggle("RS", EditorStyles.Button);
- rotateSnapInput = new GUIFloatField();
- viewButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.View);
- moveButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Move);
- rotateButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Rotate);
- scaleButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Scale);
- localCoordButton.OnClick += () => OnCoordinateModeButtonClicked(HandleCoordinateMode.Local);
- worldCoordButton.OnClick += () => OnCoordinateModeButtonClicked(HandleCoordinateMode.World);
- pivotButton.OnClick += () => OnPivotModeButtonClicked(HandlePivotMode.Pivot);
- centerButton.OnClick += () => OnPivotModeButtonClicked(HandlePivotMode.Center);
- moveSnapButton.OnToggled += (bool active) => OnMoveSnapToggled(active);
- moveSnapInput.OnChanged += (float value) => OnMoveSnapValueChanged(value);
- rotateSnapButton.OnToggled += (bool active) => OnRotateSnapToggled(active);
- rotateSnapInput.OnChanged += (float value) => OnRotateSnapValueChanged(value);
- GUILayout handlesLayout = mainLayout.AddLayoutX();
- handlesLayout.AddElement(viewButton);
- handlesLayout.AddElement(moveButton);
- handlesLayout.AddElement(rotateButton);
- handlesLayout.AddElement(scaleButton);
- handlesLayout.AddSpace(10);
- handlesLayout.AddElement(localCoordButton);
- handlesLayout.AddElement(worldCoordButton);
- handlesLayout.AddSpace(10);
- handlesLayout.AddElement(pivotButton);
- handlesLayout.AddElement(centerButton);
- handlesLayout.AddFlexibleSpace();
- handlesLayout.AddElement(moveSnapButton);
- handlesLayout.AddElement(moveSnapInput);
- handlesLayout.AddSpace(10);
- handlesLayout.AddElement(rotateSnapButton);
- handlesLayout.AddElement(rotateSnapInput);
- UpdateRenderTexture(Width, Height - HeaderHeight);
- }
- private void OnDestroy()
- {
- if (camera != null)
- {
- camera.sceneObject.Destroy();
- }
- }
- private bool ScreenToScenePos(Vector2I screenPos, out Vector2I scenePos)
- {
- scenePos = screenPos;
- Vector2I windowPos = ScreenToWindowPos(screenPos);
- Rect2I bounds = GUILayoutUtility.CalculateBounds(renderTextureGUI);
- if (bounds.Contains(windowPos))
- {
- scenePos.x = windowPos.x - bounds.x;
- scenePos.y = windowPos.y - bounds.y;
- return true;
- }
- return false;
- }
- private void EditorUpdate()
- {
- // Refresh GUI buttons if needed (in case someones changes the values from script)
- if (editorSettingsHash != EditorSettings.Hash)
- {
- UpdateButtonStates();
- editorSettingsHash = EditorSettings.Hash;
- }
- // Update scene view handles and selection
- sceneViewHandler.Update();
- bool handleActive = false;
- if (Input.IsPointerButtonUp(PointerButton.Left))
- {
- if (sceneViewHandler.IsHandleActive())
- {
- sceneViewHandler.ClearHandleSelection();
- handleActive = true;
- }
- }
- if (!HasFocus)
- return;
- Vector2I scenePos;
- if (ScreenToScenePos(Input.PointerPosition, out scenePos))
- {
- if (Input.IsPointerButtonDown(PointerButton.Left))
- {
- sceneViewHandler.TrySelectHandle(scenePos);
- }
- else if (Input.IsPointerButtonUp(PointerButton.Left))
- {
- if (!handleActive)
- {
- bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
- Input.IsButtonHeld(ButtonCode.RightControl);
- sceneViewHandler.PickObject(scenePos, ctrlHeld);
- }
- }
- }
- sceneViewHandler.UpdateHandle(scenePos, Input.PointerDelta);
- }
- protected override void WindowResized(int width, int height)
- {
- UpdateRenderTexture(width, height - HeaderHeight);
- base.WindowResized(width, height);
- }
- protected override void FocusChanged(bool inFocus)
- {
- if (!inFocus)
- {
- sceneViewHandler.ClearHandleSelection();
- }
- }
- private void OnSceneToolButtonClicked(SceneViewTool tool)
- {
- EditorApplication.ActiveSceneTool = tool;
- editorSettingsHash = EditorSettings.Hash;
- }
- private void OnCoordinateModeButtonClicked(HandleCoordinateMode mode)
- {
- EditorApplication.ActiveCoordinateMode = mode;
- editorSettingsHash = EditorSettings.Hash;
- }
- private void OnPivotModeButtonClicked(HandlePivotMode mode)
- {
- EditorApplication.ActivePivotMode = mode;
- editorSettingsHash = EditorSettings.Hash;
- }
- private void OnMoveSnapToggled(bool active)
- {
- Handles.MoveHandleSnapActive = active;
- editorSettingsHash = EditorSettings.Hash;
- }
- private void OnMoveSnapValueChanged(float value)
- {
- Handles.MoveSnapAmount = MathEx.Clamp(value, 0.01f, 1000.0f);
- editorSettingsHash = EditorSettings.Hash;
- }
- private void OnRotateSnapToggled(bool active)
- {
- Handles.RotateHandleSnapActive = active;
- editorSettingsHash = EditorSettings.Hash;
- }
- private void OnRotateSnapValueChanged(float value)
- {
- Handles.RotateSnapAmount = MathEx.Clamp(value, 0.01f, 360.0f);
- editorSettingsHash = EditorSettings.Hash;
- }
- private void UpdateButtonStates()
- {
- switch (EditorApplication.ActiveSceneTool)
- {
- case SceneViewTool.View:
- viewButton.ToggleOn();
- break;
- case SceneViewTool.Move:
- moveButton.ToggleOn();
- break;
- case SceneViewTool.Rotate:
- rotateButton.ToggleOn();
- break;
- case SceneViewTool.Scale:
- scaleButton.ToggleOn();
- break;
- }
- switch (EditorApplication.ActiveCoordinateMode)
- {
- case HandleCoordinateMode.Local:
- localCoordButton.ToggleOn();
- break;
- case HandleCoordinateMode.World:
- worldCoordButton.ToggleOn();
- break;
- }
- switch (EditorApplication.ActivePivotMode)
- {
- case HandlePivotMode.Center:
- centerButton.ToggleOn();
- break;
- case HandlePivotMode.Pivot:
- pivotButton.ToggleOn();
- break;
- }
- if (Handles.MoveHandleSnapActive)
- moveSnapButton.ToggleOn();
- else
- moveSnapButton.ToggleOff();
- moveSnapInput.Value = Handles.MoveSnapAmount;
- if (Handles.RotateHandleSnapActive)
- rotateSnapButton.ToggleOn();
- else
- rotateSnapButton.ToggleOff();
- moveSnapInput.Value = Handles.RotateSnapAmount.Degrees;
- }
- private void UpdateRenderTexture(int width, int height)
- {
- width = MathEx.Max(20, width);
- height = MathEx.Max(20, height);
- renderTexture = new RenderTexture2D(PixelFormat.R8G8B8A8, width, height);
- renderTexture.Priority = 1;
- if (camera == null)
- {
- SceneObject sceneCameraSO = new SceneObject("SceneCamera");
- camera = sceneCameraSO.AddComponent<Camera>();
- camera.Target = renderTexture;
- camera.ViewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
- sceneCameraSO.Position = new Vector3(0, 0.5f, 1);
- sceneCameraSO.LookAt(new Vector3(0, 0, 0));
- camera.Priority = 1;
- camera.NearClipPlane = 0.005f;
- camera.FarClipPlane = 1000.0f;
- cameraController = sceneCameraSO.AddComponent<SceneCamera>();
- renderTextureGUI = new GUIRenderTexture(renderTexture);
- mainLayout.AddElement(renderTextureGUI);
- sceneViewHandler = new SceneViewHandler(this, camera);
- }
- else
- {
- camera.Target = renderTexture;
- renderTextureGUI.RenderTexture = renderTexture;
- }
- // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
- // render target destroy/create cycle for every single pixel.
- camera.AspectRatio = width / (float)height;
- }
- }
- }
|