FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
nodeeditor/src/NodeConnectionInteraction.cpp at bt · BehaviorTree/nodeeditor · GitHub
BehaviorTree
/
nodeeditor
Public
forked from
l3enQ/nodeeditor
Notifications
You must be signed in to change notification settings
Fork
0
Star
4
Code
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
nodeeditor
/
src
/
NodeConnectionInteraction.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
152 lines (104 loc) · 4.88 KB
Breadcrumbs
nodeeditor
/
src
/
NodeConnectionInteraction.cpp
Copy path
File metadata and controls
152 lines (104 loc) · 4.88 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#
include
"
NodeConnectionInteraction.hpp
"
#
include
"
AbstractNodeGeometry.hpp
"
#
include
"
BasicGraphicsScene.hpp
"
#
include
"
ConnectionGraphicsObject.hpp
"
#
include
"
ConnectionIdUtils.hpp
"
#
include
"
NodeGraphicsObject.hpp
"
#
include
"
UndoCommands.hpp
"
#
include
<
QtCore/QDebug
>
#
include
<
QUndoStack
>
namespace
QtNodes
{
NodeConnectionInteraction::NodeConnectionInteraction
(NodeGraphicsObject &ngo,
ConnectionGraphicsObject &cgo,
BasicGraphicsScene &scene)
: _ngo(ngo)
, _cgo(cgo)
, _scene(scene)
{}
bool
NodeConnectionInteraction::canConnect
(PortIndex *portIndex)
const
{
//
1. Connection requires a port.
PortType requiredPort = _cgo.
connectionState
().
requiredPort
();
if
(requiredPort == PortType::None) {
return
false
;
}
NodeId connectedNodeId =
getNodeId
(
oppositePort
(requiredPort), _cgo.
connectionId
());
//
2. Forbid connecting the node to itself.
if
(_ngo.
nodeId
() == connectedNodeId)
return
false
;
//
3. Connection loose end is above the node port.
QPointF connectionPoint = _cgo.
sceneTransform
().
map
(_cgo.
endPoint
(requiredPort));
*portIndex =
nodePortIndexUnderScenePoint
(requiredPort, connectionPoint);
if
(*portIndex == InvalidPortIndex) {
return
false
;
}
//
4. Model allows connection.
AbstractGraphModel &model = _ngo.
nodeScene
()->
graphModel
();
ConnectionId connectionId =
makeCompleteConnectionId
(_cgo.
connectionId
(),
//
incomplete
_ngo.
nodeId
(),
//
missing node id
*portIndex);
//
missing port index
return
model.
connectionPossible
(connectionId);
}
bool
NodeConnectionInteraction::tryConnect
()
const
{
//
1. Check conditions from 'canConnect'.
PortIndex targetPortIndex = InvalidPortIndex;
if
(!
canConnect
(&targetPortIndex)) {
return
false
;
}
//
2. Create new connection.
ConnectionId incompleteConnectionId = _cgo.
connectionId
();
ConnectionId newConnectionId =
makeCompleteConnectionId
(incompleteConnectionId,
_ngo.
nodeId
(),
targetPortIndex);
_ngo.
nodeScene
()->
resetDraftConnection
(
true
);
_ngo.
nodeScene
()->
undoStack
().
push
(
new
ConnectCommand
(_ngo.
nodeScene
(), newConnectionId));
return
true
;
}
bool
NodeConnectionInteraction::disconnect
(PortType portToDisconnect)
const
{
ConnectionId connectionId = _cgo.
connectionId
();
_scene.
undoStack
().
push
(
new
DisconnectCommand
(&_scene, connectionId));
AbstractNodeGeometry &geometry = _scene.
nodeGeometry
();
QPointF scenePos = geometry.
portScenePosition
(_ngo.
nodeId
(),
portToDisconnect,
getPortIndex
(portToDisconnect, connectionId),
_ngo.
sceneTransform
());
//
Converted to "draft" connection with the new incomplete id.
ConnectionId incompleteConnectionId =
makeIncompleteConnectionId
(connectionId, portToDisconnect);
//
Grabs the mouse
auto
const
&draftConnection = _scene.
makeDraftConnection
(incompleteConnectionId);
QPointF
const
looseEndPos = draftConnection->
mapFromScene
(scenePos);
draftConnection->
setEndPoint
(portToDisconnect, looseEndPos);
//
Repaint connection points.
NodeId connectedNodeId =
getNodeId
(
oppositePort
(portToDisconnect), connectionId);
_scene.
nodeGraphicsObject
(connectedNodeId)->
update
();
NodeId disconnectedNodeId =
getNodeId
(portToDisconnect, connectionId);
_scene.
nodeGraphicsObject
(disconnectedNodeId)->
update
();
return
true
;
}
//
------------------ util functions below
PortType
NodeConnectionInteraction::connectionRequiredPort
()
const
{
auto
const
&state = _cgo.
connectionState
();
return
state.
requiredPort
();
}
QPointF
NodeConnectionInteraction::nodePortScenePosition
(PortType portType,
PortIndex portIndex)
const
{
AbstractNodeGeometry &geometry = _scene.
nodeGeometry
();
QPointF p = geometry.
portScenePosition
(_ngo.
nodeId
(),
portType,
portIndex,
_ngo.
sceneTransform
());
return
p;
}
PortIndex
NodeConnectionInteraction::nodePortIndexUnderScenePoint
(PortType portType,
QPointF
const
&scenePoint)
const
{
AbstractNodeGeometry &geometry = _scene.
nodeGeometry
();
QTransform sceneTransform = _ngo.
sceneTransform
();
QPointF nodePoint = sceneTransform.
inverted
().
map
(scenePoint);
return
geometry.
checkPortHit
(_ngo.
nodeId
(), portType, nodePoint);
}
}
//
namespace QtNodes
Back
|
FazBrowse Home
|
New Git URL