|
@@ -5,8 +5,11 @@ import javafx.beans.property.SimpleDoubleProperty;
|
|
|
import javafx.beans.value.ChangeListener;
|
|
|
import javafx.beans.value.ObservableValue;
|
|
|
import javafx.event.Event;
|
|
|
+import javafx.geometry.Bounds;
|
|
|
import javafx.scene.Group;
|
|
|
import javafx.scene.input.*;
|
|
|
+import javafx.scene.layout.GridPane;
|
|
|
+import javafx.scene.layout.StackPane;
|
|
|
import javafx.scene.paint.Color;
|
|
|
import javafx.scene.shape.Circle;
|
|
|
import javafx.scene.text.Font;
|
|
@@ -19,19 +22,22 @@ import org.json.simple.parser.ParseException;
|
|
|
import java.util.Vector;
|
|
|
import java.util.function.DoubleUnaryOperator;
|
|
|
|
|
|
-public class Connectable extends Group implements IGUIEventClient ,ChangeListener<Number>{
|
|
|
+public class Connectable extends GridPane implements IGUIEventClient ,ChangeListener<Number>{
|
|
|
private GUIEventHandler _eventHandler;
|
|
|
private Circle _border;
|
|
|
private Circle _circle;
|
|
|
- public boolean connected = false;
|
|
|
+ private boolean _connected = false;
|
|
|
+ private int _connections = 0;
|
|
|
+
|
|
|
public boolean optimizable = false;
|
|
|
private Module _parent;
|
|
|
private ConnectableType _type;
|
|
|
|
|
|
+ protected StackPane _connection;
|
|
|
protected Text _label;
|
|
|
public String Name = "";
|
|
|
public String ID = "";
|
|
|
- public int Appendix = 0;
|
|
|
+ public long Appendix = 0;
|
|
|
public Vector<Double> Value = new Vector<>();
|
|
|
public DoubleProperty x = new SimpleDoubleProperty();
|
|
|
public DoubleProperty y = new SimpleDoubleProperty();
|
|
@@ -46,8 +52,8 @@ public class Connectable extends Group implements IGUIEventClient ,ChangeListene
|
|
|
if (json.containsKey("name")){
|
|
|
Name = (String) json.get("name");
|
|
|
}
|
|
|
- if (json.containsKey("id")){
|
|
|
- Appendix = (int) json.get("id");
|
|
|
+ if (json.containsKey("appendix")){
|
|
|
+ Appendix = (long) json.get("appendix");
|
|
|
}
|
|
|
if (json.containsKey("value")){
|
|
|
for (Object o: (JSONArray)json.get("value")) {
|
|
@@ -55,6 +61,9 @@ public class Connectable extends Group implements IGUIEventClient ,ChangeListene
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ setHgap(5);
|
|
|
+ setVgap(5);
|
|
|
+
|
|
|
ID = Name + Appendix;
|
|
|
_label = new Text(Name);
|
|
|
|
|
@@ -67,9 +76,15 @@ public class Connectable extends Group implements IGUIEventClient ,ChangeListene
|
|
|
_parent.hlayout.translateXProperty().addListener(this);
|
|
|
_parent.hlayout.translateYProperty().addListener(this);
|
|
|
|
|
|
+ _parent.hlayout.layoutXProperty().addListener(this);
|
|
|
+ _parent.hlayout.layoutYProperty().addListener(this);
|
|
|
+
|
|
|
_parent.vlayout.translateXProperty().addListener(this);
|
|
|
_parent.vlayout.translateYProperty().addListener(this);
|
|
|
|
|
|
+ _parent.vlayout.layoutXProperty().addListener(this);
|
|
|
+ _parent.vlayout.layoutYProperty().addListener(this);
|
|
|
+
|
|
|
if (_type == ConnectableType.INPUT){
|
|
|
_parent.inputs.layoutXProperty().addListener(this);
|
|
|
_parent.inputs.layoutYProperty().addListener(this);
|
|
@@ -87,9 +102,11 @@ public class Connectable extends Group implements IGUIEventClient ,ChangeListene
|
|
|
_circle = new Circle(0,0,5);
|
|
|
_circle.setFill(Color.WHITE);
|
|
|
|
|
|
- _circle.setOnMouseClicked(e -> {
|
|
|
+ _connection = new StackPane(_border,_circle);
|
|
|
+
|
|
|
+ _connection.setOnMouseClicked(e -> {
|
|
|
if (e.getButton() == MouseButton.SECONDARY) {
|
|
|
- if (!connected) {
|
|
|
+ if (!_connected) {
|
|
|
optimizable = !optimizable;
|
|
|
updateColor();
|
|
|
}
|
|
@@ -97,9 +114,14 @@ public class Connectable extends Group implements IGUIEventClient ,ChangeListene
|
|
|
|
|
|
});
|
|
|
|
|
|
- getChildren().addAll(_border, _circle);
|
|
|
+ if (_type == ConnectableType.INPUT){
|
|
|
+ add(_connection,0,0);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ add(_connection,1,0);
|
|
|
+ }
|
|
|
|
|
|
- _circle.addEventHandler(MouseEvent.DRAG_DETECTED, e -> {
|
|
|
+ _connection.addEventHandler(MouseEvent.DRAG_DETECTED, e -> {
|
|
|
Dragboard db = startDragAndDrop(TransferMode.MOVE);
|
|
|
ClipboardContent content = new ClipboardContent();
|
|
|
JSONObject jmsg = generateJSON();
|
|
@@ -110,9 +132,9 @@ public class Connectable extends Group implements IGUIEventClient ,ChangeListene
|
|
|
e.consume();
|
|
|
});
|
|
|
|
|
|
- _circle.addEventHandler(MouseEvent.MOUSE_DRAGGED, Event::consume);
|
|
|
+ _connection.addEventHandler(MouseEvent.MOUSE_DRAGGED, Event::consume);
|
|
|
|
|
|
- _circle.addEventHandler(DragEvent.DRAG_OVER, e -> {
|
|
|
+ _connection.addEventHandler(DragEvent.DRAG_OVER, e -> {
|
|
|
Dragboard db = e.getDragboard();
|
|
|
if (db.hasString()) {
|
|
|
e.acceptTransferModes(TransferMode.MOVE);
|
|
@@ -120,7 +142,7 @@ public class Connectable extends Group implements IGUIEventClient ,ChangeListene
|
|
|
e.consume();
|
|
|
});
|
|
|
|
|
|
- _circle.addEventHandler(DragEvent.DRAG_DROPPED, e -> {
|
|
|
+ _connection.addEventHandler(DragEvent.DRAG_DROPPED, e -> {
|
|
|
Dragboard db = e.getDragboard();
|
|
|
boolean success = false;
|
|
|
if (db.hasString()) {
|
|
@@ -146,7 +168,7 @@ public class Connectable extends Group implements IGUIEventClient ,ChangeListene
|
|
|
//e.consume();
|
|
|
});
|
|
|
|
|
|
- _circle.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
|
|
|
+ _connection.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
|
|
|
if(e.getButton() == MouseButton.PRIMARY) {
|
|
|
_eventHandler.publishEvent("MOUSE_PRESSED", generateJSON());
|
|
|
}
|
|
@@ -158,7 +180,7 @@ public class Connectable extends Group implements IGUIEventClient ,ChangeListene
|
|
|
}
|
|
|
|
|
|
private void updateColor(){
|
|
|
- if (connected){
|
|
|
+ if (_connected){
|
|
|
_circle.setFill(Color.YELLOW);
|
|
|
} else if (optimizable) {
|
|
|
_circle.setFill(Color.GREEN);
|
|
@@ -183,23 +205,61 @@ public class Connectable extends Group implements IGUIEventClient ,ChangeListene
|
|
|
return json;
|
|
|
}
|
|
|
|
|
|
+ public JSONObject getJSONIdentifier(){
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ json.put("prefix", _parent.getAsPrefix());
|
|
|
+ json.put("name", Name);
|
|
|
+ json.put("id", Appendix);
|
|
|
+ if (_type == ConnectableType.INPUT){
|
|
|
+ json.put("type", "input");
|
|
|
+ }else if(_type == ConnectableType.OUTPUT){
|
|
|
+ json.put("type", "output");
|
|
|
+ }else{
|
|
|
+ json.put("type", "anything");
|
|
|
+ }
|
|
|
+ return json;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isConnected(){
|
|
|
+ return _connected;
|
|
|
+ }
|
|
|
+
|
|
|
public void setConnected(boolean state){
|
|
|
- if (state != connected){
|
|
|
- connected = state;
|
|
|
+ if (state){
|
|
|
+ ++_connections;
|
|
|
+ if (_type == ConnectableType.INPUT && _connections > 1){
|
|
|
+ _connections = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ --_connections;
|
|
|
+ if (_connections < 0){
|
|
|
+ _connections = 0;
|
|
|
+ }
|
|
|
+ if (_connections > 0){
|
|
|
+ state = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (state != _connected){
|
|
|
+ _connected = state;
|
|
|
updateColor();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
|
|
|
+ double dx, dy;
|
|
|
if (_type == ConnectableType.INPUT) {
|
|
|
- x.setValue(_parent.getTranslateX() + _parent.inputs.getTranslateX() + _parent.inputs.getLayoutX() + _parent.hlayout.getTranslateX() + _parent.hlayout.getLayoutX() + _parent.vlayout.getTranslateX() + _parent.vlayout.getLayoutX() + getLayoutX());
|
|
|
- y.setValue(_parent.getTranslateY() + _parent.inputs.getLayoutY() + _parent.hlayout.getTranslateY() + _parent.hlayout.getLayoutY() + _parent.vlayout.getTranslateY() + _parent.vlayout.getLayoutY() + getLayoutY());
|
|
|
+ dx = _parent.inputs.getTranslateX() + _parent.inputs.getLayoutX();
|
|
|
+ dy = _parent.inputs.getLayoutY();
|
|
|
}
|
|
|
else{
|
|
|
- x.setValue(_parent.getTranslateX() + _parent.outputs.getTranslateX() + _parent.outputs.getLayoutX() + _parent.hlayout.getTranslateX() + _parent.hlayout.getLayoutX() + _parent.vlayout.getTranslateX() + _parent.vlayout.getLayoutX() + getLayoutX());
|
|
|
- y.setValue(_parent.getTranslateY() + _parent.outputs.getLayoutY() + _parent.hlayout.getTranslateY() + _parent.hlayout.getLayoutY() + _parent.vlayout.getTranslateY() + _parent.vlayout.getLayoutY() + getLayoutY());
|
|
|
+ dx = _parent.outputs.getTranslateX() + _parent.outputs.getLayoutX();
|
|
|
+ dy = _parent.outputs.getLayoutY();
|
|
|
}
|
|
|
+ Bounds con_bounds = _connection.getLayoutBounds();
|
|
|
+ x.setValue(dx + _parent.getTranslateX()+ _parent.hlayout.getTranslateX() + _parent.hlayout.getLayoutX() + _parent.vlayout.getTranslateX() + _parent.vlayout.getLayoutX() + getLayoutX() + _connection.getLayoutX() + con_bounds.getWidth()/2);
|
|
|
+ y.setValue(dy + _parent.getTranslateY()+ _parent.hlayout.getTranslateY() + _parent.hlayout.getLayoutY()+ _parent.vlayout.getTranslateY() + _parent.vlayout.getLayoutY() + getLayoutY() + _connection.getLayoutY() + con_bounds.getHeight()/2);
|
|
|
}
|
|
|
|
|
|
@Override
|