Page 1 of 1

Bug: IMachineConnections on GUI init

Posted: Mon Apr 15, 2013 1:07 pm
by IXix
I have the following code to track the I/O connections of my PatchBay machine. It works fine for inputs but outputs aren't registered when the GUI dll is loaded. On song load, none of the outputs are noticed but those connected afterwards work as expected.

Code: Select all

void OnConnectionAdded(IMachineConnection c)
{
	if (c.Destination == Machine) // Input connection
	{
		m_InputConnections.Add(c, c.DestinationChannel);
		c.PropertyChanged += OnConnectionChanged;
                c.Source.PropertyChanged += Source_PropertyChanged;
	}
	else if (c.Source == Machine) // Output connection
	{
		m_OutputConnections.Add(c, c.SourceChannel);
		c.PropertyChanged += OnConnectionChanged;
		c.Source.PropertyChanged += Source_PropertyChanged;
	}
}

Re: Bug: IMachineConnections on GUI init

Posted: Mon Apr 15, 2013 3:02 pm
by oskari
The connections already exist when the GUI is created so you have to add them manually in IMachineGUI.Machine setter:

Code: Select all


foreach (var c in machine.Inputs.Concat(machine.Outputs))
    OnConnectionAdded(c)


Re: Bug: IMachineConnections on GUI init

Posted: Mon Apr 15, 2013 5:12 pm
by IXix
oskari wrote:The connections already exist when the GUI is created so you have to add them manually in IMachineGUI.Machine setter
Ah okay. Thanks! :)

edit: I only needed to do foreach (var c in machine.Outputs). The inputs add themselves already.

Re: Bug: IMachineConnections on GUI init

Posted: Mon Apr 15, 2013 6:13 pm
by oskari
I think you are subscribing to IMachineGraph.ConnectionAdded in the wrong place (constructor?). It should be done in IMachineGUI.Machine setter as well. You should always use this pattern (this one uses MachineAdded instead of ConnectionAdded but it's the same idea):

Code: Select all

		IMachine machine;
		public IMachine Machine
		{
			get { return machine; }
			set
			{
				if (machine != null)
				{
					machine.Graph.Buzz.Song.MachineAdded -= new Action<IMachine>(Song_MachineAdded);
					machine.Graph.Buzz.Song.MachineRemoved -= new Action<IMachine>(Song_MachineRemoved);
					machine.GetParameter("Ctrl. Root").UnsubscribeEvents(0, RootNoteValueChanged, null);
				}

				machine = value;

				if (machine != null)
				{
					machine.Graph.Buzz.Song.MachineAdded += new Action<IMachine>(Song_MachineAdded);
					machine.Graph.Buzz.Song.MachineRemoved += new Action<IMachine>(Song_MachineRemoved);
					machine.GetParameter("Ctrl. Root").SubscribeEvents(0, RootNoteValueChanged, null);

					foreach (var m in machine.Graph.Buzz.Song.Machines)
						Song_MachineAdded(m);

				}
			}
		}

Re: Bug: IMachineConnections on GUI init

Posted: Tue Apr 16, 2013 11:14 am
by IXix
oskari wrote:I think you are subscribing to IMachineGraph.ConnectionAdded in the wrong place (constructor?). It should be done in IMachineGUI.Machine setter as well.
I'm doing it in a function called from the setter. Everything happens in InitControl().

Code: Select all

public IMachine Machine
{
	get { return machine; }
	set
	{
		if (machine != null) { }

		machine = value;

		if (machine != null) { InitControl(); }
	}
}
InitControl() references Machine though where perhaps it should use machine instead? Would that make a difference?