Ported from Godot4: Fixes to editor subgizmos

* Fixed subgizmo editing on scaled nodes.
* Added more clarifications on the coordinate space of subgizmos.
* Given input priority to the transform gizmo over subgizmo selection.
- JFonS
d7b58ebc9c
This commit is contained in:
Relintai 2022-08-12 00:43:53 +02:00
parent 4b939998a7
commit 2bc7355910
2 changed files with 47 additions and 27 deletions

View File

@ -49,7 +49,7 @@
</argument>
<description>
Override this method to commit a group of subgizmos being edited (see [method _subgizmos_intersect_ray] and [method _subgizmos_intersect_frustum]). This usually means creating an [UndoRedo] action for the change, using the current transforms as "do" and the [code]restore[/code] transforms as "undo".
If the [code]cancel[/code] argument is [code]true[/code], the [code]restore[/code] transforms should be directly set, without any [UndoRedo] action. Called for this plugin's active gizmos.
If the [code]cancel[/code] argument is [code]true[/code], the [code]restore[/code] transforms should be directly set, without any [UndoRedo] action. As with all subgizmo methods, transforms are given in local space respect to the gizmo's Spatial. Called for this plugin's active gizmos.
</description>
</method>
<method name="create_gizmo" qualifiers="virtual">
@ -127,7 +127,7 @@
All built-in editor gizmos return a priority of [code]-1[/code]. If not overridden, this method will return [code]0[/code], which means custom gizmos will automatically get higher priority than built-in gizmos.
</description>
</method>
<method name="_get_subgizmo_transform" qualifiers="virtual">
<method name="get_subgizmo_transform" qualifiers="virtual">
<return type="Transform">
</return>
<argument index="0" name="gizmo" type="EditorSpatialGizmo">
@ -135,7 +135,7 @@
<argument index="1" name="id" type="int">
</argument>
<description>
Override this method to return the current transform of a subgizmo. This transform will be requested at the start of an edit and used in the [code]restore[/code] argument in [method _commit_subgizmos]. Called for this plugin's active gizmos.
Override this method to return the current transform of a subgizmo. As with all subgizmo methods, the transform should be in local space respect to the gizmo's Node3D. This transform will be requested at the start of an edit and used in the [code]restore[/code] argument in [method _commit_subgizmos]. Called for this plugin's active gizmos.
</description>
</method>
<method name="has_gizmo" qualifiers="virtual">

View File

@ -1550,15 +1550,15 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
//gizmo has priority over everything
bool can_select_gizmos = true;
bool can_select_gizmos = spatial_editor->get_single_selected_node();
{
int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
can_select_gizmos = view_menu->get_popup()->is_item_checked(idx);
can_select_gizmos = can_select_gizmos && view_menu->get_popup()->is_item_checked(idx);
}
if (can_select_gizmos && spatial_editor->get_single_selected_node()) {
SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(spatial_editor->get_single_selected_node());
// Gizmo handles
if (can_select_gizmos) {
Vector<Ref<SpatialGizmo>> gizmos = spatial_editor->get_single_selected_node()->get_gizmos();
bool intersected_handle = false;
@ -1569,6 +1569,40 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
continue;
}
int gizmo_handle = -1;
seg->handles_intersect_ray(camera, _edit.mouse_pos, b->get_shift(), gizmo_handle);
if (gizmo_handle != -1) {
_edit.gizmo = seg;
_edit.gizmo_handle = gizmo_handle;
_edit.gizmo_initial_value = seg->get_handle_value(gizmo_handle);
intersected_handle = true;
break;
}
}
if (intersected_handle) {
break;
}
}
// Transform gizmo
if (_transform_gizmo_select(_edit.mouse_pos)) {
break;
}
// Subgizmos
if (can_select_gizmos) {
SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(spatial_editor->get_single_selected_node());
Vector<Ref<SpatialGizmo>> gizmos = spatial_editor->get_single_selected_node()->get_gizmos();
bool intersected_subgizmo = false;
for (int i = 0; i < gizmos.size(); i++) {
Ref<EditorSpatialGizmo> seg = gizmos[i];
if ((!seg.is_valid())) {
continue;
}
int subgizmo_id = seg->subgizmos_intersect_ray(camera, _edit.mouse_pos);
if (subgizmo_id != -1) {
ERR_CONTINUE(!se);
@ -1590,31 +1624,18 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
}
seg->redraw();
spatial_editor->update_transform_gizmo();
intersected_handle = true;
break;
}
int gizmo_handle = -1;
seg->handles_intersect_ray(camera, _edit.mouse_pos, b->get_shift(), gizmo_handle);
if (gizmo_handle != -1) {
_edit.gizmo = seg;
_edit.gizmo_handle = gizmo_handle;
_edit.gizmo_initial_value = seg->get_handle_value(gizmo_handle);
intersected_handle = true;
spatial_editor->update_transform_gizmo();
intersected_subgizmo = true;
break;
}
}
if (intersected_handle) {
if (intersected_subgizmo) {
break;
}
}
if (_transform_gizmo_select(_edit.mouse_pos)) {
break;
}
clicked = 0;
if ((spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT && b->get_command()) || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE) {
@ -1933,7 +1954,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
Transform xform = GE->get();
Transform new_xform = _compute_transform(TRANSFORM_SCALE, se->original * xform, xform, motion, snap, local_coords);
if (!local_coords) {
new_xform = se->original.inverse() * new_xform;
new_xform = se->original.affine_inverse() * new_xform;
}
se->gizmo->set_subgizmo_transform(GE->key(), new_xform);
}
@ -2031,7 +2052,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
for (Map<int, Transform>::Element *GE = se->subgizmos.front(); GE; GE = GE->next()) {
Transform xform = GE->get();
Transform new_xform = _compute_transform(TRANSFORM_TRANSLATE, se->original * xform, xform, motion, snap, local_coords);
new_xform = se->original.inverse() * new_xform;
new_xform = se->original.affine_inverse() * new_xform;
se->gizmo->set_subgizmo_transform(GE->key(), new_xform);
}
@ -2120,7 +2141,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
Transform new_xform = _compute_transform(TRANSFORM_ROTATE, se->original * xform, xform, compute_axis, angle, local_coords);
if (!local_coords) {
new_xform = se->original.inverse() * new_xform;
new_xform = se->original.affine_inverse() * new_xform;
}
se->gizmo->set_subgizmo_transform(GE->key(), new_xform);
}
@ -7548,7 +7569,6 @@ bool SpatialEditor::is_gizmo_visible() const {
return gizmo.visible;
}
void SpatialEditor::snap_cursor_to_plane(const Plane &p_plane) {
//cursor.pos=p_plane.project(cursor.pos);
}