forked from Shardstone/trail-into-darkness
feat: add channel enable/disable, prefab docs, and UI updates
Add per-channel enable/disable toggle to the in-game logging system with Enable()/Disable() on InGameLogger and EnableChannel/DisableChannel on IGameLogStore. Update README with prefab setup guide and enable/disable documentation. Update character creation and log container prefabs. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -135,6 +135,32 @@ var tradeLogger = new InGameLogger(logStore, MyLogChannels.Trading);
|
||||
tradeLogger.Log("Sold Iron Sword for 50 gold.");
|
||||
```
|
||||
|
||||
## Enable / Disable Channels
|
||||
|
||||
Each logger can be toggled on or off. When disabled, calls to `Log` are silently dropped (no allocation, no entry added). All channels are enabled by default.
|
||||
|
||||
```csharp
|
||||
var combatLog = new InGameLogger(logStore, LogChannel.Combat);
|
||||
|
||||
combatLog.Disable();
|
||||
combatLog.Log("This message is silently dropped.");
|
||||
|
||||
combatLog.Enable();
|
||||
combatLog.Log("Back online.");
|
||||
|
||||
if(combatLog.IsEnabled) {
|
||||
// check state
|
||||
}
|
||||
```
|
||||
|
||||
The state lives in the `IGameLogStore`, so disabling a channel from one `InGameLogger` instance disables it for all instances using the same store and channel. You can also call the store directly:
|
||||
|
||||
```csharp
|
||||
logStore.DisableChannel(LogChannel.World);
|
||||
logStore.EnableChannel(LogChannel.World);
|
||||
bool enabled = logStore.IsChannelEnabled(LogChannel.World);
|
||||
```
|
||||
|
||||
## Rich Text
|
||||
|
||||
Log messages support TextMeshPro rich text tags. The `InGameLogger.Log(message, hexColor)` overload wraps the message in a `<color>` tag automatically:
|
||||
@@ -194,13 +220,19 @@ logStore.RestoreFromSaveData(saveData);
|
||||
- `InGameLogger(IGameLogStore store, LogChannel channel)` -- constructor
|
||||
- `void Log(string message)` -- add an entry to the store
|
||||
- `void Log(string message, string hexColor)` -- add a color-wrapped entry
|
||||
- `void Enable()` -- enable this logger's channel
|
||||
- `void Disable()` -- disable this logger's channel (Log calls are silently dropped)
|
||||
- `bool IsEnabled` -- whether this logger's channel is currently enabled
|
||||
|
||||
### IGameLogStore / GameLogStore
|
||||
|
||||
- `GameLogStore(int capacity = 500)` -- constructor with ring buffer size
|
||||
- `int Count` -- current number of entries
|
||||
- `int Capacity` -- maximum entries before oldest are overwritten
|
||||
- `void Add(LogChannel channel, string message)` -- add an entry
|
||||
- `void Add(LogChannel channel, string message)` -- add an entry (silently dropped if channel is disabled)
|
||||
- `void EnableChannel(LogChannel channel)` -- enable a channel
|
||||
- `void DisableChannel(LogChannel channel)` -- disable a channel
|
||||
- `bool IsChannelEnabled(LogChannel channel)` -- check if a channel is enabled
|
||||
- `void Clear()` -- remove all entries
|
||||
- `void Clear(LogChannel channel)` -- remove entries for a specific channel
|
||||
- `ReadOnlySpan<LogEntry> GetEntries()` -- all entries in chronological order
|
||||
|
||||
@@ -5,6 +5,7 @@ using UnityEngine;
|
||||
namespace Jovian.InGameLogging {
|
||||
public sealed class GameLogStore : IGameLogStore {
|
||||
readonly LogEntry[] buffer;
|
||||
readonly HashSet<LogChannel> disabledChannels = new();
|
||||
int head;
|
||||
int count;
|
||||
|
||||
@@ -21,6 +22,9 @@ namespace Jovian.InGameLogging {
|
||||
}
|
||||
|
||||
public void Add(LogChannel channel, string message) {
|
||||
if(disabledChannels.Contains(channel)) {
|
||||
return;
|
||||
}
|
||||
var entry = new LogEntry(message, channel, Time.time);
|
||||
buffer[head] = entry;
|
||||
head = (head + 1) % buffer.Length;
|
||||
@@ -30,6 +34,18 @@ namespace Jovian.InGameLogging {
|
||||
OnEntryAdded?.Invoke(entry);
|
||||
}
|
||||
|
||||
public void EnableChannel(LogChannel channel) {
|
||||
disabledChannels.Remove(channel);
|
||||
}
|
||||
|
||||
public void DisableChannel(LogChannel channel) {
|
||||
disabledChannels.Add(channel);
|
||||
}
|
||||
|
||||
public bool IsChannelEnabled(LogChannel channel) {
|
||||
return !disabledChannels.Contains(channel);
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
head = 0;
|
||||
count = 0;
|
||||
|
||||
@@ -7,6 +7,9 @@ namespace Jovian.InGameLogging {
|
||||
int Capacity { get; }
|
||||
|
||||
void Add(LogChannel channel, string message);
|
||||
void EnableChannel(LogChannel channel);
|
||||
void DisableChannel(LogChannel channel);
|
||||
bool IsChannelEnabled(LogChannel channel);
|
||||
void Clear();
|
||||
void Clear(LogChannel channel);
|
||||
|
||||
|
||||
@@ -19,5 +19,17 @@ namespace Jovian.InGameLogging {
|
||||
public void Log(string message, string hexColor) {
|
||||
store.Add(channel, $"<color={hexColor}>{message}</color>");
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Enable() {
|
||||
store.EnableChannel(channel);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Disable() {
|
||||
store.DisableChannel(channel);
|
||||
}
|
||||
|
||||
public bool IsEnabled => store.IsChannelEnabled(channel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ using UnityEngine;
|
||||
namespace Jovian.InGameLogging {
|
||||
[Serializable]
|
||||
public readonly struct LogChannel : IEquatable<LogChannel> {
|
||||
[SerializeField] readonly string id;
|
||||
private readonly string id;
|
||||
|
||||
public string Id => id;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user