Skip to content

Commit

Permalink
Scene Assistant will now attempt to save and retrieve the last select…
Browse files Browse the repository at this point in the history
…ed object in the Id dropdown
  • Loading branch information
idaocracy committed Aug 8, 2023
1 parent ef58b0c commit b3caaf8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 31 deletions.
35 changes: 26 additions & 9 deletions Editor/SceneAssistantEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace NaninovelSceneAssistant
public partial class SceneAssistantEditor : EditorWindow
{
private static SceneAssistantEditor sceneAssistantEditor;
public string[] Tabs { get; protected set; } = new string[] { "Objects", "Variables", "Unlockables", "Scripts" };
public static string[] Tabs { get; protected set; } = new string[] { "Objects", "Variables", "Unlockables", "Scripts" };
protected static INaninovelObjectData CurrentObject => sceneAssistantManager.ObjectList.Values?.ToArray()[objectIndex];
protected static string ClipboardString { get => clipboardString; set { clipboardString = value; EditorGUIUtility.systemCopyBuffer = value; if (logResults) Debug.Log(value); } }
protected ScriptImporterEditor[] VisualEditors => Resources.FindObjectsOfTypeAll<ScriptImporterEditor>();
protected bool ExcludeState { get => CommandParameterData.ExcludeState; set => CommandParameterData.ExcludeState = value; }
protected bool ExcludeDefault { get => CommandParameterData.ExcludeDefault; set => CommandParameterData.ExcludeDefault = value; }
protected static ScriptImporterEditor[] VisualEditors => Resources.FindObjectsOfTypeAll<ScriptImporterEditor>();
protected static bool ExcludeState { get => CommandParameterData.ExcludeState; set => CommandParameterData.ExcludeState = value; }
protected static bool ExcludeDefault { get => CommandParameterData.ExcludeDefault; set => CommandParameterData.ExcludeDefault = value; }

private static SceneAssistantManager sceneAssistantManager;
private static IScriptPlayer scriptPlayer;
Expand All @@ -36,6 +36,9 @@ public partial class SceneAssistantEditor : EditorWindow
private static bool logResults;
private static bool defaultRollbackValue;
private static bool disableRollback;

private static int lastIndex = 0;
private static string lastObject;

[MenuItem("Naninovel/Scene Assistant", false, 360)]
public static void ShowWindow()
Expand Down Expand Up @@ -65,8 +68,8 @@ private static void SetupAndInitializeSceneAssistant()

if(sceneAssistantManager.Initialized) return;
sceneAssistantManager.InitializeSceneAssistant();
sceneAssistantManager.OnSceneAssistantCleared += HandleSceneAssistantUpdate;
sceneAssistantManager.OnSceneAssistantReset += HandleSceneAssistantUpdate;
sceneAssistantManager.OnSceneAssistantCleared += HandleSceneAssistantCleared;
sceneAssistantManager.OnSceneAssistantReset += HandleSceneAssistantReset;

defaultRollbackValue = inputManager.GetRollback().Enabled;
}
Expand All @@ -76,16 +79,30 @@ private void OnDestroy()
if (sceneAssistantManager != null && sceneAssistantManager.Initialized)
{
sceneAssistantManager.DestroySceneAssistant();
sceneAssistantManager.OnSceneAssistantCleared -= HandleSceneAssistantUpdate;
sceneAssistantManager.OnSceneAssistantReset -= HandleSceneAssistantUpdate;
sceneAssistantManager.OnSceneAssistantCleared -= HandleSceneAssistantCleared;
sceneAssistantManager.OnSceneAssistantReset -= HandleSceneAssistantReset;
}
}

private static void HandleSceneAssistantUpdate()
private static void HandleSceneAssistantCleared()
{
lastObject = CurrentObject.Id;
lastIndex = objectIndex;
objectIndex = 0;
sceneAssistantEditor.Repaint();
}


private static void HandleSceneAssistantReset()
{
if(!string.IsNullOrEmpty(lastObject))
{
if(sceneAssistantManager.ObjectList.Keys.ElementAt(lastIndex) == lastObject) objectIndex = lastIndex;
}
else objectIndex = 0;

sceneAssistantEditor.Repaint();
}

public void OnGUI()
{
Expand Down
22 changes: 10 additions & 12 deletions Runtime/ObjectData/Data/ActorData/OrthoActorData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public OrthoActorData(string id) : base(id) {}

protected override float? DefaultZOffset => Config.ZOffset;

protected virtual async UniTask<IReadOnlyCollection<string>> GetOrthoAppearanceList()
protected virtual async UniTask<string[]> GetOrthoAppearanceList()
{
var resourceProviderManager = Engine.GetService<IResourceProviderManager>();
var providers = resourceProviderManager.GetProviders(Metadata.Loader.ProviderTypes);
Expand All @@ -43,7 +43,7 @@ protected virtual async UniTask<IReadOnlyCollection<string>> GetOrthoAppearanceL
else if(Actor is VideoCharacter || Actor is VideoBackground) return await LocateResourcesAtPathAsync<VideoClip>();
else return await LocateResourcesAtPathAsync<UnityEngine.Object>();

async UniTask <IReadOnlyCollection<string>> LocateResourcesAtPathAsync<T>() where T: UnityEngine.Object
async UniTask <string[]> LocateResourcesAtPathAsync<T>() where T: UnityEngine.Object
{
var actorPath = Metadata.Loader.PathPrefix + "/" + Id;
var resourcePaths = await providers.LocateResourcesAsync<T>(actorPath);
Expand All @@ -54,32 +54,30 @@ protected virtual async UniTask<IReadOnlyCollection<string>> GetOrthoAppearanceL
var appearance = path.Remove(actorPath + "/");
appearances.Add(appearance);
}
return appearances;
return appearances.ToArray();
}
}

protected override void GetAppearanceData()
{
var appearances = GetOrthoAppearanceList().Result;

if(appearances.Count > 0)
if(appearances.Length > 0)
{
CommandParameters.Add(new CommandParameterData<string>(Appearance, () => Actor.Appearance ?? GetDefaultAppearance(), v => Actor.Appearance = (string)v, (i, p) => i.StringDropdownField(p, appearances.ToArray()), defaultValue: GetDefaultAppearance()));
CommandParameters.Add(new CommandParameterData<string>(Appearance, () => Actor.Appearance ?? GetDefaultAppearance(appearances), v => Actor.Appearance = (string)v, (i, p) => i.StringDropdownField(p, appearances), defaultValue: GetDefaultAppearance(appearances)));
}
else CommandParameters.Add(new CommandParameterData<string>(Appearance, () => Actor.Appearance, v => Actor.Appearance = (string)v, (i, p) => i.StringField(p)));

}

protected string GetDefaultAppearance()
protected string GetDefaultAppearance(string[] appearances)
{
var appearancePaths = GetOrthoAppearanceList().Result;

if (appearancePaths != null && appearancePaths.Count > 0)
if (appearances != null && appearances.Length > 0)
{
if (appearancePaths.Any(t => t.EqualsFast(Id))) return appearancePaths.First(t => t.EqualsFast(Id));
if (appearancePaths.Any(t => t.EqualsFast("Default"))) return appearancePaths.First(t => t.EqualsFast("Default"));
if (appearances.Any(t => t.EqualsFast(Id))) return appearances.First(t => t.EqualsFast(Id));
if (appearances.Any(t => t.EqualsFast("Default"))) return appearances.First(t => t.EqualsFast("Default"));
}
return appearancePaths.FirstOrDefault();
return appearances.FirstOrDefault();
}

#if UNITY_EDITOR
Expand Down
2 changes: 1 addition & 1 deletion Runtime/ObjectData/Data/ActorData/TextPrinterData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override void GetAppearanceData()
{
#if UNITY_EDITOR
var appearances = GetPrinterAppearances();
if(appearances.Length > 0)
if(appearances != null && appearances.Length > 0)
CommandParameters.Add(new CommandParameterData<string>(Appearance, () => Actor.Appearance ?? appearances[0], v => Actor.Appearance = (string)v, (i, p) => i.StringDropdownField(p, appearances), defaultValue: appearances[0]));
#else
CommandParameters.Add(new CommandParameterData<string>(Appearance, () => Actor.Appearance, v => Actor.Appearance = (string)v, (i, p) => i.StringField(p)));
Expand Down
40 changes: 31 additions & 9 deletions Runtime/UI/Menus/SceneAssistant/SceneAssistantMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class SceneAssistantMenu : SceneAssistantWindowMenu, ISceneAssistantLayou
public List<ObjectTypeToggle> ObjectTypeToggles { get => objectTypeToggleContainer.GetComponentsInChildren<ObjectTypeToggle>().ToList(); }
public INaninovelObjectData CurrentObject { get; protected set; }

private static int lastIndex;
private static string lastObject;

public TMP_InputField CopyBufferField => copyBufferField;

private Transform targetContainer;
Expand Down Expand Up @@ -74,11 +77,38 @@ protected override void OnDisable()
commandNameField.onSubmit.RemoveListener(SaveCommandString);
}

protected override void ClearMenu()
{
if(CurrentObject != null)
{
lastIndex = idDropdown.value;
lastObject = idDropdown.options.ElementAt(lastIndex).text;
}

idDropdown.ClearOptions();
ClearParameterFields();
ClearToggles();
saveInfoBox.text = String.Empty;
}

protected override void ResetMenu()
{
idDropdown.AddOptions(Manager.ObjectList.Keys.Select(v => new TMP_Dropdown.OptionData(v)).ToList());
ResetToggles();
DisplayObjectParameters(0);
//DisplayObjectParameters(0);
if(!string.IsNullOrEmpty(lastObject))
{
if(Manager.ObjectList.Keys.ElementAt(lastIndex) == lastObject)
{
idDropdown.value = lastIndex;
DisplayObjectParameters(lastIndex);
}
}
else
{
idDropdown.value = 0;
DisplayObjectParameters(0);
}
}

private void ResetToggles()
Expand All @@ -98,14 +128,6 @@ protected void DisplayObjectParameters(int index)
saveInfoBox.text = String.Empty;
}

protected override void ClearMenu()
{
idDropdown.ClearOptions();
ClearParameterFields();
ClearToggles();
saveInfoBox.text = String.Empty;
}

protected void ClearParameterFields()
{
foreach (var field in DataFields) Destroy(field.GameObject);
Expand Down

0 comments on commit b3caaf8

Please sign in to comment.