Skip to content
Draft
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
29 changes: 29 additions & 0 deletions src/Blocktavius.AppDQB2/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,35 @@ public ItemCollection GetValues()
}
}

public sealed class SnippetsItemsSource : IItemsSource
{
public ItemCollection GetValues()
{
var list = new ItemCollection();
if (currentProject != null)
{
foreach (var snippet in currentProject.ExtractedSnippets)
{
list.Add(snippet, snippet.Name);
}
}
return list;
}
}

public sealed class RotationItemsSource : IItemsSource
{
public ItemCollection GetValues()
{
var list = new ItemCollection();
list.Add(0);
list.Add(90);
list.Add(180);
list.Add(270);
return list;
}
}

public static IEnumerable<DependencyObject> LogicalAncestors(this DependencyObject obj)
{
do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Blocktavius.AppDQB2.Resources;

namespace Blocktavius.AppDQB2.Persistence;

sealed class ResourceDeserializationContext
{
public required IAreaManager AreaManager { get; init; }
public required IReadOnlyList<SlotVM> Slots { get; init; }
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Blocktavius.AppDQB2.Resources;

namespace Blocktavius.AppDQB2.Persistence;

public sealed class ScriptDeserializationContext
{
internal ScriptDeserializationContext(IReadOnlyList<ExtractedSnippetResourceVM> snippets)
{
this.Snippets = snippets;
}

public required IAreaManager AreaManager { get; init; }
public required IBlockManager BlockManager { get; init; }
internal IReadOnlyList<ExtractedSnippetResourceVM> Snippets { get; }
}
26 changes: 24 additions & 2 deletions src/Blocktavius.AppDQB2/Persistence/V1/ProjectV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ public required IReadOnlyList<ScriptV1>? Scripts

public required int? SelectedScriptIndex { get; init; }

private IContentEqualityList<ExtractedSnippetV1>? _extractedSnippets = null;
public required IReadOnlyList<ExtractedSnippetV1>? ExtractedSnippets
{
get => _extractedSnippets;
init => _extractedSnippets = value?.ToContentEqualityList();
}

public ProjectV1 VerifyProfileHash(ProfileSettings profile)
{
if (profile.VerificationHash == ProfileVerificationHash)
Expand Down Expand Up @@ -170,10 +177,25 @@ sealed record RectV1
public required int X1 { get; init; }
public required int Z1 { get; init; }

public Rect ToCoreRect()
/// <summary>
/// Most of the time the user is entering all 4 values inclusively, but core algorithms
/// want the end to be exclusive.
/// </summary>
public Rect ToCoreRectInclusive()
{
var start = new XZ(Math.Min(X0, X1), Math.Min(Z0, Z1));
var end = new XZ(Math.Max(X0, X1), Math.Max(Z0, Z1));
var end = new XZ(Math.Max(X0, X1) + 1, Math.Max(Z0, Z1) + 1);
return new Rect(start, end);
}
}

sealed record ExtractedSnippetV1
{
public required string? PersistentId { get; init; }
public required string? Name { get; init; }
public required SlotReferenceV1? SourceSlot { get; init; }
public required string? SourceStgdatFilename { get; init; }
public required string? AreaPersistentId { get; init; }
public required RectV1? CustomRectArea { get; init; }
public required int? FloorY { get; init; }
}
6 changes: 6 additions & 0 deletions src/Blocktavius.AppDQB2/ProjectControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
</TabItem.Header>
<local:ScriptsControl></local:ScriptsControl>
</TabItem>
<TabItem>
<TabItem.Header>
<TextBlock>Resources</TextBlock>
</TabItem.Header>
<local:ResourcesControl></local:ResourcesControl>
</TabItem>
</TabControl>
</Grid>
</UserControl>
37 changes: 35 additions & 2 deletions src/Blocktavius.AppDQB2/ProjectVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Blocktavius.AppDQB2.Persistence;

namespace Blocktavius.AppDQB2;

Expand Down Expand Up @@ -59,6 +60,7 @@ sealed partial class ProjectVM : ViewModelBaseWithCustomTypeDescriptor, IBlockLi
// commands
public I.Project.CommandEditChunkGrid CommandEditChunkGrid { get; }
public ICommand CommandAddScript { get; }
public ICommand CommandAddSnippet { get; }

// wrapper properties
private ProfileSettings getProfile => xProfile.Value;
Expand Down Expand Up @@ -113,10 +115,18 @@ public ProjectVM(IServices services, ProfileSettings profile)
CommandExportChunkMask = new RelayCommand(_ => chunkGridLayer.ChunkGridImage != null, ExportChunkMask);
CommandExportMinimap = new RelayCommand(_ => minimapLayer?.MinimapImage != null, ExportMinimap);
CommandAddScript = new RelayCommand(_ => true, AddScript);
CommandAddSnippet = new RelayCommand(_ => true, AddSnippet);

ForceUpdateProfile(profile);
}

private void AddSnippet(object? _)
{
var newResource = new Resources.ExtractedSnippetResourceVM();
newResource.Name = $"New Snippet {ExtractedSnippets.Count + 1}";
ExtractedSnippets.Add(newResource);
}

private void ExportChunkMask(object? arg) => ExportImage(chunkGridLayer.ChunkGridImage, "exported-chunk-mask.png", 1.0);

private void ExportMinimap(object? arg) => ExportImage(minimapLayer?.MinimapImage, "exported-minimap.png", 0.5);
Expand Down Expand Up @@ -248,6 +258,11 @@ public ILayerVM? SelectedLayer

public ObservableCollection<ScriptVM> Scripts { get; } = new();

public ObservableCollection<Resources.ExtractedSnippetResourceVM> ExtractedSnippets { get; } = new();

public IReadOnlyList<IAreaVM> AvailableAreas
=> Layers.Select(layer => layer.SelfAsAreaVM).WhereNotNull().ToList();

private ScriptVM? _selectedScript;
public ScriptVM? SelectedScript
{
Expand Down Expand Up @@ -302,7 +317,7 @@ private void ExpandChunks(IReadOnlySet<ChunkOffset> expansion)
return null;
}

var context = new StageRebuildContext(workingStage);
var context = new StageRebuildContext(workingStage, stageLoader);
var mutation = this.SelectedScript?.BuildMutation(context);
if (mutation != null)
{
Expand Down Expand Up @@ -393,6 +408,7 @@ public ProjectV1 ToPersistModel()
ChunkGridVisible = chunkGridLayer.IsVisible,
Scripts = this.Scripts.Select(s => s.ToPersistModelConcrete()).ToList(),
SelectedScriptIndex = SelectedScript == null ? null : Scripts.IndexOf(SelectedScript),
ExtractedSnippets = this.ExtractedSnippets.Select(s => s.ToPersistModel()).ToList(),
};
}

Expand Down Expand Up @@ -455,7 +471,20 @@ private void Reload(ProjectV1 project, ExternalImageManager imageManager)
Layers.Add(chunkGridLayer);
SelectedLayer = Layers.FirstOrDefault();

var scriptContext = new Persistence.ScriptDeserializationContext
var resourceContext = new Persistence.ResourceDeserializationContext
{
AreaManager = this,
Slots = getSourceSlots,
};

ExtractedSnippets.Clear();
foreach (var snippet in project.ExtractedSnippets.EmptyIfNull())
{
var snippetVM = Resources.ExtractedSnippetResourceVM.Load(snippet, resourceContext);
ExtractedSnippets.Add(snippetVM);
}

var scriptContext = new Persistence.ScriptDeserializationContext(ExtractedSnippets)
{
AreaManager = this,
BlockManager = this,
Expand All @@ -469,6 +498,9 @@ private void Reload(ProjectV1 project, ExternalImageManager imageManager)
}
SelectedScript = Scripts.ElementAtOrDefault(project.SelectedScriptIndex.GetValueOrDefault(-1));




changeset.Complete();
}

Expand All @@ -490,6 +522,7 @@ private void Reload(ProjectV1 project, ExternalImageManager imageManager)
return Blockdata.AllBlockVMs.FirstOrDefault(x => x.PersistentId == persistentId);
}


static partial class MyProperty
{
public sealed class Profile : NotnullOriginProp<Profile, ProfileSettings>, I.Project.Profile { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<UserControl x:Class="Blocktavius.AppDQB2.Resources.ExtractedSnippetResourceEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:app="clr-namespace:Blocktavius.AppDQB2"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="400">
<StackPanel Margin="10">
<TextBlock Text="Name:" FontWeight="Bold"/>
<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,0,10"/>

<TextBlock Text="Source Slot:" FontWeight="Bold"/>
<ComboBox ItemsSource="{Binding DataContext.SourceSlots82, RelativeSource={RelativeSource AncestorType={x:Type app:ResourcesControl}}}"
SelectedItem="{Binding SourceSlot}"
DisplayMemberPath="Name" Margin="0,0,0,10"/>

<TextBlock Text="Source Stage:" FontWeight="Bold"/>
<ComboBox ItemsSource="{Binding SourceSlot.Stages}"
SelectedItem="{Binding SourceStage}"
DisplayMemberPath="Name" Margin="0,0,0,10"/>

<TextBlock Text="Floor Y:" FontWeight="Bold"></TextBlock>
<xctk:IntegerUpDown Value="{Binding FloorY}" Minimum="0" Maximum="95" Margin="0,0,0,10"></xctk:IntegerUpDown>

<GroupBox Header="Area" Margin="0,10,0,0">
<StackPanel>
<TextBlock Text="Pre-defined Area:" Margin="0,5,0,0"/>
<ComboBox ItemsSource="{Binding DataContext.AvailableAreas, RelativeSource={RelativeSource AncestorType={x:Type app:ResourcesControl}}}"
SelectedItem="{Binding AreaDefiner.Area}"
DisplayMemberPath="DisplayName" Margin="0,0,0,10"/>

<TextBlock Text="Or define custom rectangle:" FontStyle="Italic" Margin="0,5,0,5"/>

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<Label Grid.Row="0" Grid.Column="0" Content="X Begin:"/>
<xctk:IntegerUpDown Grid.Row="0" Grid.Column="1" Value="{Binding AreaDefiner.RectAreaBeginX, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,5"/>
<Label Grid.Row="0" Grid.Column="2" Content="Z Begin:"/>
<xctk:IntegerUpDown Grid.Row="0" Grid.Column="3" Value="{Binding AreaDefiner.RectAreaBeginZ, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,0,5"/>

<Label Grid.Row="1" Grid.Column="0" Content="X End:"/>
<xctk:IntegerUpDown Grid.Row="1" Grid.Column="1" Value="{Binding AreaDefiner.RectAreaEndX, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"/>
<Label Grid.Row="1" Grid.Column="2" Content="Z End:"/>
<xctk:IntegerUpDown Grid.Row="1" Grid.Column="3" Value="{Binding AreaDefiner.RectAreaEndZ, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</StackPanel>
</GroupBox>
</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Windows.Controls;

namespace Blocktavius.AppDQB2.Resources
{
/// <summary>
/// Interaction logic for ExtractedSnippetResourceEditor.xaml
/// </summary>
public partial class ExtractedSnippetResourceEditor : UserControl
{
public ExtractedSnippetResourceEditor()
{
InitializeComponent();
}
}
}
Loading