Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / ConnectionManager.java
NoranekoFelician 14 days ago 3 KB ・Editor用のCameraViewを追加
package gameEngine;

import java.util.ArrayList;
import java.util.List;

import gameEngine.entites.EditorEntity;
import gameEngine.entites.editorComponents.*;
import gameEngine.input.Input;
import gameEngine.input.MouseInput;

public class ConnectionManager {
    private final List<Connection> connections = new ArrayList<>();
    private PortView pressedPort;

    public void handlePortPress(PortView port) {
        pressedPort = port;
    }

    public void handlePortRelease(PortView port) {
        if (pressedPort != null && pressedPort != port && pressedPort.getPortType() != port.getPortType()) {
            if (pressedPort.isConnectedTo(port)) {
                System.out.println("Already connected");
            } else {
                System.out.println("Connect!");
                Connection newConnection = new Connection(pressedPort, port);
                connections.add(newConnection);
                pressedPort.addConnectedPort(port);
                port.addConnectedPort(pressedPort);

                EditorEntity portParentA = pressedPort.getParent();
                EditorEntity portParentB = port.getParent();
                if (portParentA.getEditorComponent(EntityView.class) != null || portParentA.getEditorComponent(CameraView.class) != null) {
                    portParentA.ComponentConnections.add(
                            portParentB.getEditorComponent(ComponentView.class).connectionType
                    );
                } else if (portParentB.getEditorComponent(EntityView.class) != null || portParentB.getEditorComponent(CameraView.class) != null) {
                    portParentB.ComponentConnections.add(
                            portParentA.getEditorComponent(ComponentView.class).connectionType
                    );
                }
            }
        }
        pressedPort = null;
    }

    public void update() {
        for (Connection connection : connections) {
            connection.render();

            if (Input.GetMouseButtonDown(0)) {
                float mouseX = MouseInput.getX();
                float mouseY = MouseInput.getY();

                if (connection.isRemoveButtonClicked(mouseX, mouseY)) {
                    System.out.println("Connection removed");
                    removeConnection(connection.getPortA(), connection.getPortB());
                    break;
                }
            }
        }
    }

    public void removeConnection(PortView portA, PortView portB) {
        Connection toRemove = null;
        for (Connection connection : connections) {
            if ((connection.getPortA() == portA && connection.getPortB() == portB) ||
                    (connection.getPortA() == portB && connection.getPortB() == portA)) {
                toRemove = connection;
                break;
            }
        }

        if (toRemove != null) {
            connections.remove(toRemove);
            portA.removeConnectedPort(portB);
            portB.removeConnectedPort(portA);

            EditorEntity portParentA = portA.getParent();
            EditorEntity portParentB = portB.getParent();

            if (portParentA.getEditorComponent(EntityView.class) != null) {
                portParentA.ComponentConnections.remove(portParentB.getEditorComponent(ComponentView.class).connectionType);
            } else {
                portParentB.ComponentConnections.remove(portParentA.getEditorComponent(ComponentView.class).connectionType);
            }
        }
    }
}