Skip to content

Commit

Permalink
Handle local coords in blender-style transforms.
Browse files Browse the repository at this point in the history
Pressing XX/YY/ZZ will lock the transform to a local (rather than
global) axis.

This is achieved by temporarily toggling the local transform button. I
did this (vs handling it in the transform functions) for 3 reasons:

- Transform logic for translate/rotate (but not scale) appears to be
  tightly coupled to the gizmo
- This ensures the gizmo changes to indicate we're transforming
  locally/globally
- Toggling the button state in the UI also gives the user feedback about
  the nature of the transform.

The original state of the button is reset when the transform completes.

See godotengine/godot-proposals#1215.
  • Loading branch information
rcorre committed Jul 19, 2020
1 parent 27b66ae commit 4bb6738
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
46 changes: 30 additions & 16 deletions editor/plugins/spatial_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ void SpatialEditorViewport::_update_name() {

void SpatialEditorViewport::_compute_edit(const Point2 &p_point) {

_edit.original_local = spatial_editor->are_local_coords_enabled();
_edit.click_ray = _get_ray(Vector2(p_point.x, p_point.y));
_edit.click_ray_pos = _get_ray_pos(Vector2(p_point.x, p_point.y));
_edit.plane = TRANSFORM_VIEW;
Expand Down Expand Up @@ -1384,6 +1385,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
}
undo_redo->commit_action();
_edit.mode = TRANSFORM_NONE;
spatial_editor->set_local_coords_enabled(_edit.original_local);
set_message("");
}

Expand Down Expand Up @@ -1980,30 +1982,42 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {

if (_edit.mode != TRANSFORM_NONE) {
// We're actively transforming, handle keys specially
bool handled = true;
TransformPlane new_plane = TRANSFORM_VIEW;
String new_message;
if (ED_IS_SHORTCUT("spatial_editor/lock_transform_x", p_event)) {
_edit.plane = TRANSFORM_X_AXIS;
set_message(TTR("X-Axis Transform."), 2);
new_plane = TRANSFORM_X_AXIS;
new_message = TTR("X-Axis Transform.");
} else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_y", p_event)) {
_edit.plane = TRANSFORM_Y_AXIS;
set_message(TTR("Y-Axis Transform."), 2);
new_plane = TRANSFORM_Y_AXIS;
new_message = TTR("Y-Axis Transform.");
} else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_z", p_event)) {
_edit.plane = TRANSFORM_Z_AXIS;
set_message(TTR("Z-Axis Transform."), 2);
new_plane = TRANSFORM_Z_AXIS;
new_message = TTR("Z-Axis Transform.");
} else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_yz", p_event)) {
_edit.plane = TRANSFORM_YZ;
set_message(TTR("YZ-Plane Transform."), 2);
new_plane = TRANSFORM_YZ;
new_message = TTR("YZ-Plane Transform.");
} else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_xz", p_event)) {
_edit.plane = TRANSFORM_XZ;
set_message(TTR("XZ-Plane Transform."), 2);
new_plane = TRANSFORM_XZ;
new_message = TTR("XZ-Plane Transform.");
} else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_xy", p_event)) {
_edit.plane = TRANSFORM_XY;
set_message(TTR("XY-Plane Transform."), 2);
} else {
handled = false;
new_plane = TRANSFORM_XY;
new_message = TTR("XY-Plane Transform.");
}

if (handled) {
if (new_plane != TRANSFORM_VIEW) {
if (new_plane != _edit.plane) {
// lock me once and get a global constraint
_edit.plane = new_plane;
spatial_editor->set_local_coords_enabled(false);
} else if (!spatial_editor->are_local_coords_enabled()) {
// lock me twice and get a local constraint
spatial_editor->set_local_coords_enabled(true);
} else {
// lock me thrice and we're back where we started
_edit.plane = TRANSFORM_VIEW;
spatial_editor->set_local_coords_enabled(false);
}
set_message(new_message, 2);
accept_event();
return;
}
Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/spatial_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ class SpatialEditorViewport : public Control {
int gizmo_handle;
Variant gizmo_initial_value;
Vector3 gizmo_initial_pos;
bool original_local;
} _edit;

struct Cursor {
Expand Down Expand Up @@ -745,6 +746,7 @@ class SpatialEditor : public VBoxContainer {

ToolMode get_tool_mode() const { return tool_mode; }
bool are_local_coords_enabled() const { return tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->is_pressed(); }
void set_local_coords_enabled(bool on) const { tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->set_pressed(on); }
bool is_snap_enabled() const { return snap_enabled ^ snap_key_enabled; }
float get_translate_snap() const;
float get_rotate_snap() const;
Expand Down

0 comments on commit 4bb6738

Please sign in to comment.