1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

NumberBox - Fixed values getting coerced between 0 and 100

Data model event cycle node - Listen to the event directly, avoiding race conditions
This commit is contained in:
Robert 2023-05-26 00:16:26 +02:00
parent dc8147d5a7
commit c24e57e556
2 changed files with 76 additions and 80 deletions

View File

@ -23,7 +23,10 @@
</Border> </Border>
</Design.PreviewWith> </Design.PreviewWith>
<!-- Add Styles Here --> <Style Selector="controls|NumberBox">
<Setter Property="Maximum" Value="{x:Static system:Double.MaxValue}"></Setter>
<Setter Property="Minimum" Value="{x:Static system:Double.MinValue}"></Setter>
</Style>
<Style Selector="controls|NumberBox /template/ TextBox.NumberBoxTextBoxStyle /template/ TextBlock#PART_Prefix"> <Style Selector="controls|NumberBox /template/ TextBox.NumberBoxTextBoxStyle /template/ TextBlock#PART_Prefix">
<Setter Property="Foreground" Value="{DynamicResource TextControlForegroundDisabled}"></Setter> <Setter Property="Foreground" Value="{DynamicResource TextControlForegroundDisabled}"></Setter>
<Setter Property="Margin" Value="-4 0 -12 0"></Setter> <Setter Property="Margin" Value="-4 0 -12 0"></Setter>
@ -39,8 +42,6 @@
<Style Selector="controls|NumberBox.condensed /template/ TextBox.NumberBoxTextBoxStyle /template/ TextBlock#PART_Suffix"> <Style Selector="controls|NumberBox.condensed /template/ TextBox.NumberBoxTextBoxStyle /template/ TextBlock#PART_Suffix">
<Setter Property="Margin" Value="-4 0 0 0"></Setter> <Setter Property="Margin" Value="-4 0 0 0"></Setter>
</Style> </Style>
<Style Selector="controls|NumberBox /template/ TextBox.NumberBoxTextBoxStyle"> <Style Selector="controls|NumberBox /template/ TextBox.NumberBoxTextBoxStyle">
<Setter Property="attachedProperties:TextBoxAssist.PrefixText" Value="{TemplateBinding attachedProperties:NumberBoxAssist.PrefixText}"></Setter> <Setter Property="attachedProperties:TextBoxAssist.PrefixText" Value="{TemplateBinding attachedProperties:NumberBoxAssist.PrefixText}"></Setter>
<Setter Property="attachedProperties:TextBoxAssist.SuffixText" Value="{TemplateBinding attachedProperties:NumberBoxAssist.SuffixText}"></Setter> <Setter Property="attachedProperties:TextBoxAssist.SuffixText" Value="{TemplateBinding attachedProperties:NumberBoxAssist.SuffixText}"></Setter>

View File

@ -12,7 +12,6 @@ public class DataModelEventCycleNode : Node<DataModelPathEntity, DataModelEventC
private Type _currentType; private Type _currentType;
private DataModelPath? _dataModelPath; private DataModelPath? _dataModelPath;
private object? _lastPathValue; private object? _lastPathValue;
private DateTime _lastTrigger;
private bool _updating; private bool _updating;
public DataModelEventCycleNode() public DataModelEventCycleNode()
@ -22,8 +21,8 @@ public class DataModelEventCycleNode : Node<DataModelPathEntity, DataModelEventC
CycleValues = CreateInputPinCollection(typeof(object), "", 0); CycleValues = CreateInputPinCollection(typeof(object), "", 0);
Output = CreateOutputPin(typeof(object)); Output = CreateOutputPin(typeof(object));
CycleValues.PinAdded += CycleValuesOnPinAdded; CycleValues.PinAdded += OnCycleValuesOnPinAdded;
CycleValues.PinRemoved += CycleValuesOnPinRemoved; CycleValues.PinRemoved += OnCycleValuesOnPinRemoved;
CycleValues.Add(CycleValues.CreatePin()); CycleValues.Add(CycleValues.CreatePin());
// Monitor storage for changes // Monitor storage for changes
@ -39,24 +38,15 @@ public class DataModelEventCycleNode : Node<DataModelPathEntity, DataModelEventC
{ {
Script = script; Script = script;
if (Storage == null) if (Storage != null)
return;
UpdateDataModelPath(); UpdateDataModelPath();
} }
public override void Evaluate() public override void Evaluate()
{ {
object? pathValue = _dataModelPath?.GetValue(); object? pathValue = _dataModelPath?.GetValue();
bool hasTriggered = pathValue is IDataModelEvent dataModelEvent ? EvaluateEvent(dataModelEvent) : EvaluateValue(pathValue); if (pathValue is not IDataModelEvent && EvaluateValue(pathValue))
Cycle();
if (hasTriggered)
{
_currentIndex++;
if (_currentIndex >= CycleValues.Count())
_currentIndex = 0;
}
object? outputValue = CycleValues.ElementAt(_currentIndex).PinValue; object? outputValue = CycleValues.ElementAt(_currentIndex).PinValue;
if (Output.Type.IsInstanceOfType(outputValue)) if (Output.Type.IsInstanceOfType(outputValue))
@ -65,15 +55,6 @@ public class DataModelEventCycleNode : Node<DataModelPathEntity, DataModelEventC
Output.Value = Output.Type.GetDefault()!; Output.Value = Output.Type.GetDefault()!;
} }
private bool EvaluateEvent(IDataModelEvent dataModelEvent)
{
if (dataModelEvent.LastTrigger <= _lastTrigger)
return false;
_lastTrigger = dataModelEvent.LastTrigger;
return true;
}
private bool EvaluateValue(object? pathValue) private bool EvaluateValue(object? pathValue)
{ {
if (Equals(pathValue, _lastPathValue)) if (Equals(pathValue, _lastPathValue))
@ -83,51 +64,26 @@ public class DataModelEventCycleNode : Node<DataModelPathEntity, DataModelEventC
return true; return true;
} }
private void CycleValuesOnPinAdded(object? sender, SingleValueEventArgs<IPin> e) private void Cycle()
{ {
e.Value.PinConnected += OnPinConnected; _currentIndex++;
e.Value.PinDisconnected += OnPinDisconnected;
}
private void CycleValuesOnPinRemoved(object? sender, SingleValueEventArgs<IPin> e) if (_currentIndex >= CycleValues.Count())
{ _currentIndex = 0;
e.Value.PinConnected -= OnPinConnected;
e.Value.PinDisconnected -= OnPinDisconnected;
}
private void OnPinDisconnected(object? sender, SingleValueEventArgs<IPin> e)
{
ProcessPinDisconnected();
}
private void OnPinConnected(object? sender, SingleValueEventArgs<IPin> e)
{
ProcessPinConnected(e.Value);
}
private void ProcessPinConnected(IPin source)
{
if (_updating)
return;
try
{
_updating = true;
// No need to change anything if the types haven't changed
if (_currentType != source.Type)
ChangeCurrentType(source.Type);
}
finally
{
_updating = false;
}
} }
private void UpdateDataModelPath() private void UpdateDataModelPath()
{ {
DataModelPath? old = _dataModelPath; DataModelPath? old = _dataModelPath;
if (old?.GetValue() is IDataModelEvent oldEvent)
oldEvent.EventTriggered -= OnEventTriggered;
_dataModelPath = Storage != null ? new DataModelPath(Storage) : null; _dataModelPath = Storage != null ? new DataModelPath(Storage) : null;
if (_dataModelPath?.GetValue() is IDataModelEvent newEvent)
newEvent.EventTriggered += OnEventTriggered;
old?.Dispose(); old?.Dispose();
} }
@ -139,10 +95,28 @@ public class DataModelEventCycleNode : Node<DataModelPathEntity, DataModelEventC
_currentType = type; _currentType = type;
} }
private void ProcessPinDisconnected() private void OnEventTriggered(object? sender, EventArgs e)
{
Cycle();
}
private void OnCycleValuesOnPinAdded(object? sender, SingleValueEventArgs<IPin> e)
{
e.Value.PinConnected += OnPinConnected;
e.Value.PinDisconnected += OnPinDisconnected;
}
private void OnCycleValuesOnPinRemoved(object? sender, SingleValueEventArgs<IPin> e)
{
e.Value.PinConnected -= OnPinConnected;
e.Value.PinDisconnected -= OnPinDisconnected;
}
private void OnPinDisconnected(object? sender, SingleValueEventArgs<IPin> e)
{ {
if (_updating) if (_updating)
return; return;
try try
{ {
// If there's still a connected pin, stick to the current type // If there's still a connected pin, stick to the current type
@ -157,9 +131,30 @@ public class DataModelEventCycleNode : Node<DataModelPathEntity, DataModelEventC
} }
} }
private void OnPinConnected(object? sender, SingleValueEventArgs<IPin> e)
{
if (_updating)
return;
try
{
_updating = true;
// No need to change anything if the types haven't changed
if (_currentType != e.Value.Type)
ChangeCurrentType(e.Value.Type);
}
finally
{
_updating = false;
}
}
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
if (_dataModelPath?.GetValue() is IDataModelEvent newEvent)
newEvent.EventTriggered -= OnEventTriggered;
_dataModelPath?.Dispose(); _dataModelPath?.Dispose();
} }
} }