Skip to content

Commit

Permalink
s
Browse files Browse the repository at this point in the history
  • Loading branch information
poi-vrc committed Apr 30, 2024
1 parent 8d6ca51 commit 6e2fd73
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,97 +13,62 @@
using Chocopoi.AvatarLib.Animations;
using Chocopoi.DressingTools.Components.OneConf;
using Chocopoi.DressingTools.OneConf;
using Chocopoi.DressingTools.OneConf.Cabinet;
using Chocopoi.DressingTools.OneConf.Serialization;
using Chocopoi.DressingTools.OneConf.Wearable.Modules.BuiltIn;
using Chocopoi.DressingTools.UI.Views;
using UnityEngine;

namespace Chocopoi.DressingTools.Configurator.Modules
{
internal class OneConfArmatureMappingModule : IArmatureMergingModule
internal class OneConfArmatureMappingModule : OneConfModuleBase, IArmatureMergingModule
{
public Transform TargetArmature
{
get
{
if (!_avatarGameObject.TryGetComponent<DTCabinet>(out var cabinetComp) ||
!CabinetConfigUtility.TryDeserialize(cabinetComp.ConfigJson, out var cabinetConfig))
{
cabinetConfig = new CabinetConfig();
}
if (string.IsNullOrEmpty(cabinetConfig.avatarArmatureName))
{
return null;
}
return OneConfUtils.GuessArmature(_avatarGameObject, cabinetConfig.avatarArmatureName);
ReadCabinetConfig(out _, out var config);
return string.IsNullOrEmpty(config.avatarArmatureName) ?
null :
OneConfUtils.GuessArmature(_avatarGameObject, config.avatarArmatureName);
}
set
{
CabinetConfig cabinetConfig;
if (_avatarGameObject.TryGetComponent<DTCabinet>(out var cabinetComp))
WriteCabinetConfig((comp, config) =>
{
if (!CabinetConfigUtility.TryDeserialize(cabinetComp.ConfigJson, out cabinetConfig))
{
cabinetConfig = new CabinetConfig();
}
}
else
{
cabinetComp = _avatarGameObject.AddComponent<DTCabinet>();
cabinetConfig = new CabinetConfig();
}
cabinetConfig.avatarArmatureName =
value == null ?
"" :
AnimationUtils.GetRelativePath(value.transform, cabinetComp.transform);
cabinetComp.ConfigJson = CabinetConfigUtility.Serialize(cabinetConfig);
config.avatarArmatureName =
value == null ?
"" :
AnimationUtils.GetRelativePath(value.transform, comp.transform);
});
}
}
public Transform SourceArmature
{
get
{
if (!WearableConfigUtility.TryDeserialize(_wearableComp.ConfigJson, out var config))
{
return null;
}
var module = config.FindModuleConfig<ArmatureMappingWearableModuleConfig>();
if (module == null || string.IsNullOrEmpty(module.wearableArmatureName))
{
return null;
}
return OneConfUtils.GuessArmature(_wearableComp.gameObject, module.wearableArmatureName);
ReadWearableModule<ArmatureMappingWearableModuleConfig>(out var module);
if (module == null) return null;
return string.IsNullOrEmpty(module.wearableArmatureName) ?
null :
OneConfUtils.GuessArmature(_wearableComp.gameObject, module.wearableArmatureName);
}
set
{
if (!WearableConfigUtility.TryDeserialize(_wearableComp.ConfigJson, out var config))
{
return;
}
var module = config.FindModuleConfig<ArmatureMappingWearableModuleConfig>();
if (module == null)
WriteWearableModule<ArmatureMappingWearableModuleConfig>(module =>
{
return;
}
module.wearableArmatureName =
value == null ?
"" :
AnimationUtils.GetRelativePath(value.transform, _wearableComp.transform);
_wearableComp.ConfigJson = WearableConfigUtility.Serialize(config);
module.wearableArmatureName =
value == null ?
"" :
AnimationUtils.GetRelativePath(value.transform, _wearableComp.transform);
});
}
}

private readonly GameObject _avatarGameObject;
private readonly DTWearable _wearableComp;

public OneConfArmatureMappingModule(GameObject avatarGameObject, DTWearable wearableComp)
: base(avatarGameObject, wearableComp)
{
_avatarGameObject = avatarGameObject;
_wearableComp = wearableComp;
}

public ElementView CreateView()
public override ElementView CreateView()
{
throw new System.NotImplementedException();
}
Expand Down
113 changes: 113 additions & 0 deletions Editor/Configurator/Modules/OneConfModuleBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2024 chocopoi
*
* This file is part of DressingTools.
*
* DressingTools is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* DressingTools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with DressingFramework. If not, see <https://www.gnu.org/licenses/>.
*/

using System;
using Chocopoi.DressingTools.Components.OneConf;
using Chocopoi.DressingTools.OneConf.Cabinet;
using Chocopoi.DressingTools.OneConf.Serialization;
using Chocopoi.DressingTools.OneConf.Wearable;
using Chocopoi.DressingTools.UI.Views;
using UnityEngine;

namespace Chocopoi.DressingTools.Configurator.Modules
{
internal abstract class OneConfModuleBase : IModule
{
protected readonly GameObject _avatarGameObject;
protected readonly DTWearable _wearableComp;

public OneConfModuleBase(GameObject avatarGameObject, DTWearable wearableComp)
{
_avatarGameObject = avatarGameObject;
_wearableComp = wearableComp;
}

public abstract ElementView CreateView();

protected void ReadCabinetConfig(out DTCabinet comp, out CabinetConfig config)
{
if (_avatarGameObject.TryGetComponent(out comp))
{
if (!CabinetConfigUtility.TryDeserialize(comp.ConfigJson, out config))
{
config = new CabinetConfig();
}
}
else
{
comp = null;
config = new CabinetConfig();
}
}

protected void WriteCabinetConfig(Action<DTCabinet, CabinetConfig> func)
{
CabinetConfig config;
if (_avatarGameObject.TryGetComponent<DTCabinet>(out var cabinetComp))
{
if (!CabinetConfigUtility.TryDeserialize(cabinetComp.ConfigJson, out config))
{
config = new CabinetConfig();
}
}
else
{
cabinetComp = _avatarGameObject.AddComponent<DTCabinet>();
config = new CabinetConfig();
}
func?.Invoke(cabinetComp, config);
cabinetComp.ConfigJson = CabinetConfigUtility.Serialize(config);
}

protected void ReadWearableConfig(out WearableConfig config)
{
if (!WearableConfigUtility.TryDeserialize(_wearableComp.ConfigJson, out config))
{
config = null;
}
}

protected void ReadWearableModule<T>(out T module) where T : IModuleConfig
{
ReadWearableConfig(out var config);
if (config == null)
{
module = default;
return;
}
module = config.FindModuleConfig<T>();
}

protected void WriteWearableConfig(Action<WearableConfig> func)
{
if (!WearableConfigUtility.TryDeserialize(_wearableComp.ConfigJson, out var config))
{
return;
}
func?.Invoke(config);
_wearableComp.ConfigJson = WearableConfigUtility.Serialize(config);
}

protected void WriteWearableModule<T>(Action<T> func) where T : IModuleConfig
{
WriteWearableConfig((config) =>
{
var module = config.FindModuleConfig<T>();
if (module == null)
{
return;
}
func?.Invoke(module);
});
}
}
}
11 changes: 11 additions & 0 deletions Editor/Configurator/Modules/OneConfModuleBase.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6e2fd73

Please sign in to comment.