Skip to content

Commit

Permalink
Merge pull request #1226 from Caraxi/BoneRename
Browse files Browse the repository at this point in the history
Make bones renamable
  • Loading branch information
Yuki-Codes authored Sep 5, 2022
2 parents 0cb3301 + 9faf3e8 commit fcc2241
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Anamnesis/Actor/Pages/PosePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@

<Grid Grid.Row="0" Grid.ColumnSpan="2" Margin="8, 4, 6, 3">

<XivToolsWpf:TextBlock Style="{StaticResource Header}"
Text="{Binding Skeleton.CurrentBone.Tooltip}"
FontWeight="Bold"/>
<TextBox
Text="{Binding Skeleton.CurrentBone.Tooltip, UpdateSourceTrigger=PropertyChanged}"
Margin="0"
Padding="1"
FontSize="10"
FontWeight="Bold"
BorderBrush="Transparent"
Visibility="{Binding Skeleton.CurrentBone, Converter={StaticResource NotNullToVisibilityConverter}}"
Style="{StaticResource MaterialDesignTextBox}"/>

<XivToolsWpf:TextBlock Style="{StaticResource Header}"
Text="{Binding Skeleton.CurrentBone.BoneName}"
Expand Down
17 changes: 17 additions & 0 deletions Anamnesis/Actor/Posing/Visuals/BoneVisual3d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,30 @@ public string Tooltip
{
get
{
string? customName = CustomBoneNameService.GetBoneName(this.BoneName);

if (!string.IsNullOrEmpty(customName))
return customName;

string str = LocalizationService.GetString(this.TooltipKey, true);

if (string.IsNullOrEmpty(str))
return this.BoneName;

return str;
}

set
{
if (string.IsNullOrEmpty(value) || LocalizationService.GetString(this.TooltipKey, true) == value)
{
CustomBoneNameService.SetBoneName(this.BoneName, null);
}
else
{
CustomBoneNameService.SetBoneName(this.BoneName, value);
}
}
}

public BoneVisual3d? Parent
Expand Down
1 change: 1 addition & 0 deletions Anamnesis/ServiceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public async Task InitializeServices()
await Add<AnimationService>();
await Add<Keyboard.HotkeyService>();
await Add<HistoryService>();
await Add<CustomBoneNameService>();

IsInitialized = true;

Expand Down
77 changes: 77 additions & 0 deletions Anamnesis/Services/CustomBoneNameService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// © Anamnesis.
// Licensed under the MIT license.
namespace Anamnesis.Services;

using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using Anamnesis.Files;
using Anamnesis.Serialization;

public class CustomBoneNameService : ServiceBase<CustomBoneNameService>
{
private static readonly string SavePath = FileService.ParseToFilePath(FileService.StoreDirectory + "/CustomBoneNames.json");
private static Dictionary<string, string> customBoneNames = new Dictionary<string, string>();

public static string? GetBoneName(string bone)
{
if (customBoneNames.TryGetValue(bone, out var customName))
return customName;
return null;
}

public static void SetBoneName(string bone, string? customName)
{
if (customBoneNames.ContainsKey(bone))
{
if (string.IsNullOrEmpty(customName))
{
customBoneNames.Remove(bone);
}
else
{
customBoneNames[bone] = customName;
}
}
else
{
if (string.IsNullOrEmpty(customName))
return;
customBoneNames.Add(bone, customName);
}

Save();
}

public static void Save()
{
string json = SerializerService.Serialize(customBoneNames);
File.WriteAllText(SavePath, json);
}

public override async Task Initialize()
{
await base.Initialize();
try
{
if (File.Exists(SavePath))
{
string json = File.ReadAllText(SavePath);
customBoneNames = SerializerService.Deserialize<Dictionary<string, string>>(json);
}
}
catch (Exception ex)
{
Log.Warning(ex, "Failed to load custom bone names.");
customBoneNames = new Dictionary<string, string>();
Save();
}
}

public override Task Shutdown()
{
Save();
return base.Shutdown();
}
}

0 comments on commit fcc2241

Please sign in to comment.