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.Sprite;
import gameEngine.input.Input;
import gameEngine.input.MouseInput;
import gameEngine.views.Window;
import org.joml.Vector3f;

public class PortView {
    enum PortType {OUT, IN}
    private PortType portType;
    private ConnectionManager connectionManager;
    private Sprite sprite;
    private Vector3f position = new Vector3f(0,0,0);
    private GameObject parent;

    public PortView(PortType portType, GameObject parent){
        this.parent = parent;
        EditorScene editorScene = (EditorScene) Window.get().getScene();
        this.connectionManager = editorScene.connectionManager;
        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;
        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)) {
            connectionManager.handlePortPress(this);
        }

        if (Input.GetMouseButtonUp(0) && isMouseOver(mouseX, mouseY)) {
            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 Entity getParent() {
        return parent;
    }

}