Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / GameEditor.java
NoranekoFelician on 7 Oct 7 KB Editor用Windowを仮で作成
package gameEngine;

import gameEngine.entites.GameObject;
import gameEngine.entites.gameComponents.ColorController;
import gameEngine.entites.gameComponents.Mesh;
import gameEngine.scenes.*;
import gameEngine.scenes.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

public class GameEditor extends JFrame {

    private JList<String> objectList;
    private DefaultListModel<String> listModel;
    private JTextField posXField, posYField, posZField;
    private JTextField rotXField, rotYField, rotZField;
    private JTextField scaleXField, scaleYField, scaleZField;
    private JTextField nameField;
    private Scene gameScene;
    private Timer updateTimer;

    public GameEditor() {
        setTitle("Game Object Editor");
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);  // 閉じられないようにする

        // 左側のリスト(GameObjectの一覧)
        listModel = new DefaultListModel<>();
        objectList = new JList<>(listModel);
        JScrollPane scrollPane = new JScrollPane(objectList);
        scrollPane.setPreferredSize(new Dimension(150, 400));

        // 右側のパネル(プロパティの編集)
        JPanel propertyPanel = new JPanel();
        propertyPanel.setLayout(new GridLayout(6, 1));  // NewGameObjectボタン用に行を追加

        // Name
        JPanel namePanel = new JPanel(new FlowLayout());
        namePanel.add(new JLabel("Name:"));
        nameField = new JTextField(20);
        namePanel.add(nameField);
        propertyPanel.add(namePanel);

        // Position
        JPanel posPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 5));  // 間隔をゼロに設定
        posPanel.add(new JLabel("Position:"));
        posXField = new JTextField(5);
        posYField = new JTextField(5);
        posZField = new JTextField(5);
        posPanel.add(posXField);
        posPanel.add(posYField);
        posPanel.add(posZField);
        propertyPanel.add(posPanel);

        // Rotation
        JPanel rotPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 5));
        rotPanel.add(new JLabel("Rotation:"));
        rotXField = new JTextField(5);
        rotYField = new JTextField(5);
        rotZField = new JTextField(5);
        rotPanel.add(rotXField);
        rotPanel.add(rotYField);
        rotPanel.add(rotZField);
        propertyPanel.add(rotPanel);

        // Scale
        JPanel scalePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 5));
        scalePanel.add(new JLabel("Scale:"));
        scaleXField = new JTextField(5);
        scaleYField = new JTextField(5);
        scaleZField = new JTextField(5);
        scalePanel.add(scaleXField);
        scalePanel.add(scaleYField);
        scalePanel.add(scaleZField);
        propertyPanel.add(scalePanel);

        // New GameObjectボタンを追加
        JButton newGameObjectButton = new JButton("New GameObject");
        newGameObjectButton.addActionListener(e -> createNewGameObject());
        propertyPanel.add(newGameObjectButton);

        // Applyボタン
        JButton applyButton = new JButton("Apply");
        applyButton.addActionListener(new ApplyButtonListener());
        propertyPanel.add(applyButton);

        // リスト選択時の処理
        objectList.addListSelectionListener(e -> loadSelectedGameObject());

        // UIを追加
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(scrollPane, BorderLayout.WEST);
        getContentPane().add(propertyPanel, BorderLayout.CENTER);

        setVisible(true);
    }

    // New GameObjectを作成
    private void createNewGameObject() {
        gameScene.CreateNewObject();
        updateGameObjectList(gameScene);
    }

    public void updateListByScene(Scene gameScene){
        this.gameScene = gameScene;
        updateGameObjectList(gameScene);
    }

    // GameObjectのリストを更新
    public void updateGameObjectList(Scene gameScene) {
        listModel.clear();
        for (Map.Entry<Integer, GameObject> entry : gameScene.gameObjects.entrySet()) {
            listModel.addElement(entry.getValue().name);  // 名前をリストに表示
        }
    }

    // 選択されたGameObjectのプロパティをロードする
    private void loadSelectedGameObject() {
        String selectedName = objectList.getSelectedValue();
        if (selectedName != null) {
            for (Map.Entry<Integer, GameObject> entry : gameScene.gameObjects.entrySet()) {
                GameObject gameObject = entry.getValue();
                if (gameObject.name.equals(selectedName)) {
                    nameField.setText(gameObject.name);
                    posXField.setText(String.valueOf(gameObject.transform.position.x));
                    posYField.setText(String.valueOf(gameObject.transform.position.y));
                    posZField.setText(String.valueOf(gameObject.transform.position.z));
                    rotXField.setText(String.valueOf(gameObject.transform.rotation.x));
                    rotYField.setText(String.valueOf(gameObject.transform.rotation.y));
                    rotZField.setText(String.valueOf(gameObject.transform.rotation.z));
                    scaleXField.setText(String.valueOf(gameObject.transform.scale.x));
                    scaleYField.setText(String.valueOf(gameObject.transform.scale.y));
                    scaleZField.setText(String.valueOf(gameObject.transform.scale.z));
                    break;
                }
            }
        }
    }

    // 選択されたGameObjectのプロパティを更新
    private class ApplyButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String selectedName = objectList.getSelectedValue();
            if (selectedName != null) {
                for (Map.Entry<Integer, GameObject> entry : gameScene.gameObjects.entrySet()) {
                    GameObject gameObject = entry.getValue();
                    if (gameObject.name.equals(selectedName)) {
                        gameObject.name = nameField.getText();
                        gameObject.transform.setPosition(
                                Float.parseFloat(posXField.getText()),
                                Float.parseFloat(posYField.getText()),
                                Float.parseFloat(posZField.getText())
                        );
                        gameObject.transform.setRotation(
                                Float.parseFloat(rotXField.getText()),
                                Float.parseFloat(rotYField.getText()),
                                Float.parseFloat(rotZField.getText())
                        );
                        gameObject.transform.setScale(
                                Float.parseFloat(scaleXField.getText()),
                                Float.parseFloat(scaleYField.getText()),
                                Float.parseFloat(scaleZField.getText())
                        );
                        updateGameObjectList(gameScene);
                        break;
                    }
                }
            }
        }
    }
}