Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / entites / gameComponents / PortView.java
NoranekoFelician 9 hours ago 2 KB ・Runnable→voidへ変更
package gameEngine.entites.gameComponents;

import gameEngine.ConnectionManager;
import gameEngine.entites.Entity;
import gameEngine.entites.GameObject;
import gameEngine.scenes.EditorScene;
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<>();

    public PortView(PortType portType, GameObject parent){
        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);
        }

        if (Input.GetMouseButtonUp(0) && 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);
    }

}