Skip to content

Commit

Permalink
s
Browse files Browse the repository at this point in the history
  • Loading branch information
poi-vrc committed May 2, 2024
1 parent 0d3503b commit 2514321
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 123 deletions.
29 changes: 27 additions & 2 deletions Editor/Configurator/Avatar/AvatarUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,28 @@

using System.Collections.Generic;
using Chocopoi.DressingFramework;
using Chocopoi.DressingTools.Components.Cabinet;
using Chocopoi.DressingTools.Components.OneConf;
using Chocopoi.DressingTools.Configurator.Cabinet;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Chocopoi.DressingTools.Configurator.Avatar
{
internal static class AvatarUtils
{
public static List<GameObject> FindSceneAvatars(Scene scene)
{
return DKRuntimeUtils.FindSceneAvatars(scene);
}

public static IAvatarSettings GetAvatarSettings(GameObject avatarGameObject)
{
if (avatarGameObject == null)
{
return null;
}

if (avatarGameObject.TryGetComponent<DTCabinet>(out _))
{
return new OneConfAvatarSettings(avatarGameObject);
Expand All @@ -30,9 +42,22 @@ public static IAvatarSettings GetAvatarSettings(GameObject avatarGameObject)
return null;
}

public static List<GameObject> FindSceneAvatars(Scene scene)
public static IWardrobeProvider GetWardrobeProvider(GameObject avatarGameObject)
{
return DKRuntimeUtils.FindSceneAvatars(scene);
if (avatarGameObject == null)
{
return null;
}

if (avatarGameObject.TryGetComponent<DTCabinet>(out _))
{
return new OneConfCabinetProvider(avatarGameObject);
}
if (avatarGameObject.TryGetComponent<DTWardrobe>(out _))
{
return new DTWardrobeProvider(avatarGameObject);
}
return null;
}
}
}
3 changes: 2 additions & 1 deletion Editor/Configurator/Cabinet/DTConfigurableOutfit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Chocopoi.DressingTools.Configurator.Modules;
using Chocopoi.DressingTools.UI.Views;
using UnityEngine;
using UnityEngine.UIElements;

namespace Chocopoi.DressingTools.Configurator.Cabinet
{
Expand All @@ -31,7 +32,7 @@ public DTConfigurableOutfit(DTAlternateOutfit outfitComp)
_outfitComp = outfitComp;
}

public ElementView CreateView()
public VisualElement CreateView()
{
throw new System.NotImplementedException();
}
Expand Down
11 changes: 9 additions & 2 deletions Editor/Configurator/Cabinet/DTWardrobeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ namespace Chocopoi.DressingTools.Configurator.Cabinet
{
internal class DTWardrobeProvider : IWardrobeProvider
{
public List<IConfigurableOutfit> GetOutfitsInAvatars(GameObject avatar)
private readonly GameObject _avatarGameObject;

public DTWardrobeProvider(GameObject avatarGameObject)
{
_avatarGameObject = avatarGameObject;
}

public List<IConfigurableOutfit> GetOutfits()
{
var comps = avatar.GetComponentsInChildren<DTAlternateOutfit>(true);
var comps = _avatarGameObject.GetComponentsInChildren<DTAlternateOutfit>(true);
var outfits = new List<IConfigurableOutfit>();
foreach (var comp in comps)
{
Expand Down
4 changes: 2 additions & 2 deletions Editor/Configurator/Cabinet/IConfigurableOutfit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

using System.Collections.Generic;
using Chocopoi.DressingTools.Configurator.Modules;
using Chocopoi.DressingTools.UI.Views;
using UnityEngine;
using UnityEngine.UIElements;

namespace Chocopoi.DressingTools.Configurator.Cabinet
{
Expand All @@ -24,6 +24,6 @@ internal interface IConfigurableOutfit
Texture2D Icon { get; }

List<IModule> GetModules();
ElementView CreateView();
VisualElement CreateView();
}
}
3 changes: 2 additions & 1 deletion Editor/Configurator/Cabinet/IWardrobeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Chocopoi.DressingTools.Configurator.Cabinet
{
internal interface IWardrobeProvider
{
List<IConfigurableOutfit> GetOutfitsInAvatars(GameObject avatar);
List<IConfigurableOutfit> GetOutfits();
void RemoveOutfit(IConfigurableOutfit outfit);
}
}
18 changes: 15 additions & 3 deletions Editor/Configurator/Cabinet/OneConfCabinetProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,27 @@ namespace Chocopoi.DressingTools.Configurator.Cabinet
{
internal class OneConfCabinetProvider : IWardrobeProvider
{
public List<IConfigurableOutfit> GetOutfitsInAvatars(GameObject avatarGameObject)
private readonly GameObject _avatarGameObject;

public OneConfCabinetProvider(GameObject avatarGameObject)
{
_avatarGameObject = avatarGameObject;
}

public List<IConfigurableOutfit> GetOutfits()
{
var wearables = OneConfUtils.GetCabinetWearables(avatarGameObject);
var wearables = OneConfUtils.GetCabinetWearables(_avatarGameObject);
var outfits = new List<IConfigurableOutfit>();
foreach (var wearable in wearables)
{
outfits.Add(new OneConfConfigurableOutfit(avatarGameObject, wearable));
outfits.Add(new OneConfConfigurableOutfit(_avatarGameObject, wearable));
}
return outfits;
}

public void RemoveOutfit(IConfigurableOutfit outfit)
{
throw new System.NotImplementedException();
}
}
}
3 changes: 2 additions & 1 deletion Editor/Configurator/Cabinet/OneConfConfigurableOutfit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Chocopoi.DressingTools.OneConf.Wearable.Modules.BuiltIn;
using Chocopoi.DressingTools.UI.Views;
using UnityEngine;
using UnityEngine.UIElements;

namespace Chocopoi.DressingTools.Configurator.Cabinet
{
Expand Down Expand Up @@ -52,7 +53,7 @@ public OneConfConfigurableOutfit(GameObject avatarGameObject, DTWearable wearabl
_wearableComp = wearableComp;
}

public ElementView CreateView()
public VisualElement CreateView()
{
throw new System.NotImplementedException();
}
Expand Down
145 changes: 34 additions & 111 deletions Editor/UI/Presenters/AvatarPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
* You should have received a copy of the GNU General Public License along with DressingTools. If not, see <https://www.gnu.org/licenses/>.
*/

using System.Collections.Generic;
using Chocopoi.DressingFramework.Localization;
using Chocopoi.DressingTools.Configurator.Avatar;
using Chocopoi.DressingTools.Localization;
using Chocopoi.DressingTools.UI.Views;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Chocopoi.DressingTools.UI.Presenters
{
Expand Down Expand Up @@ -86,77 +89,32 @@ private void OnSelectedCabinetChange()

private void OnAvatarSettingsChange()
{
var cabinets = OneConfUtils.GetAllCabinets();

if (cabinets.Length == 0)
{
_view.ShowCreateCabinetBackButton = false;
_view.ShowCreateCabinetPanel = true;
return;
}
_view.ShowCreateCabinetBackButton = true;

var cabinet = cabinets[_view.SelectedAvatarIndex];

cabinet.RootGameObject = _view.CabinetAvatarGameObject;

if (_cabinetConfig == null)
var settings = AvatarUtils.GetAvatarSettings(_view.SelectedAvatarGameObject);
if (settings == null)
{
Debug.LogWarning("[DressingTools] Cabinet config is uninitialized from UI but cabinet settings changed.");
return;
}

_cabinetConfig.avatarArmatureName = _view.CabinetAvatarArmatureName;
_cabinetConfig.groupDynamics = _view.CabinetGroupDynamics;
_cabinetConfig.groupDynamicsSeparateGameObjects = _view.CabinetGroupDynamicsSeparateGameObjects;
_cabinetConfig.animationWriteDefaultsMode = (CabinetConfig.WriteDefaultsMode)_view.SettingsAnimationWriteDefaultsMode;

var cabAnimConfig = _cabinetConfig.FindModuleConfig<CabinetAnimCabinetModuleConfig>();
if (cabAnimConfig == null)
{
cabAnimConfig = new CabinetAnimCabinetModuleConfig();
_cabinetConfig.modules.Add(new CabinetModule()
{
config = cabAnimConfig,
moduleName = CabinetAnimCabinetModuleConfig.ModuleIdentifier
});
}

cabAnimConfig.thumbnails = _view.CabinetUseThumbnailsAsMenuIcons;
cabAnimConfig.menuInstallPath = _view.CabinetMenuInstallPath;
cabAnimConfig.menuItemName = _view.CabinetMenuItemName;
cabAnimConfig.networkSynced = _view.CabinetNetworkSynced;
cabAnimConfig.saved = _view.CabinetSaved;
cabAnimConfig.resetCustomizablesOnSwitch = _view.CabinetResetCustomizablesOnSwitch;

cabinet.ConfigJson = CabinetConfigUtility.Serialize(_cabinetConfig);
// TODO: explicitly convert one by one
settings.WriteDefaultsMode = (WriteDefaultsModes)_view.SettingsAnimationWriteDefaultsMode;
}

private void OnForceUpdateView()
{
UpdateView();
}

public void SelectCabinet(DTCabinet cabinet)
public void SelectAvatar(GameObject avatarGameObject)
{
var cabinets = OneConfUtils.GetAllCabinets();

if (cabinets.Length == 0)
{
_view.ShowCreateCabinetBackButton = false;
_view.ShowCreateCabinetPanel = true;
return;
}
_view.ShowCreateCabinetBackButton = true;
_view.ShowCreateCabinetPanel = false;
var avatars = AvatarUtils.FindSceneAvatars(SceneManager.GetActiveScene());

// refresh the keys first
UpdateCabinetSelectionDropdown(cabinets);
UpdateAvatarSelectionDropdown(avatars);

// find matching index
for (var i = 0; i < cabinets.Length; i++)
for (var i = 0; i < avatars.Count; i++)
{
if (cabinets[i] == cabinet)
if (avatars[i] == avatarGameObject)
{
_view.SelectedAvatarIndex = i;
break;
Expand All @@ -167,93 +125,58 @@ public void SelectCabinet(DTCabinet cabinet)
UpdateView();
}

private void UpdateCabinetSelectionDropdown(DTCabinet[] cabinets)
private void UpdateAvatarSelectionDropdown(List<GameObject> avatars)
{
// cabinet selection dropdown
// avatar selection dropdown
_view.AvailableAvatarSelections.Clear();
for (var i = 0; i < cabinets.Length; i++)
for (var i = 0; i < avatars.Count; i++)
{
_view.AvailableAvatarSelections.Add(cabinets[i].RootGameObject != null ? cabinets[i].RootGameObject.name : t._("cabinet.editor.cabinetContent.popup.cabinetOptions.cabinetNameNoGameObjectAttached", i + 1));
_view.AvailableAvatarSelections.Add(avatars[i].name);
}
}

private void UpdateCabinetContentView()
private void UpdateAvatarContentView()
{
var cabinets = OneConfUtils.GetAllCabinets();

if (cabinets.Length == 0)
{
_view.ShowCreateCabinetBackButton = false;
_view.ShowCreateCabinetPanel = true;
return;
}
_view.ShowCreateCabinetBackButton = true;

UpdateCabinetSelectionDropdown(cabinets);
_view.InstalledOutfitPreviews.Clear();

if (_view.SelectedAvatarIndex < 0 || _view.SelectedAvatarIndex >= cabinets.Length)
var avatars = AvatarUtils.FindSceneAvatars(SceneManager.GetActiveScene());
UpdateAvatarSelectionDropdown(avatars);
if (_view.SelectedAvatarIndex < 0 || _view.SelectedAvatarIndex >= avatars.Count)
{
// invalid selected cabinet index, setting it back to 0
_view.SelectedAvatarIndex = 0;
}
_view.SelectedAvatarGameObject = avatars[_view.SelectedAvatarIndex];

// clear views
_view.InstalledOutfitPreviews.Clear();

// update selected cabinet view
var cabinet = cabinets[_view.SelectedAvatarIndex];

// cabinet json is broken, ask user whether to make a new one or not
if (!CabinetConfigUtility.TryDeserialize(cabinet.ConfigJson, out _cabinetConfig) || !_cabinetConfig.IsValid())
{
Debug.LogWarning("[DressingTools] [CabinetPresenter] Unable to deserialize cabinet config or invalid configuration! Using new config instead");
_cabinetConfig = new CabinetConfig();
cabinet.ConfigJson = CabinetConfigUtility.Serialize(_cabinetConfig);
}

_view.CabinetAvatarGameObject = cabinet.RootGameObject;
_view.CabinetAvatarArmatureName = _cabinetConfig.avatarArmatureName;
_view.CabinetGroupDynamics = _cabinetConfig.groupDynamics;
_view.CabinetGroupDynamicsSeparateGameObjects = _cabinetConfig.groupDynamicsSeparateGameObjects;
_view.SettingsAnimationWriteDefaultsMode = (int)_cabinetConfig.animationWriteDefaultsMode;
UpdateCabinetAnimationConfig();
// TODO: explicitly convert one by one
var settings = AvatarUtils.GetAvatarSettings(_view.SelectedAvatarGameObject);
_view.SettingsAnimationWriteDefaultsMode = (int)settings.WriteDefaultsMode;

var wearables = OneConfUtils.GetCabinetWearables(cabinet.RootGameObject);
var wardrobe = AvatarUtils.GetWardrobeProvider(_view.SelectedAvatarGameObject);
var outfits = wardrobe.GetOutfits();

foreach (var wearable in wearables)
foreach (var outfit in outfits)
{
var config = WearableConfigUtility.Deserialize(wearable.ConfigJson);
_view.InstalledOutfitPreviews.Add(new OutfitPreview()
{
name = config != null ?
config.info.name :
t._("cabinet.editor.cabinetContent.wearablePreview.name.unableToLoadConfiguration"),
thumbnail = config != null && config.info.thumbnail != null ?
OneConfUtils.GetTextureFromBase64(config.info.thumbnail) :
null,
name = outfit.Name,
thumbnail = outfit.Icon,
RemoveButtonClick = () =>
{
if (wearable is DTWearable dtWearable)
{
cabinet.RemoveWearable(dtWearable);
UpdateView();
}
else
{
Debug.LogWarning("[DressingTools] Removing non-DressingTools wearable is not currently supported");
}
wardrobe.RemoveOutfit(outfit);
UpdateView();
},
EditButtonClick = () =>
{
_view.StartDressing(cabinet.RootGameObject, wearable.RootGameObject);
_view.StartDressing(_view.SelectedAvatarGameObject, outfit.RootTransform.gameObject);
}
});
}
}

private void UpdateView()
{
UpdateCabinetContentView();
UpdateAvatarContentView();
_view.Repaint();
}

Expand Down
1 change: 1 addition & 0 deletions Editor/UI/Views/AvatarSubView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Chocopoi.DressingTools.Localization;
using Chocopoi.DressingTools.UI.Presenters;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

Expand Down

0 comments on commit 2514321

Please sign in to comment.