Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New grid related features #931

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions UndertaleModLib/Models/UndertaleRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public void Unserialize(UndertaleReader reader)
}
}

public void SetupRoom(bool calculateGrid = true)
public void SetupRoom(bool calculateGridWidth = true, bool calculateGridHeight = true)
{
foreach (Layer layer in Layers)
{
Expand All @@ -362,7 +362,7 @@ public void SetupRoom(bool calculateGrid = true)
foreach (UndertaleRoom.Background bgnd in Backgrounds)
bgnd.ParentRoom = this;

if (calculateGrid)
if (calculateGridWidth || calculateGridHeight)
{
// Automagically set the grid size to whatever most tiles are sized

Expand Down Expand Up @@ -393,21 +393,21 @@ public void SetupRoom(bool calculateGrid = true)
{
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 a default starting grid is set in the settings, use that instead
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is slightly misleading, as in this code block the starting grid is not get from the settings as the grid isn't even recalculated in that case.

if (tileSizes.Count > 0)
{
var largestKey = tileSizes.Aggregate((x, y) => x.Value > y.Value ? x : y).Key;
GridWidth = largestKey.X;
GridHeight = largestKey.Y;
if (calculateGridWidth)
GridWidth = largestKey.X;

if (calculateGridHeight)
GridHeight = largestKey.Y;
}
}
}
Expand Down
45 changes: 36 additions & 9 deletions UndertaleModTool/Controls/UndertaleRoomRenderer.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<Border Background="{x:Static SystemColors.WindowBrush}" Grid.Row="0" Grid.Column="0" Panel.ZIndex="1">
<TextBlock HorizontalAlignment="Center" Text="{Binding ., Mode=OneTime, Converter={StaticResource RoomCaptionConverter}}" FontSize="14" FontStyle="Italic" Margin="5"/>
</Border>
<ItemsControl Grid.Column="0" Grid.Row="1" Panel.ZIndex="0" Name="RoomGraphics" Background="Black" ClipToBounds="True">
<ItemsControl Grid.Column="0" Grid.Row="1" Panel.ZIndex="0" Name="RoomGraphics" Background="Gray" ClipToBounds="True">
<ItemsControl.Style>
<Style TargetType="ItemsControl">
<Style.Triggers>
Expand Down Expand Up @@ -81,12 +81,42 @@
<Canvas x:Name="RoomCanvas" Width="{Binding Width, Mode=OneTime}" Height="{Binding Height, Mode=OneTime}" IsItemsHost="True" AllowDrop="True" ClipToBounds="True"
SnapsToDevicePixels="True" RenderOptions.BitmapScalingMode="NearestNeighbor"
Loaded="RoomCanvas_Loaded">
<Canvas.OpacityMask>
<DrawingBrush TileMode="Tile" ViewportUnits="Absolute">
<DrawingBrush.Viewport>
<MultiBinding Converter="{StaticResource GridConverter}">
<Binding Path="GridWidth" Mode="OneWay"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the data bindings that you've changed/added should be OneTime.

<Binding Path="GridHeight" Mode="OneWay"/>
</MultiBinding>
</DrawingBrush.Viewport>
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry>
<RectangleGeometry.Rect>
<MultiBinding Converter="{StaticResource GridConverter}">
<Binding Path="GridWidth" Mode="OneWay"/>
<Binding Path="GridHeight" Mode="OneWay"/>
</MultiBinding>
</RectangleGeometry.Rect>
</RectangleGeometry>
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<SolidColorBrush Color="Black"></SolidColorBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Pen>
<Pen Brush="Transparent" Thickness="{Binding GridThicknessPx, Mode=OneWay}"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Canvas.OpacityMask>
<Canvas.Background>
<DrawingBrush TileMode="Tile" ViewportUnits="Absolute">
<DrawingBrush.Viewport>
<MultiBinding Converter="{StaticResource GridConverter}">
<Binding Path="GridWidth" Mode="OneTime"/>
<Binding Path="GridHeight" Mode="OneTime"/>
<Binding Path="GridWidth" Mode="OneWay"/>
<Binding Path="GridHeight" Mode="OneWay"/>
</MultiBinding>
</DrawingBrush.Viewport>
<DrawingBrush.Drawing>
Expand All @@ -95,22 +125,19 @@
<RectangleGeometry>
<RectangleGeometry.Rect>
<MultiBinding Converter="{StaticResource GridConverter}">
<Binding Path="GridWidth" Mode="OneTime"/>
<Binding Path="GridHeight" Mode="OneTime"/>
<Binding Path="GridWidth" Mode="OneWay"/>
<Binding Path="GridHeight" Mode="OneWay"/>
</MultiBinding>
</RectangleGeometry.Rect>
</RectangleGeometry>
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<!-- that binding replaces itself with proper one -->
<!-- this binding replaces itself with proper one -->
<MultiBinding Mode="OneTime" Converter="{StaticResource BGColorConverter}">
<Binding Path="." RelativeSource="{RelativeSource Self}" Mode="OneTime"/>
<Binding Path="Flags" Mode="OneTime"/>
</MultiBinding>
</GeometryDrawing.Brush>
<GeometryDrawing.Pen>
<Pen Brush="Gray" Thickness="{Binding GridThicknessPx, Mode=OneTime}"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
Expand Down
2 changes: 1 addition & 1 deletion UndertaleModTool/Controls/UndertaleRoomRenderer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public UndertaleRoomRenderer()

private void RoomRenderer_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
(DataContext as UndertaleRoom)?.SetupRoom(!bgGridDisabled);
(DataContext as UndertaleRoom)?.SetupRoom(!bgGridDisabled, !bgGridDisabled);
UndertaleRoomEditor.GenerateSpriteCache(DataContext as UndertaleRoom);
}

Expand Down
39 changes: 32 additions & 7 deletions UndertaleModTool/Editors/UndertaleRoomEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@
<TextBlock Grid.Row="1" Grid.Column="0" Margin="3">Image speed</TextBlock>
<TextBox Grid.Row="1" Grid.Column="2" Margin="3" Text="{Binding ImageSpeed}"/>
</Grid>

<TextBlock Grid.Row="8" Grid.Column="0" Margin="3">Pre Create code</TextBlock>
<local:UndertaleObjectReference Grid.Row="9" Grid.Column="2" Margin="3" ObjectReference="{Binding PreCreateCode}" ObjectType="{x:Type undertale:UndertaleCode}" ObjectEventType="{x:Static undertale:EventType.PreCreate}" ObjectEventSubtype="0"/>
</Grid>
Expand Down Expand Up @@ -973,7 +973,7 @@

<!-- Don't forget to modify "UndertaleRoomRenderer" control as well -->
<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 Name="RoomGraphics" Background="Gray" MouseDown="RectangleBackground_MouseDown" MouseMove="RectangleBackground_MouseMove" MouseUp="RectangleBackground_MouseUp" MouseWheel="Canvas_MouseWheel">
<ItemsControl.Style>
<Style TargetType="ItemsControl">
<Style.Triggers>
Expand Down Expand Up @@ -1011,6 +1011,36 @@
<ItemsPanelTemplate>
<local:RoomCanvas x:Name="RoomCanvas" Width="{Binding Width, Mode=OneWay}" Height="{Binding Height, Mode=OneWay}" IsItemsHost="True" AllowDrop="True"
DragOver="Canvas_DragOver" Drop="Canvas_Drop" Loaded="RoomCanvas_Loaded">
<Canvas.OpacityMask>
<DrawingBrush TileMode="Tile" ViewportUnits="Absolute">
<DrawingBrush.Viewport>
<MultiBinding Converter="{StaticResource GridConverter}">
<Binding Path="GridWidth" Mode="OneWay"/>
<Binding Path="GridHeight" Mode="OneWay"/>
</MultiBinding>
</DrawingBrush.Viewport>
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry>
<RectangleGeometry.Rect>
<MultiBinding Converter="{StaticResource GridConverter}">
<Binding Path="GridWidth" Mode="OneWay"/>
<Binding Path="GridHeight" Mode="OneWay"/>
</MultiBinding>
</RectangleGeometry.Rect>
</RectangleGeometry>
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<SolidColorBrush Color="Black"></SolidColorBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Pen>
<Pen Brush="Transparent" Thickness="{Binding GridThicknessPx, Mode=OneWay}"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Canvas.OpacityMask>
<Canvas.Background>
<DrawingBrush TileMode="Tile" ViewportUnits="Absolute">
<DrawingBrush.Viewport>
Expand Down Expand Up @@ -1038,9 +1068,6 @@
<Binding Path="Flags" Mode="OneTime"/>
</MultiBinding>
</GeometryDrawing.Brush>
<GeometryDrawing.Pen>
<Pen Brush="Gray" Thickness="{Binding GridThicknessPx, Mode=OneWay}"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
Expand All @@ -1053,7 +1080,6 @@
<Setter Property="RenderOptions.BitmapScalingMode" Value="NearestNeighbor"/>
<Setter Property="UIElement.SnapsToDevicePixels" Value="True"/>
</Style>

<DataTemplate DataType="{x:Type undertale:UndertaleRoom+Layer}">
plrusek marked this conversation as resolved.
Show resolved Hide resolved
<ItemsControl ItemsSource="{Binding ., Mode=OneWay, Converter={StaticResource LayerItemsSourceConverter}}">
<ItemsControl.ItemsPanel>
Expand Down Expand Up @@ -1081,7 +1107,6 @@
</ItemsControl.Style>
</ItemsControl>
</DataTemplate>

<DataTemplate DataType="{x:Type undertale:UndertaleRoom+Background}">
plrusek marked this conversation as resolved.
Show resolved Hide resolved
<Rectangle Width="{Binding BackgroundDefinition.Texture.SourceWidth, Mode=OneWay}" Height="{Binding BackgroundDefinition.Texture.SourceHeight, Mode=OneWay}"
MouseDown="Rectangle_MouseDown"
Expand Down
15 changes: 12 additions & 3 deletions UndertaleModTool/Editors/UndertaleRoomEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void UndertaleRoomEditor_DataContextChanged(object sender, DependencyPro
GameObjItems.Header = room.Flags.HasFlag(RoomEntryFlags.IsGMS2)
? "Game objects (from all layers)"
: "Game objects";
room.SetupRoom();
this.SetupRoomWithGrids(room);
GenerateSpriteCache(DataContext as UndertaleRoom);

if (room.Layers.Count > 0) // if GMS 2+
Expand Down Expand Up @@ -462,7 +462,7 @@ private void Rectangle_MouseUp(object sender, MouseButtonEventArgs e)
}

// recalculates room grid size
room.SetupRoom();
this.SetupRoomWithGrids(room);
}
else if (obj is GameObject gameObj)
{
Expand Down Expand Up @@ -832,6 +832,7 @@ private void SelectObject(UndertaleObject obj)

resListView.IsExpanded = true;
resListView.BringIntoView();
resListView.Focus();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be after line 856 (resItem.IsSelected = true;).

resListView.UpdateLayout();

StackPanel resPanel = MainWindow.FindVisualChild<StackPanel>(resListView);
Expand Down Expand Up @@ -1316,7 +1317,7 @@ public void Command_Paste(object sender, ExecutedRoutedEventArgs e)
}

SelectObject(layer);
room.SetupRoom(false);
room.SetupRoom(false, false);
}

private void AddObjectInstance(UndertaleRoom room)
Expand Down Expand Up @@ -1670,6 +1671,14 @@ private void LayerCanvas_Unloaded(object sender, RoutedEventArgs e)
if (canvas.CurrentLayer is not null)
ObjElemDict.Remove(canvas.CurrentLayer);
}
private void SetupRoomWithGrids(UndertaleRoom room)
{
if (Settings.Instance.GridWidthEnabled)
room.GridWidth = Settings.Instance.GlobalGridWidth;
if (Settings.Instance.GridHeightEnabled)
room.GridHeight = Settings.Instance.GlobalGridHeight;
room.SetupRoom(!Settings.Instance.GridWidthEnabled, !Settings.Instance.GridHeightEnabled);
}
}

public partial class RoomCanvas : Canvas
Expand Down
2 changes: 1 addition & 1 deletion UndertaleModTool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,7 @@ private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEv

private void MainTree_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
OpenInTab(Highlighted);
OpenInTab(Highlighted, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not mentioned on the PR description.
I personally do not like everything opening in a new tab.

}

private void MainTree_KeyUp(object sender, KeyEventArgs e)
Expand Down
4 changes: 4 additions & 0 deletions UndertaleModTool/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public class Settings

public bool DeleteOldProfileOnSave { get; set; } = false;
public bool WarnOnClose { get; set; } = true;
public double GlobalGridWidth { get; set; } = 0;
public bool GridWidthEnabled { get; set; } = false;
public double GlobalGridHeight { get; set; } = 0;
public bool GridHeightEnabled { get; set; } = false;

public static Settings Instance;

Expand Down
Loading