From d9aa123bce0b186f3861318cb89482faece4ff0a Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 20 Sep 2024 18:33:08 +0200 Subject: [PATCH] Add custom grid snapping value for translation #1823 --- Source/Editor/Viewport/EditorGizmoViewport.cs | 29 ++++++++++- .../Viewport/MainEditorGizmoViewport.cs | 2 +- Source/Engine/Render2D/SpriteAtlas.cs | 51 ++++++++++++++++++- 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/Source/Editor/Viewport/EditorGizmoViewport.cs b/Source/Editor/Viewport/EditorGizmoViewport.cs index 0a0591015..d230f53fb 100644 --- a/Source/Editor/Viewport/EditorGizmoViewport.cs +++ b/Source/Editor/Viewport/EditorGizmoViewport.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using FlaxEditor.Gizmo; using FlaxEditor.GUI.ContextMenu; +using FlaxEditor.GUI.Input; using FlaxEditor.SceneGraph; using FlaxEditor.Viewport.Cameras; using FlaxEditor.Viewport.Widgets; @@ -298,9 +299,33 @@ namespace FlaxEditor.Viewport } var buttonBB = translateSnappingCM.AddButton("Bounding Box").LinkTooltip("Snaps the selection based on it's bounding volume"); buttonBB.Tag = -1.0f; - translateSnappingCM.ButtonClicked += button => + var buttonCustom = translateSnappingCM.AddButton("Custom"); + buttonCustom.LinkTooltip("Custom grid size"); + const float defaultCustomTranslateSnappingValue = 250.0f; + float customTranslateSnappingValue = transformGizmo.TranslationSnapValue; + if (customTranslateSnappingValue < 0.0f) + customTranslateSnappingValue = defaultCustomTranslateSnappingValue; + foreach (var v in TranslateSnapValues) { - var v = (float)button.Tag; + if (Mathf.Abs(transformGizmo.TranslationSnapValue - v) < 0.001f) + { + customTranslateSnappingValue = defaultCustomTranslateSnappingValue; + break; + } + } + buttonCustom.Tag = customTranslateSnappingValue; + var customValue = new FloatValueBox(customTranslateSnappingValue, Style.Current.FontMedium.MeasureText(buttonCustom.Text).X + 5, 2, 70.0f, 0.001f, float.MaxValue, 0.1f) + { + Parent = buttonCustom + }; + customValue.ValueChanged += () => + { + buttonCustom.Tag = customValue.Value; + buttonCustom.Click(); + }; + translateSnappingCM.ButtonClicked += b => + { + var v = (float)b.Tag; transformGizmo.TranslationSnapValue = v; if (v < 0.0f) translateSnapping.Text = "Bounding Box"; diff --git a/Source/Editor/Viewport/MainEditorGizmoViewport.cs b/Source/Editor/Viewport/MainEditorGizmoViewport.cs index cf39357e3..cbb35e155 100644 --- a/Source/Editor/Viewport/MainEditorGizmoViewport.cs +++ b/Source/Editor/Viewport/MainEditorGizmoViewport.cs @@ -221,7 +221,7 @@ namespace FlaxEditor.Viewport editor.SceneEditing.SelectionChanged += OnSelectionChanged; // Gizmo widgets - AddGizmoViewportWidgets(this, TransformGizmo); + AddGizmoViewportWidgets(this, TransformGizmo, true); // Show grid widget _showGridButton = ViewWidgetShowMenu.AddButton("Grid", () => Grid.Enabled = !Grid.Enabled); diff --git a/Source/Engine/Render2D/SpriteAtlas.cs b/Source/Engine/Render2D/SpriteAtlas.cs index 68d65c85a..2adc06570 100644 --- a/Source/Engine/Render2D/SpriteAtlas.cs +++ b/Source/Engine/Render2D/SpriteAtlas.cs @@ -1,10 +1,11 @@ // Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. using System; +using System.Runtime.CompilerServices; namespace FlaxEngine { - partial struct SpriteHandle + partial struct SpriteHandle : IEquatable { /// /// Invalid sprite handle. @@ -107,5 +108,53 @@ namespace FlaxEngine Atlas.SetSprite(Index, ref sprite); } } + + /// + /// Tests for equality between two objects. + /// + /// The first value to compare. + /// The second value to compare. + /// true if has the same value as ; otherwise, false. + public static bool operator ==(SpriteHandle left, SpriteHandle right) + { + return left.Index == right.Index && left.Atlas == right.Atlas; + } + + /// + /// Tests for inequality between two objects. + /// + /// The first value to compare. + /// The second value to compare. + /// true if has a different value than ; otherwise, false. + public static bool operator !=(SpriteHandle left, SpriteHandle right) + { + return left.Index != right.Index || left.Atlas != right.Atlas; + } + + /// + public bool Equals(SpriteHandle other) + { + return Index == other.Index && Atlas == other.Atlas; + } + + /// + public override bool Equals(object obj) + { + return obj is SpriteHandle other && Equals(other); + } + + /// + public override int GetHashCode() + { + return HashCode.Combine(Atlas, Index); + } + + /// + public override string ToString() + { + if (Atlas) + return $"{System.IO.Path.GetFileNameWithoutExtension(Atlas.Path)}[{Index}]"; + return "Invalid"; + } } }