using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.ComponentModel; using System.Windows; using System.Windows.Input; using System.Windows.Media; using BuzzGUI.Interfaces; using BuzzGUI.Common; namespace BuzzGUI.ParameterWindow { public class ParameterWindowVM : CommandSink, INotifyPropertyChanged, IMachineGUIHost { public event PropertyChangedEventHandler PropertyChanged; IMachine machine; IMachineGUI machineGUI; public IMachine Machine { get { return machine; } set { if (machine != null) { if (machineGUI != null) { machineGUI.Machine = null; machineGUI = null; PropertyChanged.Raise(this, "EmbeddedGUI"); } machine.PropertyChanged -= new PropertyChangedEventHandler(MachinePropertyChanged); foreach (var p in Parameters) p.Release(); Parameters = null; PropertyChanged.Raise(this, "Parameters"); } machine = value; if (machine != null) { machine.PropertyChanged += new PropertyChangedEventHandler(MachinePropertyChanged); CreateParameters(); if (machine.DLL.GUIFactory != null) { machineGUI = machine.DLL.GUIFactory.CreateGUI(this); machineGUI.Machine = machine; PropertyChanged.Raise(this, "EmbeddedGUI"); } } } } void MachinePropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "TrackCount") CreateParameters(); else if (e.PropertyName == "Presets") PropertyChanged.Raise(this, e.PropertyName); else if (e.PropertyName == "Attributes") { PropertyChanged.Raise(this, "Attributes"); foreach (ParameterVM pvm in Parameters) PropertyChanged.Raise(pvm, "Attributes"); } } void CreateParameters() { if (Parameters != null) { foreach (var p in Parameters) p.Release(); } List parameters = new List(); foreach (IParameterGroup pg in machine.ParameterGroups) { for (int t = 0; t < pg.TrackCount; t++) { foreach (IParameter p in pg.Parameters) { if ((p.Flags & ParameterFlags.State) == ParameterFlags.State) parameters.Add(new ParameterVM(this, p, t)); } } } Parameters = new ReadOnlyCollection(parameters); PropertyChanged.Raise(this, "Parameters"); } public static readonly RoutedCommand EditCommand = new RoutedCommand(); public static readonly RoutedCommand CopyCommand = new RoutedCommand(); public static readonly RoutedCommand RandomCommand = new RoutedCommand(); public static readonly RoutedCommand HelpCommand = new RoutedCommand(); public static readonly RoutedCommand SelectPresetCommand = new RoutedCommand(); SimpleCommand setAttributeCommand; public ParameterWindowVM() { base.RegisterCommand(EditCommand, enabled => { return true; }, execute => { machine.ShowPresetEditor(); } ); base.RegisterCommand(CopyCommand, enabled => { return true; }, execute => { machine.CopyParameters(); } ); base.RegisterCommand(HelpCommand, enabled => { return true; }, execute => { machine.ShowHelp(); } ); base.RegisterCommand(RandomCommand, enabled => { return true; }, execute => { Randomize(); } ); base.RegisterCommand(SelectPresetCommand, enabled => { return true; }, preset => { machine.SelectPreset(preset as string); } ); setAttributeCommand = new SimpleCommand { CanExecuteDelegate = x => true, ExecuteDelegate = _x => { var x = _x as Tuple; machine.Attributes[x.Item1].Value = x.Item2; } }; } public void Release() { base.UnregisterCommand(EditCommand); base.UnregisterCommand(CopyCommand); base.UnregisterCommand(HelpCommand); base.UnregisterCommand(RandomCommand); base.UnregisterCommand(SelectPresetCommand); } public string MachineInfoName { get { return machine.DLL.Info.Name; } } public string MachineName { get { return machine.Name; } } public ReadOnlyCollection Parameters { get; private set; } public ReadOnlyCollection Presets { get { return Machine.DLL.Presets; } } void Randomize() { foreach (ParameterVM pvm in Parameters) { if (pvm.Parameter.Group.Type != ParameterGroupType.Input) pvm.Randomize(); } } public IIndex ThemeColors { get { return machine.DLL.Buzz.ThemeColors; } } public FrameworkElement EmbeddedGUI { get { return machineGUI as FrameworkElement; } } #region Attributes const int MaxAttributeRange = 256; List GetAttributeValues(int index) { var a = machine.Attributes[index]; if (a.MaxValue - a.MinValue > MaxAttributeRange) return null; List l = new List(); for (int v = a.MinValue; v <= a.MaxValue; v++) { l.Add(new MenuItemVM() { Text = v.ToString(), IsEnabled = true, IsDefault = v == a.DefValue, IsChecked = v == a.Value, Command = setAttributeCommand, CommandParameter = Tuple.Create(index, v) }); } return l; } public bool HasAttributes { get { return machine.Attributes.Count > 0; } } public IList Attributes { get { var attributes = machine.Attributes; List l = new List(); if (attributes.Count > 0) { int index = 0; foreach (var a in attributes) { var v = GetAttributeValues(index); l.Add(new MenuItemVM() { Text = a.Name, Children = v, IsEnabled = v != null }); index++; } } else { l.Add(new MenuItemVM() { Text = "(no attributes)" }); } return l; } } #endregion } }