1- using PlayEveryWare . EpicOnlineServices . Editor . Utility ;
2- using System ;
3- using System . Threading . Tasks ;
4- using UnityEditor ;
5- using UnityEngine ;
1+ /*
2+ * Copyright (c) 2024 PlayEveryWare
3+ *
4+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5+ * of this software and associated documentation files (the "Software"), to deal
6+ * in the Software without restriction, including without limitation the rights
7+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+ * copies of the Software, and to permit persons to whom the Software is
9+ * furnished to do so, subject to the following conditions:
10+ *
11+ * The above copyright notice and this permission notice shall be included in all
12+ * copies or substantial portions of the Software.
13+ *
14+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+ * SOFTWARE.
21+ */
22+
623
724namespace PlayEveryWare . EpicOnlineServices . Editor . Windows
825{
9- public sealed class ClientCredentialsEncryptionKeyWindow : ModalInputWindow < string >
10- {
11- public ClientCredentialsEncryptionKeyWindow ( ) : base ( 600f , 50f )
12- {
13- }
14-
15- public static void Show (
16- string input ,
17- Action < string > onSubmitCallback ,
18- string windowTitle = "Client Credentials Encryption Key" ,
19- string inputPrompt = "Enter the encryption key for these client credentials here:" )
20- {
21- ScheduleShow < ClientCredentialsEncryptionKeyWindow > (
22- input ,
23- onSubmitCallback ,
24- EOSClientCredentials . IsEncryptionKeyValid ,
25- windowTitle ,
26- inputPrompt ,
27- "Invalid encryption key. Encryption key must be 64 characters long and contain only alphanumeric characters." ) ;
28- }
29-
30- protected override void RenderModalContents ( )
31- {
32- GUILayout . BeginHorizontal ( ) ;
33-
34- _input = GUILayout . TextField ( _input , GUILayout . Width ( GUIEditorUtility . MeasureLabelWidth ( 64 ) ) , GUILayout . Height ( 20 ) ) ;
35-
36- GUILayout . Space ( 5f ) ;
37-
38- if ( GUILayout . Button (
39- new GUIContent ( EditorGUIUtility . IconContent ( "Refresh" ) . image ,
40- "Click here to generate a new encryption key." ) , GUILayout . Height ( 20 ) , GUILayout . Width ( 50 ) ) )
41- {
42- _input = EOSClientCredentials . GenerateEncryptionKey ( ) ;
43- }
44- GUILayout . EndHorizontal ( ) ;
45- }
46- }
47-
48- public abstract class ModalInputWindow < TInputType > : EOSEditorWindow
26+ using PlayEveryWare . EpicOnlineServices . Editor . Utility ;
27+ using System ;
28+ using UnityEditor ;
29+ using UnityEngine ;
30+
31+ /// <summary>
32+ /// Represents an editor window that is modal in nature - it is designed to
33+ /// only provide input for a single value, and not be dismissed from focus
34+ /// unless canceled or closed.
35+ /// </summary>
36+ /// <typeparam name="TInputType">
37+ /// The type of value that the modal window is designed to edit.
38+ /// </typeparam>
39+ public abstract class ModalEOSEditorWindow < TInputType > : EOSEditorWindow
4940 {
41+ /// <summary>
42+ /// The value being edited.
43+ /// </summary>
5044 protected TInputType _input ;
45+
46+ /// <summary>
47+ /// The prompt to display indicating what the value means.
48+ /// </summary>
5149 private string _inputPrompt ;
50+
51+ /// <summary>
52+ /// The prompt to display if the input is not valid.
53+ /// </summary>
5254 private string _errorPrompt ;
55+
56+ /// <summary>
57+ /// The action to take when the modal window is submitted.
58+ /// </summary>
5359 private Action < TInputType > _onSubmit ;
60+
61+ /// <summary>
62+ /// The function used to validate the input.
63+ /// </summary>
5464 private Func < TInputType , bool > _validateFunction ;
65+
66+ /// <summary>
67+ /// Flag that indicates whether the error prompt should be displayed.
68+ /// </summary>
5569 private bool _showError ;
5670
5771 // Keep a reference to prevent focus loss
58- private static ModalInputWindow < TInputType > s_currentWindow ;
72+ private static ModalEOSEditorWindow < TInputType > s_currentWindow ;
5973
60- protected ModalInputWindow ( float width , float height ) : base ( "" , height , width , false )
74+ /// <summary>
75+ /// Constructs a noew modal eos editor window.
76+ /// </summary>
77+ /// <param name="windowTitle">Window title.</param>
78+ /// <param name="width">The fixed width of the window.</param>
79+ /// <param name="height">The fixed height of the window.</param>
80+ protected ModalEOSEditorWindow ( string windowTitle , float width , float height ) : base ( windowTitle , height , width , false )
6181 {
6282 }
6383
6484 /// <summary>
6585 /// Helper to schedule the showing of the modal window.
6686 /// </summary>
87+ /// <typeparam name="TWindowType">Type of value modal window is designed to edit.</typeparam>
88+ /// <param name="input">The value that the modal window provides editing of.</param>
89+ /// <param name="onSubmitCallback">The action that takes place when the modal window is submitted with a valid value.</param>
90+ /// <param name="validateFunction">Function used to validate the input.</param>
91+ /// <param name="inputPrompt">The prompt to display in the modal window.</param>
92+ /// <param name="errorPrompt">The error to display if the input value is invalid.</param>
6793 protected static void ScheduleShow < TWindowType > (
6894 TInputType input ,
6995 Action < TInputType > onSubmitCallback ,
7096 Func < TInputType , bool > validateFunction ,
71- string windowTitle ,
7297 string inputPrompt ,
7398 string errorPrompt
74- ) where TWindowType : ModalInputWindow < TInputType >
99+ ) where TWindowType : ModalEOSEditorWindow < TInputType >
75100 {
76101 void ShowAndUnsubscribe ( )
77102 {
78103 // Unsubscribe first to ensure it runs only once
79104 EditorApplication . update -= ShowAndUnsubscribe ;
80105
81106 // Open the modal window
82- ShowWindow < TWindowType > ( input , onSubmitCallback , validateFunction , windowTitle , inputPrompt , errorPrompt ) ;
107+ ShowWindow < TWindowType > ( input , onSubmitCallback , validateFunction , inputPrompt , errorPrompt ) ;
83108 }
84109
85110 // Subscribe the delegate to execute on the next frame
@@ -90,19 +115,17 @@ public static void ShowWindow<TWindowType>(
90115 TInputType input ,
91116 Action < TInputType > onSubmitCallback ,
92117 Func < TInputType , bool > validateFunction ,
93- string windowTitle ,
94118 string inputPrompt ,
95- string errorPrompt ) where TWindowType : ModalInputWindow < TInputType >
119+ string errorPrompt ) where TWindowType : ModalEOSEditorWindow < TInputType >
96120 {
97121 if ( s_currentWindow != null )
98122 {
99123 s_currentWindow . Focus ( ) ;
100124 return ;
101125 }
102126
103- ModalInputWindow < TInputType > window = CreateInstance < TWindowType > ( ) ;
127+ ModalEOSEditorWindow < TInputType > window = CreateInstance < TWindowType > ( ) ;
104128 window . _input = input ;
105- window . WindowTitle = windowTitle ;
106129 window . _inputPrompt = inputPrompt ;
107130 window . _onSubmit = onSubmitCallback ;
108131 window . _validateFunction = validateFunction ;
@@ -155,35 +178,45 @@ protected override void OnDestroy()
155178 base . OnDestroy ( ) ;
156179 }
157180
181+ /// <summary>
182+ /// Deriving modal window implementations should implement this function
183+ /// to render the contents of the input.
184+ /// </summary>
158185 protected abstract void RenderModalContents ( ) ;
159186
160187 protected override void RenderWindow ( )
161188 {
162189 bool shouldClose = false ;
163190
191+ // Render the prompt text
164192 EditorGUILayout . LabelField ( _inputPrompt , GUILayout . Width (
165193 GUIEditorUtility . MeasureLabelWidth ( _inputPrompt ) )
166194 ) ;
167195
196+ // Display error if it needs to be displayed.
168197 if ( _showError )
169198 {
170199 EditorGUILayout . HelpBox ( _errorPrompt , MessageType . Warning ) ;
171200 }
172201
202+ // Render the contents that are unique to the modal window implementation.
173203 RenderModalContents ( ) ;
174204
175205 EditorGUILayout . Space ( ) ;
176206
177207 GUILayout . BeginHorizontal ( ) ;
178208 if ( GUILayout . Button ( "Save" ) )
179209 {
210+ // Try to validate the value being submitted
180211 if ( _validateFunction ( _input ) )
181212 {
213+ // If successful, then call the submit action.
182214 _onSubmit ? . Invoke ( _input ) ;
183215 shouldClose = true ;
184216 }
185217 else
186218 {
219+ // Otherwise, turn on the flag that indicates the error should be displayed.
187220 _showError = true ;
188221 }
189222 }
@@ -195,6 +228,7 @@ protected override void RenderWindow()
195228 }
196229 GUILayout . EndHorizontal ( ) ;
197230
231+ // Close if it should be.
198232 if ( shouldClose )
199233 {
200234 Close ( ) ;
0 commit comments