Skip to content

Commit

Permalink
Added framework for Object sub-classification
Browse files Browse the repository at this point in the history
Also fixed a bunch of warnings
  • Loading branch information
basilicon committed Sep 15, 2023
1 parent ded87c3 commit 888301b
Show file tree
Hide file tree
Showing 22 changed files with 290 additions and 179 deletions.
Binary file modified .vs/ProjectEvaluation/smm2saveeditor.metadata.v7.bin
Binary file not shown.
Binary file modified .vs/ProjectEvaluation/smm2saveeditor.projects.v7.bin
Binary file not shown.
Binary file modified .vs/SMM2SaveEditor/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified .vs/SMM2SaveEditor/v17/.futdcache.v2
Binary file not shown.
Binary file modified .vs/SMM2SaveEditor/v17/.suo
Binary file not shown.
8 changes: 4 additions & 4 deletions src/Level/Entities/Obj.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ public enum Sizes : uint
public Obj()
{
InitializeComponent();
img = this.Find<Image>("Sprite");
grid = this.Find<Grid>("LayoutGrid");
img = this.Find<Image>("Sprite")!;
grid = this.Find<Grid>("LayoutGrid")!;
}

public override void LoadFromStream(KaitaiStream io)
Expand Down Expand Up @@ -864,7 +864,7 @@ private object FindSpriteMapping()
}

object myMapping;
if (!spriteMappings.TryGetValue((ushort)id, out myMapping))
if (!spriteMappings.TryGetValue((ushort)id, out myMapping!))
{
throw new System.Exception($"No valid sprite mapping for {id}!");
}
Expand Down Expand Up @@ -923,7 +923,7 @@ private void SetSprite(Bitmap bitmap, Image? image = null)
}

image.Source = bitmap;
image.PointerPressed += OnClick;
image.PointerPressed += OnClick!;
}
}
}
9 changes: 9 additions & 0 deletions src/Level/Entities/Objs/Enemy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using SMM2SaveEditor.Entities;

namespace SMM2SaveEditor.Entities.Objs
{
public partial class Enemy : Obj
{

}
}
3 changes: 2 additions & 1 deletion src/Level/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Avalonia.VisualTree;
using Avalonia.Media;
using System;
using System.Collections;

namespace SMM2SaveEditor
{
Expand Down Expand Up @@ -39,7 +40,7 @@ public void OnClick(object sender, PointerPressedEventArgs e)

Entity? styledElement = visual.FindAncestorOfType<Entity>();
e.Handled = true;
EntityEditor.Instance.OpenOptions(e.GetCurrentPoint(visual).Position, styledElement);
EntityEditor.Instance.OpenOptions(styledElement);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Utility/EditorHelpers/EntityEditor.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
x:Class="SMM2SaveEditor.Utility.EditorHelpers.EntityEditor"
Background="DarkGray">
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<Grid ColumnDefinitions="*,*" Name="EditorGrid" VerticalAlignment="Top"/>
<StackPanel Name="EditorStack" Orientation="Vertical"/>
</ScrollViewer>
</UserControl>
175 changes: 10 additions & 165 deletions src/Utility/EditorHelpers/EntityEditor.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;

namespace SMM2SaveEditor.Utility.EditorHelpers
{
Expand All @@ -15,189 +16,33 @@ public partial class EntityEditor : UserControl

private Entity? objRef = null;

private List<string> labels;
private Grid grid;
private StackPanel editorStack;

public EntityEditor()
{
Instance = this;
InitializeComponent();

grid = this.Find<Grid>("EditorGrid");
if (grid == null) throw new Exception("No grid found!");
editorStack = this.Find<StackPanel>("EditorStack")!;
}

private void OnApply()
public void OpenOptions(Entity entity)
{
if (labels == null || objRef == null) return;

IDictionary<string, object> options = new Dictionary<string, object>();

foreach (var child in grid.Children)
{
if (Grid.GetColumn(child) == 0) continue;

int row = Grid.GetRow(child);
string key = labels[row];

if (child is EnumDropdown dropdown)
{
options.Add(key, dropdown.enumValue);
}
else if (child is FlagEditor flagEditor)
{
options.Add(key, flagEditor.flag);
}
else if (child is NumericUpDown numericUpDown)
{
options.Add(key, Convert.ToInt32(numericUpDown.Value));
}
if (child is TextBox textBox)
{
options.Add(key, textBox.Text);
}
}

objRef.SetValuesFromDictionary(objRef.GetType(), options);
objRef.UpdateSprite();
}

private void ApplyOption(string key, object value)
{
if (objRef == null)
{
Debug.WriteLine("Object reference not set!");
return;
}

Type type = objRef.GetType();
var field = type.GetField(key);

if (field == null)
{
Debug.WriteLine($"Property {key} is invalid!");
return;
}

field.SetValue(objRef, value);
}

public void OpenOptions(Point p, Entity entity)
{
grid.Children.RemoveAll(grid.Children);
editorStack.Children.RemoveAll(editorStack.Children);
GC.Collect();
GC.WaitForPendingFinalizers();

objRef = entity;

IDictionary<string, object> options = entity.AsDictionary(entity.GetType());

grid.ColumnDefinitions = new ColumnDefinitions("2*,*");
grid.RowDefinitions = new RowDefinitions(ObjectExtensions.EqualSpacingDefinition(options.Count));

int counter = 0;
labels = new(options.Count);
foreach (KeyValuePair<string, object> kvp in options)
foreach (Type t in entity.GetType().GetInheritanceHierarchy())
{
TextBlock textBlock = new();
textBlock.Text = kvp.Key;
textBlock.TextAlignment = TextAlignment.Center;
textBlock.MinHeight = 20;
grid.Children.Add(textBlock);
Grid.SetColumn(textBlock, 0);
Grid.SetRow(textBlock, counter);
textBlock.Margin = new Thickness(5);

Type type = kvp.Value.GetType();

Control o = new();

if (type.IsSubclassOf(typeof(Enum)))
{
EnumDropdown enumDropdown = new();
enumDropdown.SetEnum(type, kvp.Value);
enumDropdown.SelectionChanged += (o, e) => {
ApplyOption(kvp.Key, (o as EnumDropdown).enumValue);
};

o = enumDropdown;
}
else
if (kvp.Key == "flag" || kvp.Key == "cflag")
{
FlagEditor flagEditor = new();
flagEditor.SetFlag((uint)kvp.Value);
flagEditor.ValueChanged += (o) =>
{
ApplyOption(kvp.Key, (o as FlagEditor).flag);
};

o = flagEditor;
}
else
if (IsIntegerType(type))
{
NumericUpDown numericUpDown = new();
numericUpDown.Increment = 1;
numericUpDown.Value = Convert.ToDecimal(kvp.Value);
numericUpDown.ValueChanged += (o, e) => {
ApplyOption(kvp.Key, Convert.ToInt64((o as NumericUpDown).Value));
};
ObjectEditor objectEditor = new();
editorStack.Children.Add(objectEditor);
objectEditor.OpenOptions((Convert.ChangeType(entity, t) as Entity)!);

o = numericUpDown;
}
else
if (type == typeof(string))
{
TextBox textBox = new();
textBox.Text = (string)kvp.Value;
textBox.TextChanged += (o, e) => {
ApplyOption(kvp.Key, (o as TextBox).Text);
};

textBox.TextWrapping = TextWrapping.Wrap;
textBox.VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch;
textBox.HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch;

o = textBox;
}
else
{
TextBox valueBlock = new();
valueBlock.Text = kvp.Value.ToString();
valueBlock.TextInput += (o, e) => {
ApplyOption(kvp.Key, (o as TextBox).Text);
};

o = valueBlock;
}

grid.Children.Add(o);
Grid.SetColumn(o, 1);
Grid.SetRow(o, counter);
o.Margin = new Thickness(5);

counter++;
labels.Add(kvp.Key);
Debug.WriteLine(t.Name);
}
}

private static bool IsIntegerType(Type t)
{
switch (Type.GetTypeCode(t))
{
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
return true;
default:
return false;
}
}
}
}
14 changes: 14 additions & 0 deletions src/Utility/EditorHelpers/ObjectEditor.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SMM2SaveEditor.Utility.EditorHelpers.ObjectEditor">
<StackPanel Spacing="5" Margin="10" Name="RootStackPanel">
<StackPanel Orientation="Horizontal" Spacing="5">
<ToggleButton Name="ExpandButton" IsChecked="True">v</ToggleButton>
<TextBlock Name="EditorHeader" FontSize="20" FontWeight="Bold">Header</TextBlock>
</StackPanel>
<Grid ColumnDefinitions="*,*" Name="EditorGrid" VerticalAlignment="Top"/>
</StackPanel>
</UserControl>
Loading

0 comments on commit 888301b

Please sign in to comment.