Skip to content

Commit f1f584d

Browse files
committed
Added events: OnConsoleEnabled, OnConsoleDisabled, OnConsoleOpened, OnConsoleClosed, OnConsoleFocused, OnConsoleLostFocus.
1 parent 6741bed commit f1f584d

File tree

2 files changed

+57
-52
lines changed

2 files changed

+57
-52
lines changed

Runtime/DevConsole.cs

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ public static InputKey? ToggleKey
9292

9393
#endregion
9494

95+
#region Events
96+
97+
public static event Action OnConsoleEnabled;
98+
99+
public static event Action OnConsoleDisabled;
100+
101+
public static event Action OnConsoleOpened;
102+
103+
public static event Action OnConsoleClosed;
104+
105+
public static event Action OnConsoleFocused;
106+
107+
public static event Action OnConsoleFocusLost;
108+
109+
#endregion
110+
95111
#region Static methods
96112

97113
/// <summary>
@@ -288,62 +304,40 @@ public static void ClearConsole()
288304
_console.ClearConsole();
289305
}
290306

291-
/// <summary>
292-
/// Register a callback for when the dev console is opened.
293-
/// </summary>
294-
/// <param name="callback"></param>
295-
public static void Register_OnDevConsoleOpened(Action callback)
296-
{
297-
if (callback == null)
298-
{
299-
throw new ArgumentNullException(nameof(callback));
300-
}
307+
#region Invoke events
301308

302-
_console.OnDevConsoleOpened += callback;
309+
internal static void InvokeOnConsoleEnabled()
310+
{
311+
OnConsoleEnabled?.Invoke();
303312
}
304313

305-
/// <summary>
306-
/// Deregister a callback for when the dev console is opened.
307-
/// </summary>
308-
/// <param name="callback"></param>
309-
public static void Deregister_OnDevConsoleOpened(Action callback)
314+
internal static void InvokeOnConsoleDisabled()
310315
{
311-
if (callback == null)
312-
{
313-
throw new ArgumentNullException(nameof(callback));
314-
}
315-
316-
_console.OnDevConsoleOpened -= callback;
316+
OnConsoleDisabled?.Invoke();
317317
}
318318

319-
/// <summary>
320-
/// Register a callback for when the dev console is closed.
321-
/// </summary>
322-
/// <param name="callback"></param>
323-
public static void Register_OnDevConsoleClosed(Action callback)
319+
internal static void InvokeOnConsoleOpened()
324320
{
325-
if (callback == null)
326-
{
327-
throw new ArgumentNullException(nameof(callback));
328-
}
321+
OnConsoleOpened?.Invoke();
322+
}
329323

330-
_console.OnDevConsoleClosed += callback;
324+
internal static void InvokeOnConsoleClosed()
325+
{
326+
OnConsoleClosed?.Invoke();
331327
}
332328

333-
/// <summary>
334-
/// Deregister a callback for when the dev console is closed.
335-
/// </summary>
336-
/// <param name="callback"></param>
337-
public static void Deregister_OnDevConsoleClosed(Action callback)
329+
internal static void InvokeOnConsoleFocused()
338330
{
339-
if (callback == null)
340-
{
341-
throw new ArgumentNullException(nameof(callback));
342-
}
331+
OnConsoleFocused?.Invoke();
332+
}
343333

344-
_console.OnDevConsoleClosed -= callback;
334+
internal static void InvokeOnConsoleFocusLost()
335+
{
336+
OnConsoleFocusLost?.Invoke();
345337
}
346338

339+
#endregion
340+
347341
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
348342
#pragma warning disable IDE0051
349343
private static void Init()

Runtime/DevConsoleMono.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ internal sealed class DevConsoleMono : MonoBehaviour
129129
#region Input fields
130130

131131
private bool _focusInputField = false;
132+
private bool _oldFocusInputField = false;
132133
private Dictionary<InputKey, string> _bindings = new Dictionary<InputKey, string>();
133134

134135
#endregion
@@ -216,14 +217,6 @@ private int InputCaretPosition
216217

217218
#endregion
218219

219-
#region Events
220-
221-
internal event Action OnDevConsoleOpened;
222-
223-
internal event Action OnDevConsoleClosed;
224-
225-
#endregion
226-
227220
#region Methods
228221

229222
#region Console methods
@@ -242,6 +235,8 @@ internal void EnableConsole()
242235
_screenSize = new Vector2Int(Screen.width, Screen.height);
243236
ConsoleIsEnabled = true;
244237
enabled = true;
238+
239+
DevConsole.InvokeOnConsoleEnabled();
245240
}
246241

247242
internal void DisableConsole()
@@ -264,6 +259,8 @@ internal void DisableConsole()
264259
//Application.logMessageReceivedThreaded -= OnLogMessageReceived;
265260
ConsoleIsEnabled = false;
266261
enabled = false;
262+
263+
DevConsole.InvokeOnConsoleDisabled();
267264
}
268265

269266
internal void OpenConsole()
@@ -288,7 +285,7 @@ internal void OpenConsole()
288285
_focusInputField = true;
289286
InputText = InputText.TrimEnd('`');
290287

291-
OnDevConsoleOpened?.Invoke();
288+
DevConsole.InvokeOnConsoleOpened();
292289
}
293290

294291
internal void CloseConsole()
@@ -305,7 +302,7 @@ internal void CloseConsole()
305302
_repositioning = false;
306303
_resizing = false;
307304

308-
OnDevConsoleClosed?.Invoke();
305+
DevConsole.InvokeOnConsoleClosed();
309306
}
310307

311308
internal void ToggleConsole()
@@ -666,6 +663,20 @@ private void Update()
666663
_focusInputField = false;
667664
}
668665

666+
// Check if the input field focus changed and invoke the event
667+
if (_inputField.isFocused != _oldFocusInputField)
668+
{
669+
if (_inputField.isFocused)
670+
{
671+
DevConsole.InvokeOnConsoleFocused();
672+
}
673+
else
674+
{
675+
DevConsole.InvokeOnConsoleFocusLost();
676+
}
677+
_oldFocusInputField = _inputField.isFocused;
678+
}
679+
669680
// Move the developer console using the mouse position
670681
if (_repositioning)
671682
{

0 commit comments

Comments
 (0)