A game about forced loneliness, made by TACStudios
1#if UNITY_EDITOR
2
3using System;
4using UnityEditor;
5
6namespace UnityEngine.InputSystem.Editor
7{
8 // Input system UI dialogs centralized as utility methods.
9 // In the future we may introduce possibility to mock/stub these dialogs via delegates to allow
10 // automated testing of dialog options and secure that no dialogs are shown in editor testing.
11 internal static class Dialog
12 {
13 // Represents the result of the user selecting a dialog option.
14 public enum Result
15 {
16 // User decided that unsaved changes should be saved to the associated asset.
17 Save = 0,
18
19 // Operation was cancelled by the user.
20 Cancel = 1,
21
22 // Unsaved changes should be discarded and NOT be saved to the associated asset.
23 Discard = 2,
24
25 // User decided to delete the associated resource.
26 Delete = 3,
27 }
28
29 // User UI dialog windows related to InputActionAssets
30 public static class InputActionAsset
31 {
32 #region Save Changes Dialog
33
34 private static Func<string, Result> saveChanges = DefaultSaveChanges;
35
36 internal static void SetSaveChanges(Func<string, Result> dialog)
37 {
38 saveChanges = dialog ?? DefaultSaveChanges;
39 }
40
41 private static Result DefaultSaveChanges(string path)
42 {
43 var id = EditorUtility.DisplayDialogComplex(
44 title: "Input Action Asset has been modified",
45 message: $"Do you want to save the changes you made in:\n{path}\n\nYour changes will be lost if you don't save them.",
46 ok: "Save",
47 cancel: "Cancel",
48 alt: "Don't Save");
49 switch (id)
50 {
51 case 0: return Result.Save;
52 case 1: return Result.Cancel;
53 case 2: return Result.Discard;
54 default: throw new ArgumentOutOfRangeException(nameof(id));
55 }
56 }
57
58 // Shows a dialog prompting the user to save or discard unsaved changes.
59 // May return Result.Save, Result.Cancel or Result.Discard.
60 public static Result ShowSaveChanges(string path)
61 {
62 return saveChanges(path);
63 }
64
65 #endregion
66
67 #region Discard Unsaved Changes Dialog
68
69 private static Func<string, Result> discardUnsavedChanges = DefaultDiscardUnsavedChanges;
70
71 internal static void SetDiscardUnsavedChanges(Func<string, Result> dialog)
72 {
73 discardUnsavedChanges = dialog ?? DefaultDiscardUnsavedChanges;
74 }
75
76 private static Result DefaultDiscardUnsavedChanges(string path)
77 {
78 var pressedOkButton = EditorUtility.DisplayDialog(
79 title: "Unsaved changes",
80 message:
81 $"You have unsaved changes for '{path}'. Do you want to discard the changes and delete the asset?",
82 ok: "Yes, Delete",
83 cancel: "No, Cancel");
84 return pressedOkButton ? Result.Discard : Result.Cancel;
85 }
86
87 // Shows a dialog prompting the user to discard changes or cancel the operation.
88 // May return Result.Discard or Result.Cancel.
89 public static Result ShowDiscardUnsavedChanges(string path)
90 {
91 return discardUnsavedChanges(path);
92 }
93
94 #endregion
95
96 #region Create and overwrite existing asset dialog
97
98 private static Func<string, Result>
99 createAndOverwriteExistingAsset = DefaultCreateAndOverwriteExistingAsset;
100
101 internal static void SetCreateAndOverwriteExistingAsset(Func<string, Result> dialog)
102 {
103 createAndOverwriteExistingAsset = dialog ?? DefaultCreateAndOverwriteExistingAsset;
104 }
105
106 private static Result DefaultCreateAndOverwriteExistingAsset(string path)
107 {
108 var pressedOkButton = EditorUtility.DisplayDialog(
109 title: "Create Input Action Asset",
110 message: $"This will overwrite the existing asset: '{path}'. Continue and overwrite?",
111 ok: "Ok",
112 cancel: "Cancel");
113 return pressedOkButton ? Result.Discard : Result.Cancel;
114 }
115
116 // Shows a dialog prompting the user whether the intention is to create an asset and overwrite the
117 // currently existing asset. May return Result.Discard to overwrite or Result.Cancel to cancel.
118 public static Result ShowCreateAndOverwriteExistingAsset(string path)
119 {
120 return createAndOverwriteExistingAsset(path);
121 }
122
123 #endregion
124 }
125
126 // User UI dialog windows related to InputControlSchemes
127 public static class ControlScheme
128 {
129 private static Func<string, Result> deleteControlScheme = DefaultDeleteControlScheme;
130
131 internal static void SetDeleteControlScheme(Func<string, Result> dialog)
132 {
133 deleteControlScheme = dialog ?? DefaultDeleteControlScheme;
134 }
135
136 private static Result DefaultDeleteControlScheme(string controlSchemeName)
137 {
138 // Ask for confirmation.
139 var pressedOkButton = EditorUtility.DisplayDialog("Delete scheme?",
140 message: $"Do you want to delete control scheme '{controlSchemeName}'?",
141 ok: "Delete",
142 cancel: "Cancel");
143 return pressedOkButton ? Result.Delete : Result.Cancel;
144 }
145
146 // Shows a dialog prompting the user to delete a control scheme or cancel the operation.
147 // May return Result.Delete or Result.Cancel.
148 public static Result ShowDeleteControlScheme(string controlSchemeName)
149 {
150 return deleteControlScheme(controlSchemeName);
151 }
152 }
153 }
154}
155
156#endif // UNITY_EDITOR