Bug: IMachineConnections on GUI init

Post Reply
User avatar
IXix
Posts: 1113
Joined: Wed Nov 23, 2011 3:24 pm

Bug: IMachineConnections on GUI init

Post 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;
	}
}
oskari
Site Admin
Posts: 296
Joined: Mon Nov 21, 2011 2:04 pm

Re: Bug: IMachineConnections on GUI init

Post 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)

User avatar
IXix
Posts: 1113
Joined: Wed Nov 23, 2011 3:24 pm

Re: Bug: IMachineConnections on GUI init

Post 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.
oskari
Site Admin
Posts: 296
Joined: Mon Nov 21, 2011 2:04 pm

Re: Bug: IMachineConnections on GUI init

Post 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);

				}
			}
		}
User avatar
IXix
Posts: 1113
Joined: Wed Nov 23, 2011 3:24 pm

Re: Bug: IMachineConnections on GUI init

Post 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?
Post Reply