Skip to content

Commit 4bb6738

Browse files
committed
Handle local coords in blender-style transforms.
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.
1 parent 27b66ae commit 4bb6738

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

editor/plugins/spatial_editor_plugin.cpp

+30-16
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ void SpatialEditorViewport::_update_name() {
752752

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

755+
_edit.original_local = spatial_editor->are_local_coords_enabled();
755756
_edit.click_ray = _get_ray(Vector2(p_point.x, p_point.y));
756757
_edit.click_ray_pos = _get_ray_pos(Vector2(p_point.x, p_point.y));
757758
_edit.plane = TRANSFORM_VIEW;
@@ -1384,6 +1385,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
13841385
}
13851386
undo_redo->commit_action();
13861387
_edit.mode = TRANSFORM_NONE;
1388+
spatial_editor->set_local_coords_enabled(_edit.original_local);
13871389
set_message("");
13881390
}
13891391

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

19811983
if (_edit.mode != TRANSFORM_NONE) {
19821984
// We're actively transforming, handle keys specially
1983-
bool handled = true;
1985+
TransformPlane new_plane = TRANSFORM_VIEW;
1986+
String new_message;
19841987
if (ED_IS_SHORTCUT("spatial_editor/lock_transform_x", p_event)) {
1985-
_edit.plane = TRANSFORM_X_AXIS;
1986-
set_message(TTR("X-Axis Transform."), 2);
1988+
new_plane = TRANSFORM_X_AXIS;
1989+
new_message = TTR("X-Axis Transform.");
19871990
} else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_y", p_event)) {
1988-
_edit.plane = TRANSFORM_Y_AXIS;
1989-
set_message(TTR("Y-Axis Transform."), 2);
1991+
new_plane = TRANSFORM_Y_AXIS;
1992+
new_message = TTR("Y-Axis Transform.");
19901993
} else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_z", p_event)) {
1991-
_edit.plane = TRANSFORM_Z_AXIS;
1992-
set_message(TTR("Z-Axis Transform."), 2);
1994+
new_plane = TRANSFORM_Z_AXIS;
1995+
new_message = TTR("Z-Axis Transform.");
19931996
} else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_yz", p_event)) {
1994-
_edit.plane = TRANSFORM_YZ;
1995-
set_message(TTR("YZ-Plane Transform."), 2);
1997+
new_plane = TRANSFORM_YZ;
1998+
new_message = TTR("YZ-Plane Transform.");
19961999
} else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_xz", p_event)) {
1997-
_edit.plane = TRANSFORM_XZ;
1998-
set_message(TTR("XZ-Plane Transform."), 2);
2000+
new_plane = TRANSFORM_XZ;
2001+
new_message = TTR("XZ-Plane Transform.");
19992002
} else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_xy", p_event)) {
2000-
_edit.plane = TRANSFORM_XY;
2001-
set_message(TTR("XY-Plane Transform."), 2);
2002-
} else {
2003-
handled = false;
2003+
new_plane = TRANSFORM_XY;
2004+
new_message = TTR("XY-Plane Transform.");
20042005
}
20052006

2006-
if (handled) {
2007+
if (new_plane != TRANSFORM_VIEW) {
2008+
if (new_plane != _edit.plane) {
2009+
// lock me once and get a global constraint
2010+
_edit.plane = new_plane;
2011+
spatial_editor->set_local_coords_enabled(false);
2012+
} else if (!spatial_editor->are_local_coords_enabled()) {
2013+
// lock me twice and get a local constraint
2014+
spatial_editor->set_local_coords_enabled(true);
2015+
} else {
2016+
// lock me thrice and we're back where we started
2017+
_edit.plane = TRANSFORM_VIEW;
2018+
spatial_editor->set_local_coords_enabled(false);
2019+
}
2020+
set_message(new_message, 2);
20072021
accept_event();
20082022
return;
20092023
}

editor/plugins/spatial_editor_plugin.h

+2
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ class SpatialEditorViewport : public Control {
364364
int gizmo_handle;
365365
Variant gizmo_initial_value;
366366
Vector3 gizmo_initial_pos;
367+
bool original_local;
367368
} _edit;
368369

369370
struct Cursor {
@@ -745,6 +746,7 @@ class SpatialEditor : public VBoxContainer {
745746

746747
ToolMode get_tool_mode() const { return tool_mode; }
747748
bool are_local_coords_enabled() const { return tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->is_pressed(); }
749+
void set_local_coords_enabled(bool on) const { tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->set_pressed(on); }
748750
bool is_snap_enabled() const { return snap_enabled ^ snap_key_enabled; }
749751
float get_translate_snap() const;
750752
float get_rotate_snap() const;

0 commit comments

Comments
 (0)