Selaa lähdekoodia

GUI: add connection

Willi Zschiebsch 4 vuotta sitten
vanhempi
commit
867436b2ab

+ 80 - 20
src/main/java/mdd/client/Connectable.java

@@ -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

+ 8 - 0
src/main/java/mdd/client/GUIMain.java

@@ -1,8 +1,10 @@
 package mdd.client;
 
 import javafx.application.Application;
+import javafx.application.Platform;
 import javafx.beans.value.ChangeListener;
 import javafx.beans.value.ObservableValue;
+import javafx.event.EventHandler;
 import javafx.fxml.FXML;
 import javafx.fxml.FXMLLoader;
 import javafx.scene.Parent;
@@ -11,6 +13,7 @@ import javafx.scene.control.SplitPane;
 import javafx.scene.input.KeyCode;
 import javafx.stage.Stage;
 import javafx.scene.input.KeyEvent;
+import javafx.stage.WindowEvent;
 import org.json.simple.JSONObject;
 
 
@@ -75,6 +78,11 @@ public class GUIMain extends Application implements IGUIEventClient{
             }
 
         });
+        primaryStage.setOnCloseRequest(e -> {
+            Platform.exit();
+            System.exit(0);
+        });
+
         primaryStage.show();
 
 

+ 2 - 5
src/main/java/mdd/client/Input.java

@@ -16,11 +16,8 @@ public class Input  extends Connectable {
         _input = new TextField(Value.toString());
         _input.setPrefWidth(50);
 
-        VBox vbox = new VBox(_label, _input);
-        vbox.setTranslateX(10);
-        vbox.setTranslateY(-10);
-
-        getChildren().add(vbox);
+        add(_label,1,0);
+        add(_input,1,1);
     }
 
     public void setConnected(boolean state){

+ 67 - 8
src/main/java/mdd/client/Module.java

@@ -39,6 +39,7 @@ public class Module extends Group implements IGUIEventClient, ChangeListener<Bou
     protected GUIEventHandler _eventHandler;
 
     public String Name = "TEST";
+    public long Appendix = 0;
     public String ID = "TEST-1";
     public Vector<String> Prefix;
 
@@ -54,7 +55,8 @@ public class Module extends Group implements IGUIEventClient, ChangeListener<Bou
 
     public Module(JSONObject json){
         Name = json.get("name").toString();
-        ID = Name + json.get("id").toString();
+        Appendix = (long)json.get("id");
+        ID = Name +Appendix;
         Type = json.get("type").toString();
         Key = json.get("key").toString();
         Prefix = new Vector<>();
@@ -90,9 +92,13 @@ public class Module extends Group implements IGUIEventClient, ChangeListener<Bou
                     ObservableList<String> options = FXCollections.observableArrayList();
                     System.out.println(jconfig.toString());
                     for (Object opt : (JSONArray) jconfig.get("options")){
+                        String item = opt.toString();
                         options.add(opt.toString());
+
                     }
-                    configs.add(new ComboBox(options),1,counter);
+                    ComboBox cbox = new ComboBox(options);
+                    cbox.getSelectionModel().select(jconfig.get("value").toString());
+                    configs.add(cbox,1,counter);
                 }
                 else{
                     configs.add(new TextField(jconfig.get("value").toString()),1,counter);
@@ -101,6 +107,11 @@ public class Module extends Group implements IGUIEventClient, ChangeListener<Bou
                 ++counter;
             }
         }
+        if (json.containsKey("GUI")) {
+            JSONObject jgui = (JSONObject) json.get("GUI");
+            setTranslateX((double)jgui.get("posX"));
+            setTranslateY((double)jgui.get("posY"));
+        }
 
         Color color = Color.DARKCYAN;
 
@@ -173,6 +184,29 @@ public class Module extends Group implements IGUIEventClient, ChangeListener<Bou
             }
         });
 
+        addEventHandler(MouseEvent.MOUSE_RELEASED, e -> {
+            // System.out.println("[Module]: dragg event detected!");
+            if(e.getButton() == MouseButton.PRIMARY) {
+                if (isSelected){
+                    JSONObject jgui_args = new JSONObject();
+                    jgui_args.put("posX", getTranslateX());
+                    jgui_args.put("posY", getTranslateY());
+                    JSONObject jgui = new JSONObject();
+                    jgui.put("GUI", jgui_args);
+                    JSONObject jmsg = new JSONObject();
+                    jmsg.put("operation", "change");
+                    JSONObject jargs = new JSONObject();
+                    jargs.put("subject",  getJSONIdentifier());
+                    jargs.put("object",   jgui);
+                    jmsg.put("args", jargs);
+                    _eventHandler.publishEvent("try", jmsg);
+                }
+            }
+            if(e.getButton() == MouseButton.SECONDARY) {
+
+            }
+        });
+
         vlayout.layoutBoundsProperty().addListener(this);
     }
 
@@ -215,8 +249,10 @@ public class Module extends Group implements IGUIEventClient, ChangeListener<Bou
     public JSONObject getJSONIdentifier(){
         JSONObject json = new JSONObject();
         json.put("prefix", Prefix);
-        json.put("id", ID);
+        json.put("name", Name);
+        json.put("id", Appendix);
         json.put("type", Type);
+        json.put("key", Key);
         return json;
     }
 
@@ -229,14 +265,37 @@ public class Module extends Group implements IGUIEventClient, ChangeListener<Bou
     @Override
     public void processGUIEvent(String event, JSONObject args) {
         if (event == "MOUSE_PRESSED"){
-
-
             if (!args.equals(getJSONIdentifier())){
-                if (isSelected){
-                    System.out.println("[Module]: deselect: " + ID);
-                }
                 isSelected = false;
+                return;
+            }
+            return;
+        }
+        if (event.equals("change")){
+            if (!args.containsKey("args")){
+                return;
+            }
+            JSONObject jargs = (JSONObject) args.get("args");
+            if (!jargs.containsKey("subject")){
+                return;
+            }
+            JSONObject jsub = (JSONObject) jargs.get("subject");
+            if (!jsub.equals(getJSONIdentifier())){
+                return;
+            }
+            if (!args.containsKey("operation")){
+                return;
+            }
+            JSONObject jobj = (JSONObject) jargs.get("object");
+            switch((String) args.get("operation")){
+                case "change":
+                    if (jobj.containsKey("GUI")){
+                        JSONObject jgui = (JSONObject) jobj.get("GUI");
+                        setTranslateX((double)jgui.get("posX"));
+                        setTranslateY((double)jgui.get("posY"));
+                    }
             }
+            return;
         }
     }
 

+ 1 - 3
src/main/java/mdd/client/Output.java

@@ -9,9 +9,7 @@ public class Output extends Connectable {
 
     public Output(Module parent, JSONObject json){
         super(parent, ConnectableType.OUTPUT, json);
-        _label.setTranslateX(-50);
-        _label.setTranslateY(7);
-        getChildren().add(_label);
+        add(_label,0,0);
     }
 
 }

+ 52 - 26
src/main/java/mdd/client/Processor.java

@@ -40,6 +40,19 @@ public class Processor extends Module{
             }
         }
 
+        if (json.containsKey("connections")){
+            for (Object o : (JSONArray) json.get("connections")){
+                JSONObject jobj = (JSONObject) o;
+                JSONObject jout = (JSONObject)jobj.get("output");
+                for (Object ins : (JSONArray) jobj.get("inputs")){
+                    JSONObject jin = (JSONObject) ins;
+                    Input in = getModule(((JSONArray)jin.get("prefix")).get(0).toString()).getInput(jin.get("name").toString()+jin.get("appendix").toString());
+                    Output out = getModule(((JSONArray)jout.get("prefix")).get(0).toString()).getOutput(jout.get("name").toString()+jout.get("appendix").toString());
+                    connections.getChildren().add(new Connection(in, out));
+                }
+            }
+        }
+
         modules.addEventHandler(MouseEvent.DRAG_DETECTED, e -> {
             // System.out.println("[Module]: dragg event detected!");
             if(e.getButton() == MouseButton.PRIMARY) {
@@ -104,15 +117,19 @@ public class Processor extends Module{
                         }
                     }
                     if (in != null && out != null) {
-                        System.out.println("[Processor] Dropped: success!");
-                        if (in.connected) {
-                            for (int i = 0; i < connections.getChildren().size(); ++i) {
-                                if (((Connection) connections.getChildren().get(i)).in.equals(in)) {
-                                    connections.getChildren().remove(i);
-                                }
-                            }
-                        }
-                        connections.getChildren().add(new Connection(in, out));
+                        clearWrongConnections(in, out);
+                        JSONObject jmsg = new JSONObject();
+                        jmsg.put("operation","add");
+                        JSONObject jargs = new JSONObject();
+                        jargs.put("subject",  getJSONIdentifier());
+                        JSONObject jconnection = new JSONObject();
+                        jconnection.put("type","connection");
+                        jconnection.put("input",in.getJSONIdentifier());
+                        jconnection.put("output",out.getJSONIdentifier());
+                        jargs.put("object", jconnection);
+                        jmsg.put("args", jargs);
+                        _eventHandler.publishEvent("try",jmsg);
+
                         success = true;
                     }
 
@@ -126,6 +143,21 @@ public class Processor extends Module{
         });
     }
 
+    private void clearWrongConnections(Input in, Output out){
+        if (in != null && out != null) {
+            if (in.isConnected()) {
+                for (int i = 0; i < connections.getChildren().size(); ++i) {
+                    Connection con = (Connection) connections.getChildren().get(i);
+                    if (con.in.equals(in)) {
+                        con.in.setConnected(false);
+                        con.out.setConnected(false);
+                        connections.getChildren().remove(i);
+                    }
+                }
+            }
+        }
+    }
+
     public Module getModule(String id){
         for (Node node : modules.getChildren()){
             if (node instanceof Module){
@@ -180,6 +212,7 @@ public class Processor extends Module{
                     _eventHandler.publishEvent("OPEN_PROCESSOR", json);
                     System.out.println("[Processor]: published");
                 }
+                return;
             }
         }
         if (detected_drag){
@@ -206,10 +239,12 @@ public class Processor extends Module{
                 temp_connection.setEndX(connectable.x.getValue()+100);
                 temp_connection.setEndY(connectable.y.getValue()+100);
                 //connections.getChildren().add(temp_connection);
+                return;
             }
         }
         if (event.equals("change")){
             if (!args.containsKey("operation")){return;}
+            if (!args.containsKey("args")){return;}
             JSONObject jargs = (JSONObject) args.get("args");
             if (!jargs.containsKey("subject")){return;}
             JSONObject jsub = (JSONObject)jargs.get("subject");
@@ -223,8 +258,14 @@ public class Processor extends Module{
                 case "add":
                     switch (jobj.get("type").toString()){
                         case "connection":
-                            //Connection connection = new Connection(jobj);
-                            //addConnection(connection);
+                            System.out.println("[Processor]: Add Connection");
+                            System.out.println(jobj.toString());
+                            JSONObject jin = (JSONObject)jobj.get("input");
+                            JSONObject jout = (JSONObject)jobj.get("output");
+                            Input in = getModule(((JSONArray)jin.get("prefix")).get(0).toString()).getInput(jin.get("name").toString()+jin.get("id").toString());
+                            Output out = getModule(((JSONArray)jout.get("prefix")).get(0).toString()).getOutput(jout.get("name").toString()+jout.get("id").toString());
+                            clearWrongConnections(in,out);
+                            connections.getChildren().add(new Connection(in, out));
                             break;
                         case "module":
                             modules.getChildren().add(new Module(jobj));
@@ -252,20 +293,5 @@ public class Processor extends Module{
                 }
             }
         }
-        if (event.equals("state")){
-            /*
-            "{
-            "id":{...},
-            "modules":
-                [{"id": {"type": "processor"}, "modules":[{},{},{}], "connections":[{},{},{}], "inputs": [{}], "outputs": [], "config":{}},
-                {"type": "module", "id": {}, "inputs": [{}], "outputs": [], "config":{}},
-                {2}],
-            "connections":[{},{}]
-            }"
-             */
-            if (args.get("id").equals(getJSONIdentifier())){
-                System.out.println("[Processor] not implemented load function called!");
-            }
-        }
     }
 }