Skip to content

Commit

Permalink
Create viewmodels for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
virusek20 committed Mar 30, 2019
1 parent 64211ca commit ba7d2bd
Show file tree
Hide file tree
Showing 22 changed files with 523 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Globalization;
using System.Windows.Data;
using SatisfactorySaveEditor.ViewModel;
using SatisfactorySaveEditor.ViewModel.Property;
using SatisfactorySaveParser.PropertyTypes;

namespace SatisfactorySaveEditor.Converter
Expand All @@ -12,29 +13,29 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
{
switch (value)
{
case ArrayProperty arp:
case ArrayPropertyViewModel arp:
return $"Array ({AddViewModel.FromStringType(arp.Type)})";
case BoolProperty bop:
case BoolPropertyViewModel bop:
return "Boolean";
case ByteProperty byp:
case BytePropertyViewModel byp:
return "Byte";
case EnumProperty enp:
case EnumPropertyViewModel enp:
return "Enum";
case FloatProperty flp:
case FloatPropertyViewModel flp:
return "Float";
case IntProperty inp:
case IntPropertyViewModel inp:
return "Int";
case MapProperty map: // heh
case MapPropertyViewModel map: // heh
return "Map";
case NameProperty nap: // THESE NAMES KEEP GETTING BETTER
case NamePropertyViewModel nap: // THESE NAMES KEEP GETTING BETTER
return "Name";
case ObjectProperty obp:
case ObjectPropertyViewModel obp:
return "Object";
case StrProperty strip:
case StrPropertyViewModel strip:
return "String";
case StructProperty strup:
case StructPropertyViewModel strup:
return "Struct";
case TextProperty tep:
case TextPropertyViewModel tep:
return "text";
default:
throw new ArgumentOutOfRangeException(nameof(value), value, null);
Expand Down
19 changes: 17 additions & 2 deletions SatisfactorySaveEditor/Model/SaveObjectModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections.ObjectModel;
using System.Linq;
using GalaSoft.MvvmLight;
using SatisfactorySaveEditor.Util;
using SatisfactorySaveEditor.ViewModel.Property;
using SatisfactorySaveParser;
using SatisfactorySaveParser.PropertyTypes;

namespace SatisfactorySaveEditor.Model
{
Expand All @@ -13,7 +15,8 @@ public class SaveObjectModel : ObservableObject
private bool isExpanded;

public ObservableCollection<SaveObjectModel> Items { get; } = new ObservableCollection<SaveObjectModel>();
public ObservableCollection<SerializedProperty> Fields => Model.DataFields;

public ObservableCollection<SerializedPropertyViewModel> Fields { get; }

public string Title
{
Expand Down Expand Up @@ -46,6 +49,8 @@ public SaveObjectModel(SaveObject model)
Model = model;
Title = model.InstanceName;
RootObject = model.RootObject;

Fields = new ObservableCollection<SerializedPropertyViewModel>(Model.DataFields.Select(PropertyViewModelMapper.Convert));
}

public SaveObjectModel(string title)
Expand Down Expand Up @@ -92,6 +97,16 @@ public virtual void ApplyChanges()
{
item.ApplyChanges();
}

// This is because the named only (pink) nodes aren't actually a valid object in the game
if (Model == null) return;

Model.DataFields.Clear();
foreach (var field in Fields)
{
field.ApplyChanges();
Model.DataFields.Add(field.Model);
}
}
}
}
14 changes: 14 additions & 0 deletions SatisfactorySaveEditor/SatisfactorySaveEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,22 @@
<Compile Include="Model\SaveObjectModel.cs" />
<Compile Include="Util\EditorTreeNode.cs" />
<Compile Include="Util\BindingProxy.cs" />
<Compile Include="Util\PropertyViewModelMapper.cs" />
<Compile Include="ViewModel\AddViewModel.cs" />
<Compile Include="ViewModel\MainViewModel.cs" />
<Compile Include="ViewModel\Property\ArrayPropertyViewModel.cs" />
<Compile Include="ViewModel\Property\BoolPropertyViewModel.cs" />
<Compile Include="ViewModel\Property\BytePropertyViewModel.cs" />
<Compile Include="ViewModel\Property\EnumPropertyViewModel.cs" />
<Compile Include="ViewModel\Property\FloatPropertyViewModel.cs" />
<Compile Include="ViewModel\Property\IntPropertyViewModel.cs" />
<Compile Include="ViewModel\Property\MapPropertyViewModel.cs" />
<Compile Include="ViewModel\Property\NamePropertyViewModel.cs" />
<Compile Include="ViewModel\Property\ObjectPropertyViewModel.cs" />
<Compile Include="ViewModel\Property\SerializedPropertyViewModel.cs" />
<Compile Include="ViewModel\Property\StrPropertyViewModel.cs" />
<Compile Include="ViewModel\Property\StructPropertyViewModel.cs" />
<Compile Include="ViewModel\Property\TextPropertyViewModel.cs" />
<Compile Include="ViewModel\ViewModelLocator.cs" />
<Compile Include="View\PropertiesControl.xaml.cs">
<DependentUpon>PropertiesControl.xaml</DependentUpon>
Expand Down
42 changes: 42 additions & 0 deletions SatisfactorySaveEditor/Util/PropertyViewModelMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using SatisfactorySaveEditor.ViewModel.Property;
using SatisfactorySaveParser.PropertyTypes;

namespace SatisfactorySaveEditor.Util
{
public static class PropertyViewModelMapper
{
public static SerializedPropertyViewModel Convert(SerializedProperty property)
{
switch (property)
{
case ArrayProperty arp:
return new ArrayPropertyViewModel(arp);
case BoolProperty bop:
return new BoolPropertyViewModel(bop);
case ByteProperty byp:
return new BytePropertyViewModel(byp);
case EnumProperty enp:
return new EnumPropertyViewModel(enp);
case FloatProperty flp:
return new FloatPropertyViewModel(flp);
case IntProperty inp:
return new IntPropertyViewModel(inp);
case MapProperty map:
return new MapPropertyViewModel(map);
case NameProperty nap:
return new NamePropertyViewModel(nap);
case ObjectProperty obp:
return new ObjectPropertyViewModel(obp);
case StrProperty strip:
return new StrPropertyViewModel(strip);
case StructProperty strup:
return new StructPropertyViewModel(strup);
case TextProperty tep:
return new TextPropertyViewModel(tep);
default:
throw new ArgumentOutOfRangeException(nameof(property), property, null);
}
}
}
}
24 changes: 12 additions & 12 deletions SatisfactorySaveEditor/View/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,23 @@
<ContentControl.Resources>
<DataTemplate DataType="{x:Type model:SaveEntityModel}">
<DockPanel>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding Title, FallbackValue='No item selected', StringFormat='Entity name: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding RootObject, FallbackValue='No item selected', StringFormat='Root: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding Int4, FallbackValue='No item selected', StringFormat='Int4: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding Int6, FallbackValue='No item selected', StringFormat='Int6: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding Rotation, FallbackValue='No item selected', StringFormat='Rotation: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding Position, FallbackValue='No item selected', StringFormat='Position: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding Scale, FallbackValue='No item selected', StringFormat='Scale: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding ParentObjectName, FallbackValue='No item selected', StringFormat='ParentObjectName: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding ParentObjectRoot, FallbackValue='No item selected', StringFormat='ParentObjectRoot: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding Title, StringFormat='Entity name: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding RootObject, StringFormat='Root: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding Int4, StringFormat='Int4: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding Int6, StringFormat='Int6: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding Rotation, StringFormat='Rotation: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding Position, StringFormat='Position: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding Scale, StringFormat='Scale: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding ParentObjectName, StringFormat='ParentObjectName: {0}'}"/>
<TextBlock TextWrapping="Wrap" DockPanel.Dock="Top" Text="{Binding ParentObjectRoot, StringFormat='ParentObjectRoot: {0}'}"/>
<view:PropertiesControl Properties="{Binding Fields}" AddPropertyCommand="{Binding Source={StaticResource Proxy}, Path=Data.AddPropertyCommand}"/>
</DockPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type model:SaveComponentModel}">
<DockPanel>
<TextBlock DockPanel.Dock="Top" Text="{Binding Title, FallbackValue='No item selected', StringFormat='Component name: {0}'}"/>
<TextBlock DockPanel.Dock="Top" Text="{Binding RootObject, FallbackValue='No item selected', StringFormat='Root: {0}'}"/>
<TextBlock DockPanel.Dock="Top" Text="{Binding ParentEntityName, FallbackValue='No item selected', StringFormat='ParentEntityName: {0}'}"/>
<TextBlock DockPanel.Dock="Top" Text="{Binding Title, StringFormat='Component name: {0}'}"/>
<TextBlock DockPanel.Dock="Top" Text="{Binding RootObject, StringFormat='Root: {0}'}"/>
<TextBlock DockPanel.Dock="Top" Text="{Binding ParentEntityName, StringFormat='ParentEntityName: {0}'}"/>
<view:PropertiesControl Properties="{Binding Fields}" AddPropertyCommand="{Binding Source={StaticResource Proxy}, Path=Data.AddPropertyCommand}"/>
</DockPanel>
</DataTemplate>
Expand Down
8 changes: 4 additions & 4 deletions SatisfactorySaveEditor/View/PropertiesControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SatisfactorySaveParser.PropertyTypes;
using SatisfactorySaveEditor.ViewModel.Property;

namespace SatisfactorySaveEditor.View
{
Expand All @@ -22,13 +22,13 @@ public ICommand AddPropertyCommand
set => SetValue(AddPropertyCommandProperty, value);
}

public ObservableCollection<SerializedProperty> Properties
public ObservableCollection<SerializedPropertyViewModel> Properties
{
get => (ObservableCollection<SerializedProperty>)GetValue(PropertiesProperty);
get => (ObservableCollection<SerializedPropertyViewModel>)GetValue(PropertiesProperty);
set => SetValue(PropertiesProperty, value);
}

public static readonly DependencyProperty AddPropertyCommandProperty = DependencyProperty.Register("AddPropertyCommand", typeof(ICommand), typeof(PropertiesControl));
public static readonly DependencyProperty PropertiesProperty = DependencyProperty.Register("Properties", typeof(ObservableCollection<SerializedProperty>), typeof(PropertiesControl));
public static readonly DependencyProperty PropertiesProperty = DependencyProperty.Register("Properties", typeof(ObservableCollection<SerializedPropertyViewModel>), typeof(PropertiesControl));
}
}
33 changes: 16 additions & 17 deletions SatisfactorySaveEditor/View/PropertyTemplateDictionary.xaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:propertyTypes="clr-namespace:SatisfactorySaveParser.PropertyTypes;assembly=SatisfactorySaveParser"
xmlns:view="clr-namespace:SatisfactorySaveEditor.View"
xmlns:structs="clr-namespace:SatisfactorySaveParser.PropertyTypes.Structs;assembly=SatisfactorySaveParser">
<DataTemplate DataType="{x:Type propertyTypes:ArrayProperty}">
xmlns:property="clr-namespace:SatisfactorySaveEditor.ViewModel.Property">
<DataTemplate DataType="{x:Type property:ArrayPropertyViewModel}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
Expand All @@ -18,31 +17,31 @@
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Grid.Row="1" Content="Add element" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type view:MainWindow}}, Path=DataContext.AddPropertyCommand}" CommandParameter="{Binding .}"/>
<Button Grid.Row="1" Content="Add element" Command="{Binding AddElementCommand}"/>
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type propertyTypes:BoolProperty}">
<DataTemplate DataType="{x:Type property:BoolPropertyViewModel}">
<CheckBox IsChecked="{Binding Value, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type propertyTypes:ByteProperty}">
<DataTemplate DataType="{x:Type property:BytePropertyViewModel}">
<TextBox Margin="4" VerticalContentAlignment="Center" Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type propertyTypes:EnumProperty}">
<DataTemplate DataType="{x:Type property:EnumPropertyViewModel}">
<TextBox Margin="4" VerticalContentAlignment="Center" Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type propertyTypes:FloatProperty}">
<DataTemplate DataType="{x:Type property:FloatPropertyViewModel}">
<TextBox Margin="4" VerticalContentAlignment="Center" Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type propertyTypes:IntProperty}">
<DataTemplate DataType="{x:Type property:IntPropertyViewModel}">
<TextBox Margin="4" VerticalContentAlignment="Center" Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type propertyTypes:MapProperty}">
<DataTemplate DataType="{x:Type property:MapPropertyViewModel}">
<Label Margin="4" VerticalContentAlignment="Center" Content="// TODO: MAP STUFF"/>
</DataTemplate>
<DataTemplate DataType="{x:Type propertyTypes:NameProperty}">
<DataTemplate DataType="{x:Type property:NamePropertyViewModel}">
<TextBox Margin="4" VerticalContentAlignment="Center" Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type propertyTypes:ObjectProperty}">
<DataTemplate DataType="{x:Type property:ObjectPropertyViewModel}">
<StackPanel Orientation="Vertical">
<DockPanel>
<Label DockPanel.Dock="Left" Target="{Binding ElementName=ParentBox}" Content="Parent:"/>
Expand All @@ -61,13 +60,13 @@
</DockPanel>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type propertyTypes:StrProperty}">
<DataTemplate DataType="{x:Type property:StrPropertyViewModel}">
<TextBox Margin="4" VerticalContentAlignment="Center" Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type propertyTypes:StructProperty}">
<DataTemplate DataType="{x:Type property:StructPropertyViewModel}">
<StackPanel Orientation="Vertical">
<Label Margin="4,0,4,0" VerticalContentAlignment="Center" Content="{Binding Type, FallbackValue='OH NO'}" ContentStringFormat="Type: {0}"/>
<ItemsControl ItemsSource="{Binding Data.Fields}">
<Label Margin="4,0,4,0" VerticalContentAlignment="Center" Content="{Binding Type}" ContentStringFormat="Type: {0}"/>
<ItemsControl ItemsSource="{Binding Fields}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<GroupBox Header="{Binding PropertyName}">
Expand All @@ -78,7 +77,7 @@
</ItemsControl>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type propertyTypes:TextProperty}">
<DataTemplate DataType="{x:Type property:TextPropertyViewModel}">
<StackPanel Orientation="Vertical">
<DockPanel>
<Label DockPanel.Dock="Left" Target="{Binding ElementName=ValueBox}" Content="Value:"/>
Expand Down
3 changes: 2 additions & 1 deletion SatisfactorySaveEditor/ViewModel/AddViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using SatisfactorySaveEditor.Model;
using SatisfactorySaveEditor.Util;
using SatisfactorySaveParser.PropertyTypes;

namespace SatisfactorySaveEditor.ViewModel
Expand Down Expand Up @@ -188,7 +189,7 @@ private void Ok(Window obj)
{
var property = CreateProperty(type, name);
if (type == AddTypeEnum.Array) ((ArrayProperty) property).Type = FromAddTypeEnum(arrayType);
ObjectModel.Fields.Add(property);
ObjectModel.Fields.Add(PropertyViewModelMapper.Convert(property));

obj.Close();
}
Expand Down
Loading

0 comments on commit ba7d2bd

Please sign in to comment.