Skip to content

Commit 3112518

Browse files
authored
Merge pull request #1952 from CST1229/tile-editor
GMS2 tile editor
2 parents a1edc32 + 38fc767 commit 3112518

File tree

6 files changed

+1585
-5
lines changed

6 files changed

+1585
-5
lines changed

UndertaleModLib/Models/UndertaleRoom.cs

+8
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,14 @@ public uint TilesY
16231623
}
16241624
public uint[][] TileData { get => _tileData; set { _tileData = value; OnPropertyChanged(); } }
16251625

1626+
/// <summary>
1627+
/// Invokes PropertyChanged on the TileData property. Use when mutating the TileData e.g alongside XAML.
1628+
/// </summary>
1629+
public void TileDataUpdated()
1630+
{
1631+
OnPropertyChanged(nameof(TileData));
1632+
}
1633+
16261634
/// <inheritdoc />
16271635
public event PropertyChangedEventHandler PropertyChanged;
16281636
protected void OnPropertyChanged([CallerMemberName] string name = null)

UndertaleModTool/Converters/UndertaleCachedImageLoader.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,14 @@ public ImageSource CreateLayerSource(in Layer.LayerTilesData tilesData, in Under
516516
resBMP.RotateFlip(RotateFlipType.Rotate90FlipNone);
517517
break;
518518
case 5:
519-
resBMP.RotateFlip(RotateFlipType.Rotate270FlipY);
519+
// axes flipped since flip/mirror is done before rotation
520+
resBMP.RotateFlip(RotateFlipType.Rotate90FlipY);
520521
break;
521522
case 6:
522-
resBMP.RotateFlip(RotateFlipType.Rotate90FlipY);
523+
resBMP.RotateFlip(RotateFlipType.Rotate90FlipX);
523524
break;
524525
case 7:
525-
resBMP.RotateFlip(RotateFlipType.Rotate270FlipNone);
526+
resBMP.RotateFlip(RotateFlipType.Rotate90FlipXY);
526527
break;
527528

528529
default:

UndertaleModTool/Editors/UndertaleRoomEditor.xaml UndertaleModTool/Editors/UndertaleRoomEditor/UndertaleRoomEditor.xaml

+6-2
Original file line numberDiff line numberDiff line change
@@ -917,9 +917,11 @@
917917
<Grid.ColumnDefinitions>
918918
<ColumnDefinition Width="*"/>
919919
<ColumnDefinition Width="*"/>
920+
<ColumnDefinition Width="*"/>
920921
</Grid.ColumnDefinitions>
921922
<local:TextBoxDark Grid.Column="0" Margin="3" Text="{Binding TilesX}" ToolTip="Horizontal"/>
922923
<local:TextBoxDark Grid.Column="1" Margin="3" Text="{Binding TilesY}" ToolTip="Vertical"/>
924+
<local:ButtonDark Grid.Column="2" Margin="3" Click="AutoSizeTile_Click" ToolTip="Automatically set size to match room size">Auto</local:ButtonDark>
923925
</Grid>
924926

925927
<TextBlock Grid.Row="1" Grid.Column="0" Margin="3">Tile set</TextBlock>
@@ -930,9 +932,11 @@
930932
<Grid.ColumnDefinitions>
931933
<ColumnDefinition Width="*"/>
932934
<ColumnDefinition Width="*"/>
935+
<ColumnDefinition Width="*"/>
933936
</Grid.ColumnDefinitions>
934-
<local:ButtonDark Grid.Column="0" Margin="3" Click="TileDataExport_Click">Export</local:ButtonDark>
935-
<local:ButtonDark Grid.Column="1" Margin="3" Click="TileDataImport_Click">Import</local:ButtonDark>
937+
<local:ButtonDark Grid.Column="0" Margin="3" Click="EditTiles_Click">Edit</local:ButtonDark>
938+
<local:ButtonDark Grid.Column="1" Margin="3" Click="TileDataExport_Click">Export</local:ButtonDark>
939+
<local:ButtonDark Grid.Column="2" Margin="3" Click="TileDataImport_Click">Import</local:ButtonDark>
936940
</Grid>
937941
</Grid>
938942
</DataTemplate>

UndertaleModTool/Editors/UndertaleRoomEditor.xaml.cs UndertaleModTool/Editors/UndertaleRoomEditor/UndertaleRoomEditor.xaml.cs

+76
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ public void SaveImagePNG(Stream outfile)
107107
visualOffProp.SetValue(roomCanvas, prevOffset);
108108
}
109109

110+
111+
public RenderTargetBitmap GetTileEditorPreview(Layer tilemap)
112+
{
113+
if (roomCanvas is null)
114+
{
115+
if (MainWindow.FindVisualChild<Canvas>(RoomGraphics) is Canvas canv && canv.Name == "RoomCanvas")
116+
roomCanvas = canv;
117+
else
118+
throw new Exception("\"RoomCanvas\" not found.");
119+
}
120+
121+
bool prevVisible = tilemap.IsVisible;
122+
tilemap.IsVisible = false;
123+
object prevOffset = visualOffProp.GetValue(roomCanvas);
124+
visualOffProp.SetValue(roomCanvas, new Vector(0, 0));
125+
Brush gridOpacMask = roomCanvas.OpacityMask;
126+
roomCanvas.OpacityMask = null;
127+
128+
RenderTargetBitmap target = new((int)roomCanvas.RenderSize.Width, (int)roomCanvas.RenderSize.Height, 96, 96, PixelFormats.Pbgra32);
129+
130+
target.Render(roomCanvas);
131+
132+
tilemap.IsVisible = prevVisible;
133+
visualOffProp.SetValue(roomCanvas, prevOffset);
134+
roomCanvas.OpacityMask = gridOpacMask;
135+
return target;
136+
}
137+
110138
private void ExportAsPNG_Click(object sender, RoutedEventArgs e)
111139
{
112140
SaveFileDialog dlg = new();
@@ -1981,6 +2009,54 @@ public static void SetupRoomWithGrids(UndertaleRoom room)
19812009
room.GridThicknessPx = 1;
19822010
room.SetupRoom(!Settings.Instance.GridWidthEnabled, !Settings.Instance.GridHeightEnabled);
19832011
}
2012+
2013+
private void EditTiles_Click(object sender, RoutedEventArgs e)
2014+
{
2015+
if (ObjectEditor.Content is Layer lay)
2016+
{
2017+
Layer.LayerTilesData data = lay.TilesData;
2018+
2019+
if (data.Background is null)
2020+
{
2021+
mainWindow.ShowMessage("The layer must have a tileset set!");
2022+
return;
2023+
}
2024+
if (data.TilesX <= 0)
2025+
{
2026+
mainWindow.ShowMessage("The layer's horizontal size must be larger than 0 tiles!");
2027+
return;
2028+
}
2029+
if (data.TilesY <= 0)
2030+
{
2031+
mainWindow.ShowMessage("The layer's horizontal size must be larger than 0 tiles!");
2032+
return;
2033+
}
2034+
2035+
UndertaleTileEditor TileEditor = new UndertaleTileEditor(lay);
2036+
TileEditor.RoomPreview = GetTileEditorPreview(lay);
2037+
TileEditor.ShowDialog();
2038+
}
2039+
}
2040+
2041+
private void AutoSizeTile_Click(object sender, RoutedEventArgs e)
2042+
{
2043+
if (ObjectEditor.Content is Layer lay && this.DataContext is UndertaleRoom room)
2044+
{
2045+
Layer.LayerTilesData data = lay.TilesData;
2046+
2047+
if (data.Background is null)
2048+
{
2049+
mainWindow.ShowMessage("The layer must have a tileset set!");
2050+
return;
2051+
}
2052+
data.TilesX = (uint)Math.Ceiling(
2053+
Convert.ToDouble(room.Width) / Convert.ToDouble(data.Background.GMS2TileWidth)
2054+
);
2055+
data.TilesY = (uint)Math.Ceiling(
2056+
Convert.ToDouble(room.Height) / Convert.ToDouble(data.Background.GMS2TileHeight)
2057+
);
2058+
}
2059+
}
19842060
}
19852061

19862062
public partial class RoomCanvas : Canvas

0 commit comments

Comments
 (0)