Skip to content

Commit 90e4851

Browse files
committed
Some refactoring for the ink player window. Prevent it from force-showing itself on recompile.
1 parent b056416 commit 90e4851

File tree

1 file changed

+57
-44
lines changed

1 file changed

+57
-44
lines changed

Packages/Ink/Editor/Tools/Player Window/InkPlayerWindow.cs

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,11 @@ public class InkPlayerWindow : EditorWindow {
5050
// Create or get the window. If creating, dock it on the same panel as the inspector.
5151
[MenuItem("Window/Ink Player %#i", false, 2300)]
5252
public static InkPlayerWindow GetWindow () {
53-
System.Type windowType = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.InspectorWindow");
54-
return GetWindow<InkPlayerWindow>(windowTitle, true, windowType);
53+
return GetWindow(true);
5554
}
5655

5756
public static InkPlayerWindow GetWindow (bool focus) {
58-
System.Type windowType = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.InspectorWindow");
57+
Type windowType = typeof(Editor).Assembly.GetType("UnityEditor.InspectorWindow");
5958
return GetWindow<InkPlayerWindow>(windowTitle, focus, windowType);
6059
}
6160

@@ -64,8 +63,8 @@ public static InkPlayerWindow GetWindow (bool focus) {
6463

6564

6665
// Entry point for loading and playing a story.
67-
public static void LoadAndPlay (TextAsset storyJSONTextAsset) {
68-
GetWindow();
66+
public static void LoadAndPlay (TextAsset storyJSONTextAsset, bool focusWindow = true) {
67+
GetWindow(focusWindow);
6968
if(InkPlayerWindow.story != null) {
7069
if(EditorUtility.DisplayDialog("Story in progress", "The Ink Player Window is already playing a story. Would you like to stop it and load the new story?", "Stop and load", "Cancel")) {
7170
InkPlayerWindow.Stop();
@@ -76,8 +75,8 @@ public static void LoadAndPlay (TextAsset storyJSONTextAsset) {
7675
}
7776
}
7877

79-
public static void LoadAndPlay (string storyJSON) {
80-
GetWindow();
78+
public static void LoadAndPlay (string storyJSON, bool focusWindow = true) {
79+
GetWindow(focusWindow);
8180
if(InkPlayerWindow.story != null) {
8281
if(EditorUtility.DisplayDialog("Story in progress", "The Ink Player Window is already playing a story. Would you like to stop it and load the new story?", "Stop and load", "Cancel")) {
8382
InkPlayerWindow.Stop();
@@ -98,7 +97,7 @@ public static bool GetGameWindowIsMaximised () {
9897

9998
// Loads an existing story to the player window. Handy for debugging stories running in games in editor.
10099
public static void Attach (Story story) {
101-
Attach(story, InkPlayerWindow.InkPlayerParams.ForAttachedStories);
100+
Attach(story, InkPlayerWindow.InkPlayerParams.DisableInteraction);
102101
}
103102
public static void Attach (Story story, InkPlayerParams inkPlayerParams) {
104103
Clear();
@@ -133,7 +132,7 @@ public static void Detach () {
133132
/// <param name="story">Story.</param>
134133
/// <param name="label">Label.</param>
135134
public static void DrawStoryPropertyField (Story story, ref bool expanded, GUIContent label) {
136-
DrawStoryPropertyField(story, InkPlayerParams.ForAttachedStories, ref expanded, label);
135+
DrawStoryPropertyField(story, InkPlayerParams.DisableInteraction, ref expanded, label);
137136
}
138137
public static void DrawStoryPropertyField (Story story, InkPlayerParams playerParams, ref bool expanded, GUIContent label, bool interactable = false) {
139138
EditorGUILayout.BeginHorizontal();
@@ -254,7 +253,7 @@ public static InkPlayerParams Standard {
254253
return new InkPlayerParams();
255254
}
256255
}
257-
public static InkPlayerParams ForAttachedStories {
256+
public static InkPlayerParams DisableInteraction {
258257
get {
259258
var inkPlayerParams = new InkPlayerParams();
260259
inkPlayerParams.disablePlayControls = true;
@@ -417,8 +416,9 @@ static TextAsset storyJSONTextAsset {
417416

418417

419418

420-
419+
// This tracks the story state each time the user advances the story so that we can undo/redo
421420
static UndoHistory<InkPlayerHistoryItem> storyStateHistory = new UndoHistory<InkPlayerHistoryItem>();
421+
// This tracks the story output so we can view it in the content panel
422422
static List<InkHistoryContentItem> storyHistory = new List<InkHistoryContentItem>();
423423

424424

@@ -642,7 +642,7 @@ public enum AutoScrollSmoothingMode {
642642
void OnEnable () {
643643
if(isOpen) return;
644644
isOpen = true;
645-
645+
646646
InkPlayerWindowState.OnCreateOrLoad += () => {
647647
// InkPlayerWindowState.Instance.functionPanelState.functionParams = InkPlayerWindowState.Instance.functionParams;
648648
BuildFunctionInputList();
@@ -656,7 +656,7 @@ void OnEnable () {
656656
var lastLoadedStory = InkPlayerWindowState.Instance.TryGetLastStoryJSONAsset();
657657
if(lastLoadedStory != null) {
658658
if(InkPlayerWindowState.Instance.lastStoryWasPlaying) {
659-
LoadAndPlay(lastLoadedStory);
659+
LoadAndPlay(lastLoadedStory, false);
660660
} else {
661661
TryPrepareInternal(lastLoadedStory);
662662
}
@@ -682,6 +682,7 @@ void OnBecameInvisible () {
682682

683683
void OnDestroy () {
684684
isOpen = false;
685+
Clear();
685686
}
686687

687688
private static void Update () {
@@ -866,24 +867,36 @@ static bool TryPrepareInternal (string newStoryJSON) {
866867
}
867868

868869
static void OnUnsetStory () {
870+
// Unsubscribe from all story events we subscribed to
869871
_story.onDidContinue -= OnDidContinue;
870872
_story.onMakeChoice -= OnMakeChoice;
871873
_story.onEvaluateFunction -= OnEvaluateFunction;
872874
_story.onCompleteEvaluateFunction -= OnCompleteEvaluateFunction;
873875
_story.onChoosePathString -= OnChoosePathString;
874876
_story.state.onDidLoadState -= OnLoadState;
875-
foreach(var observedVariableName in InkPlayerWindowState.Instance.observedVariablesPanelState.restorableObservedVariableNames) {
877+
878+
// Clear any exceptions related to the story we were storing
879+
playStoryException = null;
880+
881+
// Clear the history
882+
ClearStoryHistory();
883+
884+
// Unobserve all observed variables.
885+
foreach(var observedVariableName in InkPlayerWindowState.Instance.observedVariablesPanelState.restorableObservedVariableNames)
876886
UnobserveVariable(observedVariableName, false);
877-
}
878887
InkPlayerWindowState.Instance.observedVariablesPanelState.observedVariables.Clear();
879-
888+
880889
InkPlayerWindowState.Instance.lastStoryWasPlaying = false;
890+
881891
InkPlayerWindowState.Save();
882892
}
883893

884894

885895
static void OnSetStory () {
896+
// Allow function fallbacks so we can subscribe to them and avoid throwing errors.
886897
_story.allowExternalFunctionFallbacks = true;
898+
899+
// Subscribe to all story events we'll use
887900
_story.onDidContinue += OnDidContinue;
888901
_story.onMakeChoice += OnMakeChoice;
889902
_story.onEvaluateFunction += OnEvaluateFunction;
@@ -940,13 +953,15 @@ static void PingAutomator () {
940953
static void Stop () {
941954
Clear ();
942955
}
943-
956+
957+
// This function's role isn't clear. It's used both when restarting a story and when clearing it entirely. We should probably have two separate functions.
944958
static void Clear () {
945-
if(storyStateHistory != null) storyStateHistory.Clear();
946-
if(storyHistory != null) storyHistory.Clear();
959+
// InkPlayerWindowState.Instance.observedVariablesPanelState.restorableObservedVariableNames.Clear();
960+
961+
// Clear the variable panel search
962+
RefreshVisibleVariables();
963+
947964
story = null;
948-
RefreshVisibleHistory();
949-
RefreshVisibleVariables();
950965
}
951966

952967
static void Restart () {
@@ -984,6 +999,27 @@ static void AddToStateHistory () {
984999
storyStateHistory.AddToUndoHistory(historyItem);
9851000
}
9861001

1002+
static void ClearStoryHistory () {
1003+
storyHistory.Clear();
1004+
storyStateHistory.Clear();
1005+
RefreshVisibleHistory();
1006+
ScrollToBottom();
1007+
}
1008+
1009+
static void CopyStoryHistoryToClipboard () {
1010+
StringBuilder sb = new StringBuilder("Story Log\n");
1011+
foreach(InkHistoryContentItem content in storyHistory) {
1012+
sb.AppendLine();
1013+
sb.Append(content.time.ToShortDateString());
1014+
sb.Append(" ");
1015+
sb.Append(content.time.ToLongTimeString());
1016+
sb.Append(" (");
1017+
sb.Append(content.contentType.ToString());
1018+
sb.Append(") ");
1019+
sb.Append(content.content);
1020+
}
1021+
GUIUtility.systemCopyBuffer = sb.ToString();
1022+
}
9871023
static void Undo () {
9881024
InkPlayerHistoryItem item = storyStateHistory.Undo();
9891025
story.state.LoadJson(item.inkStateJSON);
@@ -1143,8 +1179,6 @@ void DisplayHeader () {
11431179
if(EditorGUI.EndChangeCheck()) {
11441180
if(storyJSONTextAsset == null) {
11451181
story = null;
1146-
1147-
playStoryException = null;
11481182
} else {
11491183
Stop();
11501184
Play(storyJSONTextAsset);
@@ -1336,27 +1370,6 @@ void DrawVisibilityOptions () {
13361370
EditorGUILayout.EndHorizontal();
13371371
}
13381372

1339-
void ClearStoryHistory () {
1340-
storyHistory.Clear();
1341-
RefreshVisibleHistory();
1342-
ScrollToBottom();
1343-
}
1344-
1345-
void CopyStoryHistoryToClipboard () {
1346-
StringBuilder sb = new StringBuilder("Story Log\n");
1347-
foreach(InkHistoryContentItem content in storyHistory) {
1348-
sb.AppendLine();
1349-
sb.Append(content.time.ToShortDateString());
1350-
sb.Append(" ");
1351-
sb.Append(content.time.ToLongTimeString());
1352-
sb.Append(" (");
1353-
sb.Append(content.contentType.ToString());
1354-
sb.Append(") ");
1355-
sb.Append(content.content);
1356-
}
1357-
GUIUtility.systemCopyBuffer = sb.ToString();
1358-
}
1359-
13601373
static bool ShouldShowContentWithSearchString (string contentString, string searchString) {
13611374
if(StringContains(contentString, searchString, StringComparison.OrdinalIgnoreCase)) return true;
13621375
return false;

0 commit comments

Comments
 (0)