Skip to content

Commit

Permalink
feat(Menu): added logs logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Iam1337 committed Apr 3, 2023
1 parent 5046751 commit d9bcead
Show file tree
Hide file tree
Showing 52 changed files with 540 additions and 172 deletions.
98 changes: 95 additions & 3 deletions Assets/extDebug/Examples/extDebug.Menu/Example.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

using UnityEngine;

using System;

using System.Collections.Generic;
using extDebug.Menu;
using Random = UnityEngine.Random;

namespace extDebug.Examples.Menu
{
Expand All @@ -27,6 +28,85 @@ private enum ExampleFlags
Three = 1 << 2,
}

private class ExampleLogsContainer : IDMLogsContainer
{
private enum LogType
{
Info,
Warning,
Error
}

private struct LogData
{
public LogType Type;
public string Message;
}

private bool _isDirty = false;

private readonly Dictionary<LogType, Color> _tagsColors = new()
{
{ LogType.Info, Color.white},
{ LogType.Warning, Color.yellow },
{ LogType.Error, Color.red },
};

private readonly Dictionary<LogType, String> _tags = new()
{
{ LogType.Info, "[I]" },
{ LogType.Warning, "[W]" },
{ LogType.Error, "[E]" },
};
private readonly List<LogData> _logs = new();

public ExampleLogsContainer()
{
for (var i = 0; i < 100; i++) AddRandom();
}

public void AddRandom()
{
_logs.Add(new LogData
{
Type = (LogType)Random.Range(0, 3),
Message = Guid.NewGuid() + (Random.value > 0.5f ? Guid.NewGuid().ToString() : string.Empty )
});

_isDirty = true;
}

public bool IsDirty()
{
var isDirty = _isDirty;
_isDirty = false;
return isDirty;
}

public bool GetLog(int index, out string tag, out Color tagColor, out string message, out Color messageColor)
{
tag = string.Empty;
tagColor = Color.clear;
message = string.Empty;
messageColor = Color.white;

if (index >= _logs.Count)
return false;

var log = _logs[_logs.Count - 1 - index];
tag = _tags[log.Type];
tagColor = _tagsColors[log.Type];
message = log.Message;

return true;
}

public int GetLogsCount()
{
return _logs.Count;
}
}

#endregion

#region Private Vars
Expand Down Expand Up @@ -95,6 +175,8 @@ private enum ExampleFlags

private ExampleFlags _flagsStorage;

private ExampleLogsContainer _logsContainer;

#endregion

#region Unity Methods
Expand Down Expand Up @@ -140,6 +222,10 @@ private void Start()
DM.Add("Storage Values/Enum", () => _enumStorage, v => _enumStorage = v, order: 11).SetStorage(storage);
DM.Add("Storage Values/Flags", () => _flagsStorage, v => _flagsStorage = v, order: 12).SetStorage(storage);

// Logs
_logsContainer = new ExampleLogsContainer();
DM.Add("Logs", _logsContainer, "Logs Example", 10);

// Dynamic
DM.Add("Dynamic Transforms", FindObjectsOfType<Transform>, (branch, transform) =>
{
Expand All @@ -149,6 +235,12 @@ private void Start()
DM.Open();
}

#endregion
protected void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
_logsContainer.AddRandom();
}

#endregion
}
}
2 changes: 1 addition & 1 deletion Assets/extDebug/Examples/extDebug.Notifications/Example.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

using UnityEngine;

Expand Down
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/Draw/DG.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

using System.Collections.Generic;

Expand Down
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/FloatUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

namespace extDebug
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/Heatmap/DH.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

using UnityEngine;

Expand Down
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/Heatmap/DHSession.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

using UnityEngine;

Expand Down
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/Hooks.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

using UnityEngine;

Expand Down
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/HooksBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

using UnityEngine;

Expand Down
8 changes: 6 additions & 2 deletions Assets/extDebug/Scripts/Menu/DM.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

using UnityEngine;

Expand Down Expand Up @@ -80,7 +80,7 @@ static DM()

public static void Open() => Container.Open();

public static void Open(DMBranch branch) => Container.Open(branch);
public static void Open(IDMBranch branch) => Container.Open(branch);

public static void Back() => Container.Back();

Expand Down Expand Up @@ -174,6 +174,10 @@ public static DMVector3Int Add(string path, Func<Vector3Int> getter, Action<Vect
public static DMBranch Add<T>(string path, Func<IEnumerable<T>> getter, Action<DMBranch, T> buildCallback = null, Func<T, string> nameCallback = null, string description = "", int order = 0) =>
Container.Add(path, getter, buildCallback, nameCallback, description, order);

// Logs
public static DMLogs Add(string path, IDMLogsContainer logger, string description = "", int size = 10, int order = 0) =>
Container.Add(path, logger, description, size, order);

#endregion

#region Private Methods
Expand Down
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/Menu/DMAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

using System;

Expand Down
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/Menu/DMBool.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

using System;

Expand Down
52 changes: 33 additions & 19 deletions Assets/extDebug/Scripts/Menu/DMBranch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

using UnityEngine;

Expand All @@ -7,7 +7,7 @@

namespace extDebug.Menu
{
public class DMBranch : DMItem, IDMBranch
public class DMBranch : DMItem, IDMBranch, IDMContainer
{
#region Public Methods

Expand All @@ -25,7 +25,7 @@ public DMItem Current
}
}

public Action<DMBranch> OnOpen;
public Action<DMBranch> OnOpen;

public Action<DMBranch> OnClose;

Expand Down Expand Up @@ -176,20 +176,21 @@ public DMBranch Add<T>(string path, Func<IEnumerable<T>> getter, Action<DMBranch
return dynamicBranch;
}

// Repaint
public DMLogs Add(string path, IDMLogsContainer logsContainer, string description = "", int size = 10, int order = 0) =>
new DMLogs(this, path, description, logsContainer, size, order);

// Repaint
public void RequestRepaint() => _canRepaint = true;

public void RequestRepaint(float duration) => _canRepaintUntil = Time.unscaledTime + duration;
public void RequestRepaint(float duration) => _canRepaintUntil = Time.unscaledTime + duration;

// Other
public override string ToString() => $"Branch: {_nameField.Value}, Desc: {_descriptionField.Value}";

#endregion

#region Internal Methods

internal IReadOnlyList<DMItem> GetItems() => _items.AsReadOnly();


internal void Resort()
{
int Comparison(DMItem x, DMItem y) => x.Order.CompareTo(y.Order);
Expand Down Expand Up @@ -235,18 +236,31 @@ internal DMBranch Get(string path, bool create = false)
return branch;
}

internal bool CanRepaint() => _canRepaint || _canRepaintUntil > Time.unscaledTime || Time.unscaledTime > _autoRepaintAt;

internal void CompleteRepaint()
{
if (_autoRepaintPeriod > 0)
_autoRepaintAt = Time.unscaledTime + _autoRepaintPeriod;

_canRepaint = false;
}

#endregion

#region IDMBranch

DMContainer IDMBranch.Container
{
set => Container = value;
}

IReadOnlyList<DMItem> IDMBranch.GetItems() => _items.AsReadOnly();

bool IDMBranch.CanRepaint() => _canRepaint || _canRepaintUntil > Time.unscaledTime || Time.unscaledTime > _autoRepaintAt;

void IDMBranch.CompleteRepaint()
{
if (_autoRepaintPeriod > 0)
_autoRepaintAt = Time.unscaledTime + _autoRepaintPeriod;

_canRepaint = false;
}

#endregion

#region Protected Methods

protected override void OnEvent(EventArgs eventArgs)
Expand Down Expand Up @@ -282,7 +296,7 @@ protected override void OnEvent(EventArgs eventArgs)
else if (eventArgs.Key == EventKey.Left)
{
var currentItem = Current;
if (currentItem is DMBranch)
if (currentItem is IDMBranch)
{
if (Container.IsVisible)
Container.Back();
Expand All @@ -295,7 +309,7 @@ protected override void OnEvent(EventArgs eventArgs)
else if (eventArgs.Key == EventKey.Right)
{
var currentItem = Current;
if (currentItem is DMBranch currentBranch)
if (currentItem is IDMBranch currentBranch)
{
if (Container.IsVisible && IsEnabled())
Container.Open(currentBranch);
Expand All @@ -308,7 +322,7 @@ protected override void OnEvent(EventArgs eventArgs)
else if (eventArgs.Key == EventKey.Reset)
{
var currentItem = Current;
if (currentItem is DMBranch currentBranch)
if (currentItem is IDMBranch currentBranch)
{
// None
}
Expand Down
20 changes: 11 additions & 9 deletions Assets/extDebug/Scripts/Menu/DMContainer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

using UnityEngine;

Expand All @@ -7,7 +7,7 @@

namespace extDebug.Menu
{
public class DMContainer : IDMBranch
public class DMContainer : IDMContainer
{
#region Public Vars

Expand All @@ -27,11 +27,11 @@ public class DMContainer : IDMBranch

private const float kRepeatInterval = 0.1f;

private DMBranch _currentBranch;
private IDMBranch _currentBranch;

private DMBranch _previousBranch => _branchesStack.Count > 0 ? _branchesStack.Peek() : null;
private IDMBranch _previousBranch => _branchesStack.Count > 0 ? _branchesStack.Peek() : null;

private readonly Stack<DMBranch> _branchesStack = new Stack<DMBranch>();
private readonly Stack<IDMBranch> _branchesStack = new();

private EventKey _previousKey;

Expand All @@ -56,12 +56,11 @@ public DMContainer(string name, IDMInput input, IDMRender render)
// Setup modules.
Input = input;
Render = render;

}
}

public void Open() => Open(Root);

public void Open(DMBranch branch)
public void Open(IDMBranch branch)
{
if (branch == null)
throw new ArgumentNullException(nameof(branch));
Expand Down Expand Up @@ -249,7 +248,10 @@ public DMVector3Int Add(string path, Func<Vector3Int> getter, Action<Vector3Int>
public DMBranch Add<T>(string path, Func<IEnumerable<T>> getter, Action<DMBranch, T> buildCallback = null, Func<T, string> nameCallback = null, string description = "", int order = 0) =>
Root.Add(path, getter, buildCallback, nameCallback, description, order);

#endregion
public DMLogs Add(string path, IDMLogsContainer logsContainer, string description = "", int size = 10, int order = 0) =>
Root.Add(path, logsContainer, description, size, order);

#endregion

#region Private Methods

Expand Down
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/Menu/DMDefaultInput.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021 dr. ext (Vladimir Sigalkin) */
/* Copyright (c) 2023 dr. ext (Vladimir Sigalkin) */

using UnityEngine;

Expand Down
Loading

0 comments on commit d9bcead

Please sign in to comment.