| |
---|
| | |
---|
| | import gameEngine.entites.Entity; |
---|
| | import gameEngine.entites.GameObject; |
---|
| | import gameEngine.entites.gameComponents.GameComponent; |
---|
| | import gameEngine.entites.gameComponents.Mesh; |
---|
| | import gameEngine.entites.gameComponents.MoveImage; |
---|
| | import gameEngine.scenes.*; |
---|
| | |
---|
| | import javax.swing.*; |
---|
| | import java.awt.*; |
---|
| |
---|
| | 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(600, 500); |
---|
| | setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // 閉じられないようにする |
---|
| | setLayout(null); // レイアウトを絶対座標に設定 |
---|
| | initializeUIComponents();// 各UI要素の初期化 |
---|
| | initializeUpdateTimer(); // 定期的にリスト更新を行うタイマーを初期化 |
---|
| | setVisible(true); // フレームを表示 |
---|
| | } |
---|
| | |
---|
| | // UI要素を初期化し、配置関数を呼び出す |
---|
| |
---|
| | listModel = new DefaultListModel<>(); |
---|
| | objectList = new JList<>(listModel); |
---|
| | JScrollPane scrollPane = new JScrollPane(objectList); |
---|
| | |
---|
| | 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(); |
---|
| | Entity entity = gameScene.getEntity(value); |
---|
| | label.setText(entity.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; |
---|
| | } |
---|
| | }); |
---|
| | |
---|
| | componentListModel = new DefaultListModel<GameComponent>(); |
---|
| | componentList = new JList<GameComponent>(componentListModel); |
---|
| | objectList.setCellRenderer(createEntityRenderer()); |
---|
| | componentListModel = new DefaultListModel<>(); |
---|
| | componentList = new JList<>(componentListModel); |
---|
| | JScrollPane componentScrollPane = new JScrollPane(componentList); |
---|
| | componentList.setCellRenderer(new ListCellRenderer<GameComponent>() { |
---|
| | @Override |
---|
| | public Component getListCellRendererComponent(JList<? extends GameComponent> list, GameComponent value, int index, boolean isSelected, boolean cellHasFocus) { |
---|
| | JLabel label = new JLabel(); |
---|
| | label.setText(value.getClass().getSimpleName()); // クラス名を表示 |
---|
| | if (isSelected) { |
---|
| | label.setBackground(list.getSelectionBackground()); |
---|
| | label.setForeground(list.getSelectionForeground()); |
---|
| | } else { |
---|
| | label.setBackground(list.getBackground()); |
---|
| | label.setForeground(list.getForeground()); |
---|
| | } |
---|
| | label.setOpaque(true); // 背景色が表示されるようにする |
---|
| | return label; |
---|
| | } |
---|
| | }); |
---|
| | componentList.setCellRenderer(createComponentRenderer()); |
---|
| | |
---|
| | // 各UIコンポーネントの作成と配置 |
---|
| | nameField = new JTextField(20); |
---|
| | nameLabel = new JLabel("Name:"); |
---|
| |
---|
| | scaleXField = new JTextField(5); |
---|
| | scaleYField = new JTextField(5); |
---|
| | scaleZField = new JTextField(5); |
---|
| | |
---|
| | JButton newGameObjectButton = new JButton("New Entity"); |
---|
| | JButton newGameObjectButton = new JButton("Add Entity"); |
---|
| | newGameObjectButton.addActionListener(e -> createNewGameObject()); |
---|
| | |
---|
| | JButton addComponentButton = new JButton("Add Component"); |
---|
| | addComponentButton.addActionListener(e -> addComponentToGameObject()); |
---|
| |
---|
| | loadSelectedGameObject(); // オブジェクトをロード |
---|
| | } |
---|
| | }); |
---|
| | } |
---|
| | private JTextField createTextField() { |
---|
| | return new JTextField(5); |
---|
| | } |
---|
| | |
---|
| | // タイマーによる定期的なリスト更新 |
---|
| | private void initializeUpdateTimer() { |
---|
| | updateTimer = new Timer(1000, e -> { |
---|
| | updateGameObjectList(gameScene); |
---|
| | if (objectList.getSelectedValue() != null) { |
---|
| | Entity selectedEntity = gameScene.getEntity(objectList.getSelectedValue()); |
---|
| | if (selectedEntity instanceof GameObject) { |
---|
| | updateComponentList((GameObject) selectedEntity); |
---|
| | } |
---|
| | } |
---|
| | }); |
---|
| | updateTimer.start(); |
---|
| | } |
---|
| | |
---|
| | private ListCellRenderer<String> createEntityRenderer() { |
---|
| | return (list, value, index, isSelected, cellHasFocus) -> { |
---|
| | JLabel label = new JLabel(); |
---|
| | Entity entity = gameScene.getEntity(value); |
---|
| | label.setText(entity.name); |
---|
| | label.setBackground(isSelected ? list.getSelectionBackground() : list.getBackground()); |
---|
| | label.setForeground(isSelected ? list.getSelectionForeground() : list.getForeground()); |
---|
| | label.setOpaque(true); |
---|
| | return label; |
---|
| | }; |
---|
| | } |
---|
| | |
---|
| | private ListCellRenderer<GameComponent> createComponentRenderer() { |
---|
| | return (list, value, index, isSelected, cellHasFocus) -> { |
---|
| | JLabel label = new JLabel(value.getClass().getSimpleName()); |
---|
| | label.setBackground(isSelected ? list.getSelectionBackground() : list.getBackground()); |
---|
| | label.setForeground(isSelected ? list.getSelectionForeground() : list.getForeground()); |
---|
| | label.setOpaque(true); |
---|
| | return label; |
---|
| | }; |
---|
| | } |
---|
| | |
---|
| | private void setComponentBounds(Component component, int x, int y, int width, int height) { |
---|
| | component.setBounds(x, y, width, height); |
---|
| | add(component); //フレームに追加 |
---|
| | } |
---|
| | |
---|
| | private void createNewGameObject() { |
---|
| | gameScene.createNewObject(); // 新しいGameObjectを作成 |
---|
| | gameScene.addNewObject(); // 新しいGameObjectを作成 |
---|
| | updateGameObjectList(gameScene); // オブジェクトリストの更新 |
---|
| | objectList.repaint(); // リストを再描画 |
---|
| | objectList.revalidate(); // リストを再構築 |
---|
| | objectList.setSelectedIndex(listModel.getSize() - 1); // 新規作成したオブジェクトを選択 |
---|
| | applyGameObjectChanges(); // 作成直後に即座にプロパティを反映させる |
---|
| | } |
---|
| | |
---|
| |
---|
| | if (selectedIdStr != null) { |
---|
| | Entity selectedEntity = gameScene.getEntity(selectedIdStr); |
---|
| | if (selectedEntity instanceof GameObject) { |
---|
| | GameObject gameObject = (GameObject) selectedEntity; |
---|
| | gameScene.addComponentObjectSelect(gameObject); // コンポーネントの追加処理 |
---|
| | System.out.println("仮でMeshコンポーネントを付与します"); |
---|
| | gameScene.addComponentToGameObject(gameObject, new MoveImage(gameObject)); |
---|
| | updateComponentList(gameObject); // コンポーネントリストの更新 |
---|
| | updateGameObjectList(gameScene); // オブジェクトリストの更新 |
---|
| | JOptionPane.showMessageDialog(this, "Component Added to " + selectedEntity.name); |
---|
| | } |
---|
| |
---|
| | if (selectedEntity instanceof GameObject) { |
---|
| | GameObject gameObject = (GameObject) selectedEntity; |
---|
| | GameComponent selectedComponent = componentList.getSelectedValue(); // GameComponentとして取得 |
---|
| | if (selectedComponent != null) { |
---|
| | gameScene.removeComponentObjectSelect(gameObject, selectedComponent); // GameComponentオブジェクトを渡す |
---|
| | gameScene.removeComponentFromGameObject(gameObject, selectedComponent); |
---|
| | updateComponentList(gameObject); // コンポーネントリストの更新 |
---|
| | updateGameObjectList(gameScene); // オブジェクトリストの更新 |
---|
| | JOptionPane.showMessageDialog(this, "Component Removed from " + selectedEntity.name); |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| |
---|
| | Entity entity = entry.getValue(); |
---|
| | listModel.addElement(entity.getId()); // そのままIDをリストに追加 |
---|
| | } |
---|
| | objectList.repaint(); // リストを再描画 |
---|
| | objectList.revalidate(); // リストを再構築 |
---|
| | if (!listModel.isEmpty()) { |
---|
| | objectList.setSelectedIndex(0); // 最初の項目を自動選択 |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | private void updateComponentList(GameObject gameObject) { |
---|
| | componentListModel.clear(); // リストをクリア |
---|
| | List<GameComponent> components = gameObject.gameComponents; |
---|
| | for (GameComponent component : components) { |
---|
| | componentListModel.addElement(component); // コンポーネントを追加 |
---|
| | } |
---|
| | if (components.isEmpty()) { |
---|
| | // 選択解除して、空の状態を扱う |
---|
| | componentList.clearSelection(); |
---|
| | componentList.repaint(); |
---|
| | } |
---|
| | componentList.repaint(); |
---|
| | } |
---|
| | |
---|
| | // 選択されたGameObjectのプロパティをロードする |
---|
| | private void loadSelectedGameObject() { |
---|
| |
---|
| | if (selectedIdStr != null) { |
---|
| | Entity selectedEntity = gameScene.getEntity(selectedIdStr); // IDのまま取得 |
---|
| | if (selectedEntity != null) { |
---|
| | loadObjectProperties(selectedEntity); |
---|
| | |
---|
| | // コンポーネントリストの更新 |
---|
| | if (selectedEntity instanceof GameObject) { |
---|
| | updateComponentList((GameObject) selectedEntity); |
---|
| | } |
---|
| | } |
---|
| |
---|
| | Float.parseFloat(scaleZField.getText()) |
---|
| | ); |
---|
| | |
---|
| | updateGameObjectList(gameScene); // オブジェクトリストの再描画 |
---|
| | objectList.repaint(); // リストの再描画を強制 |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | |
---|
| |
---|
| | |