Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing lines between windows just like in Interface Builder

I have two windows, each with a round NSView:

------------------     ------------
|X-+ Oscillator  |     |X-+ Mixer |
|----------------|     |----------|
|                |     |          |
|        O       |     |     O    |
|                |     |          |
|                |     |          |
------------------     ------------

Both windows are in a seperate NIB. I want to be able to draw a line between the two round NSViews at runtime, using drag'n'drop, just like connecting IBOutlets in Interface Builder:

------------------     ------------
|X-+ Oscillator  |     |X-+ Mixer |
|----------------|     |----------|
|                |     |          |
|        O-------+-----+-----O    |
|                |     |          |
|                |     |          |
------------------     ------------

I also want to be able to determine to which NSViews the NSViews are connected using the drawn lines. I need this to connect Oscillators to Mixers. I also want to be able to remove the connections by dragging into empty space.

Can anyone explain me how to do this? Thanks.


1 Answers

The problem is having a view into which you can draw that spans the two windows' combined rects (or the screen or screens they're on). This is typically done with a borderless, transparent "overlay window." There are lots of examples of a borderless, transparent window available online.

On the start of the drag, the overlay window appears (but is invisible to the user) positioned over your drawing area. As the drag is updated, you draw your line, translating the overlay view coordinates to/from the source/target view coordinates. When the drag is complete, do some animation (like a fade or a blink) then order out the overlay window.

like image 159
Joshua Nozzi Avatar answered Dec 17 '25 01:12

Joshua Nozzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!