Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / entites / editorComponents / EntityView.java
package gameEngine.entites.editorComponents;

import gameEngine.entites.EditorEntity;
import gameEngine.geometry.Transform;
import gameEngine.scenes.EditorScene;
import gameEngine.views.Sprite;
import gameEngine.views.Text;
import gameEngine.views.Window;
import org.joml.Vector3f;

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

public class EntityView extends Draggable{

    private Sprite sprite;
    private Text text;
    private PortView portviewA;

    public EntityView(EditorEntity parent) {
        this.parent = parent;
        this.sprite = new Sprite(Window.resourcePath + "EntityView.png");
        this.text = new Text(parent.screenTransform.position.x, parent.screenTransform.position.y, "Entity", 14);
        sprite.updateSpriteDimensions();
        portviewA = new PortView(PortView.PortType.IN, parent);
    }

    @Override
    public void init() {
        sprite.update();
        sprite.updateSpriteDimensions();
    }

    @Override
    public void update() {
        Transform transform = parent.transform;
        Transform screenTransform = parent.screenTransform;

        if(Window.get().getScene() instanceof EditorScene) {
            Vector3f cameraPosition = Window.get().getScene().getCamera().getPosition();
            Vector3f actualPosition = new Vector3f(screenTransform.position).sub(cameraPosition);

            sprite.setPosition(actualPosition);
            sprite.setRotation(transform.rotation);
            sprite.setScale(transform.scale);
            sprite.update();

            text.setPosition(new Vector3f(0,-16,0).add(actualPosition));
            text.setRotation(transform.rotation);
            text.setScale(transform.scale);
            text.update();

            Vector3f adjustedPos = new Vector3f(actualPosition.x - 30, actualPosition.y + 8, actualPosition.z);
            portviewA.update(adjustedPos, transform.rotation, transform.scale);
            portviewA.handleDragging();
        }


    }

    @Override
    public EditorComponent copy() {
        return this;
    }


    @Override
    protected boolean isMouseOver(float mouseX, float mouseY) {
        return sprite.isMouseOver(mouseX, mouseY);
    }

    @Override
    protected void leftDragStartAction(float mouseX, float mouseY) {
        Vector3f cameraPosition = Window.get().getScene().getCamera().getPosition();

        float worldMouseX = mouseX + cameraPosition.x;
        float worldMouseY = mouseY + cameraPosition.y;

        dragOffsetX = worldMouseX - parent.transform.position.x;
        dragOffsetY = worldMouseY - parent.transform.position.y;
    }

    @Override
    protected void leftDraggingAction(float mouseX, float mouseY) {
        Vector3f cameraPosition = Window.get().getScene().getCamera().getPosition();

        float worldMouseX = mouseX + cameraPosition.x;
        float worldMouseY = mouseY + cameraPosition.y;

        float targetX = worldMouseX - dragOffsetX;
        float targetY = worldMouseY - dragOffsetY;

        parent.transform.setPosition(
                targetX,
                targetY,
                parent.transform.position.z
        );

        parent.screenTransform.setPosition(
                parent.transform.position.x - cameraPosition.x,
                parent.transform.position.y - cameraPosition.y,
                parent.transform.position.z - - cameraPosition.z
        );
    }

    @Override
    protected void rightDragStartAction(float mouseX, float mouseY) {
        Vector3f cameraPosition = Window.get().getScene().getCamera().getPosition();

        float worldMouseX = mouseX + cameraPosition.x;
        float worldMouseY = mouseY + cameraPosition.y;

        dragOffsetX = worldMouseX - parent.transform.position.x;
        dragOffsetY = worldMouseY - parent.transform.position.y;
    }

    @Override
    protected void rightDraggingAction(float mouseX, float mouseY) {
        Vector3f cameraPosition = Window.get().getScene().getCamera().getPosition();

        float worldMouseX = mouseX + cameraPosition.x;
        float worldMouseY = mouseY + cameraPosition.y;

        float targetX = worldMouseX - dragOffsetX;
        float targetY = worldMouseY - dragOffsetY;

        float deltaX = targetX - parent.transform.position.x;
        float deltaY = targetY - parent.transform.position.y;

        parent.transform.setPosition(
                targetX,
                targetY,
                parent.transform.position.z
        );

        parent.screenTransform.setPosition(
                parent.transform.position.x - cameraPosition.x,
                parent.transform.position.y - cameraPosition.y,
                parent.transform.position.z - cameraPosition.z
        );

        List<ComponentView> connectedComponents = getConnectedComponentViews();
        for (ComponentView component : connectedComponents) {
            EditorEntity componentParent = component.parent;
            componentParent.transform.setPosition(
                    componentParent.transform.position.x + deltaX,
                    componentParent.transform.position.y + deltaY,
                    componentParent.transform.position.z
            );

            componentParent.screenTransform.setPosition(
                    componentParent.transform.position.x - cameraPosition.x,
                    componentParent.transform.position.y - cameraPosition.y,
                    componentParent.transform.position.z - cameraPosition.z
            );
        }
    }

    public PortView getPortView() {
        return portviewA;
    }

    public List<ComponentView> getConnectedComponentViews() {
        List<ComponentView> connectedComponentViews = new ArrayList<>();

        for (PortView connectedPort : portviewA.getConnectedPorts()) {
            EditorEntity parentEntity = connectedPort.getParent();

            if (parentEntity != null) {
                for (EditorComponent component : parentEntity.editorComponents) {
                    if (component instanceof ComponentView) {
                        connectedComponentViews.add((ComponentView) component);
                    }
                }
            }
        }
        return connectedComponentViews;
    }
}