From 45bf4f25259a72a9964b8c599ddad707bf292089 Mon Sep 17 00:00:00 2001 From: NyakoFox Date: Sat, 19 Feb 2022 15:51:20 -0400 Subject: [PATCH 1/5] Change tile selection to be much easier --- .../Editors/UndertaleRoomEditor.xaml | 4 +- .../Editors/UndertaleRoomEditor.xaml.cs | 132 ++++++++++-------- 2 files changed, 72 insertions(+), 64 deletions(-) diff --git a/UndertaleModTool/Editors/UndertaleRoomEditor.xaml b/UndertaleModTool/Editors/UndertaleRoomEditor.xaml index 6933273c3..ef0d4481b 100644 --- a/UndertaleModTool/Editors/UndertaleRoomEditor.xaml +++ b/UndertaleModTool/Editors/UndertaleRoomEditor.xaml @@ -610,7 +610,7 @@ - + @@ -624,7 +624,7 @@ - + diff --git a/UndertaleModTool/Editors/UndertaleRoomEditor.xaml.cs b/UndertaleModTool/Editors/UndertaleRoomEditor.xaml.cs index 9268f4fe3..9adb8a4b3 100644 --- a/UndertaleModTool/Editors/UndertaleRoomEditor.xaml.cs +++ b/UndertaleModTool/Editors/UndertaleRoomEditor.xaml.cs @@ -114,8 +114,7 @@ private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEv } private UndertaleObject movingObj; - private UndertaleRoom.Tile movingTile; - private double hotpointX, hotpointY, hotpointTileX, hotpointTileY; + private double hotpointX, hotpointY; private ScaleTransform canvasSt = new ScaleTransform(); private void Rectangle_MouseDown(object sender, MouseButtonEventArgs e) @@ -229,66 +228,6 @@ private void Rectangle_MouseDown(object sender, MouseButtonEventArgs e) } } - private void RectangleTile_MouseDown(object sender, MouseButtonEventArgs e) - { - var element = sender as Rectangle; - UndertaleRoom.Tile clickedObj = (sender as Rectangle).DataContext as UndertaleRoom.Tile; - var mousePos = e.GetPosition(element.Parent as UIElement); - movingTile = clickedObj; - hotpointTileX = mousePos.X - movingTile.SourceX; - hotpointTileY = mousePos.Y - movingTile.SourceY; - } - - private void RectangleTile_MouseMove(object sender, MouseEventArgs e) - { - if (movingTile != null) - { - var element = sender as UIElement; - UndertaleRoom room = this.DataContext as UndertaleRoom; - - var maxX = movingTile.Tpag.BoundingWidth; - var maxY = movingTile.Tpag.BoundingHeight; - - var mousePos = e.GetPosition(element); - - int tgtX = (int)Math.Clamp(mousePos.X - hotpointTileX, 0, maxX - movingTile.Width); - int tgtY = (int)Math.Clamp(mousePos.Y - hotpointTileY, 0, maxY - movingTile.Height); - - int scaleX = (int)Math.Clamp(mousePos.X - movingTile.SourceX, 0, maxX); - int scaleY = (int)Math.Clamp(mousePos.Y - movingTile.SourceY, 0, maxY); - - int gridSize = Convert.ToInt32(room.Grid); - if (gridSize <= 0) - gridSize = 1; - else if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control)) - gridSize = gridSize / 2; - else if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift)) - gridSize = gridSize * 2; - - // Snap to grid - tgtX = (tgtX + gridSize / 2) / gridSize * gridSize; - tgtY = (tgtY + gridSize / 2) / gridSize * gridSize; - - scaleX = (scaleX + gridSize / 2) / gridSize * gridSize; - scaleY = (scaleY + gridSize / 2) / gridSize * gridSize; - if (Keyboard.Modifiers.HasFlag(ModifierKeys.Alt)) - { - movingTile.Width = (uint)scaleX; - movingTile.Height = (uint)scaleY; - } - else - { - movingTile.SourceX = (uint)tgtX; - movingTile.SourceY = (uint)tgtY; - } - } - } - - private void RectangleTile_MouseUp(object sender, MouseButtonEventArgs e) - { - movingTile = null; - } - private void Rectangle_MouseMove(object sender, MouseEventArgs e) { if (movingObj != null) @@ -325,6 +264,75 @@ private void Rectangle_MouseMove(object sender, MouseEventArgs e) } } + private Point GetGridMouseCoordinates(Point mousePos, UndertaleRoom room) + { + int gridSize = Convert.ToInt32(room.Grid); + if (gridSize <= 0) + gridSize = 1; + else if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control)) + gridSize = gridSize / 2; + else if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift)) + gridSize = gridSize * 2; + + return new Point(Math.Floor(mousePos.X / gridSize) * gridSize, Math.Floor(mousePos.Y / gridSize) * gridSize); + } + + double scaleOriginX, scaleOriginY; + private void RectangleTile_MouseDown(object sender, MouseButtonEventArgs e) + { + var element = sender as Canvas; + var tileSelector = element.FindName("TileSelector") as Rectangle; + var mousePos = e.GetPosition(element); + var clickedTile = tileSelector.DataContext as UndertaleRoom.Tile; + UndertaleRoom room = this.DataContext as UndertaleRoom; + + Point gridMouseCoordinates = GetGridMouseCoordinates(mousePos, room); + scaleOriginX = gridMouseCoordinates.X; + scaleOriginY = gridMouseCoordinates.Y; + clickedTile.SourceX = (uint)gridMouseCoordinates.X; + clickedTile.SourceY = (uint)gridMouseCoordinates.Y; + clickedTile.Width = (uint)room.Grid; + clickedTile.Height = (uint)room.Grid; + } + + private void RectangleTile_MouseMove(object sender, MouseEventArgs e) + { + if (e.LeftButton != MouseButtonState.Pressed) return; + + var element = sender as Canvas; + var tileSelector = element.FindName("TileSelector") as Rectangle; + var mousePos = e.GetPosition(element); + + var clickedTile = tileSelector.DataContext as UndertaleRoom.Tile; + + UndertaleRoom room = this.DataContext as UndertaleRoom; + + Point gridMouseCoordinates = GetGridMouseCoordinates(mousePos, room); + + if (Keyboard.Modifiers.HasFlag(ModifierKeys.Alt)) + { + double differenceX = gridMouseCoordinates.X - scaleOriginX; + double differenceY = gridMouseCoordinates.Y - scaleOriginY; + clickedTile.Width = (uint)Math.Clamp(Math.Abs(differenceX), 0, clickedTile.Tpag.BoundingWidth ) + (uint)room.Grid; + clickedTile.Height = (uint)Math.Clamp(Math.Abs(differenceY), 0, clickedTile.Tpag.BoundingHeight) + (uint)room.Grid; + + if (differenceX < 0) + clickedTile.SourceX = (uint)gridMouseCoordinates.X; + else + clickedTile.SourceX = (uint)scaleOriginX; + + if (differenceY < 0) + clickedTile.SourceY = (uint)gridMouseCoordinates.Y; + else + clickedTile.SourceY = (uint)scaleOriginY; + } + else + { + clickedTile.SourceX = (uint)gridMouseCoordinates.X; + clickedTile.SourceY = (uint)gridMouseCoordinates.Y; + } + } + private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e) { e.Handled = true; From a41845cca20a2b2ba7874adaf69fb4110abac46d Mon Sep 17 00:00:00 2001 From: NyakoFox Date: Sat, 19 Feb 2022 17:14:14 -0400 Subject: [PATCH 2/5] Make grid adjust automatically according to tiles --- UndertaleModLib/Models/UndertaleRoom.cs | 28 +++++++++- .../Editors/UndertaleRoomEditor.xaml | 11 ++-- .../Editors/UndertaleRoomEditor.xaml.cs | 52 ++++++++++++------- 3 files changed, 66 insertions(+), 25 deletions(-) diff --git a/UndertaleModLib/Models/UndertaleRoom.cs b/UndertaleModLib/Models/UndertaleRoom.cs index 1be0e33c4..b82b18f1e 100644 --- a/UndertaleModLib/Models/UndertaleRoom.cs +++ b/UndertaleModLib/Models/UndertaleRoom.cs @@ -3,6 +3,7 @@ using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; +using System.Drawing; using System.IO; using System.Linq; using System.Text; @@ -42,7 +43,8 @@ public enum RoomEntryFlags : uint public float GravityX { get; set; } = 0; public float GravityY { get; set; } = 10; public float MetersPerPixel { get; set; } = 0.1f; - public double Grid { get; set; } = 16d; + public double GridWidth { get; set; } = 16d; + public double GridHeight { get; set; } = 16d; public double GridThicknessPx { get; set; } = 1d; public UndertalePointerList Backgrounds { get; private set; } = new UndertalePointerList(); public UndertalePointerList Views { get; private set; } = new UndertalePointerList(); @@ -237,6 +239,30 @@ public void SetupRoom() } foreach (UndertaleRoom.Background bgnd in Backgrounds) bgnd.ParentRoom = this; + + // Automagically set the grid size to whatever most tiles are sized + + Dictionary tileSizes = new Dictionary(); + + // Loop through each tile and save how many times their sizes are used + foreach (UndertaleRoom.Tile tile in Tiles) + { + Point scale = new Point((int)tile.Width, (int)tile.Height); + if (tileSizes.ContainsKey(scale)) + { + tileSizes[scale]++; + } else { + tileSizes.Add(scale, 1); + } + } + + // If tiles exist at all, grab the most used tile size and use that as our grid size + if (tileSizes.Count > 0) + { + var largestKey = tileSizes.Aggregate((x, y) => x.Value > y.Value ? x : y).Key; + GridWidth = largestKey.X; + GridHeight = largestKey.Y; + } } public override string ToString() diff --git a/UndertaleModTool/Editors/UndertaleRoomEditor.xaml b/UndertaleModTool/Editors/UndertaleRoomEditor.xaml index ef0d4481b..07f95a297 100644 --- a/UndertaleModTool/Editors/UndertaleRoomEditor.xaml +++ b/UndertaleModTool/Editors/UndertaleRoomEditor.xaml @@ -351,11 +351,14 @@ - Grid - + Grid Width + - Grid Thickness - + Grid Height + + + Grid Thickness + diff --git a/UndertaleModTool/Editors/UndertaleRoomEditor.xaml.cs b/UndertaleModTool/Editors/UndertaleRoomEditor.xaml.cs index 9adb8a4b3..c483a3129 100644 --- a/UndertaleModTool/Editors/UndertaleRoomEditor.xaml.cs +++ b/UndertaleModTool/Editors/UndertaleRoomEditor.xaml.cs @@ -239,17 +239,23 @@ private void Rectangle_MouseMove(object sender, MouseEventArgs e) int tgtX = (int)(mousePos.X - hotpointX); int tgtY = (int)(mousePos.Y - hotpointY); - int gridSize = Convert.ToInt32(room.Grid); - if (gridSize <= 0) - gridSize = 1; - else if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control)) - gridSize = gridSize/2; + int gridWidth = Math.Max(Convert.ToInt32(room.GridWidth ), 1); + int gridHeight = Math.Max(Convert.ToInt32(room.GridHeight), 1); + + if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control)) + { + gridWidth /= 2; + gridHeight /= 2; + } else if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift)) - gridSize = gridSize*2; + { + gridWidth *= 2; + gridHeight *= 2; + } // Snap to grid - tgtX = ((tgtX + gridSize / 2) / gridSize) * gridSize; - tgtY = ((tgtY + gridSize / 2) / gridSize) * gridSize; + tgtX = ((tgtX + gridWidth / 2) / gridWidth ) * gridWidth; + tgtY = ((tgtY + gridHeight / 2) / gridHeight) * gridHeight; if (movingObj is UndertaleRoom.GameObject) { @@ -266,15 +272,21 @@ private void Rectangle_MouseMove(object sender, MouseEventArgs e) private Point GetGridMouseCoordinates(Point mousePos, UndertaleRoom room) { - int gridSize = Convert.ToInt32(room.Grid); - if (gridSize <= 0) - gridSize = 1; - else if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control)) - gridSize = gridSize / 2; - else if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift)) - gridSize = gridSize * 2; + int gridWidth = Math.Max(Convert.ToInt32(room.GridWidth ), 1); + int gridHeight = Math.Max(Convert.ToInt32(room.GridHeight), 1); + + if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control)) + { + gridWidth /= 2; + gridHeight /= 2; + } + else if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift)) + { + gridWidth *= 2; + gridHeight *= 2; + } - return new Point(Math.Floor(mousePos.X / gridSize) * gridSize, Math.Floor(mousePos.Y / gridSize) * gridSize); + return new Point(Math.Floor(mousePos.X / gridWidth) * gridWidth, Math.Floor(mousePos.Y / gridHeight) * gridHeight); } double scaleOriginX, scaleOriginY; @@ -291,8 +303,8 @@ private void RectangleTile_MouseDown(object sender, MouseButtonEventArgs e) scaleOriginY = gridMouseCoordinates.Y; clickedTile.SourceX = (uint)gridMouseCoordinates.X; clickedTile.SourceY = (uint)gridMouseCoordinates.Y; - clickedTile.Width = (uint)room.Grid; - clickedTile.Height = (uint)room.Grid; + clickedTile.Width = (uint)room.GridWidth; + clickedTile.Height = (uint)room.GridHeight; } private void RectangleTile_MouseMove(object sender, MouseEventArgs e) @@ -313,8 +325,8 @@ private void RectangleTile_MouseMove(object sender, MouseEventArgs e) { double differenceX = gridMouseCoordinates.X - scaleOriginX; double differenceY = gridMouseCoordinates.Y - scaleOriginY; - clickedTile.Width = (uint)Math.Clamp(Math.Abs(differenceX), 0, clickedTile.Tpag.BoundingWidth ) + (uint)room.Grid; - clickedTile.Height = (uint)Math.Clamp(Math.Abs(differenceY), 0, clickedTile.Tpag.BoundingHeight) + (uint)room.Grid; + clickedTile.Width = (uint)Math.Clamp(Math.Abs(differenceX), 0, clickedTile.Tpag.BoundingWidth ) + (uint)room.GridWidth; + clickedTile.Height = (uint)Math.Clamp(Math.Abs(differenceY), 0, clickedTile.Tpag.BoundingHeight) + (uint)room.GridHeight; if (differenceX < 0) clickedTile.SourceX = (uint)gridMouseCoordinates.X; From 97bcc8069c66fd309ffe001b00e13acc462a8a54 Mon Sep 17 00:00:00 2001 From: NyakoFox Date: Sat, 19 Feb 2022 20:56:31 -0400 Subject: [PATCH 3/5] Add tile painting, fix grid rendering, fix adding objects to rooms being offset --- UndertaleModLib/Models/UndertaleRoom.cs | 2 +- UndertaleModTool/Converters/GridConverter.cs | 8 +- .../Editors/UndertaleRoomEditor.xaml | 23 +++- .../Editors/UndertaleRoomEditor.xaml.cs | 129 +++++++++++++++--- 4 files changed, 132 insertions(+), 30 deletions(-) diff --git a/UndertaleModLib/Models/UndertaleRoom.cs b/UndertaleModLib/Models/UndertaleRoom.cs index b82b18f1e..649f5ce47 100644 --- a/UndertaleModLib/Models/UndertaleRoom.cs +++ b/UndertaleModLib/Models/UndertaleRoom.cs @@ -247,7 +247,7 @@ public void SetupRoom() // Loop through each tile and save how many times their sizes are used foreach (UndertaleRoom.Tile tile in Tiles) { - Point scale = new Point((int)tile.Width, (int)tile.Height); + Point scale = new((int)tile.Width, (int)tile.Height); if (tileSizes.ContainsKey(scale)) { tileSizes[scale]++; diff --git a/UndertaleModTool/Converters/GridConverter.cs b/UndertaleModTool/Converters/GridConverter.cs index 80784b86b..4f2e28a20 100644 --- a/UndertaleModTool/Converters/GridConverter.cs +++ b/UndertaleModTool/Converters/GridConverter.cs @@ -9,14 +9,14 @@ namespace UndertaleModTool { - public class GridConverter : IValueConverter + public class GridConverter : IMultiValueConverter { - public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) + public object Convert(object[] values, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) { - return "0,0,"+value.ToString()+","+value.ToString(); + return new Rect(0, 0, (double)values[0], (double)values[1]); } - public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) + public object[] ConvertBack(object value, System.Type[] targetType, object parameter, System.Globalization.CultureInfo culture) { return null; } diff --git a/UndertaleModTool/Editors/UndertaleRoomEditor.xaml b/UndertaleModTool/Editors/UndertaleRoomEditor.xaml index 07f95a297..36217cfde 100644 --- a/UndertaleModTool/Editors/UndertaleRoomEditor.xaml +++ b/UndertaleModTool/Editors/UndertaleRoomEditor.xaml @@ -906,8 +906,8 @@ - - + +