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

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

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

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class GameEditor extends JFrame {

    private JList<String> objectList;
    private DefaultListModel<String> listModel;
    private JLabel nameLabel, posLabel, rotLabel, scaLabel;
    private JTextField posXField, posYField, posZField;
    private JTextField rotXField, rotYField, rotZField;
    private JTextField scaleXField, scaleYField, scaleZField;
    private JTextField nameField;
    private Scene gameScene;

    public GameEditor() {
        setTitle("Game Object Editor");
        setSize(600, 500);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);  // 閉じられないようにする
        setLayout(null); // レイアウトを絶対座標に設定

        // 各UI要素の初期化
        initializeUIComponents();

        // フレームを表示
        setVisible(true);
    }

    // UI要素を初期化し、配置関数を呼び出す
    private void initializeUIComponents() {
        listModel = new DefaultListModel<>();
        objectList = new JList<>(listModel);
        JScrollPane scrollPane = new JScrollPane(objectList);

        // カスタムレンダラーを使用して、リストにGameObjectの名前を表示する
        objectList.setCellRenderer(new ListCellRenderer<String>() {
            @Override
            public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) {
                JLabel label = new JLabel();
                int id = Integer.parseInt(value);  // 内部でIDを使用
                GameObject gameObject = gameScene.gameObjects.get(id);
                label.setText(gameObject.name);  // リストには名前を表示する
                if (isSelected) {
                    label.setBackground(list.getSelectionBackground());
                    label.setForeground(list.getSelectionForeground());
                } else {
                    label.setBackground(list.getBackground());
                    label.setForeground(list.getForeground());
                }
                label.setOpaque(true);  // 背景色が表示されるようにする
                return label;
            }
        });


        // 各UIコンポーネントの作成と配置
        nameField = new JTextField(20);
        nameLabel = new JLabel("Name:");
        posLabel = new JLabel("Position:");
        rotLabel = new JLabel("Rotation:");
        scaLabel = new JLabel("Scale:");
        posXField = new JTextField(5);
        posYField = new JTextField(5);
        posZField = new JTextField(5);
        rotXField = new JTextField(5);
        rotYField = new JTextField(5);
        rotZField = new JTextField(5);
        scaleXField = new JTextField(5);
        scaleYField = new JTextField(5);
        scaleZField = new JTextField(5);

        JButton newGameObjectButton = new JButton("New GameObject");
        newGameObjectButton.addActionListener(e -> createNewGameObject());

        JButton applyButton = new JButton("Apply");
        applyButton.addActionListener(new ApplyButtonListener());

        // 新たにAddComponentボタンを追加
        JButton addComponentButton = new JButton("Add Component");
        addComponentButton.addActionListener(e -> addComponentToGameObject());

        // 各要素の位置とサイズを設定
        setComponentBounds(scrollPane, 10, 10, 150, 400);
        setComponentBounds(nameLabel, 200, 10, 100, 30);
        setComponentBounds(nameField, 250, 10, 200, 30);
        setComponentBounds(posLabel, 200, 50, 100, 30);
        setComponentBounds(posXField, 250, 50, 50, 30);
        setComponentBounds(posYField, 310, 50, 50, 30);
        setComponentBounds(posZField, 370, 50, 50, 30);
        setComponentBounds(rotLabel, 200, 90, 100, 30);
        setComponentBounds(rotXField, 250, 90, 50, 30);
        setComponentBounds(rotYField, 310, 90, 50, 30);
        setComponentBounds(rotZField, 370, 90, 50, 30);
        setComponentBounds(scaLabel, 200, 130, 100, 30);
        setComponentBounds(scaleXField, 250, 130, 50, 30);
        setComponentBounds(scaleYField, 310, 130, 50, 30);
        setComponentBounds(scaleZField, 370, 130, 50, 30);
        setComponentBounds(newGameObjectButton, 200, 170, 150, 30);
        setComponentBounds(applyButton, 200, 210, 150, 30);
        setComponentBounds(addComponentButton, 200, 250, 150, 30);

        // コンポーネントをフレームに追加
        add(scrollPane);
        add(nameLabel);
        add(nameField);
        add(posLabel);
        add(posXField);
        add(posYField);
        add(posZField);
        add(rotLabel);
        add(rotXField);
        add(rotYField);
        add(rotZField);
        add(scaLabel);
        add(scaleXField);
        add(scaleYField);
        add(scaleZField);
        add(newGameObjectButton);
        add(applyButton);
        add(addComponentButton);

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

    // コンポーネントの位置とサイズを設定する関数
    private void setComponentBounds(Component component, int x, int y, int width, int height) {
        component.setBounds(x, y, width, height);
    }

    // New GameObjectを作成し、画面を更新
    private void createNewGameObject() {
        gameScene.CreateNewObject(); // 新しいGameObjectを作成
        applyGameObjectChanges();
        repaint();  // 画面を再描画
    }

    // GameObjectにコンポーネントを追加する処理
    private void addComponentToGameObject() {
        String selectedIdStr = objectList.getSelectedValue();
        if (selectedIdStr != null) {
            int selectedId = Integer.parseInt(selectedIdStr);
            GameObject selectedObject = gameScene.gameObjects.get(selectedId);
            if (selectedObject != null) {
                // コンポーネントを追加 (例: Mesh など)
                gameScene.AddComponent(selectedObject);
                JOptionPane.showMessageDialog(this, "Component Added to " + selectedObject.name);
            }
        }
    }

    // GameObjectのリストを更新
    public void updateGameObjectList(Scene gameScene) {
        listModel.clear();
        for (Map.Entry<Integer, GameObject> entry : gameScene.gameObjects.entrySet()) {
            GameObject gameObject = entry.getValue();
            listModel.addElement(String.valueOf(gameObject.getId()));  // IDをリストに表示(内部)
        }
    }

    // 選択されたGameObjectのプロパティをロードする
    private void loadSelectedGameObject() {
        String selectedIdStr = objectList.getSelectedValue();
        if (selectedIdStr != null) {
            int selectedId = Integer.parseInt(selectedIdStr);  // IDとして扱う
            GameObject selectedObject = gameScene.gameObjects.get(selectedId);
            if (selectedObject != null) {
                nameField.setText(selectedObject.name);
                posXField.setText(String.valueOf(selectedObject.transform.position.x));
                posYField.setText(String.valueOf(selectedObject.transform.position.y));
                posZField.setText(String.valueOf(selectedObject.transform.position.z));
                rotXField.setText(String.valueOf(selectedObject.transform.rotation.x));
                rotYField.setText(String.valueOf(selectedObject.transform.rotation.y));
                rotZField.setText(String.valueOf(selectedObject.transform.rotation.z));
                scaleXField.setText(String.valueOf(selectedObject.transform.scale.x));
                scaleYField.setText(String.valueOf(selectedObject.transform.scale.y));
                scaleZField.setText(String.valueOf(selectedObject.transform.scale.z));
            }
        }
    }

    // 選択されたGameObjectのプロパティを更新
    private class ApplyButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            applyGameObjectChanges();
        }
    }

    private void applyGameObjectChanges() {
        String selectedIdStr = objectList.getSelectedValue();
        if (selectedIdStr != null) {
            int selectedId = Integer.parseInt(selectedIdStr);  // IDとして扱う
            GameObject selectedObject = gameScene.gameObjects.get(selectedId);
            if (selectedObject != null) {
                selectedObject.name = nameField.getText();
                selectedObject.transform.setPosition(
                        Float.parseFloat(posXField.getText()),
                        Float.parseFloat(posYField.getText()),
                        Float.parseFloat(posZField.getText())
                );
                selectedObject.transform.setRotation(
                        Float.parseFloat(rotXField.getText()),
                        Float.parseFloat(rotYField.getText()),
                        Float.parseFloat(rotZField.getText())
                );
                selectedObject.transform.setScale(
                        Float.parseFloat(scaleXField.getText()),
                        Float.parseFloat(scaleYField.getText()),
                        Float.parseFloat(scaleZField.getText())
                );
                updateGameObjectList(gameScene); // リストの更新
            }
        }
    }

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



}