Fix position of Tooltips

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.
This commit is contained in:
Markus Sauermann 2022-11-13 21:38:29 +01:00
parent b05e1e7d69
commit c4ed247f5f
5 changed files with 32 additions and 11 deletions

View file

@ -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 {

View file

@ -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: {

View file

@ -649,6 +649,7 @@ public:
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;
@ -775,6 +776,7 @@ public:
ClearMode get_clear_mode() const;
virtual Transform2D get_screen_transform() const override;
virtual Transform2D get_popup_base_transform() const override;
SubViewport();
~SubViewport();

View file

@ -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);

View file

@ -314,6 +314,7 @@ public:
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;