Skip to content

Commit

Permalink
Merge pull request #755 from NyakoFox/room-editor-changes
Browse files Browse the repository at this point in the history
Room editor improvements
  • Loading branch information
Grossley authored Feb 20, 2022
2 parents 5386b29 + 5e2c92a commit 2847119
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 71 deletions.
28 changes: 27 additions & 1 deletion UndertaleModLib/Models/UndertaleRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Background> Backgrounds { get; private set; } = new UndertalePointerList<Background>();
public UndertalePointerList<View> Views { get; private set; } = new UndertalePointerList<View>();
Expand Down Expand Up @@ -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<Point, uint> tileSizes = new Dictionary<Point, uint>();

// Loop through each tile and save how many times their sizes are used
foreach (UndertaleRoom.Tile tile in Tiles)
{
Point scale = new((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()
Expand Down
8 changes: 4 additions & 4 deletions UndertaleModTool/Converters/GridConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
38 changes: 27 additions & 11 deletions UndertaleModTool/Editors/UndertaleRoomEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,14 @@
<TextBox Grid.Column="1" Margin="3" Text="{Binding GravityY}" ToolTip="Y"/>
</Grid>

<TextBlock Grid.Row="12" Grid.Column="0" Margin="3">Grid</TextBlock>
<TextBox Grid.Row="12" Grid.Column="1" Margin="3" Text="{Binding Grid}"/>
<TextBlock Grid.Row="12" Grid.Column="0" Margin="3">Grid Width</TextBlock>
<TextBox Grid.Row="12" Grid.Column="1" Margin="3" Text="{Binding GridWidth}"/>

<TextBlock Grid.Row="13" Grid.Column="0" Margin="3">Grid Thickness</TextBlock>
<TextBox Grid.Row="13" Grid.Column="1" Margin="3" Text="{Binding GridThicknessPx}"/>
<TextBlock Grid.Row="13" Grid.Column="0" Margin="3">Grid Height</TextBlock>
<TextBox Grid.Row="13" Grid.Column="1" Margin="3" Text="{Binding GridHeight}"/>

<TextBlock Grid.Row="14" Grid.Column="0" Margin="3">Grid Thickness</TextBlock>
<TextBox Grid.Row="14" Grid.Column="1" Margin="3" Text="{Binding GridThicknessPx}"/>
</Grid>
</DataTemplate>

Expand Down Expand Up @@ -610,7 +613,7 @@
</Grid>

<Viewbox Grid.Row="3" Grid.Column="1" Margin="3" Stretch="Uniform" StretchDirection="DownOnly">
<Canvas Width="{Binding Tpag.BoundingWidth}" Height="{Binding Tpag.BoundingHeight}" SnapsToDevicePixels="True" MouseMove="RectangleTile_MouseMove" MouseUp="RectangleTile_MouseUp">
<Canvas Width="{Binding Tpag.BoundingWidth}" Height="{Binding Tpag.BoundingHeight}" SnapsToDevicePixels="True" MouseDown="RectangleTile_MouseDown" MouseMove="RectangleTile_MouseMove">
<Border>
<Border.Background>
<DrawingBrush Stretch="None" TileMode="Tile" Viewport="0,0,20,20" ViewportUnits="Absolute">
Expand All @@ -624,7 +627,7 @@
</Border.Background>
<local:UndertaleTexturePageItemDisplay DataContext="{Binding Tpag}"/>
</Border>
<Rectangle Canvas.Left="{Binding SourceX}" Canvas.Top="{Binding SourceY}" Width="{Binding Width}" Height="{Binding Height}" Stroke="#FFB23131" Panel.ZIndex="50" StrokeThickness="2" MouseDown="RectangleTile_MouseDown" SnapsToDevicePixels="True">
<Rectangle x:Name="TileSelector" Canvas.Left="{Binding SourceX}" Canvas.Top="{Binding SourceY}" Width="{Binding Width}" Height="{Binding Height}" Stroke="#FFB23131" Panel.ZIndex="50" StrokeThickness="2" SnapsToDevicePixels="True">
<Rectangle.Fill>
<SolidColorBrush Color="Red" Opacity=".1"/>
</Rectangle.Fill>
Expand Down Expand Up @@ -903,8 +906,8 @@

<GridSplitter Grid.Column="0" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Center" ShowsPreview="True" Width="Auto" Height="3"/>

<ScrollViewer Margin="0,8,0,0" Grid.Column="0" Grid.Row="2" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" ScrollChanged="ScrollViewer_ScrollChanged">
<ItemsControl Name="RoomGraphics" Background="Black" MouseMove="Rectangle_MouseMove" MouseWheel="Canvas_MouseWheel">
<ScrollViewer Name="RoomGraphicsScroll" Margin="0,8,0,0" Grid.Column="0" Grid.Row="2" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" ScrollChanged="ScrollViewer_ScrollChanged">
<ItemsControl Name="RoomGraphics" Background="Black" MouseDown="RectangleBackground_MouseDown" MouseMove="RectangleBackground_MouseMove" MouseUp="RectangleBackground_MouseUp" MouseWheel="Canvas_MouseWheel">
<ItemsControl.Style>
<Style TargetType="ItemsControl">
<Setter Property="ItemsSource" Value="{Binding Source={StaticResource AllObjectsGMS1}}"/>
Expand All @@ -919,16 +922,29 @@
</ItemsControl.Style>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Width="{Binding Width}" Height="{Binding Height}" IsItemsHost="True" AllowDrop="True" DragOver="Canvas_DragOver" Drop="Canvas_Drop">
<Canvas Name="RoomCanvas" Width="{Binding Width}" Height="{Binding Height}" IsItemsHost="True" AllowDrop="True" DragOver="Canvas_DragOver" Drop="Canvas_Drop" Loaded="Canvas_Loaded">
<!--<Canvas.RenderTransform>
<ScaleTransform ScaleX="2" ScaleY="2"></ScaleTransform>
</Canvas.RenderTransform>-->
<Canvas.Background>
<DrawingBrush TileMode="Tile" Viewport="{Binding Grid, Converter={StaticResource GridConverter}}" ViewportUnits="Absolute">
<DrawingBrush TileMode="Tile" ViewportUnits="Absolute">
<DrawingBrush.Viewport>
<MultiBinding Converter="{StaticResource GridConverter}">
<Binding Path="GridWidth"></Binding>
<Binding Path="GridHeight"></Binding>
</MultiBinding>
</DrawingBrush.Viewport>
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="{Binding Grid, Converter={StaticResource GridConverter}}"/>
<RectangleGeometry>
<RectangleGeometry.Rect>
<MultiBinding Converter="{StaticResource GridConverter}">
<Binding Path="GridWidth"></Binding>
<Binding Path="GridHeight"></Binding>
</MultiBinding>
</RectangleGeometry.Rect>
</RectangleGeometry>
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Brush="Gray" Thickness="{Binding GridThicknessPx}"/>
Expand Down
Loading

0 comments on commit 2847119

Please sign in to comment.