Skip to content

Commit

Permalink
Merge pull request #88829 from ryevdokimov/institate_message-2d
Browse files Browse the repository at this point in the history
Show instantiating or inherit message when using drag to instantiate in 2D
  • Loading branch information
akien-mga committed Feb 27, 2024
2 parents 4a49373 + 8875ff0 commit 6d8ca17
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 50 deletions.
110 changes: 61 additions & 49 deletions editor/plugins/canvas_item_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3684,65 +3684,63 @@ void CanvasItemEditor::_draw_hover() {
}
}

void CanvasItemEditor::_draw_transform_message() {
if (drag_type == DRAG_NONE || drag_selection.is_empty() || !drag_selection.front()->get()) {
return;
}
String transform_message;
Transform2D current_transform = drag_selection.front()->get()->get_global_transform();
void CanvasItemEditor::_draw_message() {
if (drag_type != DRAG_NONE && !drag_selection.is_empty() && drag_selection.front()->get()) {
Transform2D current_transform = drag_selection.front()->get()->get_global_transform();

double snap = EDITOR_GET("interface/inspector/default_float_step");
int snap_step_decimals = Math::range_step_decimals(snap);
double snap = EDITOR_GET("interface/inspector/default_float_step");
int snap_step_decimals = Math::range_step_decimals(snap);
#define FORMAT(value) (TS->format_number(String::num(value, snap_step_decimals)))

switch (drag_type) {
case DRAG_MOVE:
case DRAG_MOVE_X:
case DRAG_MOVE_Y: {
Vector2 delta = current_transform.get_origin() - original_transform.get_origin();
if (drag_type == DRAG_MOVE) {
transform_message = TTR("Moving:") + " (" + FORMAT(delta.x) + ", " + FORMAT(delta.y) + ") px";
} else if (drag_type == DRAG_MOVE_X) {
transform_message = TTR("Moving:") + " " + FORMAT(delta.x) + " px";
} else if (drag_type == DRAG_MOVE_Y) {
transform_message = TTR("Moving:") + " " + FORMAT(delta.y) + " px";
}
} break;

case DRAG_ROTATE: {
real_t delta = Math::rad_to_deg(current_transform.get_rotation() - original_transform.get_rotation());
transform_message = TTR("Rotating:") + " " + FORMAT(delta) + String::utf8(" °");
} break;

case DRAG_SCALE_X:
case DRAG_SCALE_Y:
case DRAG_SCALE_BOTH: {
Vector2 original_scale = (Math::is_zero_approx(original_transform.get_scale().x) || Math::is_zero_approx(original_transform.get_scale().y)) ? Vector2(CMP_EPSILON, CMP_EPSILON) : original_transform.get_scale();
Vector2 delta = current_transform.get_scale() / original_scale;
if (drag_type == DRAG_SCALE_BOTH) {
transform_message = TTR("Scaling:") + String::utf8(" ×(") + FORMAT(delta.x) + ", " + FORMAT(delta.y) + ")";
} else if (drag_type == DRAG_SCALE_X) {
transform_message = TTR("Scaling:") + String::utf8(" ×") + FORMAT(delta.x);
} else if (drag_type == DRAG_SCALE_Y) {
transform_message = TTR("Scaling:") + String::utf8(" ×") + FORMAT(delta.y);
}
} break;
switch (drag_type) {
case DRAG_MOVE:
case DRAG_MOVE_X:
case DRAG_MOVE_Y: {
Vector2 delta = current_transform.get_origin() - original_transform.get_origin();
if (drag_type == DRAG_MOVE) {
message = TTR("Moving:") + " (" + FORMAT(delta.x) + ", " + FORMAT(delta.y) + ") px";
} else if (drag_type == DRAG_MOVE_X) {
message = TTR("Moving:") + " " + FORMAT(delta.x) + " px";
} else if (drag_type == DRAG_MOVE_Y) {
message = TTR("Moving:") + " " + FORMAT(delta.y) + " px";
}
} break;

case DRAG_ROTATE: {
real_t delta = Math::rad_to_deg(current_transform.get_rotation() - original_transform.get_rotation());
message = TTR("Rotating:") + " " + FORMAT(delta) + String::utf8(" °");
} break;

case DRAG_SCALE_X:
case DRAG_SCALE_Y:
case DRAG_SCALE_BOTH: {
Vector2 original_scale = (Math::is_zero_approx(original_transform.get_scale().x) || Math::is_zero_approx(original_transform.get_scale().y)) ? Vector2(CMP_EPSILON, CMP_EPSILON) : original_transform.get_scale();
Vector2 delta = current_transform.get_scale() / original_scale;
if (drag_type == DRAG_SCALE_BOTH) {
message = TTR("Scaling:") + String::utf8(" ×(") + FORMAT(delta.x) + ", " + FORMAT(delta.y) + ")";
} else if (drag_type == DRAG_SCALE_X) {
message = TTR("Scaling:") + String::utf8(" ×") + FORMAT(delta.x);
} else if (drag_type == DRAG_SCALE_Y) {
message = TTR("Scaling:") + String::utf8(" ×") + FORMAT(delta.y);
}
} break;

default:
break;
}
default:
break;
}
#undef FORMAT
}

if (transform_message.is_empty()) {
if (message.is_empty()) {
return;
}

Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
Point2 msgpos = Point2(RULER_WIDTH + 5 * EDSCALE, viewport->get_size().y - 20 * EDSCALE);
viewport->draw_string(font, msgpos + Point2(1, 1), transform_message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8));
viewport->draw_string(font, msgpos + Point2(-1, -1), transform_message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8));
viewport->draw_string(font, msgpos, transform_message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(1, 1, 1, 1));
viewport->draw_string(font, msgpos + Point2(1, 1), message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8));
viewport->draw_string(font, msgpos + Point2(-1, -1), message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8));
viewport->draw_string(font, msgpos, message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(1, 1, 1, 1));
}

void CanvasItemEditor::_draw_locks_and_groups(Node *p_node, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform) {
Expand Down Expand Up @@ -3856,7 +3854,7 @@ void CanvasItemEditor::_draw_viewport() {
_draw_smart_snapping();
_draw_focus();
_draw_hover();
_draw_transform_message();
_draw_message();
}

void CanvasItemEditor::update_viewport() {
Expand Down Expand Up @@ -4740,6 +4738,7 @@ void CanvasItemEditor::_focus_selection(int p_op) {
}

void CanvasItemEditor::_reset_drag() {
message = "";
drag_type = DRAG_NONE;
drag_selection.clear();
}
Expand Down Expand Up @@ -5693,6 +5692,7 @@ void CanvasItemEditorViewport::_create_preview(const Vector<String> &files) cons

void CanvasItemEditorViewport::_remove_preview() {
if (preview_node->get_parent()) {
canvas_item_editor->message = "";
for (int i = preview_node->get_child_count() - 1; i >= 0; i--) {
Node *node = preview_node->get_child(i);
node->queue_free();
Expand Down Expand Up @@ -5938,7 +5938,19 @@ bool CanvasItemEditorViewport::can_drop_data(const Point2 &p_point, const Varian
}
Transform2D trans = canvas_item_editor->get_canvas_transform();
preview_node->set_position((p_point - trans.get_origin()) / trans.get_scale().x);
label->set_text(vformat(TTR("Adding %s..."), default_texture_node_type));
String scene_file_path = preview_node->get_child(0)->get_scene_file_path();
if (scene_file_path.is_empty() || preview_node->get_tree()->get_edited_scene_root()) {
double snap = EDITOR_GET("interface/inspector/default_float_step");
int snap_step_decimals = Math::range_step_decimals(snap);
#define FORMAT(value) (TS->format_number(String::num(value, snap_step_decimals)))
Vector2 preview_node_pos = preview_node->get_global_position();
canvas_item_editor->message = TTR("Instantiating:") + " (" + FORMAT(preview_node_pos.x) + ", " + FORMAT(preview_node_pos.y) + ") px";
label->set_text(vformat(TTR("Adding %s..."), default_texture_node_type));
} else {
canvas_item_editor->message = TTR("Creating inherited scene from: ") + scene_file_path;
}

canvas_item_editor->update_viewport();
}
return can_instantiate;
}
Expand Down
4 changes: 3 additions & 1 deletion editor/plugins/canvas_item_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ class CanvasItemEditor : public VBoxContainer {
void _draw_invisible_nodes_positions(Node *p_node, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
void _draw_locks_and_groups(Node *p_node, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
void _draw_hover();
void _draw_transform_message();
void _draw_message();

void _draw_viewport();

Expand Down Expand Up @@ -544,6 +544,8 @@ class CanvasItemEditor : public VBoxContainer {
SNAP_DEFAULT = SNAP_GRID | SNAP_GUIDES | SNAP_PIXEL,
};

String message;

Point2 snap_point(Point2 p_target, unsigned int p_modes = SNAP_DEFAULT, unsigned int p_forced_modes = 0, const CanvasItem *p_self_canvas_item = nullptr, const List<CanvasItem *> &p_other_nodes_exceptions = List<CanvasItem *>());
real_t snap_angle(real_t p_target, real_t p_start = 0) const;

Expand Down

0 comments on commit 6d8ca17

Please sign in to comment.