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

Added way to add creation code in rooms and instances #1901

Open
wants to merge 1 commit 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
89 changes: 79 additions & 10 deletions UndertaleModTool/Controls/UndertaleObjectReference.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public partial class UndertaleObjectReference : UserControl
new FrameworkPropertyMetadata(true,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

public static readonly DependencyProperty GameObjectProperty =
DependencyProperty.Register("GameObject", typeof(UndertaleGameObject),
typeof(UndertaleObjectReference),
new PropertyMetadata(null));

public static DependencyProperty ObjectEventTypeProperty =
DependencyProperty.Register("ObjectEventType", typeof(EventType),
typeof(UndertaleObjectReference),
Expand All @@ -79,6 +84,15 @@ public partial class UndertaleObjectReference : UserControl
new FrameworkPropertyMetadata((uint) 0,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

public static readonly DependencyProperty RoomProperty =
DependencyProperty.Register("Room", typeof(UndertaleRoom),
typeof(UndertaleObjectReference),
new PropertyMetadata(null));

public static readonly DependencyProperty RoomGameObjectProperty =
DependencyProperty.Register("RoomGameObject", typeof(UndertaleRoom.GameObject),
typeof(UndertaleObjectReference),
new PropertyMetadata(null));

public object ObjectReference
{
Expand All @@ -98,6 +112,12 @@ public bool CanRemove
set { SetValue(ObjectTypeProperty, value); }
}

public UndertaleGameObject GameObject
{
get { return (UndertaleGameObject)GetValue(GameObjectProperty); }
set { SetValue(GameObjectProperty, value); }
}

public EventType ObjectEventType
{
get { return (EventType)GetValue(ObjectEventTypeProperty); }
Expand All @@ -110,6 +130,19 @@ public uint ObjectEventSubtype
set { SetValue(ObjectEventSubtypeProperty, value); }
}

public UndertaleRoom Room
{
get { return (UndertaleRoom)GetValue(RoomProperty); }
set { SetValue(RoomProperty, value); }
}

public UndertaleRoom.GameObject RoomGameObject
{
get { return (UndertaleRoom.GameObject)GetValue(RoomGameObjectProperty); }
set { SetValue(RoomGameObjectProperty, value); }
}

public bool IsPreCreate { get; set; } = false;

public UndertaleObjectReference()
{
Expand Down Expand Up @@ -149,22 +182,31 @@ private void Details_Click(object sender, RoutedEventArgs e)
{
if (ObjectReference is null)
{
if (mainWindow.Selected is null)
if (GameObject is not null)
{
mainWindow.ShowError("Nothing currently selected! This is currently unsupported.");
return;
ObjectReference = GameObject.EventHandlerFor(ObjectEventType, ObjectEventSubtype, mainWindow.Data);
}
else if (mainWindow.Selected is UndertaleGameObject gameObject)
else if (Room is not null)
{
// Generate the code entry
UndertaleCode code = gameObject.EventHandlerFor(ObjectEventType, ObjectEventSubtype, mainWindow.Data.Strings, mainWindow.Data.Code, mainWindow.Data.CodeLocals);

ObjectReference = code;
if (RoomGameObject is null)
{
ObjectReference = CreationCode(mainWindow.Data, "gml_Room_" + Room.Name.Content + "_Create");
}
else
{
if (!IsPreCreate)
{
ObjectReference = CreationCode(mainWindow.Data, "gml_RoomCC_" + Room.Name.Content + "_" + RoomGameObject.InstanceID.ToString() + "_Create");
}
else
{
ObjectReference = CreationCode(mainWindow.Data, "gml_RoomCC_" + Room.Name.Content + "_" + RoomGameObject.InstanceID.ToString() + "_PreCreate");
}
}
}
else
{
mainWindow.ShowError("Adding to non-objects is currently unsupported.");
return;
mainWindow.ShowError($"Adding not supported in this situation.");
}
}
else
Expand All @@ -173,6 +215,33 @@ private void Details_Click(object sender, RoutedEventArgs e)
}
}

// TODO move this to the models
UndertaleCode CreationCode(UndertaleData data, string name)
{
var nameString = data.Strings.MakeString(name);

var code = new UndertaleCode()
{
LocalsCount = 1
};
code.Name = nameString;

data.Code.Add(code);

UndertaleCodeLocals.LocalVar argsLocal = new UndertaleCodeLocals.LocalVar();
argsLocal.Name = data.Strings.MakeString("arguments");
argsLocal.Index = 0;

UndertaleCodeLocals locals = new UndertaleCodeLocals();
locals.Name = nameString;

locals.Locals.Add(argsLocal);

data.CodeLocals.Add(locals);

return code;
}

private void Details_MouseDown(object sender, MouseButtonEventArgs e)
{
if (ObjectReference is null)
Expand Down
1 change: 1 addition & 0 deletions UndertaleModTool/Editors/UndertaleGameObjectEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@
<DataTemplate>
<local:UndertaleObjectReference ObjectReference="{Binding CodeId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ObjectType="{x:Type undertale:UndertaleCode}"
Margin="23,3,3,3" CanRemove="False"
GameObject="{Binding DataContext, RelativeSource={RelativeSource AncestorType=local:DataUserControl}}"
ObjectEventType="{Binding Text, Mode=OneTime, ElementName=EventTypeName, Converter={StaticResource EventNameConverter}, ConverterParameter=EventType}"
ObjectEventSubtype="{Binding DataContext.EventSubtype, Mode=OneWay, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataTemplate>
Expand Down
13 changes: 10 additions & 3 deletions UndertaleModTool/Editors/UndertaleRoomEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@
<CheckBox Grid.Row="6" Grid.Column="2" Margin="3" IsChecked="{Binding DrawBackgroundColor}" Visibility="{Binding Flags, Mode=OneTime, Converter={StaticResource IsGMS2Converter}}"/>

<TextBlock Grid.Row="7" Grid.Column="0" Margin="3">Creation Code</TextBlock>
<local:UndertaleObjectReference Grid.Row="7" Grid.Column="2" Margin="3" ObjectReference="{Binding CreationCodeId}" ObjectType="{x:Type undertale:UndertaleCode}" ObjectEventType="{x:Static undertale:EventType.Create}" ObjectEventSubtype="0"/>
<local:UndertaleObjectReference Grid.Row="7" Grid.Column="2" Margin="3" ObjectReference="{Binding CreationCodeId}" ObjectType="{x:Type undertale:UndertaleCode}"
Room="{Binding}"/>

<TextBlock Grid.Row="8" Grid.Column="0" Margin="3">Flags</TextBlock>
<local:TextBoxDark Grid.Row="8" Grid.Column="2" Margin="3" Text="{Binding Flags, Converter={StaticResource IsGMS2Converter}, ConverterParameter=flags}"/>
Expand Down Expand Up @@ -660,7 +661,10 @@
<local:TextBoxDark Grid.Row="2" Grid.Column="2" Margin="3" Text="{Binding InstanceID}"/>

<TextBlock Grid.Row="3" Grid.Column="0" Margin="3">Creation code</TextBlock>
<local:UndertaleObjectReference Grid.Row="3" Grid.Column="2" Margin="3" ObjectReference="{Binding CreationCode}" ObjectType="{x:Type undertale:UndertaleCode}" ObjectEventType="{x:Static undertale:EventType.Create}" ObjectEventSubtype="0"/>
<local:UndertaleObjectReference Grid.Row="3" Grid.Column="2" Margin="3" ObjectReference="{Binding CreationCode}" ObjectType="{x:Type undertale:UndertaleCode}"
Room="{Binding DataContext, RelativeSource={RelativeSource AncestorType=local:DataUserControl}}"
RoomGameObject="{Binding}"
IsPreCreate="False"/>

<TextBlock Grid.Row="4" Grid.Column="0" Margin="3">Scale</TextBlock>
<Grid Grid.Row="4" Grid.Column="2">
Expand Down Expand Up @@ -696,7 +700,10 @@
</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"/>
<local:UndertaleObjectReference Grid.Row="9" Grid.Column="2" Margin="3" ObjectReference="{Binding PreCreateCode}" ObjectType="{x:Type undertale:UndertaleCode}"
Room="{Binding DataContext, RelativeSource={RelativeSource AncestorType=local:DataUserControl}}"
RoomGameObject="{Binding}"
IsPreCreate="True"/>
</Grid>
</DataTemplate>

Expand Down
Loading