Skip to content

Commit

Permalink
Fix position of Tooltips
Browse files Browse the repository at this point in the history
CanvasItem::get_screen_transform returns a transform from the CanvasItem
to the coordinate system, where a Popup - created as a child of the
CanvasItem - should be opened.
get_screen_transform makes some simplifications, that work well, when used
in the editor, but not in general cases.

Since Popups like Tooltips are now used more commonly in projects,
it becomes necessary to correct these simplifications.

This solution introduces Viewport::get_popup_base_transform, which makes
the necessary calculations.
  • Loading branch information
Sauermann committed Jan 26, 2023
1 parent b05e1e7 commit c4ed247
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
12 changes: 1 addition & 11 deletions scene/main/canvas_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,7 @@ Transform2D CanvasItem::get_global_transform_with_canvas() const {

Transform2D CanvasItem::get_screen_transform() const {
ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
Transform2D xform = get_global_transform_with_canvas();

Window *w = Object::cast_to<Window>(get_viewport());
if (w && !w->is_embedding_subwindows()) {
Transform2D s;
s.set_origin(w->get_position());

xform = s * xform;
}

return xform;
return get_viewport()->get_popup_base_transform() * get_global_transform_with_canvas();
}

Transform2D CanvasItem::get_global_transform() const {
Expand Down
15 changes: 15 additions & 0 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4183,6 +4183,21 @@ Transform2D SubViewport::get_screen_transform() const {
return container_transform * Viewport::get_screen_transform();
}

Transform2D SubViewport::get_popup_base_transform() const {
if (is_embedding_subwindows()) {
return Transform2D();
}
SubViewportContainer *c = Object::cast_to<SubViewportContainer>(get_parent());
if (!c) {
return Viewport::get_screen_transform();
}
Transform2D container_transform;
if (c->is_stretch_enabled()) {
container_transform.scale(Vector2(c->get_stretch_shrink(), c->get_stretch_shrink()));
}
return c->get_screen_transform() * container_transform * Viewport::get_screen_transform();
}

void SubViewport::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
Expand Down
2 changes: 2 additions & 0 deletions scene/main/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ class Viewport : public Node {
bool get_canvas_cull_mask_bit(uint32_t p_layer) const;

virtual Transform2D get_screen_transform() const;
virtual Transform2D get_popup_base_transform() const { return Transform2D(); }

#ifndef _3D_DISABLED
bool use_xr = false;
Expand Down Expand Up @@ -775,6 +776,7 @@ class SubViewport : public Viewport {
ClearMode get_clear_mode() const;

virtual Transform2D get_screen_transform() const override;
virtual Transform2D get_popup_base_transform() const override;

SubViewport();
~SubViewport();
Expand Down
13 changes: 13 additions & 0 deletions scene/main/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,19 @@ Transform2D Window::get_screen_transform() const {
return embedder_transform * Viewport::get_screen_transform();
}

Transform2D Window::get_popup_base_transform() const {
if (is_embedding_subwindows()) {
return Transform2D();
}
Transform2D window_transform;
window_transform.set_origin(get_position());
window_transform *= Viewport::get_screen_transform();
if (_get_embedder()) {
return _get_embedder()->get_popup_base_transform() * window_transform;
}
return window_transform;
}

void Window::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_title", "title"), &Window::set_title);
ClassDB::bind_method(D_METHOD("get_title"), &Window::get_title);
Expand Down
1 change: 1 addition & 0 deletions scene/main/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class Window : public Viewport {
int get_theme_default_font_size() const;

virtual Transform2D get_screen_transform() const override;
virtual Transform2D get_popup_base_transform() const override;

Rect2i get_parent_rect() const;
virtual DisplayServer::WindowID get_window_id() const override;
Expand Down

0 comments on commit c4ed247

Please sign in to comment.