xref: /haiku/src/apps/cortex/DiagramView/DiagramWire.cpp (revision 8195a5a835117ab2da405e0d477153570b75d921)
1 // DiagramWire.cpp
2 
3 #include "DiagramWire.h"
4 #include "DiagramDefs.h"
5 #include "DiagramEndPoint.h"
6 #include "DiagramView.h"
7 
8 #include <Message.h>
9 #include <Messenger.h>
10 
11 __USE_CORTEX_NAMESPACE
12 
13 #include <Debug.h>
14 #define D_METHOD(x) //PRINT (x)
15 #define D_MESSAGE(x) //PRINT (x)
16 
17 // -------------------------------------------------------- //
18 // *** ctor/dtor
19 // -------------------------------------------------------- //
20 
21 DiagramWire::DiagramWire(
22 	DiagramEndPoint *fromWhich,
23 	DiagramEndPoint *toWhich)
24 	: DiagramItem(DiagramItem::M_WIRE),
25 	  m_fromEndPoint(fromWhich),
26 	  m_toEndPoint(toWhich),
27 	  m_temporary(false)
28 {
29 	D_METHOD(("DiagramWire::DiagramWire()\n"));
30 	makeDraggable(false);
31 	ASSERT(m_fromEndPoint);
32 	m_fromEndPoint->connect(this);
33 	ASSERT(m_toEndPoint);
34 	m_toEndPoint->connect(this);
35 }
36 
37 DiagramWire::DiagramWire(
38 	DiagramEndPoint *fromWhich,
39 	bool isStartPoint)
40 	: DiagramItem(DiagramItem::M_WIRE),
41 	  m_fromEndPoint(isStartPoint ? fromWhich : 0),
42 	  m_toEndPoint(isStartPoint ? 0 : fromWhich),
43 	  m_temporary(true),
44 	  m_dragEndPoint(fromWhich->connectionPoint())
45 {
46 	D_METHOD(("DiagramWire::DiagramWire(temp)\n"));
47 	makeDraggable(false);
48 	ASSERT(fromWhich);
49 	if (m_fromEndPoint)
50 	{
51 		m_fromEndPoint->connect(this);
52 	}
53 	else if (m_toEndPoint)
54 	{
55 		m_toEndPoint->connect(this);
56 	}
57 }
58 
59 DiagramWire::~DiagramWire()
60 {
61 	D_METHOD(("DiagramWire::~DiagramWire()\n"));
62 	if (m_fromEndPoint)
63 	{
64 		m_fromEndPoint->disconnect();
65 	}
66 	if (m_toEndPoint)
67 	{
68 		m_toEndPoint->disconnect();
69 	}
70 }
71 
72 // -------------------------------------------------------- //
73 // *** accessors
74 // -------------------------------------------------------- //
75 
76 BPoint DiagramWire::startConnectionPoint() const
77 {
78 	D_METHOD(("DiagramWire::startConnectionPoint()\n"));
79 	if (m_fromEndPoint)
80 	{
81 		return m_fromEndPoint->connectionPoint();
82 	}
83 	else if (m_temporary)
84 	{
85 		return m_dragEndPoint;
86 	}
87 	return BPoint(-1.0, -1.0);
88 }
89 
90 BPoint DiagramWire::endConnectionPoint() const
91 {
92 	D_METHOD(("DiagramWire::endConnectionPoint()\n"));
93 	if (m_toEndPoint)
94 	{
95 		return m_toEndPoint->connectionPoint();
96 	}
97 	else if (m_temporary)
98 	{
99 		return m_dragEndPoint;
100 	}
101 	return BPoint(-1.0, -1.0);
102 }
103 
104 // -------------------------------------------------------- //
105 // *** derived from DiagramItem
106 // -------------------------------------------------------- //
107 
108 float DiagramWire::howCloseTo(
109 	BPoint point) const
110 {
111 	D_METHOD(("DiagramWire::howCloseTo()\n"));
112 	BPoint a = startConnectionPoint();
113 	BPoint b = endConnectionPoint();
114 	float length, result;
115 	length = sqrt(pow(b.x - a.x, 2) + pow(b.y - a.y, 2));
116 	result = ((a.y - point.y) * (b.x - a.x)) - ((a.x - point.x) * (b.y - a.y));
117 	result = 3.0 - fabs(result / length);
118 	return result;
119 }
120 
121 void DiagramWire::Draw(
122 	BRect updateRect)
123 {
124 	D_METHOD(("DiagramWire::Draw()\n"));
125 	if (view())
126 	{
127 		view()->PushState();
128 		{
129 			BRegion region, clipping;
130 			region.Include(Frame() & updateRect);
131 			if (view()->GetClippingAbove(this, &clipping))
132 				region.Exclude(&clipping);
133 			view()->ConstrainClippingRegion(&region);
134 			drawWire();
135 		}
136 		view()->PopState();
137 	}
138 }
139 
140 void DiagramWire::MouseDown(
141 	BPoint point,
142 	uint32 buttons,
143 	uint32 clicks)
144 {
145 	D_METHOD(("DiagramWire::MouseDown()\n"));
146 	if (clicks == 1)
147 	{
148 		if (isSelectable())
149 		{
150 			BMessage selectMsg(M_SELECTION_CHANGED);
151 			if (modifiers() & B_SHIFT_KEY)
152 			{
153 				selectMsg.AddBool("replace", false);
154 			}
155 			else
156 			{
157 				selectMsg.AddBool("replace", true);
158 			}
159 			selectMsg.AddPointer("item", reinterpret_cast<void *>(this));
160 			DiagramView* v = view();
161 			BMessenger(v).SendMessage(&selectMsg);
162 		}
163 	}
164 }
165 
166 void DiagramWire::MessageDragged(
167 	BPoint point,
168 	uint32 transit,
169 	const BMessage *message)
170 {
171 	D_METHOD(("DiagramWire::MessageDragged()\n"));
172 	switch (message->what)
173 	{
174 		case M_WIRE_DRAGGED:
175 		{
176 			view()->trackWire(point);
177 			break;
178 		}
179 		default:
180 		{
181 			DiagramItem::MessageDragged(point, transit, message);
182 		}
183 	}
184 }
185 
186 // END -- DiagramWire.cpp --
187