Added first visual-scripting draft [WIP]

This commit is contained in:
DarthAffe committed 2021-07-15 00:20:08 +02:00
1 parent 719d95bd33
commit a45dd094a0
56 files changed
+5139 -21

No files matched your search

@@ -0,0 +1,64 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Artemis.VisualScripting.ViewModel
{
//TODO DarthAffe 13.07.2021: What is Artemis using here?
public abstract class AbstractBindable : INotifyPropertyChanged
{
#region Events
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Methods
/// <summary>
/// Checks if the property already matches the desirec value or needs to be updated.
/// </summary>
/// <typeparam name="T">Type of the property.</typeparam>
/// <param name="storage">Reference to the backing-filed.</param>
/// <param name="value">Result to apply.</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected virtual bool RequiresUpdate<T>(ref T storage, T value)
{
return !Equals(storage, value);
}
/// <summary>
/// Checks if the property already matches the desired value and updates it if not.
/// </summary>
/// <typeparam name="T">Type of the property.</typeparam>
/// <param name="storage">Reference to the backing-filed.</param>
/// <param name="value">Result to apply.</param>
/// <param name="propertyName">Name of the property used to notify listeners. This value is optional
/// and can be provided automatically when invoked from compilers that support <see cref="CallerMemberNameAttribute"/>.</param>
/// <returns><c>true</c> if the value was changed, <c>false</c> if the existing value matched the desired value.</returns>
protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (!this.RequiresUpdate(ref storage, value)) return false;
storage = value;
// ReSharper disable once ExplicitCallerInfoArgument
this.OnPropertyChanged(propertyName);
return true;
}
/// <summary>
/// Triggers the <see cref="PropertyChanged"/>-event when a a property value has changed.
/// </summary>
/// <param name="propertyName">Name of the property used to notify listeners. This value is optional
/// and can be provided automatically when invoked from compilers that support <see cref="CallerMemberNameAttribute"/>.</param>
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
@@ -0,0 +1,77 @@
using System;
using System.Windows.Input;
namespace Artemis.VisualScripting.ViewModel
{
public class ActionCommand : ICommand
{
#region Properties & Fields
private readonly Func<bool> _canExecute;
private readonly Action _command;
#endregion
#region Events
public event EventHandler CanExecuteChanged;
#endregion
#region Constructors
public ActionCommand(Action command, Func<bool> canExecute = null)
{
this._command = command;
this._canExecute = canExecute;
}
#endregion
#region Methods
public bool CanExecute(object parameter) => _canExecute?.Invoke() ?? true;
public void Execute(object parameter) => _command?.Invoke();
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, new EventArgs());
#endregion
}
public class ActionCommand<T> : ICommand
{
#region Properties & Fields
private readonly Func<T, bool> _canExecute;
private readonly Action<T> _command;
#endregion
#region Events
public event EventHandler CanExecuteChanged;
#endregion
#region Constructors
public ActionCommand(Action<T> command, Func<T, bool> canExecute = null)
{
this._command = command;
this._canExecute = canExecute;
}
#endregion
#region Methods
public bool CanExecute(object parameter) => _canExecute?.Invoke((T)parameter) ?? true;
public void Execute(object parameter) => _command?.Invoke((T)parameter);
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, new EventArgs());
#endregion
}
}