diff --git a/GameEngine/src/main/java/gameEngine/GameEditor.java b/GameEngine/src/main/java/gameEngine/GameEditor.java index fb79bb4..c32c83e 100644 --- a/GameEngine/src/main/java/gameEngine/GameEditor.java +++ b/GameEngine/src/main/java/gameEngine/GameEditor.java @@ -1,8 +1,11 @@ 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; @@ -28,6 +31,7 @@ private Button createEntityViewButton; private Text createEntityViewButtonText; + public InputField[][] inspectorInputFields; public GameEditor(Scene scene, float windowWidth, float windowHeight) { this.scene = (EditorScene) scene; @@ -71,11 +75,13 @@ 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); + Text labelText = new Text(labelX, labelY - 6, labels[i] + ":", 18); updatableviews.add(labelText); for (int j = 0; j < axes.length; j++) { @@ -85,10 +91,48 @@ 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; + 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; + } + }); + updatableviews.add(axisText); updatableviews.add(field); } } + this.inspectorInputFields = inputFields; } private void setButtonListeners() { @@ -121,6 +165,20 @@ 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(){ diff --git a/GameEngine/src/main/java/gameEngine/entites/editorComponents/Draggable.java b/GameEngine/src/main/java/gameEngine/entites/editorComponents/Draggable.java index 3c7e4f1..0ab2d51 100644 --- a/GameEngine/src/main/java/gameEngine/entites/editorComponents/Draggable.java +++ b/GameEngine/src/main/java/gameEngine/entites/editorComponents/Draggable.java @@ -26,12 +26,14 @@ if (scene.getSelectedEntity() == null) { if (Input.GetMouseButtonDown(0) && isMouseOver(mouseX, mouseY)) { isLeftDragging = true; - scene.setSelectedEntity(parent); // エンティティを選択 + scene.setSelectedEntity(parent); + scene.setClickedEntity(parent); } if (Input.GetMouseButtonDown(1) && isMouseOver(mouseX, mouseY)) { isRightDragging = true; scene.setSelectedEntity(parent); + scene.setClickedEntity(parent); } } diff --git a/GameEngine/src/main/java/gameEngine/scenes/EditorScene.java b/GameEngine/src/main/java/gameEngine/scenes/EditorScene.java index f1d5e51..f5e2de0 100644 --- a/GameEngine/src/main/java/gameEngine/scenes/EditorScene.java +++ b/GameEngine/src/main/java/gameEngine/scenes/EditorScene.java @@ -9,6 +9,7 @@ import gameEngine.entites.editorComponents.Draggable; import gameEngine.entites.editorComponents.EditorText; import gameEngine.entites.editorComponents.EntityView; +import gameEngine.geometry.Transform; import gameEngine.views.Window; import org.joml.Vector3f; @@ -23,6 +24,7 @@ private EditorEntity selectedEntity = null; public ConnectionManager connectionManager = new ConnectionManager(); public ResourceManager resourceManager = new ResourceManager(); + private EditorEntity clickedEntity = null; public EditorScene(float windowWidth, float windowHeight){ System.out.println("Active Editor scene"); @@ -110,11 +112,28 @@ return selectedEntity; } + public EditorEntity getClickedEntity(){ + return clickedEntity; + } + public void setSelectedEntity(EditorEntity editorEntity) { selectedEntity = editorEntity; System.out.println("Selected Entity: " + selectedEntity); } + public void setClickedEntity(EditorEntity editorEntity){ + clickedEntity = editorEntity; + + if (editorEntity != null) { + Transform transform = editorEntity.transform; + + // 初期値をInspectorに設定 + gameEditor.updateInspectorField(gameEditor.inspectorInputFields[0], transform.position); // Position + gameEditor.updateInspectorField(gameEditor.inspectorInputFields[1], transform.rotation); // Rotation + gameEditor.updateInspectorField(gameEditor.inspectorInputFields[2], transform.scale); // Scale + } + } + public void clearSelectedObject() { selectedEntity = null; } diff --git a/GameEngine/src/main/java/gameEngine/views/InputField.java b/GameEngine/src/main/java/gameEngine/views/InputField.java index 6324e31..6d2b179 100644 --- a/GameEngine/src/main/java/gameEngine/views/InputField.java +++ b/GameEngine/src/main/java/gameEngine/views/InputField.java @@ -13,21 +13,27 @@ private Vector3f position; private float width, height; private String placeholder; - private String inputText; + private String currentText; // 入力中のテキスト + private String displayedText; // 表示するテキスト private Sprite backgroundSprite; private int cursorPosition; // カーソル位置 private boolean showCursor; // カーソルの表示フラグ private float cursorBlinkTimer; // カーソルの点滅タイマー private final float CURSOR_BLINK_INTERVAL = 0.5f; // カーソルの点滅間隔(秒) private Color defaultColor; - private Color focusColor; + private OnChangeListener onChangeListener; + + public interface OnChangeListener { + void onChange(String newValue); + } public InputField(float posX, float posY, float width, float height, String placeholder, int textSize) { this.position = new Vector3f(posX, posY, 0); this.width = width; this.height = height; this.placeholder = placeholder; - this.inputText = ""; + this.currentText = ""; + this.displayedText = ""; this.isFocused = false; this.cursorPosition = 0; this.showCursor = true; @@ -36,10 +42,13 @@ this.backgroundSprite = new Sprite(Window.resourcePath + "EditorFrame.png", posX, posY, width, height); this.backgroundSprite.setSize(width, height); - displayText = new Text(posX+2, posY, placeholder, textSize); - defaultColor = new Color(1, 1, 1, 1); // 通常時の色 - focusColor = new Color(0.7f, 0.7f, 0.7f, 1); // フォーカス中の色 - displayText.setColor(defaultColor); + displayText = new Text(posX + 2, posY, placeholder, textSize); + displayText.setText(displayedText.isEmpty() ? placeholder : displayedText); + displayText.setColor(new Color(1,1,1,1)); // RGB値 (白) + } + + public void setOnChangeListener(OnChangeListener listener) { + this.onChangeListener = listener; } public void update() { @@ -53,12 +62,12 @@ float mouseY = MouseInput.getY(); if (Input.GetMouseButtonDown(GLFW_MOUSE_BUTTON_LEFT)) { + boolean wasFocused = isFocused; isFocused = isMouseOver(mouseX, mouseY); - if (isFocused) { - setCursorPositionAtMouse(mouseX); - displayText.setColor(focusColor); // フォーカス中は色を薄く - } else { - displayText.setColor(defaultColor); // フォーカスが外れたら元の色 + + if (!isFocused && wasFocused && onChangeListener != null) { + displayedText = currentText; // 最終的な入力内容を更新 + onChangeListener.onChange(currentText); } } @@ -66,14 +75,17 @@ handleTextInput(); if (Input.GetKeyDown(GLFW_KEY_ENTER)) { - isFocused = false; // Enterで確定 - displayText.setColor(defaultColor); // 色をリセット + // Enterキーでフォーカス解除&内容を確定 + isFocused = false; + displayedText = currentText; // 入力内容を確定 + if (onChangeListener != null) { + onChangeListener.onChange(currentText); + } } if (Input.GetKeyDown(GLFW_KEY_BACKSPACE) && cursorPosition > 0) { - inputText = inputText.substring(0, cursorPosition - 1) + inputText.substring(cursorPosition); + currentText = currentText.substring(0, cursorPosition - 1) + currentText.substring(cursorPosition); cursorPosition--; - displayText.setText(inputText.isEmpty() ? placeholder : inputText); } } } @@ -84,10 +96,9 @@ // 英字入力 for (int key = GLFW_KEY_A; key <= GLFW_KEY_Z; key++) { if (Input.GetKeyDown(key)) { - char typedChar = (char) (isShiftPressed ? key : key + 32); // シフトキーで大文字、小文字切り替え - inputText = inputText.substring(0, cursorPosition) + typedChar + inputText.substring(cursorPosition); + char typedChar = (char) (isShiftPressed ? key : key + 32); // シフトキーで大文字・小文字切り替え + currentText = currentText.substring(0, cursorPosition) + typedChar + currentText.substring(cursorPosition); cursorPosition++; - displayText.setText(inputText.isEmpty() ? placeholder : inputText); } } @@ -95,9 +106,9 @@ for (int key = GLFW_KEY_0; key <= GLFW_KEY_9; key++) { if (Input.GetKeyDown(key)) { char typedChar = (char) ('0' + (key - GLFW_KEY_0)); - inputText = inputText.substring(0, cursorPosition) + typedChar + inputText.substring(cursorPosition); + currentText = currentText.substring(0, cursorPosition) + typedChar + currentText.substring(cursorPosition); cursorPosition++; - displayText.setText(inputText.isEmpty() ? placeholder : inputText); + displayText.setText(currentText.isEmpty() ? placeholder : currentText); } } @@ -105,9 +116,9 @@ for (int key = GLFW_KEY_KP_0; key <= GLFW_KEY_KP_9; key++) { if (Input.GetKeyDown(key)) { char typedChar = (char) ('0' + (key - GLFW_KEY_KP_0)); - inputText = inputText.substring(0, cursorPosition) + typedChar + inputText.substring(cursorPosition); + currentText = currentText.substring(0, cursorPosition) + typedChar + currentText.substring(cursorPosition); cursorPosition++; - displayText.setText(inputText.isEmpty() ? placeholder : inputText); + displayText.setText(currentText.isEmpty() ? placeholder : currentText); } } @@ -129,24 +140,24 @@ // Deleteキーでカーソル右側の文字を削除 - if (Input.GetKeyDown(GLFW_KEY_DELETE) && cursorPosition < inputText.length()) { - inputText = inputText.substring(0, cursorPosition) + inputText.substring(cursorPosition + 1); - displayText.setText(inputText.isEmpty() ? placeholder : inputText); + if (Input.GetKeyDown(GLFW_KEY_DELETE) && cursorPosition < currentText.length()) { + currentText = currentText.substring(0, cursorPosition) + currentText.substring(cursorPosition + 1); + displayText.setText(currentText.isEmpty() ? placeholder : currentText); } // 左右キーでカーソル移動 if (Input.GetKeyDown(GLFW_KEY_LEFT) && cursorPosition > 0) { cursorPosition--; } - if (Input.GetKeyDown(GLFW_KEY_RIGHT) && cursorPosition < inputText.length()) { + if (Input.GetKeyDown(GLFW_KEY_RIGHT) && cursorPosition < currentText.length()) { cursorPosition++; } } private void appendChar(char c) { - inputText = inputText.substring(0, cursorPosition) + c + inputText.substring(cursorPosition); + currentText = currentText.substring(0, cursorPosition) + c + currentText.substring(cursorPosition); cursorPosition++; - displayText.setText(inputText.isEmpty() ? placeholder : inputText); + displayText.setText(currentText.isEmpty() ? placeholder : currentText); } private void updateCursorBlink(float deltaTime) { @@ -163,10 +174,10 @@ if (relativeMouseX <= 0) { cursorPosition = 0; } else if (relativeMouseX >= displayText.getDisplayedWidth()) { - cursorPosition = inputText.length(); + cursorPosition = currentText.length(); } else { float cumulativeWidth = 0; - for (int i = 0; i < inputText.length(); i++) { + for (int i = 0; i < currentText.length(); i++) { cumulativeWidth += displayText.getWidthAtIndex(i); // 各文字の幅を取得 if (cumulativeWidth >= relativeMouseX) { cursorPosition = i; // 最も近い文字位置 @@ -183,14 +194,25 @@ public void render() { backgroundSprite.update(); + displayText.setText(isFocused ? currentText : (displayedText.isEmpty() ? placeholder : displayedText)); displayText.update(); if (isFocused && showCursor) { float cursorX = position.x + displayText.getWidthAtIndex(cursorPosition); - float cursorHeight = displayText.getDisplayedHeight(); - Sprite sprite = new Sprite(Window.resourcePath + "button.png", cursorX, position.y, 1, 1); - sprite.setSize(2, height); - sprite.update(); + Text cursor = new Text(cursorX, position.y, "|", displayText.textSize); + cursor.setColor(new Color(1,1,1,1)); // RGB値 (白) + cursor.update(); + } + } + + public String getInputText() { + return currentText; + } + + public void setText(String text) { + this.displayedText = text; + if (!isFocused) { + displayText.setText(displayedText.isEmpty() ? placeholder : displayedText); } } }