Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / scenes / EditorScene.java
package gameEngine.scenes;

import gameEngine.ConnectionManager;
import gameEngine.GameEditor;
import gameEngine.ResourceManager;
import gameEngine.Time;
import gameEngine.entites.Camera;
import gameEngine.entites.EditorEntity;
import gameEngine.entites.Entity;
import gameEngine.entites.editorComponents.ComponentView;
import gameEngine.entites.editorComponents.Draggable;
import gameEngine.entites.editorComponents.EditorText;
import gameEngine.entites.editorComponents.EntityView;
import gameEngine.geometry.Transform;
import gameEngine.input.Input;
import gameEngine.views.Window;
import org.joml.Vector3f;

import java.util.HashMap;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.glClearColor;

public class EditorScene extends Scene {

    private static GameEditor gameEditor;
    public HashMap<String, EditorEntity> editorEntities = new HashMap<>();
    private EditorEntity selectedEntity = null;
    public ConnectionManager connectionManager = new ConnectionManager();
    public ResourceManager resourceManager = new ResourceManager();
    private EditorEntity clickedEntity = null;
    private Camera editorCamera;

    public EditorScene(float windowWidth, float windowHeight){
        System.out.println("Active Editor scene");
        gameEditor = new GameEditor(this, windowWidth, windowHeight);
        gameEditor.setScene(this);
        this.editorCamera = new Camera("001", Camera.ProjectionType.PERSPECTIVE);
        glClearColor(1, 1, 1, 0);
        resourceManager.addPath(Window.resourcePath + "empty.png");
        resourceManager.addPath(Window.resourcePath + "enemy1.png");
        resourceManager.addPath(Window.resourcePath + "enemy2.png");
        System.out.println(resourceManager.getPathList());
    }

    @Override
    public void update(float dt) {
        handleCameraMovement();

        updateDraggable();
        changeScene(1, dt);    //Gameシーンへの以降処理

        connectionManager.update();
        for (EditorEntity editorEntity : editorEntities.values()) {
            editorEntity.updateComponents();
        }
        gameEditor.update();
    }

    public Entity getEditorEntity(String eid) {
        return editorEntities.get(eid);
    }
    public void addEditorEntity(String eid, EditorEntity entity) {
        editorEntities.put(eid, entity);
    }
    public void removeEditorEntity(String eid) {
        editorEntities.remove(eid);
    }

    private EditorEntity createEditorEntity(){
        int entitiesLength = editorEntities.size();
        String newId = Integer.toString(entitiesLength);
        EditorEntity editorEntity = new EditorEntity(newId);
        addEditorEntity(newId, editorEntity);
        return editorEntity;
    }

    public void addNewEntity() {
        enqueueTask(this::addEntityView);
    }

    private void addEntityView(){
        EditorEntity object = createEditorEntity();
        object.transform.setPosition((float) Window.get().width / 2 ,(float) Window.get().height /2, 0);
        object.addEditorComponent(new EntityView(object));
        object.addEditorComponent(new EditorText(object, "Entity", 16));
        object.getEditorComponent(EditorText.class).setLocalPosition(new Vector3f(0,-20,0));
    }

    public void addNewMeshComponent() {
        enqueueTask(this::addMeshComponentView);
    }

    private void addMeshComponentView(){
        EditorEntity object = createEditorEntity();
        object.transform.setPosition((float) Window.get().width / 2 ,(float) Window.get().height /2, 0);
        object.addEditorComponent(new ComponentView(object, EditorEntity.Connectiontype.Mesh, resourceManager));
        object.addEditorComponent(new EditorText(object, "Mesh", 14));
        object.getEditorComponent(EditorText.class).setLocalPosition(new Vector3f(10,2,0));
    }

    public void addNewMoveImageComponent() {
        enqueueTask(this::addMoveImageComponentView);
    }

    private void addMoveImageComponentView(){
        EditorEntity object = createEditorEntity();
        object.transform.setPosition((float) Window.get().width / 2 ,(float) Window.get().height /2, 0);
        object.addEditorComponent(new ComponentView(object, EditorEntity.Connectiontype.MoveImage));
        object.addEditorComponent(new EditorText(object, "MoveImage", 14));
        object.getEditorComponent(EditorText.class).setLocalPosition(new Vector3f(10,2,0));
    }

    ///----------------------------------------------------------------
    /// Editorのカメラ
    ///----------------------------------------------------------------



    private void handleCameraMovement() {
        Vector3f velocity = new Vector3f(0, 0, 0);
        float speed = 200.0f;

        if (Input.GetKey(GLFW_KEY_UP)) {
            velocity.y -= speed * Time.deltaTime;
        }
        if (Input.GetKey(GLFW_KEY_DOWN)) {
            velocity.y += speed * Time.deltaTime;
        }
        if (Input.GetKey(GLFW_KEY_LEFT)) {
            velocity.x -= speed * Time.deltaTime;
        }
        if (Input.GetKey(GLFW_KEY_RIGHT)) {
            velocity.x += speed * Time.deltaTime;
        }

        editorCamera.move(velocity.x, velocity.y, velocity.z);
        for (EditorEntity editorEntity : editorEntities.values()) {
            editorEntity.transform.setPosition(
                    editorEntity.transform.position.x - velocity.x,
                    editorEntity.transform.position.y - velocity.y,
                    editorEntity.transform.position.z - velocity.z
            );
        }
    }

    ///----------------------------------------------------------------
    /// 選択、ドラッグ処理
    ///----------------------------------------------------------------

    public EditorEntity getSelectedEntity() {
        return selectedEntity;
    }

    public EditorEntity getClickedEntity(){
        return clickedEntity;
    }

    public void setSelectedEntity(EditorEntity editorEntity) {
        selectedEntity = editorEntity;
        System.out.println("Selected Entity: " + selectedEntity);
    }

    public void setClickedEntity(EditorEntity editorEntity){
        clickedEntity = editorEntity;

        if (editorEntity != null) {
            Transform transform = editorEntity.transform;

            // 初期値をInspectorに設定
            gameEditor.updateInspectorField(gameEditor.inspectorInputFields[0], transform.position); // Position
            gameEditor.updateInspectorField(gameEditor.inspectorInputFields[1], transform.rotation); // Rotation
            gameEditor.updateInspectorField(gameEditor.inspectorInputFields[2], transform.scale);    // Scale
        }
    }

    public void clearSelectedObject() {
        selectedEntity = null;
    }

    public void updateDraggable() {
        if (selectedEntity != null) {
            if (selectedEntity.getEditorComponent(Draggable.class) != null) {
                selectedEntity.getEditorComponent(Draggable.class).handleDragging();
            }
        } else {
            for (EditorEntity entity : editorEntities.values()) {
                    if (entity.getEditorComponent(Draggable.class) != null) {
                        entity.getEditorComponent(Draggable.class).handleDragging();
                }
            }
        }
    }
}