diff --git a/UndertaleModTool/Controls/UndertaleObjectReference.xaml.cs b/UndertaleModTool/Controls/UndertaleObjectReference.xaml.cs index 24e8812be..8ba272591 100644 --- a/UndertaleModTool/Controls/UndertaleObjectReference.xaml.cs +++ b/UndertaleModTool/Controls/UndertaleObjectReference.xaml.cs @@ -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), @@ -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 { @@ -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); } @@ -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() { @@ -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 @@ -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) diff --git a/UndertaleModTool/Editors/UndertaleGameObjectEditor.xaml b/UndertaleModTool/Editors/UndertaleGameObjectEditor.xaml index 11d5b28b1..3f20b8a83 100644 --- a/UndertaleModTool/Editors/UndertaleGameObjectEditor.xaml +++ b/UndertaleModTool/Editors/UndertaleGameObjectEditor.xaml @@ -494,6 +494,7 @@ diff --git a/UndertaleModTool/Editors/UndertaleRoomEditor.xaml b/UndertaleModTool/Editors/UndertaleRoomEditor.xaml index c1a3a53d9..f73998626 100644 --- a/UndertaleModTool/Editors/UndertaleRoomEditor.xaml +++ b/UndertaleModTool/Editors/UndertaleRoomEditor.xaml @@ -430,7 +430,8 @@ Creation Code - + Flags @@ -660,7 +661,10 @@ Creation code - + Scale @@ -696,7 +700,10 @@ Pre Create code - +