|
Matthias@224
|
1 |
/* |
|
Matthias@224
|
2 |
* Copyright (c) 2008-2011, Matthias Mann |
|
Matthias@224
|
3 |
* |
|
Matthias@224
|
4 |
* All rights reserved. |
|
Matthias@224
|
5 |
* |
|
Matthias@224
|
6 |
* Redistribution and use in source and binary forms, with or without |
|
Matthias@224
|
7 |
* modification, are permitted provided that the following conditions are met: |
|
Matthias@224
|
8 |
* |
|
Matthias@224
|
9 |
* * Redistributions of source code must retain the above copyright notice, |
|
Matthias@224
|
10 |
* this list of conditions and the following disclaimer. |
|
Matthias@224
|
11 |
* * Redistributions in binary form must reproduce the above copyright |
|
Matthias@224
|
12 |
* notice, this list of conditions and the following disclaimer in the |
|
Matthias@224
|
13 |
* documentation and/or other materials provided with the distribution. |
|
Matthias@224
|
14 |
* * Neither the name of Matthias Mann nor the names of its contributors may |
|
Matthias@224
|
15 |
* be used to endorse or promote products derived from this software |
|
Matthias@224
|
16 |
* without specific prior written permission. |
|
Matthias@224
|
17 |
* |
|
Matthias@224
|
18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
Matthias@224
|
19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
Matthias@224
|
20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
Matthias@224
|
21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
Matthias@224
|
22 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
Matthias@224
|
23 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
Matthias@224
|
24 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
Matthias@224
|
25 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
Matthias@224
|
26 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
Matthias@224
|
27 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
Matthias@224
|
28 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
Matthias@224
|
29 |
*/ |
|
Matthias@224
|
30 |
package nodes; |
|
Matthias@224
|
31 |
|
|
Matthias@224
|
32 |
import de.matthiasmann.twl.Event; |
|
Matthias@224
|
33 |
import de.matthiasmann.twl.Widget; |
|
Matthias@224
|
34 |
import de.matthiasmann.twl.renderer.AnimationState.StateKey; |
|
Matthias@224
|
35 |
|
|
Matthias@224
|
36 |
/** |
|
Matthias@224
|
37 |
* |
|
Matthias@224
|
38 |
* @author Matthias Mann |
|
Matthias@224
|
39 |
*/ |
|
Matthias@224
|
40 |
public class Pad extends Widget { |
|
Matthias@224
|
41 |
|
|
Matthias@224
|
42 |
public static final StateKey STATE_HOVER = StateKey.get("hover"); |
|
Matthias@224
|
43 |
public static final StateKey STATE_DRAG_DESTINATION = StateKey.get("dragDestination"); |
|
Matthias@224
|
44 |
|
|
Matthias@224
|
45 |
public static final int RADIUS = 5; |
|
Matthias@224
|
46 |
|
|
Matthias@224
|
47 |
private final Node node; |
|
Matthias@224
|
48 |
private final boolean input; |
|
Matthias@224
|
49 |
private Connection inConnection; |
|
Matthias@224
|
50 |
|
|
Matthias@224
|
51 |
private boolean isDragActive; |
|
Matthias@224
|
52 |
private Pad dragDestinationPad; |
|
Matthias@224
|
53 |
|
|
Matthias@224
|
54 |
public Pad(Node node, boolean input) { |
|
Matthias@224
|
55 |
this.node = node; |
|
Matthias@224
|
56 |
this.input = input; |
|
Matthias@224
|
57 |
} |
|
Matthias@224
|
58 |
|
|
Matthias@224
|
59 |
public Node getNode() { |
|
Matthias@224
|
60 |
return node; |
|
Matthias@224
|
61 |
} |
|
Matthias@224
|
62 |
|
|
Matthias@224
|
63 |
public boolean isInput() { |
|
Matthias@224
|
64 |
return input; |
|
Matthias@224
|
65 |
} |
|
Matthias@224
|
66 |
|
|
Matthias@224
|
67 |
public Connection getInConnection() { |
|
Matthias@224
|
68 |
return inConnection; |
|
Matthias@224
|
69 |
} |
|
Matthias@224
|
70 |
|
|
Matthias@224
|
71 |
public void setInConnection(Connection inConnection) { |
|
Matthias@224
|
72 |
this.inConnection = inConnection; |
|
Matthias@224
|
73 |
} |
|
Matthias@224
|
74 |
|
|
Matthias@224
|
75 |
@Override |
|
Matthias@224
|
76 |
protected boolean handleEvent(Event evt) { |
|
Matthias@224
|
77 |
if(evt.isMouseEvent()) { |
|
Matthias@224
|
78 |
getAnimationState().setAnimationState(STATE_HOVER, evt.getType() != Event.Type.MOUSE_EXITED); |
|
Matthias@224
|
79 |
} |
|
Matthias@224
|
80 |
|
|
Matthias@224
|
81 |
if(evt.getType() == Event.Type.MOUSE_DRAGGED) { |
|
Matthias@224
|
82 |
NodeArea nodeArea = node.getNodeArea(); |
|
Matthias@224
|
83 |
|
|
Matthias@224
|
84 |
if(!isDragActive) { |
|
Matthias@224
|
85 |
isDragActive = true; |
|
Matthias@224
|
86 |
|
|
Matthias@224
|
87 |
if(isInput()) { |
|
Matthias@224
|
88 |
nodeArea.removeConnection(getInConnection()); |
|
Matthias@224
|
89 |
} |
|
Matthias@224
|
90 |
} |
|
Matthias@224
|
91 |
|
|
Matthias@224
|
92 |
nodeArea.dragNewConnection(this, evt.getMouseX(), evt.getMouseY()); |
|
Matthias@224
|
93 |
|
|
Matthias@224
|
94 |
Pad pad = nodeArea.padFromMouse(evt.getMouseX(), evt.getMouseY()); |
|
Matthias@224
|
95 |
setDragDestPad(pad); |
|
Matthias@224
|
96 |
} |
|
Matthias@224
|
97 |
|
|
Matthias@224
|
98 |
if(isDragActive && evt.isMouseDragEnd()) { |
|
Matthias@224
|
99 |
NodeArea nodeArea = node.getNodeArea(); |
|
Matthias@224
|
100 |
if(dragDestinationPad != null) { |
|
Matthias@224
|
101 |
if(isInput()) { |
|
Matthias@224
|
102 |
nodeArea.addConnection(dragDestinationPad, this); |
|
Matthias@224
|
103 |
} else { |
|
Matthias@224
|
104 |
nodeArea.addConnection(this, dragDestinationPad); |
|
Matthias@224
|
105 |
} |
|
Matthias@224
|
106 |
} |
|
Matthias@224
|
107 |
setDragDestPad(null); |
|
Matthias@224
|
108 |
nodeArea.dragNewConnection(null, 0, 0); |
|
Matthias@224
|
109 |
isDragActive = false; |
|
Matthias@224
|
110 |
} |
|
Matthias@224
|
111 |
|
|
Matthias@224
|
112 |
return evt.isMouseEventNoWheel(); |
|
Matthias@224
|
113 |
} |
|
Matthias@224
|
114 |
|
|
Matthias@224
|
115 |
@Override |
|
Matthias@224
|
116 |
public int getPreferredHeight() { |
|
Matthias@224
|
117 |
return RADIUS*2; |
|
Matthias@224
|
118 |
} |
|
Matthias@224
|
119 |
|
|
Matthias@224
|
120 |
@Override |
|
Matthias@224
|
121 |
public int getPreferredWidth() { |
|
Matthias@224
|
122 |
return RADIUS*2; |
|
Matthias@224
|
123 |
} |
|
Matthias@224
|
124 |
|
|
Matthias@224
|
125 |
public int getCenterX() { |
|
Matthias@224
|
126 |
return getX() + RADIUS; |
|
Matthias@224
|
127 |
} |
|
Matthias@224
|
128 |
|
|
Matthias@224
|
129 |
public int getCenterY() { |
|
Matthias@224
|
130 |
return getY() + RADIUS; |
|
Matthias@224
|
131 |
} |
|
Matthias@224
|
132 |
|
|
Matthias@224
|
133 |
@Override |
|
Matthias@224
|
134 |
public boolean isInside(int x, int y) { |
|
Matthias@224
|
135 |
int dx = x - getCenterX(); |
|
Matthias@224
|
136 |
int dy = y - getCenterY(); |
|
Matthias@224
|
137 |
return dx*dx + dy*dy <= RADIUS*RADIUS; |
|
Matthias@224
|
138 |
} |
|
Matthias@224
|
139 |
|
|
Matthias@224
|
140 |
private void setDragDestPad(Pad pad) { |
|
Matthias@224
|
141 |
if(pad != dragDestinationPad) { |
|
Matthias@224
|
142 |
if(dragDestinationPad != null) { |
|
Matthias@224
|
143 |
dragDestinationPad.getAnimationState().setAnimationState(STATE_DRAG_DESTINATION, false); |
|
Matthias@224
|
144 |
} |
|
Matthias@224
|
145 |
dragDestinationPad = pad; |
|
Matthias@224
|
146 |
if(dragDestinationPad != null) { |
|
Matthias@224
|
147 |
dragDestinationPad.getAnimationState().setAnimationState(STATE_DRAG_DESTINATION, true); |
|
Matthias@224
|
148 |
} |
|
Matthias@224
|
149 |
} |
|
Matthias@224
|
150 |
} |
|
Matthias@224
|
151 |
} |