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

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

public class ComponentView extends Draggable{
    private final Sprite sprite;
    private final Text text;
    public EditorEntity.Connectiontype connectionType;
    private final PortView portview;

    //Meshのみ 分離を検討
    public int meshId = 0;
    public Sprite meshSprite;
    ResourceManager resourceManager;

    public ComponentView(EditorEntity parent, EditorEntity.Connectiontype connectionType, String text){
        this.parent = parent;
        this.sprite = new Sprite(Window.resourcePath + "ComponentView.png");
        sprite.updateSpriteDimensions();
        this.text = new Text(parent.screenTransform.position.x, parent.screenTransform.position.y, text, 14);
        portview = new PortView(PortView.PortType.OUT, parent);
        this.connectionType = connectionType;
    }

    public ComponentView(EditorEntity parent, EditorEntity.Connectiontype connectionType, ResourceManager resourceManager, String text){
        this(parent, connectionType, text);
        this.resourceManager = resourceManager;
        meshSprite = new Sprite(resourceManager.getPath(meshId));
    }

    @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 +92, actualPosition.y, actualPosition.z);
            portview.update(adjustedPos, transform.rotation, transform.scale);
            portview.handleDragging();

            if(meshSprite != null){
                meshSprite.setPosition(new Vector3f(actualPosition.x, actualPosition.y+30, actualPosition.z));
                meshSprite.setRotation(transform.rotation);
                meshSprite.setScale(transform.scale);
                meshSprite.update();

                float mouseX = MouseInput.getX();
                float mouseY = MouseInput.getY();
                if (Input.GetMouseButtonDown(0) && meshSprite.isMouseOver(mouseX, mouseY)) {
                    changeMeshSprite();
                }
            }
        }




    }

    private void changeMeshSprite() {
        meshId = (meshId + 1) % resourceManager.getPathList().size();

        // 新しいスプライトを設定
        String newPath = resourceManager.getPath(meshId);
        if (newPath != null) {
            meshSprite.setTexturePath(newPath);
        }
    }

    @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) {

    }

    @Override
    protected void rightDraggingAction(float mouseX, float mouseY) {

    }

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

    public String getSpritePath(){
        if(meshId == 0) return null;
        return resourceManager.getPath(meshId);
    }
}