PeriDyno 1.2.1
Loading...
Searching...
No Matches
WtFlowWidget.cpp
Go to the documentation of this file.
1#include "WtFlowWidget.h"
2
3#include "WtNodeStyle.h"
4#include <Wt/WPen.h>
5
6WtFlowWidget::WtFlowWidget(std::shared_ptr<dyno::SceneGraph> scene)
7 : mScene(scene)
8 , mZoomFactor(1.0)
9{
10 this->mouseWheel().connect(this, &WtFlowWidget::onMouseWheel);
11}
12
13void WtFlowWidget::onMouseWheel(const Wt::WMouseEvent& event)
14{
15 if (event.wheelDelta() > 0)
16 {
17 zoomIn();
18 }
19 else
20 {
21 zoomOut();
22 }
23}
24
26{
27 mZoomFactor *= 1.1;
28 update();
29}
30
32{
33 mZoomFactor /= 1.1;
34 update();
35}
36
38{
39 mZoomFactor = 1;
40 reorderFlag = true;
41 mTranslate = Wt::WPointF(0, 0);
42 update();
43}
44
46{
47 update();
48 mScene->setFrameNumber(0);
49 mScene->reset();
50 _updateCanvas.emit();
51}
52
53bool WtFlowWidget::checkMouseInRect(Wt::WPointF mousePoint, WtFlowNodeData nodeData)
54{
55 Wt::WPointF bottomRight = Wt::WPointF(nodeData.getNodeBoundingRect().bottomRight().x() + nodeData.getNodeOrigin().x()
56 , nodeData.getNodeBoundingRect().bottomRight().y() + nodeData.getNodeOrigin().y());
57
58 Wt::WPointF absTopLeft = Wt::WPointF((nodeData.getNodeOrigin().x() + mTranslate.x() - 10) * mZoomFactor, (nodeData.getNodeOrigin().y() + mTranslate.y() - 10) * mZoomFactor);
59 Wt::WPointF absBottomRight = Wt::WPointF((bottomRight.x() + mTranslate.x() + 10) * mZoomFactor, (bottomRight.y() + mTranslate.y() + 10) * mZoomFactor);
60
61 Wt::WRectF absRect = Wt::WRectF(absTopLeft, absBottomRight);
62
63 return absRect.contains(mousePoint);
64}
65
66bool WtFlowWidget::checkMouseInPoints(Wt::WPointF mousePoint, WtFlowNodeData nodeData, PortState portState)
67{
68 auto pointsData = nodeData.getPointsData();
69 Wt::WPointF origin = nodeData.getNodeOrigin();
70 Wt::WPointF trueMouse = Wt::WPointF(mousePoint.x() / mZoomFactor - mTranslate.x(), mousePoint.y() / mZoomFactor - mTranslate.y());
71
72 for (connectionPointData pointData : pointsData)
73 {
74 if (pointData.portShape == PortShape::Bullet)
75 {
76 Wt::WPointF topLeft = Wt::WPointF(pointData.diamond_out[3].x() + origin.x(), pointData.diamond_out[2].y() + origin.y());
77 Wt::WPointF bottomRight = Wt::WPointF(pointData.diamond_out[1].x() + origin.x(), pointData.diamond_out[0].y() + origin.y());
78 Wt::WRectF diamondBoundingRect = Wt::WRectF(topLeft, bottomRight);
79 if (diamondBoundingRect.contains(trueMouse))
80 {
81 sourcePoint = Wt::WPointF((topLeft.x() + bottomRight.x()) / 2, (topLeft.y() + bottomRight.y()) / 2);
82 if (portState == PortState::out)
83 {
84 outPoint = pointData;
85 }
86 if (portState == PortState::in)
87 {
88 inPoint = pointData;
89 }
90 return true;
91 }
92 }
93 else if (pointData.portShape == PortShape::Diamond)
94 {
95 Wt::WPointF topLeft = Wt::WPointF(pointData.diamond[3].x() + origin.x(), pointData.diamond[2].y() + origin.y());
96 Wt::WPointF bottomRight = Wt::WPointF(pointData.diamond[1].x() + origin.x(), pointData.diamond[0].y() + origin.y());
97 Wt::WRectF diamondBoundingRect = Wt::WRectF(topLeft, bottomRight);
98 if (diamondBoundingRect.contains(trueMouse))
99 {
100 sourcePoint = Wt::WPointF((topLeft.x() + bottomRight.x()) / 2, (topLeft.y() + bottomRight.y()) / 2);
101 if (portState == PortState::out)
102 {
103 outPoint = pointData;
104 }
105 if (portState == PortState::in)
106 {
107 inPoint = pointData;
108 }
109 return true;
110 }
111 }
112 else if (pointData.portShape == PortShape::Point)
113 {
114 auto rectTopLeft = pointData.pointRect.topLeft();
115 auto rectBottomRight = pointData.pointRect.bottomRight();
116 Wt::WPointF topLeft = Wt::WPointF(rectTopLeft.x() + origin.x(), rectTopLeft.y() + origin.y());
117 Wt::WPointF bottomRight = Wt::WPointF(rectBottomRight.x() + origin.x(), rectBottomRight.y() + origin.y());
118 Wt::WRectF diamondBoundingRect = Wt::WRectF(topLeft, bottomRight);
119 if (diamondBoundingRect.contains(trueMouse))
120 {
121 sourcePoint = Wt::WPointF((topLeft.x() + bottomRight.x()) / 2, (topLeft.y() + bottomRight.y()) / 2);
122 if (portState == PortState::out)
123 {
124 outPoint = pointData;
125 }
126 if (portState == PortState::in)
127 {
128 inPoint = pointData;
129 }
130 return true;
131 }
132 }
133 }
134 return false;
135}
136
137Wt::WPainterPath WtFlowWidget::cubicPath(Wt::WPointF source, Wt::WPointF sink)
138{
139 auto c1c2 = pointsC1C2(source, sink);
140
141 //cubic spline
142 Wt::WPainterPath cubic(source);
143
144 cubic.cubicTo(c1c2.first, c1c2.second, sink);
145
146 return cubic;
147}
148
149std::pair<Wt::WPointF, Wt::WPointF> WtFlowWidget::pointsC1C2(Wt::WPointF source, Wt::WPointF sink)
150{
151 const double defaultOffset = 200;
152
153 double xDistance = sink.x() - source.x();
154
155 double horizontalOffset = std::min(defaultOffset, std::abs(xDistance));
156
157 double verticalOffset = 0;
158
159 double ratioX = 0.5;
160
161 if (xDistance <= 0)
162 {
163 double yDistance = sink.y() - source.y();
164
165 double vector = yDistance < 0 ? -1.0 : 1.0;
166
167 verticalOffset = std::min(defaultOffset, std::abs(yDistance)) * vector;
168
169 ratioX = 1.0;
170 }
171
172 horizontalOffset *= ratioX;
173
174 Wt::WPointF c1(source.x() + horizontalOffset, source.y() + verticalOffset);
175 Wt::WPointF c2(sink.x() - horizontalOffset, sink.y() - verticalOffset);
176
177 return std::make_pair(c1, c2);
178}
179
180void WtFlowWidget::drawSketchLine(Wt::WPainter* painter, Wt::WPointF source, Wt::WPointF sink)
181{
182 auto const& connectionStyle = WtStyleCollection::connectionStyle();
183
184 Wt::WPen p;
185 p.setWidth(connectionStyle.constructionLineWidth());
186 p.setColor(connectionStyle.constructionColor());
187 p.setStyle(Wt::PenStyle::DashLine);
188
189 painter->setPen(p);
190 painter->setBrush(Wt::BrushStyle::None);
191
192 auto cubic = cubicPath(source, sink);
193
194 painter->drawPath(cubic);
195}
PortState
@ in
@ out
Wt::WRectF getNodeBoundingRect() const
std::vector< connectionPointData > getPointsData()
Wt::WPointF getNodeOrigin() const
connectionPointData outPoint
void onMouseWheel(const Wt::WMouseEvent &event)
Wt::Signal _updateCanvas
WtFlowWidget(std::shared_ptr< dyno::SceneGraph > scene)
Wt::WPainterPath cubicPath(Wt::WPointF source, Wt::WPointF sink)
Wt::WPointF sourcePoint
std::shared_ptr< dyno::SceneGraph > mScene
connectionPointData inPoint
bool checkMouseInRect(Wt::WPointF mousePoint, WtFlowNodeData nodeData)
std::pair< Wt::WPointF, Wt::WPointF > pointsC1C2(Wt::WPointF source, Wt::WPointF sink)
void drawSketchLine(Wt::WPainter *painter, Wt::WPointF source, Wt::WPointF sink)
double mZoomFactor
Wt::WPointF mTranslate
bool checkMouseInPoints(Wt::WPointF mousePoint, WtFlowNodeData nodeData, PortState portState)
static WtConnectionStyle const & connectionStyle()