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

GMS2 tile editor #1952

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions UndertaleModLib/Models/UndertaleRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,14 @@ public uint TilesY
}
public uint[][] TileData { get => _tileData; set { _tileData = value; OnPropertyChanged(); } }

/// <summary>
/// Invokes PropertyChanged on the TileData property. Use when mutating the TileData e.g alongside XAML.
/// </summary>
public void TileDataUpdated()
{
OnPropertyChanged(nameof(TileData));
}

/// <inheritdoc />
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
Expand Down
7 changes: 4 additions & 3 deletions UndertaleModTool/Converters/UndertaleCachedImageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,14 @@ public ImageSource CreateLayerSource(in Layer.LayerTilesData tilesData, in Under
resBMP.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
case 5:
resBMP.RotateFlip(RotateFlipType.Rotate270FlipY);
// axes flipped since flip/mirror is done before rotation
resBMP.RotateFlip(RotateFlipType.Rotate90FlipY);
break;
case 6:
resBMP.RotateFlip(RotateFlipType.Rotate90FlipY);
resBMP.RotateFlip(RotateFlipType.Rotate90FlipX);
break;
case 7:
resBMP.RotateFlip(RotateFlipType.Rotate270FlipNone);
resBMP.RotateFlip(RotateFlipType.Rotate90FlipXY);
break;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,11 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<local:TextBoxDark Grid.Column="0" Margin="3" Text="{Binding TilesX}" ToolTip="Horizontal"/>
<local:TextBoxDark Grid.Column="1" Margin="3" Text="{Binding TilesY}" ToolTip="Vertical"/>
<local:ButtonDark Grid.Column="2" Margin="3" Click="AutoSizeTile_Click" ToolTip="Automatically set size to match room size">Auto</local:ButtonDark>
</Grid>

<TextBlock Grid.Row="1" Grid.Column="0" Margin="3">Tile set</TextBlock>
Expand All @@ -935,9 +937,11 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<local:ButtonDark Grid.Column="0" Margin="3" Click="TileDataExport_Click">Export</local:ButtonDark>
<local:ButtonDark Grid.Column="1" Margin="3" Click="TileDataImport_Click">Import</local:ButtonDark>
<local:ButtonDark Grid.Column="0" Margin="3" Click="EditTiles_Click">Edit</local:ButtonDark>
<local:ButtonDark Grid.Column="1" Margin="3" Click="TileDataExport_Click">Export</local:ButtonDark>
<local:ButtonDark Grid.Column="2" Margin="3" Click="TileDataImport_Click">Import</local:ButtonDark>
</Grid>
</Grid>
</DataTemplate>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,34 @@ public void SaveImagePNG(Stream outfile)
visualOffProp.SetValue(roomCanvas, prevOffset);
}


public RenderTargetBitmap GetTileEditorPreview(Layer tilemap)
{
if (roomCanvas is null)
{
if (MainWindow.FindVisualChild<Canvas>(RoomGraphics) is Canvas canv && canv.Name == "RoomCanvas")
roomCanvas = canv;
else
throw new Exception("\"RoomCanvas\" not found.");
}

bool prevVisible = tilemap.IsVisible;
tilemap.IsVisible = false;
object prevOffset = visualOffProp.GetValue(roomCanvas);
visualOffProp.SetValue(roomCanvas, new Vector(0, 0));
Brush gridOpacMask = roomCanvas.OpacityMask;
roomCanvas.OpacityMask = null;

RenderTargetBitmap target = new((int)roomCanvas.RenderSize.Width, (int)roomCanvas.RenderSize.Height, 96, 96, PixelFormats.Pbgra32);

target.Render(roomCanvas);

tilemap.IsVisible = prevVisible;
visualOffProp.SetValue(roomCanvas, prevOffset);
roomCanvas.OpacityMask = gridOpacMask;
return target;
}

private void ExportAsPNG_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog dlg = new();
Expand Down Expand Up @@ -1979,6 +2007,54 @@ public static void SetupRoomWithGrids(UndertaleRoom room)
room.GridThicknessPx = 1;
room.SetupRoom(!Settings.Instance.GridWidthEnabled, !Settings.Instance.GridHeightEnabled);
}

private void EditTiles_Click(object sender, RoutedEventArgs e)
{
if (ObjectEditor.Content is Layer lay)
{
Layer.LayerTilesData data = lay.TilesData;

if (data.Background is null)
{
mainWindow.ShowMessage("The layer must have a tileset set!");
return;
}
if (data.TilesX <= 0)
{
mainWindow.ShowMessage("The layer's horizontal size must be larger than 0 tiles!");
return;
}
if (data.TilesY <= 0)
{
mainWindow.ShowMessage("The layer's horizontal size must be larger than 0 tiles!");
return;
}

UndertaleTileEditor TileEditor = new UndertaleTileEditor(lay);
TileEditor.RoomPreview = GetTileEditorPreview(lay);
TileEditor.ShowDialog();
}
}

private void AutoSizeTile_Click(object sender, RoutedEventArgs e)
{
if (ObjectEditor.Content is Layer lay && this.DataContext is UndertaleRoom room)
{
Layer.LayerTilesData data = lay.TilesData;

if (data.Background is null)
{
mainWindow.ShowMessage("The layer must have a tileset set!");
return;
}
data.TilesX = (uint)Math.Ceiling(
Convert.ToDouble(room.Width) / Convert.ToDouble(data.Background.GMS2TileWidth)
);
data.TilesY = (uint)Math.Ceiling(
Convert.ToDouble(room.Height) / Convert.ToDouble(data.Background.GMS2TileHeight)
);
}
}
}

public partial class RoomCanvas : Canvas
Expand Down
Loading
Loading