diff --git a/GameEngine/src/main/java/gameEngine/GameEditor.java b/GameEngine/src/main/java/gameEngine/GameEditor.java index 1b1b01c..d8d06d6 100644 --- a/GameEngine/src/main/java/gameEngine/GameEditor.java +++ b/GameEngine/src/main/java/gameEngine/GameEditor.java @@ -4,7 +4,6 @@ import gameEngine.entites.gameComponents.ColorController; import gameEngine.entites.gameComponents.Mesh; import gameEngine.scenes.*; -import gameEngine.scenes.*; import javax.swing.*; import java.awt.*; @@ -12,131 +11,182 @@ 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 objectList; private DefaultListModel 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; - private Timer updateTimer; public GameEditor() { setTitle("Game Object Editor"); - setSize(500, 400); + setSize(600, 500); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // 閉じられないようにする + setLayout(null); // レイアウトを絶対座標に設定 - // 左側のリスト(GameObjectの一覧) - listModel = new DefaultListModel<>(); - objectList = new JList<>(listModel); - JScrollPane scrollPane = new JScrollPane(objectList); - scrollPane.setPreferredSize(new Dimension(150, 400)); + // 各UI要素の初期化 + initializeUIComponents(); - // 右側のパネル(プロパティの編集) - 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); + // UI要素を初期化し、配置関数を呼び出す + private void initializeUIComponents() { + listModel = new DefaultListModel<>(); + objectList = new JList<>(listModel); + JScrollPane scrollPane = new JScrollPane(objectList); + + // カスタムレンダラーを使用して、リストにGameObjectの名前を表示する + objectList.setCellRenderer(new ListCellRenderer() { + @Override + public Component getListCellRendererComponent(JList 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()); } - public void updateListByScene(Scene gameScene){ - this.gameScene = gameScene; - updateGameObjectList(gameScene); + // コンポーネントの位置とサイズを設定する関数 + 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 entry : gameScene.gameObjects.entrySet()) { - listModel.addElement(entry.getValue().name); // 名前をリストに表示 + GameObject gameObject = entry.getValue(); + listModel.addElement(String.valueOf(gameObject.getId())); // IDをリストに表示(内部) } } // 選択されたGameObjectのプロパティをロードする private void loadSelectedGameObject() { - String selectedName = objectList.getSelectedValue(); - if (selectedName != null) { - for (Map.Entry 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; - } + 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)); } } } @@ -145,32 +195,42 @@ private class ApplyButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { - String selectedName = objectList.getSelectedValue(); - if (selectedName != null) { - for (Map.Entry 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; - } - } + 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); // リストの更新 } } } -} \ No newline at end of file + + public void updateListByScene(Scene gameScene) { + this.gameScene = gameScene; + updateGameObjectList(gameScene); + } + + + +}