Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / entites / gameComponents / PortView.java
package gameEngine.entites.gameComponents;

import gameEngine.ConnectionManager;
import gameEngine.entites.Entity;
import gameEngine.entites.GameObject;
import gameEngine.scenes.EditorScene;
import gameEngine.views.LineRenderer;
import gameEngine.views.Sprite;
import gameEngine.input.Input;
import gameEngine.input.MouseInput;
import gameEngine.views.Window;
import org.joml.Vector3f;

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

public class PortView {
    public enum PortType {OUT, IN}
    private final PortType portType;
    private Sprite sprite;
    private Vector3f position = new Vector3f(0,0,0);
    private GameObject parent;
    private List<PortView> connectedPorts = new ArrayList<>();
    private LineRenderer lineRenderer;
    private boolean dragging = false;

    public PortView(PortType portType, GameObject parent){
        this.lineRenderer = new LineRenderer("GameEngine/resources/Line.png");
        this.parent = parent;
        if(portType == PortType.IN){
            this.sprite = new Sprite("GameEngine/resources/InPort.png");
        } else if(portType == PortType.OUT){
            this.sprite = new Sprite("GameEngine/resources/OutPort.png");
        }
        this.portType = portType;
        assert sprite != null;
        sprite.updateSpriteDimensions();
    }

    public void update(Vector3f pos, Vector3f rot, Vector3f scale){
        sprite.setPosition(pos);
        sprite.setRotation(rot);
        sprite.setScale(scale);
        sprite.update();
        position.set(pos);
    }

    public void handleDragging() {
        float mouseX = MouseInput.getX();
        float mouseY = MouseInput.getY();

        if (Input.GetMouseButtonDown(0) && isMouseOver(mouseX, mouseY)) {
            EditorScene editorScene = (EditorScene) Window.get().getScene();
            editorScene.connectionManager.handlePortPress(this);
            dragging = true;
        }

        if (Input.GetMouseButton(0) && dragging) {
            lineRenderer.updateLine(sprite.position, new Vector3f(mouseX, mouseY, 0), 3);
        }

        if (Input.GetMouseButtonUp(0)) {
            dragging = false;
            if(isMouseOver(mouseX, mouseY)){
                EditorScene editorScene = (EditorScene) Window.get().getScene();
                editorScene.connectionManager.handlePortRelease(this);
            }
        }

    }

    protected boolean isMouseOver(float mouseX, float mouseY) {
        float width = sprite.getDisplayedWidth();
        float height = sprite.getDisplayedHeight();

        return mouseX >= position.x && mouseX <= position.x + width &&
                mouseY >= position.y && mouseY <= position.y + height;
    }

    public Vector3f getPosition() {
        return position;
    }

    public PortType getPortType(){ return portType; }

    public GameObject getParent() {
        return parent;
    }

    public void addConnectedPort(PortView portView) {
        if (!connectedPorts.contains(portView)) {
            connectedPorts.add(portView);
        }
    }

    public boolean isConnectedTo(PortView portView) {
        return connectedPorts.contains(portView);
    }

    public void removeConnectedPort(PortView portView) {
        connectedPorts.remove(portView);
    }

}