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

public class ComponentView extends Draggable{
    private final Sprite sprite;
    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){
        this.parent = parent;
        this.sprite = new Sprite(Window.resourcePath + "ComponentView.png");
        sprite.updateSpriteDimensions();
        portview = new PortView(PortView.PortType.OUT, parent);
        this.connectionType = connectionType;
    }

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

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

    @Override
    public void update() {
        sprite.setPosition(parent.transform.position);
        sprite.setRotation(parent.transform.rotation);
        sprite.setScale(parent.transform.scale);
        sprite.update();

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

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

        Vector3f pos = new Vector3f(parent.transform.position.x+92,parent.transform.position.y,parent.transform.position.z);
        portview.update(pos, parent.transform.rotation, parent.transform.scale);
        portview.handleDragging();
    }

    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 draggingAction(float mouseX, float mouseY) {
        sprite.setPosition(mouseX - sprite.getDisplayedWidth() / 2,
                mouseY - sprite.getDisplayedHeight() / 2);
    }

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

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