Skip to content

Commit

Permalink
Added predefined tags
Browse files Browse the repository at this point in the history
  • Loading branch information
And42 committed Jun 19, 2020
1 parent 4d69631 commit 36cde49
Show file tree
Hide file tree
Showing 14 changed files with 343 additions and 26 deletions.
2 changes: 2 additions & 0 deletions TerrLauncherPackCreator/Code/Converters/Converters.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@
<local:TextureModelToVerticalFramesVisibility x:Key="TextureModelToVerticalFramesVisibility"/>
<local:TextureModelToHorizontalFramesVisibility x:Key="TextureModelToHorizontalFramesVisibility"/>
<local:TextureModelToAnimateInGuiVisibility x:Key="TextureModelToAnimateInGuiVisibility"/>
<local:PredefinedTagToStringConverter x:Key="PredefinedTagToStringConverter"/>
<local:InvertBoolConverter x:Key="InvertBoolConverter"/>

</ResourceDictionary>
29 changes: 29 additions & 0 deletions TerrLauncherPackCreator/Code/Converters/InvertBoolConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Globalization;
using System.Windows.Data;

namespace TerrLauncherPackCreator.Code.Converters
{
public class InvertBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value as bool?) switch
{
false => true,
true => false,
_ => null
};
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value as bool?) switch
{
false => true,
true => false,
_ => null
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Globalization;
using System.Windows.Data;
using TerrLauncherPackCreator.Code.Enums;
using TerrLauncherPackCreator.Resources.Localizations;

namespace TerrLauncherPackCreator.Code.Converters
{
public class PredefinedTagToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value as PredefinedPackTag?) switch
{
PredefinedPackTag.Animated => StringResources.PredefinedPackTagAnimated,
_ => null
};
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var val = value as string;

if (val == StringResources.PredefinedPackTagAnimated)
return PredefinedPackTag.Animated;
return null;
}
}
}
7 changes: 7 additions & 0 deletions TerrLauncherPackCreator/Code/Enums/PredefinedPackTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TerrLauncherPackCreator.Code.Enums
{
public enum PredefinedPackTag
{
Animated = 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ private async Task<PackModel> LoadPackModelInternal(string filePath)
DescriptionRussian = packSettings.DescriptionRussian,
Version = packSettings.Version,
Guid = packSettings.Guid,
PredefinedTags = packSettings.PredefinedTags?.ToList() ?? new List<PredefinedPackTag>(0)
};

return packModel;
Expand All @@ -220,7 +221,8 @@ private void SavePackModelInternal(PackModel packModel, string filePath)
DescriptionRussian = packModel.DescriptionRussian,
Version = packModel.Version,
Guid = packModel.Guid,
Authors = string.Join("<->", authorsMappings.Select(it => it.json))
Authors = string.Join("<->", authorsMappings.Select(it => it.json)),
PredefinedTags = packModel.PredefinedTags.ToList()
};

IOUtils.TryDeleteFile(filePath, PackProcessingTries, PackProcessingSleepMs);
Expand Down
7 changes: 7 additions & 0 deletions TerrLauncherPackCreator/Code/Json/PackSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Newtonsoft.Json;
using TerrLauncherPackCreator.Code.Enums;

namespace TerrLauncherPackCreator.Code.Json
{
Expand Down Expand Up @@ -28,5 +31,9 @@ public class PackSettings

[JsonProperty("authors")]
public string Authors { get; set; }

[CanBeNull]
[JsonProperty("predefined_tags")]
public List<PredefinedPackTag> PredefinedTags { get; set; }
}
}
4 changes: 4 additions & 0 deletions TerrLauncherPackCreator/Code/Models/PackModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Windows.Media;
using JetBrains.Annotations;
using TerrLauncherPackCreator.Code.Enums;
Expand Down Expand Up @@ -52,6 +53,9 @@ [NotNull] ModifiedFileInfo[] modifiedFiles

public int Version { get; set; }

[NotNull]
public List<PredefinedPackTag> PredefinedTags { get; set; }

[NotNull]
public (string name, Color? color, string link, ImageInfo icon)[] Authors { get; set; }

Expand Down
66 changes: 64 additions & 2 deletions TerrLauncherPackCreator/Code/ViewModels/PackCreationViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
Expand All @@ -23,9 +24,13 @@ namespace TerrLauncherPackCreator.Code.ViewModels
public class PackCreationViewModel : ViewModelBase
{
private const int TerrariaStructureVersion = 3;
private const int PackStructureVersion = 6;
private const int PackStructureVersion = 7;
private static readonly ISet<string> IconExtensions = new HashSet<string> {".png", ".gif"};
private static readonly ISet<string> PreviewExtensions = new HashSet<string> {".jpg", ".png", ".gif"};
private static readonly ISet<PredefinedPackTag> AllPredefinedTags = new HashSet<PredefinedPackTag>
{
PredefinedPackTag.Animated
};

[NotNull] private readonly IPackProcessor _packProcessor;
[NotNull] private readonly AuthorsFileProcessor _authorsFileProcessor;
Expand Down Expand Up @@ -69,6 +74,16 @@ public int Version
set => SetProperty(ref _version, value);
}

public ObservableCollection<PredefinedPackTag> PredefinedTags { get; }

public IReadOnlyList<PredefinedPackTag> RemainedPredefinedTags => AllPredefinedTags.Except(PredefinedTags).ToList();

public bool IsPredefindTagsPopupOpen
{
get => _isPredefindTagsPopupOpen;
set => SetProperty(ref _isPredefindTagsPopupOpen, value);
}

// Step 2
[NotNull] public ObservableCollection<PreviewItemModel> Previews { get; }

Expand All @@ -89,6 +104,7 @@ public int Version
private string _descriptionEnglish;
private Guid _guid;
private int _version;
private bool _isPredefindTagsPopupOpen;

#endregion

Expand All @@ -97,6 +113,9 @@ public int Version
// Step 1
public IActionCommand CreateNewGuidCommand { get; }
public IActionCommand<string> DropIconCommand { get; }
public IActionCommand AddPredefinedTagCommand { get; }
public IActionCommand<PredefinedPackTag> AddSelectedTagCommand { get; }
public IActionCommand<PredefinedPackTag> RemovePredefinedTag { get; }

// Step 2
public IActionCommand<string[]> DropPreviewsCommand { get; }
Expand Down Expand Up @@ -140,8 +159,12 @@ [NotNull] AuthorsFileProcessor authorsFileProcessor
Authors = new ObservableCollection<AuthorItemModel>();

// Step 1
PredefinedTags = new ObservableCollection<PredefinedPackTag>();
CreateNewGuidCommand = new ActionCommand(CreateNewGuidCommand_Execute, CreateNewGuidCommand_CanExecute);
DropIconCommand = new ActionCommand<string>(file => { }, DropIconCommand_CanExecute);
AddPredefinedTagCommand = new ActionCommand(AddPredefinedTagExecuted, AddPredefinedTagCanExecute);
AddSelectedTagCommand = new ActionCommand<PredefinedPackTag>(AddSelectedTagExecuted);
RemovePredefinedTag = new ActionCommand<PredefinedPackTag>(RemovePredefinedTagExecuted, RemovePredefinedTagCanExecute);

// Step 2
DropPreviewsCommand =
Expand Down Expand Up @@ -169,6 +192,7 @@ [NotNull] AuthorsFileProcessor authorsFileProcessor
_packProcessor.PackLoaded += PackProcessorOnPackLoaded;
_packProcessor.PackSaved += PackProcessorOnPackSaved;

PredefinedTags.CollectionChanged += PredefinedTagsCollectionChanged;
PropertyChanged += OnPropertyChanged;
}

Expand Down Expand Up @@ -224,6 +248,8 @@ public void InitFromPackModel(PackModel packModel)
DescriptionEnglish = packModel.DescriptionEnglish;
Guid = packModel.Guid;
Version = packModel.Version;
PredefinedTags.Clear();
packModel.PredefinedTags.ForEach(PredefinedTags.Add);

var previewItems = packModel.PreviewsPaths.Select(PreviewItemModel.FromImageFile).ToArray();

Expand Down Expand Up @@ -271,6 +297,33 @@ private bool DropIconCommand_CanExecute(string filePath)
{
return !Working && File.Exists(filePath) && IconExtensions.Contains(Path.GetExtension(filePath));
}

private bool AddPredefinedTagCanExecute()
{
return !Working && RemainedPredefinedTags.Count != 0;
}

private void AddPredefinedTagExecuted()
{
IsPredefindTagsPopupOpen = true;
}

private void AddSelectedTagExecuted(PredefinedPackTag tag)
{
PredefinedTags.Add(tag);
IsPredefindTagsPopupOpen = false;
}

private bool RemovePredefinedTagCanExecute(PredefinedPackTag _)
{
return !Working;
}

private void RemovePredefinedTagExecuted(PredefinedPackTag tag)
{
PredefinedTags.Remove(tag);
}


#endregion

Expand Down Expand Up @@ -509,7 +562,8 @@ private PackModel GeneratePackModel()
DescriptionRussian = DescriptionRussian,
DescriptionEnglish = DescriptionEnglish,
Guid = Guid,
Version = Version
Version = Version,
PredefinedTags = PredefinedTags.ToList()
};
}

Expand All @@ -529,6 +583,11 @@ private void ResetCollections()
}
}

private void PredefinedTagsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged(nameof(RemainedPredefinedTags));
}

private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
Expand All @@ -547,6 +606,9 @@ private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
// Step 5
ExportPackCommand.RaiseCanExecuteChanged();
break;
case nameof(RemainedPredefinedTags):
AddPredefinedTagCommand.RaiseCanExecuteChanged();
break;
}
}

Expand Down
Loading

0 comments on commit 36cde49

Please sign in to comment.