Skip to content

Commit

Permalink
Merge pull request #313 from neph1/snapping
Browse files Browse the repository at this point in the history
grid and scene snapping
  • Loading branch information
tonihele authored Apr 21, 2022
2 parents d9fe5b9 + 89e370d commit f7f81f7
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
package com.jme3.gde.scenecomposer;

import com.jme3.asset.AssetManager;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.gde.core.scene.SceneApplication;
import com.jme3.gde.core.scene.controller.SceneToolController;
import com.jme3.gde.core.sceneexplorer.nodes.AbstractSceneExplorerNode;
import com.jme3.gde.core.sceneexplorer.nodes.JmeNode;
import com.jme3.gde.scenecomposer.gizmo.GizmoFactory;
import com.jme3.gde.scenecomposer.tools.shortcuts.ShortcutManager;
import com.jme3.input.event.KeyInputEvent;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
Expand Down Expand Up @@ -44,6 +50,8 @@ public class SceneComposerToolController extends SceneToolController {
private boolean selectTerrain = false;
private boolean selectGeometries = false;
private TransformationType transformationType = TransformationType.local;

private final float fifteenDegs = FastMath.HALF_PI / 6f;

public enum TransformationType {
local, global, camera
Expand Down Expand Up @@ -384,5 +392,95 @@ public TransformationType getTransformationType() {
public JmeNode getRootNode() {
return rootNode;
}

/**
* Update the selected spatial with translation from user input
*
* @param translation absolute translation
* @param constraints axes affected
*/
public void updateSelectedTranslation(final Vector3f translation,
final Vector3f constraints) {
if (isSnapToScene()) {
translation.set(snapToScene(translation));
}
if (isSnapToGrid()) {
if (constraints.x != 0f) {
translation.setX((int) translation.x);
}
if (constraints.y != 0f) {
translation.setY((int) translation.y);
}
if (constraints.z != 0f) {
translation.setZ((int) translation.z);
}
}
selected.setLocalTranslation(translation);
}

/**
* Update the selected spatial with rotation from user input
*
* @param rotation absolute rotation
* @param constraints axes affected
*/
public void updateSelectedRotation(final Quaternion rotation,
final Vector3f constraints) {
if (isSnapToGrid()) {
final float[] angles = new float[3];
rotation.toAngles(angles);

if (constraints.y != 0f) {
angles[1] = Math.round(angles[1] / FastMath.HALF_PI)
* fifteenDegs;
}
if (constraints.x != 0f) {
angles[0] = Math.round(angles[0] / FastMath.HALF_PI)
* fifteenDegs;
}
if (constraints.z != 0f) {
angles[2] = Math.round(angles[2] / FastMath.HALF_PI)
* fifteenDegs;
}
rotation.fromAngles(angles);
}
selected.setLocalRotation(rotation);
}

/**
* Update the selected spatial with scale from user input
*
* @param scale absolute scale
* @param constraints axes affected
*/
public void updateSelectedScale(final Vector3f scale,
final Vector3f constraints) {
if (isSnapToGrid()) {
if (constraints.x != 0f) {
scale.setX((int) Math.max(scale.x, 1));
}
if (constraints.y != 0f) {
scale.setY((int) Math.max(scale.y, 1));
}
if (constraints.z != 0f) {
scale.setZ((int) Math.max(scale.z, 1));
}
}
selected.setLocalScale(scale);
}

private Vector3f snapToScene(final Vector3f position) {
final Ray ray = new Ray(position, Vector3f.UNIT_Y.negate());
final CollisionResults collisionResults = new CollisionResults();
final Node root = getRootNode().getLookup().lookup(Node.class);
root.collideWith(ray, collisionResults);
for (CollisionResult r : collisionResults) {
if (r.getGeometry() != selected) {
position.y = r.getContactPoint().y;
break;
}
}
return position;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.gde.core.sceneexplorer.nodes.JmeNode;
import com.jme3.gde.core.sceneexplorer.nodes.JmeSpatial;
import com.jme3.gde.core.undoredo.AbstractUndoableSceneEdit;
import com.jme3.gde.scenecomposer.SceneComposerToolController;
import com.jme3.gde.scenecomposer.SceneEditTool;
Expand Down Expand Up @@ -142,7 +141,7 @@ public void draggedPrimary(Vector2f screenCoord, boolean pressed, JmeNode rootNo
position = startPosition.add(diff);
}
lastPosition = position;
toolController.getSelectedSpatial().setLocalTranslation(position);
toolController.updateSelectedTranslation(position, pickedMarker);
updateToolsTransformation();
}
}
Expand Down Expand Up @@ -210,4 +209,5 @@ public void setAfter(Vector3f after) {
this.after.set(after);
}
}

}
Loading

0 comments on commit f7f81f7

Please sign in to comment.