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 2 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
91 changes: 79 additions & 12 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 All @@ -124,7 +157,7 @@ private void UndertaleObjectReference_Loaded(object sender, RoutedEventArgs e)
var label = TryFindResource("emptyReferenceLabel") as Label;
if (label is null)
return;

string typeName = ObjectType.ToString();
string n = "";
if (typeName.StartsWith("UndertaleModLib.Models.Undertale"))
Expand All @@ -136,7 +169,7 @@ private void UndertaleObjectReference_Loaded(object sender, RoutedEventArgs e)
// If the first letter is a vowel
if (Array.IndexOf(vowels, typeName[0]) != -1)
n = "n";

label.Content = $"(drag & drop a{n} {typeName})";
}

Expand All @@ -149,22 +182,25 @@ 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 = CreateCode(mainWindow.Data, "gml_Room_" + Room.Name.Content + "_Create");
}
else
{
string suffix = !IsPreCreate ? "_Create" : "_PreCreate";
ObjectReference = CreateCode(mainWindow.Data, "gml_RoomCC_" + Room.Name.Content + "_" + RoomGameObject.InstanceID.ToString() + suffix);
}
}
else
{
mainWindow.ShowError("Adding to non-objects is currently unsupported.");
return;
mainWindow.ShowError($"Adding not supported in this situation.");
}
}
else
Expand All @@ -173,6 +209,37 @@ private void Details_Click(object sender, RoutedEventArgs e)
}
}

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

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

data.Code.Add(code);

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

UndertaleCodeLocals locals = new()
{
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