Skip to content

Commit

Permalink
Merge pull request #849 from VladiStep/objectInstImprovement
Browse files Browse the repository at this point in the history
Added the "Frame index" and "Image speed" fields.
  • Loading branch information
Grossley authored Apr 20, 2022
2 parents ad59b5c + 7462510 commit 8bc66c6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 13 deletions.
6 changes: 5 additions & 1 deletion UndertaleModLib/Models/UndertaleRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,10 @@ public class GameObject : UndertaleObjectLenCheck, RoomObject, INotifyPropertyCh
/// The image index of this object. Game Maker: Studio 2 only.
/// </summary>
public int ImageIndex { get; set; }
/// <summary>
/// A wrapper of <see cref="ImageIndex"/> that prevents using an out of bounds index.
/// </summary>
public int SafeImageIndex { get => ImageIndex; set { ImageIndex = Math.Clamp(value, 0, (ObjectDefinition?.Sprite?.Textures?.Count ?? 1) - 1); OnPropertyChanged(); } }

public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
Expand Down Expand Up @@ -1362,7 +1366,7 @@ public class SpriteInstance : UndertaleObject, INotifyPropertyChanged
public float AnimationSpeed { get; set; }
public AnimationSpeedType AnimationSpeedType { get; set; }
public float FrameIndex { get; set; }
public float SafeFrameIndex { get => FrameIndex; set { FrameIndex = Math.Clamp(value, 0, Sprite.Textures.Count - 1); OnPropertyChanged(); } }
public float SafeFrameIndex { get => FrameIndex; set { FrameIndex = Math.Clamp(value, 0, (Sprite?.Textures?.Count ?? 1) - 1); OnPropertyChanged(); } }
public float Rotation { get; set; }
public float OppositeRotation => 360F - Rotation;
public int XOffset => Sprite is not null ? X - Sprite.OriginX : X;
Expand Down
15 changes: 13 additions & 2 deletions UndertaleModTool/Controls/UndertaleRoomRenderer.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<local:CachedTileDataLoader x:Key="CachedTileDataLoader"/>
<local:TileLayerTemplateSelector x:Key="TileLayerTemplateSelector"/>
<local:TileRectanglesConverter x:Key="TileRectanglesConverter"/>
<local:CachedImageLoaderWithIndex x:Key="CachedImageLoaderWithIndex"/>
<CompositeCollection x:Key="AllObjectsGMS1">
<CollectionContainer Collection="{Binding Source={x:Reference RoomRenderer}, Path=DataContext.Backgrounds}"/>
<CollectionContainer Collection="{Binding Source={x:Reference RoomRenderer}, Path=DataContext.Tiles}"/>
Expand Down Expand Up @@ -146,9 +147,14 @@
</Rectangle.RenderTransform>
<Rectangle.Fill>
<ImageBrush
ImageSource="{Binding ObjectDefinition.Sprite.Textures[0].Texture, Converter={StaticResource UndertaleCachedImageLoader}, Mode=OneTime}"
TileMode="None"
Stretch="UniformToFill">
<ImageBrush.ImageSource>
<MultiBinding Converter="{StaticResource CachedImageLoaderWithIndex}" Mode="OneTime">
<Binding Path="ObjectDefinition.Sprite.Textures" Mode="OneTime"/>
<Binding Path="ImageIndex" Mode="OneTime"/>
</MultiBinding>
</ImageBrush.ImageSource>
</ImageBrush>
</Rectangle.Fill>
</Rectangle>
Expand Down Expand Up @@ -180,9 +186,14 @@
</Rectangle.RenderTransform>
<Rectangle.Fill>
<ImageBrush
ImageSource="{Binding Sprite.Textures[0].Texture, Mode=OneTime, Converter={StaticResource UndertaleCachedImageLoader}}"
TileMode="None"
Stretch="UniformToFill">
<ImageBrush.ImageSource>
<MultiBinding Converter="{StaticResource CachedImageLoaderWithIndex}" Mode="OneTime">
<Binding Path="Sprite.Textures" Mode="OneTime"/>
<Binding Path="FrameIndex" Mode="OneTime"/>
</MultiBinding>
</ImageBrush.ImageSource>
</ImageBrush>
</Rectangle.Fill>
</Rectangle>
Expand Down
35 changes: 35 additions & 0 deletions UndertaleModTool/Converters/DataFieldOneTimeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using UndertaleModLib;

namespace UndertaleModTool
{
public class DataFieldOneTimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is not UndertaleData data || parameter is not string par)
return null;

FieldInfo info = data.GetType().GetField(par);
object resObj = info?.GetValue(data);

if (resObj is bool res)
return res ? Visibility.Visible : Visibility.Collapsed;
else
return null;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
7 changes: 6 additions & 1 deletion UndertaleModTool/Converters/UndertaleCachedImageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,12 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur
if (textures is null)
return null;

int index = (int)(float)values[1];
int index = -1;
if (values[1] is int indexInt)
index = indexInt;
else if (values[1] is float indexFloat)
index = (int)indexFloat;

if (index > textures.Count - 1 || index < 0)
return null;
else
Expand Down
42 changes: 33 additions & 9 deletions UndertaleModTool/Editors/UndertaleRoomEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<local:TileLayerTemplateSelector x:Key="TileLayerTemplateSelector"/>
<local:TileRectanglesConverter x:Key="TileRectanglesConverter"/>
<local:CachedImageLoaderWithIndex x:Key="CachedImageLoaderWithIndex"/>
<local:DataFieldOneTimeConverter x:Key="DataFieldOneTimeConverter"/>
<CompositeCollection x:Key="AllObjectsGMS1">
<CollectionContainer Collection="{Binding Source={x:Reference RoomEditor}, Path=DataContext.Backgrounds}"/>
<CollectionContainer Collection="{Binding Source={x:Reference RoomEditor}, Path=DataContext.Tiles}"/>
Expand Down Expand Up @@ -355,7 +356,7 @@
</DataTemplate>

<DataTemplate DataType="{x:Type undertale:UndertaleRoom+Background}">
<Grid>
<Grid IsSharedSizeScope="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="5"/>
Expand Down Expand Up @@ -422,7 +423,7 @@
</DataTemplate>

<DataTemplate DataType="{x:Type undertale:UndertaleRoom+View}">
<Grid>
<Grid IsSharedSizeScope="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="5"/>
Expand Down Expand Up @@ -500,9 +501,9 @@
</DataTemplate>

<DataTemplate DataType="{x:Type undertale:UndertaleRoom+GameObject}">
<Grid>
<Grid IsSharedSizeScope="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="FirstColumn"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
Expand All @@ -514,6 +515,7 @@
<RowDefinition Height="Auto" SharedSizeGroup="s22"/>
<RowDefinition Height="Auto" SharedSizeGroup="s22"/>
<RowDefinition Height="Auto" SharedSizeGroup="s22"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" SharedSizeGroup="s22"/>
</Grid.RowDefinitions>
<Grid.Resources>
Expand Down Expand Up @@ -558,13 +560,30 @@
<TextBlock Grid.Row="6" Grid.Column="0" Margin="3">Rotation</TextBlock>
<TextBox Grid.Row="6" Grid.Column="2" Margin="3" Text="{Binding Rotation}"/>

<TextBlock Grid.Row="7" Grid.Column="0" Margin="3">Pre Create code</TextBlock>
<local:UndertaleObjectReference Grid.Row="7" Grid.Column="2" Margin="3" ObjectReference="{Binding PreCreateCode}" ObjectType="{x:Type undertale:UndertaleCode}" ObjectEventType="{x:Static undertale:EventType.PreCreate}" ObjectEventSubtype="0"/>
<Grid Grid.Row="7" Grid.ColumnSpan="3" Visibility="{Binding Data, ConverterParameter=GMS2_2_2_302, Mode=OneTime, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MainWindow}}, Converter={StaticResource DataFieldOneTimeConverter}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="FirstColumn"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Margin="3">Frame index</TextBlock>
<TextBox Grid.Row="0" Grid.Column="2" Margin="3" Text="{Binding SafeImageIndex}"/>

<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>
</DataTemplate>

<DataTemplate DataType="{x:Type undertale:UndertaleRoom+Tile}">
<Grid>
<Grid IsSharedSizeScope="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="5"/>
Expand Down Expand Up @@ -873,7 +892,7 @@
</DataTemplate>

<DataTemplate DataType="{x:Type undertale:UndertaleRoom+SpriteInstance}">
<Grid>
<Grid IsSharedSizeScope="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="5"/>
Expand Down Expand Up @@ -1074,9 +1093,14 @@
</Rectangle.RenderTransform>
<Rectangle.Fill>
<ImageBrush
ImageSource="{Binding ObjectDefinition.Sprite.Textures[0].Texture, Mode=OneWay, Converter={StaticResource UndertaleCachedImageLoader}}"
TileMode="None"
Stretch="UniformToFill">
<ImageBrush.ImageSource>
<MultiBinding Converter="{StaticResource CachedImageLoaderWithIndex}">
<Binding Path="ObjectDefinition.Sprite.Textures" Mode="OneWay"/>
<Binding Path="ImageIndex" Mode="OneWay"/>
</MultiBinding>
</ImageBrush.ImageSource>
</ImageBrush>
</Rectangle.Fill>
</Rectangle>
Expand Down

0 comments on commit 8bc66c6

Please sign in to comment.