Skip to content

Commit

Permalink
feat(Menu): added enable/disable state in items
Browse files Browse the repository at this point in the history
  • Loading branch information
Iam1337 committed Feb 1, 2022
1 parent 3da405a commit a41e191
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/Menu/DMBranch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ protected override void OnEvent(EventArgs eventArgs)
var currentItem = Current;
if (currentItem is DMBranch currentBranch)
{
if (Container.IsVisible)
if (Container.IsVisible && IsEnabled())
Container.Open(currentBranch);
}
else
Expand Down
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/Menu/DMDefaultRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void IDMRender.Repaint(DMBranch branch, IReadOnlyList<DMItem> items)
var item = items[i];
var selected = item == branch.Current;
var prefix = selected ? kPrefix_Selected : kPrefix;
var alpha = selected ? 1 : 0.75f;
var alpha = item.IsEnabled() ? (selected ? 1 : 0.75f) : 0.50f;

// items separator
if (order >= 0 && Math.Abs(order - item.Order) > 1)
Expand Down
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/Menu/DMEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected override void OnEvent(EventArgs eventArgs)

if (eventArgs.Key == EventKey.Right)
{
if (Container.IsVisible)
if (Container.IsVisible && IsEnabled())
Container.Open(_flagBranch);

return;
Expand Down
18 changes: 18 additions & 0 deletions Assets/extDebug/Scripts/Menu/DMItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,27 @@ public DMContainer Container
internal set => _container = value;
}

public bool IsEnabled()
{
// If the parent component is disabled, then the child must be disabled.
if (_parent != null && !_parent.IsEnabled())
return false;

return _enabledCallback?.Invoke() ?? _enabled;
}

public void SetEnabled(bool enabled) => _enabled = enabled;

public void SetEnabled(Func<bool> callback) => _enabledCallback = callback;

#endregion

#region Protected Vars

protected bool _enabled;

protected Func<bool> _enabledCallback;

protected DMContainer _container;

protected readonly DMBranch _parent;
Expand Down Expand Up @@ -240,6 +257,7 @@ protected DMItem(DMBranch parent, string path, string value, string description,
_valueField = new Field<string>(value, DM.Colors.Value);
_descriptionField = new Field<string>(description, DM.Colors.Description);

_enabled = true;
_order = order;

_parent = parent?.Get(directory, true);
Expand Down
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/Menu/DMUnityFloatStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected override void OnEvent(EventArgs eventArgs)

if (eventArgs.Key == EventKey.Right)
{
if (Container.IsVisible)
if (Container.IsVisible && IsEnabled())
Container.Open(_fieldsBranch);

return;
Expand Down
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/Menu/DMUnityIntStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected override void OnEvent(EventArgs eventArgs)

if (eventArgs.Key == EventKey.Right)
{
if (Container.IsVisible)
if (Container.IsVisible && IsEnabled())
Container.Open(_fieldsBranch);

return;
Expand Down
6 changes: 1 addition & 5 deletions Assets/extDebug/Scripts/Menu/DMValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override void OnEvent(EventArgs eventArgs)
_valueField.Color = _defaultValue.Equals(value) ? DM.Colors.Value : DM.Colors.ValueFlash;
_valueField.Value = ValueToString(value);
}
else if (eventArgs.Tag == EventTag.Input)
else if (eventArgs.Tag == EventTag.Input && IsEnabled())
{
if (eventArgs.Key == EventKey.Left && _setter != null)
{
Expand All @@ -78,10 +78,6 @@ protected override void OnEvent(EventArgs eventArgs)
{
ChangeValue(_defaultValue, true);
}
else if (eventArgs.Key == EventKey.Back)
{
Container.Back();
}
}
}

Expand Down

0 comments on commit a41e191

Please sign in to comment.