2 * Copyright (c) 1999-2000, Eric Moon.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions, and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "DiagramWire.h"
35 #include "DiagramDefs.h"
36 #include "DiagramEndPoint.h"
37 #include "DiagramView.h"
40 #include <Messenger.h>
42 __USE_CORTEX_NAMESPACE
45 #define D_METHOD(x) //PRINT (x)
46 #define D_MESSAGE(x) //PRINT (x)
48 // -------------------------------------------------------- //
50 // -------------------------------------------------------- //
52 DiagramWire::DiagramWire(
53 DiagramEndPoint
*fromWhich
,
54 DiagramEndPoint
*toWhich
)
55 : DiagramItem(DiagramItem::M_WIRE
),
56 m_fromEndPoint(fromWhich
),
57 m_toEndPoint(toWhich
),
60 D_METHOD(("DiagramWire::DiagramWire()\n"));
62 ASSERT(m_fromEndPoint
);
63 m_fromEndPoint
->connect(this);
65 m_toEndPoint
->connect(this);
68 DiagramWire::DiagramWire(
69 DiagramEndPoint
*fromWhich
,
71 : DiagramItem(DiagramItem::M_WIRE
),
72 m_fromEndPoint(isStartPoint
? fromWhich
: 0),
73 m_toEndPoint(isStartPoint
? 0 : fromWhich
),
75 m_dragEndPoint(fromWhich
->connectionPoint())
77 D_METHOD(("DiagramWire::DiagramWire(temp)\n"));
82 m_fromEndPoint
->connect(this);
84 else if (m_toEndPoint
)
86 m_toEndPoint
->connect(this);
90 DiagramWire::~DiagramWire()
92 D_METHOD(("DiagramWire::~DiagramWire()\n"));
95 m_fromEndPoint
->disconnect();
99 m_toEndPoint
->disconnect();
103 // -------------------------------------------------------- //
105 // -------------------------------------------------------- //
107 BPoint
DiagramWire::startConnectionPoint() const
109 D_METHOD(("DiagramWire::startConnectionPoint()\n"));
112 return m_fromEndPoint
->connectionPoint();
114 else if (m_temporary
)
116 return m_dragEndPoint
;
118 return BPoint(-1.0, -1.0);
121 BPoint
DiagramWire::endConnectionPoint() const
123 D_METHOD(("DiagramWire::endConnectionPoint()\n"));
126 return m_toEndPoint
->connectionPoint();
128 else if (m_temporary
)
130 return m_dragEndPoint
;
132 return BPoint(-1.0, -1.0);
135 // -------------------------------------------------------- //
136 // *** derived from DiagramItem
137 // -------------------------------------------------------- //
139 float DiagramWire::howCloseTo(
142 D_METHOD(("DiagramWire::howCloseTo()\n"));
143 BPoint a
= startConnectionPoint();
144 BPoint b
= endConnectionPoint();
145 float length
, result
;
146 length
= sqrt(pow(b
.x
- a
.x
, 2) + pow(b
.y
- a
.y
, 2));
147 result
= ((a
.y
- point
.y
) * (b
.x
- a
.x
)) - ((a
.x
- point
.x
) * (b
.y
- a
.y
));
148 result
= 3.0 - fabs(result
/ length
);
152 void DiagramWire::Draw(
155 D_METHOD(("DiagramWire::Draw()\n"));
160 BRegion region
, clipping
;
161 region
.Include(Frame() & updateRect
);
162 if (view()->GetClippingAbove(this, &clipping
))
163 region
.Exclude(&clipping
);
164 view()->ConstrainClippingRegion(®ion
);
171 void DiagramWire::MouseDown(
176 D_METHOD(("DiagramWire::MouseDown()\n"));
181 BMessage
selectMsg(M_SELECTION_CHANGED
);
182 if (modifiers() & B_SHIFT_KEY
)
184 selectMsg
.AddBool("replace", false);
188 selectMsg
.AddBool("replace", true);
190 selectMsg
.AddPointer("item", reinterpret_cast<void *>(this));
191 DiagramView
* v
= view();
192 BMessenger(v
).SendMessage(&selectMsg
);
197 void DiagramWire::MessageDragged(
200 const BMessage
*message
)
202 D_METHOD(("DiagramWire::MessageDragged()\n"));
203 switch (message
->what
)
207 view()->trackWire(point
);
212 DiagramItem::MessageDragged(point
, transit
, message
);
217 // END -- DiagramWire.cpp --