From c38da8cabf61e643a49c2fcc09b11aa3c800ac38 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Fri, 23 Dec 2022 17:37:21 -0600 Subject: [PATCH 01/10] Added resizing to the game viewport. --- Source/Editor/Windows/GameWindow.cs | 41 ++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/Source/Editor/Windows/GameWindow.cs b/Source/Editor/Windows/GameWindow.cs index c800692f7..0ca73fab8 100644 --- a/Source/Editor/Windows/GameWindow.cs +++ b/Source/Editor/Windows/GameWindow.cs @@ -238,12 +238,51 @@ namespace FlaxEditor.Windows // Setup viewport _viewport = new RenderOutputControl(task) { - AnchorPreset = AnchorPresets.StretchAll, + AnchorPreset = AnchorPresets.TopLeft, Offsets = Margin.Zero, AutoFocus = false, Parent = this }; task.PostRender += OnPostRender; + + // TODO move external and let user define. Maybe also just let them define a ratio as well + Float2 viewPortSize = new Float2(1920, 1080); + bool useCustomAspect = true; + bool freeAspect = false; + Float2 customAspect = new Float2(9, 16); + + SizeChanged += control => + { + float viewportAspectRatio = 1; + float windowAspectRatio = 1; + if (!freeAspect) + { + if (!useCustomAspect) + viewportAspectRatio = viewPortSize.X / viewPortSize.Y; + else + viewportAspectRatio = customAspect.X / customAspect.Y; + + windowAspectRatio = Size.X / Size.Y; + } + + var scaleWidth = viewportAspectRatio / windowAspectRatio; + var scaleHeight = windowAspectRatio / viewportAspectRatio; + + if (scaleHeight < 1) + { + _viewport.Width = Size.X; + _viewport.Height = Size.Y * scaleHeight; + _viewport.X = 0; + _viewport.Y = Size.Y * (1-scaleHeight)/2; + } + else + { + _viewport.Width = Size.X * scaleWidth; + _viewport.Height = Size.Y; + _viewport.X = Size.X * (1-scaleWidth)/2; + _viewport.Y = 0; + } + }; // Override the game GUI root _guiRoot = new GameRoot From 69d391d9855f81340c8c5874c439fbf9ae0c9579 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Fri, 23 Dec 2022 17:40:13 -0600 Subject: [PATCH 02/10] changed default --- Source/Editor/Windows/GameWindow.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Editor/Windows/GameWindow.cs b/Source/Editor/Windows/GameWindow.cs index 0ca73fab8..807828ccc 100644 --- a/Source/Editor/Windows/GameWindow.cs +++ b/Source/Editor/Windows/GameWindow.cs @@ -247,7 +247,7 @@ namespace FlaxEditor.Windows // TODO move external and let user define. Maybe also just let them define a ratio as well Float2 viewPortSize = new Float2(1920, 1080); - bool useCustomAspect = true; + bool useCustomAspect = false; bool freeAspect = false; Float2 customAspect = new Float2(9, 16); From 45564c940fa610d76f1a69ec25702ec18d305cc0 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 27 Dec 2022 08:46:49 -0600 Subject: [PATCH 03/10] Moved function --- Source/Editor/Windows/GameWindow.cs | 97 +++++++++++++++++------------ 1 file changed, 57 insertions(+), 40 deletions(-) diff --git a/Source/Editor/Windows/GameWindow.cs b/Source/Editor/Windows/GameWindow.cs index 807828ccc..64df517ad 100644 --- a/Source/Editor/Windows/GameWindow.cs +++ b/Source/Editor/Windows/GameWindow.cs @@ -238,51 +238,12 @@ namespace FlaxEditor.Windows // Setup viewport _viewport = new RenderOutputControl(task) { - AnchorPreset = AnchorPresets.TopLeft, + AnchorPreset = AnchorPresets.StretchAll, Offsets = Margin.Zero, AutoFocus = false, Parent = this }; task.PostRender += OnPostRender; - - // TODO move external and let user define. Maybe also just let them define a ratio as well - Float2 viewPortSize = new Float2(1920, 1080); - bool useCustomAspect = false; - bool freeAspect = false; - Float2 customAspect = new Float2(9, 16); - - SizeChanged += control => - { - float viewportAspectRatio = 1; - float windowAspectRatio = 1; - if (!freeAspect) - { - if (!useCustomAspect) - viewportAspectRatio = viewPortSize.X / viewPortSize.Y; - else - viewportAspectRatio = customAspect.X / customAspect.Y; - - windowAspectRatio = Size.X / Size.Y; - } - - var scaleWidth = viewportAspectRatio / windowAspectRatio; - var scaleHeight = windowAspectRatio / viewportAspectRatio; - - if (scaleHeight < 1) - { - _viewport.Width = Size.X; - _viewport.Height = Size.Y * scaleHeight; - _viewport.X = 0; - _viewport.Y = Size.Y * (1-scaleHeight)/2; - } - else - { - _viewport.Width = Size.X * scaleWidth; - _viewport.Height = Size.Y; - _viewport.X = Size.X * (1-scaleWidth)/2; - _viewport.Y = 0; - } - }; // Override the game GUI root _guiRoot = new GameRoot @@ -294,6 +255,62 @@ namespace FlaxEditor.Windows Parent = _viewport }; RootControl.GameRoot = _guiRoot; + + // TODO move external and let user define. Maybe also just let them define a ratio as well + Float2 viewPortSize = new Float2(2560, 1440); + //Float2 viewPortSize = new Float2(1280, 720); + //Float2 viewPortSize = new Float2(848, 480); + bool useCustomAspect = false; + bool freeAspect = false; + Float2 customAspect = new Float2(9, 16); + + SizeChanged += control => + { + float viewportAspectRatio = 1; + float windowAspectRatio = 1; + + if (!freeAspect) + { + if (!useCustomAspect) + { + viewportAspectRatio = viewPortSize.X / viewPortSize.Y; + _viewport.KeepAspectRatio = true; + _viewport.CustomResolution = new Int2((int)viewPortSize.X, (int)viewPortSize.Y); + } + else + { + viewportAspectRatio = customAspect.X / customAspect.Y; + _viewport.CustomResolution = new Int2?(); + _viewport.KeepAspectRatio = false; + } + + windowAspectRatio = Width / Height; + } + else + { + _viewport.CustomResolution = new Int2?(); + _viewport.KeepAspectRatio = false; + } + + var scaleWidth = viewportAspectRatio / windowAspectRatio; + var scaleHeight = windowAspectRatio / viewportAspectRatio; + + if (scaleHeight < 1) + { + _viewport.Width = Width; + _viewport.Height = Height * scaleHeight; + _viewport.X = 0; + _viewport.Y = Height * (1 - scaleHeight) / 2; + } + else + { + _viewport.Width = Width * scaleWidth; + _viewport.Height = Height; + _viewport.X = Width * (1 - scaleWidth) / 2; + _viewport.Y = 0; + } + }; + Editor.StateMachine.PlayingState.SceneDuplicating += PlayingStateOnSceneDuplicating; Editor.StateMachine.PlayingState.SceneRestored += PlayingStateOnSceneRestored; From b225b5a6e676a4310881d383be00ca4b34ece454 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 27 Dec 2022 11:40:17 -0600 Subject: [PATCH 04/10] Simplified setting bounds --- Source/Editor/Windows/GameWindow.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/Source/Editor/Windows/GameWindow.cs b/Source/Editor/Windows/GameWindow.cs index 64df517ad..2b1a067fe 100644 --- a/Source/Editor/Windows/GameWindow.cs +++ b/Source/Editor/Windows/GameWindow.cs @@ -297,17 +297,11 @@ namespace FlaxEditor.Windows if (scaleHeight < 1) { - _viewport.Width = Width; - _viewport.Height = Height * scaleHeight; - _viewport.X = 0; - _viewport.Y = Height * (1 - scaleHeight) / 2; + _viewport.Bounds = new Rectangle(0, Height * (1 - scaleHeight) / 2, Width, Height * scaleHeight); } else { - _viewport.Width = Width * scaleWidth; - _viewport.Height = Height; - _viewport.X = Width * (1 - scaleWidth) / 2; - _viewport.Y = 0; + _viewport.Bounds = new Rectangle(Width * (1 - scaleWidth) / 2, 0, Width * scaleWidth, Height); } }; From ca07c18b93ed59f61abadf3b92e726fc55c486e9 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Wed, 28 Dec 2022 15:51:51 -0600 Subject: [PATCH 05/10] Fist pass on game viewport resizing menu --- Source/Editor/Windows/GameWindow.cs | 276 ++++++++++++++++++++++++---- 1 file changed, 238 insertions(+), 38 deletions(-) diff --git a/Source/Editor/Windows/GameWindow.cs b/Source/Editor/Windows/GameWindow.cs index 2b1a067fe..d7b7e1fc1 100644 --- a/Source/Editor/Windows/GameWindow.cs +++ b/Source/Editor/Windows/GameWindow.cs @@ -8,6 +8,7 @@ using FlaxEditor.GUI.Input; using FlaxEditor.Options; using FlaxEngine; using FlaxEngine.GUI; +using FlaxEngine.Json; namespace FlaxEditor.Windows { @@ -106,6 +107,43 @@ namespace FlaxEditor.Windows /// public bool FocusOnPlay { get; set; } + // TODO move by other privates + private List _defaultViewportScaling = new List(); + private List _customViewportScaling = new List(); + private float _viewportAspectRatio = 1; + private float _windowAspectRatio = 1; + bool _useAspect = false; + bool _freeAspect = true; + + private enum ViewportScalingType + { + Resolution = 0, + Aspect = 1, + } + + private class ViewportScaling + { + /// + /// The name. + /// + public string Label; + + /// + /// The Type of scaling to do. + /// + public ViewportScalingType ScalingType; + + /// + /// The width and height to scale by. + /// + public Int2 Size; + + /// + /// If the scaling is active. + /// + public bool Active; + } + private class GameRoot : ContainerControl { public bool EnableEvents => !Time.GamePaused; @@ -266,45 +304,9 @@ namespace FlaxEditor.Windows SizeChanged += control => { - float viewportAspectRatio = 1; - float windowAspectRatio = 1; - - if (!freeAspect) - { - if (!useCustomAspect) - { - viewportAspectRatio = viewPortSize.X / viewPortSize.Y; - _viewport.KeepAspectRatio = true; - _viewport.CustomResolution = new Int2((int)viewPortSize.X, (int)viewPortSize.Y); - } - else - { - viewportAspectRatio = customAspect.X / customAspect.Y; - _viewport.CustomResolution = new Int2?(); - _viewport.KeepAspectRatio = false; - } - - windowAspectRatio = Width / Height; - } - else - { - _viewport.CustomResolution = new Int2?(); - _viewport.KeepAspectRatio = false; - } - - var scaleWidth = viewportAspectRatio / windowAspectRatio; - var scaleHeight = windowAspectRatio / viewportAspectRatio; - - if (scaleHeight < 1) - { - _viewport.Bounds = new Rectangle(0, Height * (1 - scaleHeight) / 2, Width, Height * scaleHeight); - } - else - { - _viewport.Bounds = new Rectangle(Width * (1 - scaleWidth) / 2, 0, Width * scaleWidth, Height); - } + ResizeViewport(); }; - + Editor.StateMachine.PlayingState.SceneDuplicating += PlayingStateOnSceneDuplicating; Editor.StateMachine.PlayingState.SceneRestored += PlayingStateOnSceneRestored; @@ -317,6 +319,86 @@ namespace FlaxEditor.Windows InputActions.Add(options => options.StepFrame, Editor.Simulation.RequestPlayOneFrame); } + private void ChangeViewportRatio(ViewportScaling v) + { + if (v == null) + return; + + // TODO make it so user can not enter values of 0 or lower + if (v.Size.Y <= 0 || v.Size.X <= 0) + { + return; + } + + if (string.Equals(v.Label, "Free Aspect") && v.Size == new Int2(1,1)) + { + _freeAspect = true; + _useAspect = true; + } + else + { + switch (v.ScalingType) + { + case ViewportScalingType.Aspect: + _useAspect = true; + _freeAspect = false; + break; + case ViewportScalingType.Resolution: + _useAspect = false; + _freeAspect = false; + break; + } + } + + _viewportAspectRatio = (float)v.Size.X / v.Size.Y; + + if (!_freeAspect) + { + if (!_useAspect) + { + _viewport.KeepAspectRatio = true; + _viewport.CustomResolution = new Int2(v.Size.X, v.Size.Y); + } + else + { + _viewport.CustomResolution = new Int2?(); + _viewport.KeepAspectRatio = true; + } + } + else + { + _viewport.CustomResolution = new Int2?(); + _viewport.KeepAspectRatio = false; + } + + ResizeViewport(); + } + + private void ResizeViewport() + { + if (!_freeAspect) + { + _windowAspectRatio = Width / Height; + } + else + { + _windowAspectRatio = 1; + } + + var scaleWidth = _viewportAspectRatio / _windowAspectRatio; + var scaleHeight = _windowAspectRatio / _viewportAspectRatio; + + if (scaleHeight < 1) + { + _viewport.Bounds = new Rectangle(0, Height * (1 - scaleHeight) / 2, Width, Height * scaleHeight); + } + else + { + _viewport.Bounds = new Rectangle(Width * (1 - scaleWidth) / 2, 0, Width * scaleWidth, Height); + } + PerformLayout(); + } + private void OnPostRender(GPUContext context, ref RenderContext renderContext) { // Debug Draw shapes @@ -422,6 +504,104 @@ namespace FlaxEditor.Windows resolutionValue.ValueChanged += () => _viewport.ResolutionScale = resolutionValue.Value; } + // Viewport aspect ratio + { + // Create default scaling options if they dont exist from deserialization. + if (_defaultViewportScaling.Count == 0) + { + Debug.Log("Had to create scalings."); + _defaultViewportScaling.Add(new ViewportScaling + { + Label = "Free Aspect", + ScalingType = ViewportScalingType.Aspect, + Size = new Int2(1,1), + Active = true, + }); + _defaultViewportScaling.Add(new ViewportScaling + { + Label = "16:9 Aspect", + ScalingType = ViewportScalingType.Aspect, + Size = new Int2(16,9), + Active = false, + }); + _defaultViewportScaling.Add(new ViewportScaling + { + Label = "16:10 Aspect", + ScalingType = ViewportScalingType.Aspect, + Size = new Int2(16,10), + Active = false, + }); + _defaultViewportScaling.Add(new ViewportScaling + { + Label = "1920x1080 Resolution", + ScalingType = ViewportScalingType.Resolution, + Size = new Int2(1920,1080), + Active = false, + }); + _defaultViewportScaling.Add(new ViewportScaling + { + Label = "2560x1440 Resolution", + ScalingType = ViewportScalingType.Resolution, + Size = new Int2(2560,1440), + Active = false, + }); + } + + var aspectMenu = menu.AddChildMenu("Viewport Scaling").ContextMenu; + + for (int i = 0; i < _defaultViewportScaling.Count; i++) + { + var button = aspectMenu.AddButton(_defaultViewportScaling[i].Label); + button.CloseMenuOnClick = false; + button.Icon = _defaultViewportScaling[i].Active ? Style.Current.CheckBoxTick : SpriteHandle.Invalid; + button.Tag = _defaultViewportScaling[i]; + if (_defaultViewportScaling[i].Active) + ChangeViewportRatio(_defaultViewportScaling[i]); + } + aspectMenu.AddSeparator(); + for (int i = 0; i < _customViewportScaling.Count; i++) + { + var button = aspectMenu.AddButton(_customViewportScaling[i].Label); + button.CloseMenuOnClick = false; + button.Icon = _customViewportScaling[i].Active ? Style.Current.CheckBoxTick : SpriteHandle.Invalid; + button.Tag = _customViewportScaling[i]; + if (_customViewportScaling[i].Active) + ChangeViewportRatio(_customViewportScaling[i]); + } + + aspectMenu.ButtonClicked += button => + { + if (button.Text == "Add...") + return; + if (button.Tag == null) + return; + + // Reset selected icon on all buttons + foreach (var child in aspectMenu.Items) + { + if (child is ContextMenuButton cmb) + { + var v = (ViewportScaling)cmb.Tag; + if (cmb == button) + { + v.Active = true; + button.Icon = Style.Current.CheckBoxTick; + ChangeViewportRatio(v); + } + else if (v.Active) + { + cmb.Icon = SpriteHandle.Invalid; + v.Active = false; + } + } + } + }; + aspectMenu.AddSeparator(); + + // TODO add + var add = aspectMenu.AddButton("Add..."); + } + // Take Screenshot { var takeScreenshot = menu.AddButton("Take Screenshot"); @@ -674,6 +854,8 @@ namespace FlaxEditor.Windows { writer.WriteAttributeString("ShowGUI", ShowGUI.ToString()); writer.WriteAttributeString("ShowDebugDraw", ShowDebugDraw.ToString()); + writer.WriteAttributeString("DefaultViewportScaling", JsonSerializer.Serialize(_defaultViewportScaling)); + writer.WriteAttributeString("CustomViewportScaling", JsonSerializer.Serialize(_customViewportScaling)); } /// @@ -683,6 +865,24 @@ namespace FlaxEditor.Windows ShowGUI = value1; if (bool.TryParse(node.GetAttribute("ShowDebugDraw"), out value1)) ShowDebugDraw = value1; + if (node.HasAttribute("CustomViewportScaling")) + _customViewportScaling = JsonSerializer.Deserialize>(node.GetAttribute("CustomViewportScaling")); + + for (int i = 0; i < _customViewportScaling.Count; i++) + { + if (_customViewportScaling[i].Active) + ChangeViewportRatio(_customViewportScaling[i]); + } + + if (node.HasAttribute("DefaultViewportScaling")) + _defaultViewportScaling = JsonSerializer.Deserialize>(node.GetAttribute("DefaultViewportScaling")); + + for (int i = 0; i < _defaultViewportScaling.Count; i++) + { + if (_defaultViewportScaling[i].Active) + ChangeViewportRatio(_defaultViewportScaling[i]); + } + } /// From 63343d4224d1d526a267de7dd805d7447f105d02 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Thu, 29 Dec 2022 00:31:19 -0600 Subject: [PATCH 06/10] 2nd pass on viewport sizing CM. Added add menu and apply and delete. --- Source/Editor/Windows/GameWindow.cs | 289 +++++++++++++++++++++++----- 1 file changed, 237 insertions(+), 52 deletions(-) diff --git a/Source/Editor/Windows/GameWindow.cs b/Source/Editor/Windows/GameWindow.cs index d7b7e1fc1..426750846 100644 --- a/Source/Editor/Windows/GameWindow.cs +++ b/Source/Editor/Windows/GameWindow.cs @@ -547,59 +547,9 @@ namespace FlaxEditor.Windows }); } - var aspectMenu = menu.AddChildMenu("Viewport Scaling").ContextMenu; + var vsMenu = menu.AddChildMenu("Viewport Size").ContextMenu; - for (int i = 0; i < _defaultViewportScaling.Count; i++) - { - var button = aspectMenu.AddButton(_defaultViewportScaling[i].Label); - button.CloseMenuOnClick = false; - button.Icon = _defaultViewportScaling[i].Active ? Style.Current.CheckBoxTick : SpriteHandle.Invalid; - button.Tag = _defaultViewportScaling[i]; - if (_defaultViewportScaling[i].Active) - ChangeViewportRatio(_defaultViewportScaling[i]); - } - aspectMenu.AddSeparator(); - for (int i = 0; i < _customViewportScaling.Count; i++) - { - var button = aspectMenu.AddButton(_customViewportScaling[i].Label); - button.CloseMenuOnClick = false; - button.Icon = _customViewportScaling[i].Active ? Style.Current.CheckBoxTick : SpriteHandle.Invalid; - button.Tag = _customViewportScaling[i]; - if (_customViewportScaling[i].Active) - ChangeViewportRatio(_customViewportScaling[i]); - } - - aspectMenu.ButtonClicked += button => - { - if (button.Text == "Add...") - return; - if (button.Tag == null) - return; - - // Reset selected icon on all buttons - foreach (var child in aspectMenu.Items) - { - if (child is ContextMenuButton cmb) - { - var v = (ViewportScaling)cmb.Tag; - if (cmb == button) - { - v.Active = true; - button.Icon = Style.Current.CheckBoxTick; - ChangeViewportRatio(v); - } - else if (v.Active) - { - cmb.Icon = SpriteHandle.Invalid; - v.Active = false; - } - } - } - }; - aspectMenu.AddSeparator(); - - // TODO add - var add = aspectMenu.AddButton("Add..."); + CreateViewportSizingContextMenu(vsMenu); } // Take Screenshot @@ -630,6 +580,241 @@ namespace FlaxEditor.Windows menu.AddSeparator(); } + private void CreateViewportSizingContextMenu(ContextMenu vsMenu) + { + // add default viewport sizing options + for (int i = 0; i < _defaultViewportScaling.Count; i++) + { + var button = vsMenu.AddButton(_defaultViewportScaling[i].Label); + button.CloseMenuOnClick = false; + button.Icon = _defaultViewportScaling[i].Active ? Style.Current.CheckBoxTick : SpriteHandle.Invalid; + button.Tag = _defaultViewportScaling[i]; + if (_defaultViewportScaling[i].Active) + ChangeViewportRatio(_defaultViewportScaling[i]); + + button.Clicked += () => + { + if (button.Tag == null) + return; + + // Reset selected icon on all buttons + foreach (var child in vsMenu.Items) + { + if (child is ContextMenuButton cmb) + { + var v = (ViewportScaling)cmb.Tag; + if (cmb == button) + { + v.Active = true; + button.Icon = Style.Current.CheckBoxTick; + ChangeViewportRatio(v); + } + else if (v.Active) + { + cmb.Icon = SpriteHandle.Invalid; + v.Active = false; + } + } + } + }; + } + vsMenu.AddSeparator(); + + // Add custom viewport options + for (int i = 0; i < _customViewportScaling.Count; i++) + { + var childCM = vsMenu.AddChildMenu(_customViewportScaling[i].Label); + childCM.CloseMenuOnClick = false; + childCM.Icon = _customViewportScaling[i].Active ? Style.Current.CheckBoxTick : SpriteHandle.Invalid; + childCM.Tag = _customViewportScaling[i]; + if (_customViewportScaling[i].Active) + ChangeViewportRatio(_customViewportScaling[i]); + var applyButton = childCM.ContextMenu.AddButton("Apply"); + applyButton.Tag = childCM.Tag = _customViewportScaling[i]; + applyButton.CloseMenuOnClick = false; + applyButton.Clicked += () => + { + if (childCM.Tag == null) + return; + + // Reset selected icon on all buttons + foreach (var child in vsMenu.Items) + { + if (child is ContextMenuButton cmb) + { + var v = (ViewportScaling)child.Tag; + if (child == childCM) + { + v.Active = true; + childCM.Icon = Style.Current.CheckBoxTick; + ChangeViewportRatio(v); + } + else if (v.Active) + { + cmb.Icon = SpriteHandle.Invalid; + v.Active = false; + } + } + } + }; + + var deleteButton = childCM.ContextMenu.AddButton("Delete"); + deleteButton.CloseMenuOnClick = false; + deleteButton.Clicked += () => + { + if (childCM.Tag == null) + return; + + var v = (ViewportScaling)childCM.Tag; + if (v.Active) + { + v.Active = false; + _defaultViewportScaling[0].Active = true; + ChangeViewportRatio(_defaultViewportScaling[0]); + } + _customViewportScaling.Remove(v); + vsMenu.DisposeAllItems(); + CreateViewportSizingContextMenu(vsMenu); + vsMenu.PerformLayout(); + }; + } + + vsMenu.AddSeparator(); + + // Add button + var add = vsMenu.AddButton("Add..."); + add.CloseMenuOnClick = false; + add.Clicked += () => + { + var popup = new ContextMenuBase + { + Size = new Float2(230, 125), + ClipChildren = false, + CullChildren = false, + }; + popup.Show(add, new Float2(add.Width, 0)); + + var nameLabel = new Label + { + Parent = popup, + AnchorPreset = AnchorPresets.TopLeft, + Text = "Name", + HorizontalAlignment = TextAlignment.Near, + }; + nameLabel.LocalX += 10; + nameLabel.LocalY += 10; + + var nameTextBox = new TextBox + { + Parent = popup, + AnchorPreset = AnchorPresets.TopLeft, + IsMultiline = false, + }; + nameTextBox.LocalX += 100; + nameTextBox.LocalY += 10; + + var typeLabel = new Label + { + Parent = popup, + AnchorPreset = AnchorPresets.TopLeft, + Text = "Type", + HorizontalAlignment = TextAlignment.Near, + }; + typeLabel.LocalX += 10; + typeLabel.LocalY += 35; + + var typeDropdown = new Dropdown + { + Parent = popup, + AnchorPreset = AnchorPresets.TopLeft, + Items = { "Aspect", "Resolution" }, + SelectedItem = "Aspect", + Width = nameTextBox.Width + }; + typeDropdown.LocalY += 35; + typeDropdown.LocalX += 100; + + var whLabel = new Label + { + Parent = popup, + AnchorPreset = AnchorPresets.TopLeft, + Text = "Width & Height", + HorizontalAlignment = TextAlignment.Near, + }; + whLabel.LocalX += 10; + whLabel.LocalY += 60; + + var wValue = new IntValueBox(16) + { + Parent = popup, + AnchorPreset = AnchorPresets.TopLeft, + MinValue = 1, + Width = 55, + }; + wValue.LocalY += 60; + wValue.LocalX += 100; + + var hValue = new IntValueBox(9) + { + Parent = popup, + AnchorPreset = AnchorPresets.TopLeft, + MinValue = 1, + Width = 55, + }; + hValue.LocalY += 60; + hValue.LocalX += 165; + + var submitButton = new Button + { + Parent = popup, + AnchorPreset = AnchorPresets.TopLeft, + Text = "Submit", + Width = 70, + }; + submitButton.LocalX += 40; + submitButton.LocalY += 90; + + submitButton.Clicked += () => + { + Enum.TryParse(typeDropdown.SelectedItem, out ViewportScalingType type); + + var combineString = type == ViewportScalingType.Aspect ? ":" : "x"; + var name = nameTextBox.Text + " (" + wValue.Value + combineString + hValue.Value + ") " + typeDropdown.SelectedItem; + + var newViewportOption = new ViewportScaling + { + ScalingType = type, + Label = name, + Size = new Int2(wValue.Value, hValue.Value), + }; + + _customViewportScaling.Add(newViewportOption); + vsMenu.DisposeAllItems(); + CreateViewportSizingContextMenu(vsMenu); + vsMenu.PerformLayout(); + }; + + var cancelButton = new Button + { + Parent = popup, + AnchorPreset = AnchorPresets.TopLeft, + Text = "Cancel", + Width = 70, + }; + cancelButton.LocalX += 120; + cancelButton.LocalY += 90; + + cancelButton.Clicked += () => + { + nameTextBox.Clear(); + typeDropdown.SelectedItem = "Aspect"; + hValue.Value = 9; + wValue.Value = 16; + popup.Hide(); + }; + }; + } + /// public override void Draw() { From dc6f6e2b934b404216d3cfac2df0c82dc50cff75 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Thu, 29 Dec 2022 00:32:57 -0600 Subject: [PATCH 07/10] Clean up --- Source/Editor/Windows/GameWindow.cs | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/Source/Editor/Windows/GameWindow.cs b/Source/Editor/Windows/GameWindow.cs index 426750846..9f6a845de 100644 --- a/Source/Editor/Windows/GameWindow.cs +++ b/Source/Editor/Windows/GameWindow.cs @@ -28,6 +28,14 @@ namespace FlaxEditor.Windows private GUI.Docking.DockState _maximizeRestoreDockState; private GUI.Docking.DockPanel _maximizeRestoreDockTo; private CursorLockMode _cursorLockMode = CursorLockMode.None; + + // Viewport scaling variables + private List _defaultViewportScaling = new List(); + private List _customViewportScaling = new List(); + private float _viewportAspectRatio = 1; + private float _windowAspectRatio = 1; + bool _useAspect = false; + bool _freeAspect = true; /// /// Gets the viewport. @@ -107,14 +115,6 @@ namespace FlaxEditor.Windows /// public bool FocusOnPlay { get; set; } - // TODO move by other privates - private List _defaultViewportScaling = new List(); - private List _customViewportScaling = new List(); - private float _viewportAspectRatio = 1; - private float _windowAspectRatio = 1; - bool _useAspect = false; - bool _freeAspect = true; - private enum ViewportScalingType { Resolution = 0, @@ -294,14 +294,6 @@ namespace FlaxEditor.Windows }; RootControl.GameRoot = _guiRoot; - // TODO move external and let user define. Maybe also just let them define a ratio as well - Float2 viewPortSize = new Float2(2560, 1440); - //Float2 viewPortSize = new Float2(1280, 720); - //Float2 viewPortSize = new Float2(848, 480); - bool useCustomAspect = false; - bool freeAspect = false; - Float2 customAspect = new Float2(9, 16); - SizeChanged += control => { ResizeViewport(); @@ -324,7 +316,6 @@ namespace FlaxEditor.Windows if (v == null) return; - // TODO make it so user can not enter values of 0 or lower if (v.Size.Y <= 0 || v.Size.X <= 0) { return; From 6c3864983ff1d3c19934ce3b3badda64a10be0d4 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Thu, 29 Dec 2022 00:45:58 -0600 Subject: [PATCH 08/10] small cleanup --- Source/Editor/Windows/GameWindow.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Editor/Windows/GameWindow.cs b/Source/Editor/Windows/GameWindow.cs index 9f6a845de..cd00f7cbe 100644 --- a/Source/Editor/Windows/GameWindow.cs +++ b/Source/Editor/Windows/GameWindow.cs @@ -34,8 +34,8 @@ namespace FlaxEditor.Windows private List _customViewportScaling = new List(); private float _viewportAspectRatio = 1; private float _windowAspectRatio = 1; - bool _useAspect = false; - bool _freeAspect = true; + private bool _useAspect = false; + private bool _freeAspect = true; /// /// Gets the viewport. From 0633ed681928e89e6fd4c8058fe7193419457661 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Thu, 29 Dec 2022 00:47:30 -0600 Subject: [PATCH 09/10] Small cleanup --- Source/Editor/Windows/GameWindow.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/Editor/Windows/GameWindow.cs b/Source/Editor/Windows/GameWindow.cs index cd00f7cbe..ee7815979 100644 --- a/Source/Editor/Windows/GameWindow.cs +++ b/Source/Editor/Windows/GameWindow.cs @@ -500,7 +500,6 @@ namespace FlaxEditor.Windows // Create default scaling options if they dont exist from deserialization. if (_defaultViewportScaling.Count == 0) { - Debug.Log("Had to create scalings."); _defaultViewportScaling.Add(new ViewportScaling { Label = "Free Aspect", From 79562dc50265f940956bd07ac9e452a9dfbf0447 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Thu, 29 Dec 2022 12:14:26 -0600 Subject: [PATCH 10/10] Renamed some variables to more match what they do/are. --- Source/Editor/Windows/GameWindow.cs | 56 ++++++++++++++--------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/Source/Editor/Windows/GameWindow.cs b/Source/Editor/Windows/GameWindow.cs index ee7815979..573523b4f 100644 --- a/Source/Editor/Windows/GameWindow.cs +++ b/Source/Editor/Windows/GameWindow.cs @@ -30,8 +30,8 @@ namespace FlaxEditor.Windows private CursorLockMode _cursorLockMode = CursorLockMode.None; // Viewport scaling variables - private List _defaultViewportScaling = new List(); - private List _customViewportScaling = new List(); + private List _defaultViewportScaling = new List(); + private List _customViewportScaling = new List(); private float _viewportAspectRatio = 1; private float _windowAspectRatio = 1; private bool _useAspect = false; @@ -115,13 +115,13 @@ namespace FlaxEditor.Windows /// public bool FocusOnPlay { get; set; } - private enum ViewportScalingType + private enum ViewportScaleType { Resolution = 0, Aspect = 1, } - private class ViewportScaling + private class ViewportScaleOptions { /// /// The name. @@ -131,7 +131,7 @@ namespace FlaxEditor.Windows /// /// The Type of scaling to do. /// - public ViewportScalingType ScalingType; + public ViewportScaleType ScaleType; /// /// The width and height to scale by. @@ -311,7 +311,7 @@ namespace FlaxEditor.Windows InputActions.Add(options => options.StepFrame, Editor.Simulation.RequestPlayOneFrame); } - private void ChangeViewportRatio(ViewportScaling v) + private void ChangeViewportRatio(ViewportScaleOptions v) { if (v == null) return; @@ -328,13 +328,13 @@ namespace FlaxEditor.Windows } else { - switch (v.ScalingType) + switch (v.ScaleType) { - case ViewportScalingType.Aspect: + case ViewportScaleType.Aspect: _useAspect = true; _freeAspect = false; break; - case ViewportScalingType.Resolution: + case ViewportScaleType.Resolution: _useAspect = false; _freeAspect = false; break; @@ -500,38 +500,38 @@ namespace FlaxEditor.Windows // Create default scaling options if they dont exist from deserialization. if (_defaultViewportScaling.Count == 0) { - _defaultViewportScaling.Add(new ViewportScaling + _defaultViewportScaling.Add(new ViewportScaleOptions { Label = "Free Aspect", - ScalingType = ViewportScalingType.Aspect, + ScaleType = ViewportScaleType.Aspect, Size = new Int2(1,1), Active = true, }); - _defaultViewportScaling.Add(new ViewportScaling + _defaultViewportScaling.Add(new ViewportScaleOptions { Label = "16:9 Aspect", - ScalingType = ViewportScalingType.Aspect, + ScaleType = ViewportScaleType.Aspect, Size = new Int2(16,9), Active = false, }); - _defaultViewportScaling.Add(new ViewportScaling + _defaultViewportScaling.Add(new ViewportScaleOptions { Label = "16:10 Aspect", - ScalingType = ViewportScalingType.Aspect, + ScaleType = ViewportScaleType.Aspect, Size = new Int2(16,10), Active = false, }); - _defaultViewportScaling.Add(new ViewportScaling + _defaultViewportScaling.Add(new ViewportScaleOptions { Label = "1920x1080 Resolution", - ScalingType = ViewportScalingType.Resolution, + ScaleType = ViewportScaleType.Resolution, Size = new Int2(1920,1080), Active = false, }); - _defaultViewportScaling.Add(new ViewportScaling + _defaultViewportScaling.Add(new ViewportScaleOptions { Label = "2560x1440 Resolution", - ScalingType = ViewportScalingType.Resolution, + ScaleType = ViewportScaleType.Resolution, Size = new Int2(2560,1440), Active = false, }); @@ -592,7 +592,7 @@ namespace FlaxEditor.Windows { if (child is ContextMenuButton cmb) { - var v = (ViewportScaling)cmb.Tag; + var v = (ViewportScaleOptions)cmb.Tag; if (cmb == button) { v.Active = true; @@ -632,7 +632,7 @@ namespace FlaxEditor.Windows { if (child is ContextMenuButton cmb) { - var v = (ViewportScaling)child.Tag; + var v = (ViewportScaleOptions)child.Tag; if (child == childCM) { v.Active = true; @@ -655,7 +655,7 @@ namespace FlaxEditor.Windows if (childCM.Tag == null) return; - var v = (ViewportScaling)childCM.Tag; + var v = (ViewportScaleOptions)childCM.Tag; if (v.Active) { v.Active = false; @@ -766,14 +766,14 @@ namespace FlaxEditor.Windows submitButton.Clicked += () => { - Enum.TryParse(typeDropdown.SelectedItem, out ViewportScalingType type); + Enum.TryParse(typeDropdown.SelectedItem, out ViewportScaleType type); - var combineString = type == ViewportScalingType.Aspect ? ":" : "x"; + var combineString = type == ViewportScaleType.Aspect ? ":" : "x"; var name = nameTextBox.Text + " (" + wValue.Value + combineString + hValue.Value + ") " + typeDropdown.SelectedItem; - var newViewportOption = new ViewportScaling + var newViewportOption = new ViewportScaleOptions { - ScalingType = type, + ScaleType = type, Label = name, Size = new Int2(wValue.Value, hValue.Value), }; @@ -1041,7 +1041,7 @@ namespace FlaxEditor.Windows if (bool.TryParse(node.GetAttribute("ShowDebugDraw"), out value1)) ShowDebugDraw = value1; if (node.HasAttribute("CustomViewportScaling")) - _customViewportScaling = JsonSerializer.Deserialize>(node.GetAttribute("CustomViewportScaling")); + _customViewportScaling = JsonSerializer.Deserialize>(node.GetAttribute("CustomViewportScaling")); for (int i = 0; i < _customViewportScaling.Count; i++) { @@ -1050,7 +1050,7 @@ namespace FlaxEditor.Windows } if (node.HasAttribute("DefaultViewportScaling")) - _defaultViewportScaling = JsonSerializer.Deserialize>(node.GetAttribute("DefaultViewportScaling")); + _defaultViewportScaling = JsonSerializer.Deserialize>(node.GetAttribute("DefaultViewportScaling")); for (int i = 0; i < _defaultViewportScaling.Count; i++) {