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

import gameEngine.entites.EditorEntity;
import gameEngine.geometry.Transform;
import gameEngine.scenes.EditorScene;
import gameEngine.views.*;
import gameEngine.scenes.Scene;
import org.joml.Vector3f;

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

public class GameEditor {
    private EditorScene scene;
    private float windowWidth, windowHeight;
    private final String frameImagePath = Window.resourcePath + "EditorFrame.png";

    private List<IUpdatable> updatableviews = new ArrayList<>();

    private Sprite[] EditorFrameSprite = new Sprite[4];

    private Button dtramButton;
    private Text dtramButtonText;

    private Button playButton;
    private Text playButtonText;

    private Button createMeshComponentViewButton;
    private Text createMeshComponentViewButtonText;

    private Button createMoveImageComponentViewButton;
    private Text createMoveImageComponentViewButtonText;

    private Button createMoveStraightComponentViewButton;
    private Text createMoveStraightComponentViewButtonText;

    private Button createEntityViewButton;
    private Text createEntityViewButtonText;
    public InputField[][] inspectorInputFields;

    public GameEditor(Scene scene, float windowWidth, float windowHeight) {
        this.scene = (EditorScene) scene;
        this.windowWidth = windowWidth;
        this.windowHeight = windowHeight;
        initializeEditorComponents();
    }

    private void initializeEditorComponents() {
        createFrame();

        playButton = new Button(windowWidth/ 2 -16 , 3, 1, 0.4f);
        playButtonText = new Text(windowWidth/ 2 - 10, 1, "Play", 24);

        dtramButton = new Button(100 - 16 , 3, 2.5f, 0.4f);
        dtramButtonText = new Text(100 - 10, 1, "Play DTRAM", 24);

        createMeshComponentViewButton = new Button(20, 33, 3.2f, 0.5f);
        createMeshComponentViewButtonText = new Text(20 +5.25f, 30.4f, "Add Mesh", 24);

        createMoveImageComponentViewButton = new Button(20, 73, 3.2f, 0.5f);
        createMoveImageComponentViewButtonText = new Text(20 +5.25f, 70.4f, "Add MoveImage", 24);

        createMoveStraightComponentViewButton = new Button(20, 113, 3.2f, 0.5f);
        createMoveStraightComponentViewButtonText = new Text(20 +5.25f, 113.4f, "Add MoveStraight", 24);

        createEntityViewButton = new Button(240, 33, 3.2f, 0.5f);
        createEntityViewButtonText = new Text(240 +5.25f, 30.4f, "Add Entity", 24);

        setButtonListeners();
        setInspector();
    }

    private void registerUpdatable(IUpdatable... components) {
        Collections.addAll(updatableviews, components);
    }

    private void setInspector() {
        String[] labels = {"Position", "Rotation", "Scale"};
        String[] axes = {"x", "y", "z"};
        float startX = 900;
        float startY = 40;
        float fieldWidth = 70;
        float fieldHeight = 22;
        float spacingX = 100;
        float spacingY = 40;
        float labelOffsetX = -100;
        float labelOffsetY = 5;

        InputField[][] inputFields = new InputField[3][3];

        for (int i = 0; i < labels.length; i++) {
            float labelX = startX + labelOffsetX;
            float labelY = startY + i * spacingY + labelOffsetY;

            Text labelText = new Text(labelX, labelY - 6, labels[i] + ":", 18);
            updatableviews.add(labelText);

            for (int j = 0; j < axes.length; j++) {
                float fieldX = startX + j * spacingX;
                float fieldY = startY + i * spacingY;

                Text axisText = new Text(fieldX - 20, fieldY, axes[j] + ":", 18);
                InputField field = new InputField(fieldX, fieldY, fieldWidth, fieldHeight, "0", 18);

                inputFields[i][j] = field;

                int transformType = i;
                int axisIndex = j;

                //InputFieldの値変更時
                field.setOnChangeListener(newValue -> {
                    EditorEntity clickedEntity = scene.getClickedEntity();

                    System.out.println("うおおお!" + clickedEntity);

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

                        try {
                            // 入力を解析して数値に変換
                            float value = Float.parseFloat(newValue);

                            switch (transformType) {
                                case 0:
                                    if (axisIndex == 0) transform.position.x = value;
                                    if (axisIndex == 1) transform.position.y = value;
                                    if (axisIndex == 2) transform.position.z = value;
                                    break;
                                case 1:
                                    if (axisIndex == 0) transform.rotation.x = value;
                                    if (axisIndex == 1) transform.rotation.y = value;
                                    if (axisIndex == 2) transform.rotation.z = value;
                                    break;
                                case 2:
                                    if (axisIndex == 0) transform.scale.x = value;
                                    if (axisIndex == 1) transform.scale.y = value;
                                    if (axisIndex == 2) transform.scale.z = value;
                                    break;
                            }

                            // 変化を適用
                            clickedEntity.transform = transform;

                        } catch (NumberFormatException e) {
                            // 入力が不正な場合は何もしない
                            System.err.println("Invalid input for transform value: " + newValue);
                            field.copyDisplayedTextToCurrent();
                        }
                    }
                });

                updatableviews.add(axisText);
                updatableviews.add(field);
            }
        }
        this.inspectorInputFields = inputFields;
    }

    private void setButtonListeners() {
        playButton.clearListeners();
        playButton.addListener(scene::changeSceneStart);

        dtramButton.clearListeners();
        GameEngineModelFileGenerator modelFileGenerator = new GameEngineModelFileGenerator();
        dtramButton.addListener(modelFileGenerator::generate);

        createMeshComponentViewButton.clearListeners();
        createMeshComponentViewButton.addListener(scene::addNewMeshComponent);

        createMoveImageComponentViewButton.clearListeners();
        createMoveImageComponentViewButton.addListener(scene::addNewMoveImageComponent);

        createMoveStraightComponentViewButton.clearListeners();
        createMoveStraightComponentViewButton.addListener(scene::addMoveStraightComponent);

        createEntityViewButton.clearListeners();
        createEntityViewButton.addListener(scene::addNewEntity);

        registerUpdatable(playButton, playButtonText, dtramButton, dtramButtonText,
                createMeshComponentViewButton, createMeshComponentViewButtonText,
                createMoveImageComponentViewButton, createMoveImageComponentViewButtonText,
                createEntityViewButton, createEntityViewButtonText, createMoveStraightComponentViewButton, createMoveStraightComponentViewButtonText);
    }

    public void setScene(Scene newScene) {
        this.scene = (EditorScene) newScene;
        setButtonListeners();
    }

    public void update() {
        for(Sprite editorFrameSprites : EditorFrameSprite) editorFrameSprites.update();

        for (IUpdatable updatable : updatableviews) {
            updatable.update();
        }

        if (scene.getClickedEntity() != null) {
            Transform transform = scene.getClickedEntity().transform;

            updateInspectorField(inspectorInputFields[0], transform.position); // Position
            updateInspectorField(inspectorInputFields[1], transform.rotation); // Rotation
            updateInspectorField(inspectorInputFields[2], transform.scale);    // Scale
        }
    }

    public void updateInspectorField(InputField[] fields, Vector3f values) {
        fields[0].setText(String.valueOf(values.x));
        fields[1].setText(String.valueOf(values.y));
        fields[2].setText(String.valueOf(values.z));
    }

    private void createFrame(){
        EditorFrameSprite[0] = new Sprite(frameImagePath, 0, 0, 20, 0.5f);
        EditorFrameSprite[1] = new Sprite(frameImagePath, 0, 0, 0.25f, 20);
        EditorFrameSprite[2] = new Sprite(frameImagePath, windowWidth-16, 0, 0.25f, 20);
        EditorFrameSprite[3] = new Sprite(frameImagePath, 0, windowHeight-16, 20, 0.5f);

        for (Sprite sprite : EditorFrameSprite) {
            updatableviews.add(sprite);
        }
    }
}