package gameEngine.entites.editorComponents; import gameEngine.entites.EditorEntity; import gameEngine.geometry.Transform; import gameEngine.input.Input; import gameEngine.input.MouseInput; import gameEngine.scenes.EditorScene; import gameEngine.views.Sprite; import gameEngine.views.Text; import gameEngine.views.Window; import org.joml.Vector3f; import java.util.ArrayList; import java.util.List; public abstract class DraggableComponent extends EditorComponent { protected EditorEntity parent; protected Sprite sprite; protected Text text; protected boolean isDraggable = true; protected boolean isLeftDragging = false; protected boolean isRightDragging = false; protected float dragOffsetX = 0.0f; protected float dragOffsetY = 0.0f; @Override public void init() { sprite.update(); sprite.updateSpriteDimensions(); } @Override public void update(){ if (!(Window.get().getScene() instanceof EditorScene)) return; Transform transform = parent.transform; Transform screenTransform = parent.screenTransform; Vector3f cameraPosition = Window.get().getScene().getCamera().getPosition(); Vector3f actualPosition = new Vector3f(screenTransform.position).sub(cameraPosition); //サブクラス固有処理 updatePortView(actualPosition, transform); additionalUpdate(actualPosition, transform, cameraPosition); updateSprite(actualPosition, transform); updateText(actualPosition, transform); } private void updateSprite(Vector3f actualPosition, Transform transform){ sprite.setPosition(actualPosition); sprite.setRotation(transform.rotation); sprite.setScale(transform.scale); sprite.update(); } private void updateText(Vector3f actualPosition, Transform transform){ text.setPosition(new Vector3f(0,-16,0).add(actualPosition)); text.setRotation(transform.rotation); text.setScale(transform.scale); text.update(); } public void setDraggable(boolean draggable) { this.isDraggable = draggable; } public void handleDragging() { if (!isDraggable) return; float mouseX = MouseInput.getX(); float mouseY = MouseInput.getY(); EditorScene scene = (EditorScene) Window.get().getScene(); if (scene.getSelectedEntity() == null) { if (Input.GetMouseButtonDown(0) && isMouseOver(mouseX, mouseY)) { isLeftDragging = true; scene.setSelectedEntity(parent); scene.setClickedEntity(parent); startDragAction(mouseX, mouseY); } if (Input.GetMouseButtonDown(1) && isMouseOver(mouseX, mouseY)) { isRightDragging = true; scene.setSelectedEntity(parent); scene.setClickedEntity(parent); startDragAction(mouseX, mouseY); } } if (isLeftDragging && Input.GetMouseButton(0)) { draggingAction(mouseX, mouseY, false); } if (isRightDragging && Input.GetMouseButton(1)) { draggingAction(mouseX, mouseY, true); } if (Input.GetMouseButtonUp(0)) { isLeftDragging = false; scene.clearSelectedObject(); } if (Input.GetMouseButtonUp(1)) { isRightDragging = false; scene.clearSelectedObject(); } } protected boolean isMouseOver(float mouseX, float mouseY) { return sprite.isMouseOver(mouseX, mouseY); } protected void startDragAction(float mouseX, float mouseY) { Vector3f cameraPosition = Window.get().getScene().getCamera().getPosition(); float worldMouseX = mouseX + cameraPosition.x; float worldMouseY = mouseY + cameraPosition.y; dragOffsetX = worldMouseX - parent.transform.position.x; dragOffsetY = worldMouseY - parent.transform.position.y; } protected void draggingAction(float mouseX, float mouseY, boolean isRightDrag) { Vector3f cameraPosition = Window.get().getScene().getCamera().getPosition(); float worldMouseX = mouseX + cameraPosition.x; float worldMouseY = mouseY + cameraPosition.y; float targetX = worldMouseX - dragOffsetX; float targetY = worldMouseY - dragOffsetY; float deltaX = targetX - parent.transform.position.x; float deltaY = targetY - parent.transform.position.y; parent.transform.setPosition(targetX, targetY, parent.transform.position.z); parent.screenTransform.setPosition( parent.transform.position.x - cameraPosition.x, parent.transform.position.y - cameraPosition.y, parent.transform.position.z - cameraPosition.z ); if (isRightDrag) { handleConnectedComponents(deltaX, deltaY, cameraPosition); } } private void handleConnectedComponents(float deltaX, float deltaY, Vector3f cameraPosition) { for (ComponentView component : getConnectedComponentViews()) { EditorEntity componentParent = component.parent; componentParent.transform.setPosition( componentParent.transform.position.x + deltaX, componentParent.transform.position.y + deltaY, componentParent.transform.position.z ); componentParent.screenTransform.setPosition( componentParent.transform.position.x - cameraPosition.x, componentParent.transform.position.y - cameraPosition.y, componentParent.transform.position.z - cameraPosition.z ); } } protected abstract void updatePortView(Vector3f actualPosition, Transform transform); protected abstract void additionalUpdate(Vector3f actualPosition, Transform transform, Vector3f cameraPosition); protected List<ComponentView> getConnectedComponentViews() { return new ArrayList<>(); } }