From ee830de62e4d83701a1ce59f544c0fc132a4fc8f Mon Sep 17 00:00:00 2001 From: Rodz Labs Date: Thu, 7 Nov 2024 21:42:26 +0100 Subject: [PATCH] UI scaling related fixes --- addons/flexible_layout/flexible_layout.gd | 3 +-- material_maker/globals.gd | 8 ++++++++ material_maker/main_window.gd | 6 ++++++ material_maker/nodes/base.gd | 6 +++--- material_maker/nodes/generic/generic.gd | 2 +- material_maker/panels/library/library.gd | 2 +- material_maker/panels/preview_2d/preview_2d_panel.gd | 2 +- material_maker/panels/preview_3d/preview_3d.gd | 2 +- material_maker/panels/preview_3d/preview_3d_panel.gd | 3 +-- material_maker/panels/reference/reference_panel.gd | 2 +- material_maker/windows/sdf_builder/sdf_builder.gd | 2 +- 11 files changed, 25 insertions(+), 13 deletions(-) diff --git a/addons/flexible_layout/flexible_layout.gd b/addons/flexible_layout/flexible_layout.gd index 10c57ed75..32ce80159 100644 --- a/addons/flexible_layout/flexible_layout.gd +++ b/addons/flexible_layout/flexible_layout.gd @@ -472,8 +472,7 @@ class FlexLayout: if rect.size.x == 0 or rect.size.y == 0: return if top: - var scale : float = control.get_window().content_scale_factor - rect.size = Vector2i(Vector2(rect.size)/scale) + rect.size = Vector2i(Vector2(rect.size)) top.layout(rect) main_control.layout_changed.emit() diff --git a/material_maker/globals.gd b/material_maker/globals.gd index 748d4a13a..91bc80ef2 100644 --- a/material_maker/globals.gd +++ b/material_maker/globals.gd @@ -38,6 +38,8 @@ func _enter_tree(): func _exit_tree(): config.save("user://mm_config.ini") +# Config + func has_config(key : String) -> bool: return config.has_section_key("config", key) @@ -52,6 +54,7 @@ func get_config(key : String): func set_config(key : String, value): config.set_value("config", key, value) +# Clipboard parsing func try_parse_palette(hex_values_str : String) -> Dictionary: var points = [] @@ -137,5 +140,10 @@ func parse_paste_data(data : String): last_parsed_paste_data = { type=type, graph=graph } return { type=type, graph=graph } +# Misc. UI functions + +func screen_space_mouse_position(control : Control) -> Vector2: + return control.get_local_mouse_position()*control.get_viewport().content_scale_factor+control.get_screen_position() + func set_tip_text(tip : String, timeout : float = 0.0): main_window.set_tip_text(tip, timeout) diff --git a/material_maker/main_window.gd b/material_maker/main_window.gd index bb78b0518..d5b68de58 100644 --- a/material_maker/main_window.gd +++ b/material_maker/main_window.gd @@ -235,6 +235,10 @@ func _ready() -> void: mm_logger.message("Material Maker "+ProjectSettings.get_setting("application/config/actual_release")) + size = get_viewport().size/get_viewport().content_scale_factor + position = Vector2i(0, 0) + + var menu_update_requested : bool = false func update_menus() -> void: @@ -287,6 +291,8 @@ func on_config_changed() -> void: # This prevents UI elements from being too small on hiDPI displays. ui_scale = 2 if DisplayServer.screen_get_dpi() >= 192 and DisplayServer.screen_get_size().x >= 2048 else 1 get_viewport().content_scale_factor = ui_scale + size = get_viewport().size/get_viewport().content_scale_factor + position = Vector2i(0, 0) #ProjectSettings.set_setting("display/window/stretch/scale", scale) # Clamp to reasonable values to avoid crashes on startup. diff --git a/material_maker/nodes/base.gd b/material_maker/nodes/base.gd index 747ea36ef..bbd008282 100644 --- a/material_maker/nodes/base.gd +++ b/material_maker/nodes/base.gd @@ -82,7 +82,7 @@ func randomness_button_create_popup(): if ! generator.is_seed_locked() and DisplayServer.clipboard_get().left(5) == "seed=": menu.add_item(tr("Paste seed"), 2) add_child(menu) - menu.popup(Rect2i(get_local_mouse_position()+get_screen_position(), Vector2i(0, 0))) + menu.popup(Rect2(mm_globals.screen_space_mouse_position(self), Vector2(0, 0))) menu.connect("popup_hide", Callable(menu, "queue_free")) menu.connect("id_pressed", Callable(self, "_on_seed_menu")) @@ -96,7 +96,7 @@ func buffer_button_create_popup(): menu.add_separator() menu.add_item(tr("Dump buffers"), MENU_BUFFER_DUMP) add_child(menu) - menu.popup(Rect2(get_local_mouse_position()+get_screen_position(), Vector2(0, 0))) + menu.popup(Rect2(mm_globals.screen_space_mouse_position(self), Vector2(0, 0))) menu.connect("popup_hide",Callable(menu,"queue_free")) menu.connect("id_pressed",Callable(self,"_on_buffer_menu")) @@ -281,7 +281,7 @@ func _on_gui_input(event) -> void: add_child(menu) menu.popup_hide.connect(menu.queue_free) menu.id_pressed.connect(self._on_menu_id_pressed) - menu.popup(Rect2(get_local_mouse_position()+get_screen_position(), Vector2(0, 0))) + menu.popup(Rect2(mm_globals.screen_space_mouse_position(self), Vector2(0, 0))) else: menu.free() elif doubleclicked: diff --git a/material_maker/nodes/generic/generic.gd b/material_maker/nodes/generic/generic.gd index 4915e67c4..d13ccfdc7 100644 --- a/material_maker/nodes/generic/generic.gd +++ b/material_maker/nodes/generic/generic.gd @@ -37,7 +37,7 @@ func generic_button_create_popup(): add_child(popup_menu) popup_menu.connect("popup_hide",Callable(popup_menu,"queue_free")) popup_menu.connect("id_pressed",Callable(self,"update_generic")) - popup_menu.popup(Rect2(get_local_mouse_position()+get_screen_position(), Vector2(0, 0))) + popup_menu.popup(Rect2(mm_globals.screen_space_mouse_position(self), Vector2(0, 0))) diff --git a/material_maker/panels/library/library.gd b/material_maker/panels/library/library.gd index f33e0dc7e..4646606aa 100644 --- a/material_maker/panels/library/library.gd +++ b/material_maker/panels/library/library.gd @@ -306,7 +306,7 @@ func _on_Tree_item_rmb_selected(mouse_position : Vector2i): item_menu.set_item_disabled(0, read_only) item_menu.set_item_disabled(1, read_only) item_menu.set_item_disabled(2, read_only) - item_menu.popup(Rect2(get_local_mouse_position()+get_screen_position(), Vector2(0, 0))) + item_menu.popup(Rect2(mm_globals.screen_space_mouse_position(self), Vector2(0, 0))) func _on_PopupMenu_index_pressed(index): var library_index : int = 0 diff --git a/material_maker/panels/preview_2d/preview_2d_panel.gd b/material_maker/panels/preview_2d/preview_2d_panel.gd index 47e004e19..9cf1969ac 100644 --- a/material_maker/panels/preview_2d/preview_2d_panel.gd +++ b/material_maker/panels/preview_2d/preview_2d_panel.gd @@ -203,7 +203,7 @@ func _on_gui_input(event): elif event.is_command_or_control_pressed(): zooming = true MOUSE_BUTTON_RIGHT: - $ContextMenu.popup(Rect2(get_local_mouse_position()+get_screen_position(), Vector2(0, 0))) + $ContextMenu.popup(Rect2(mm_globals.screen_space_mouse_position(self), Vector2(0, 0))) else: dragging = false zooming = false diff --git a/material_maker/panels/preview_3d/preview_3d.gd b/material_maker/panels/preview_3d/preview_3d.gd index 630e0e61a..f17e23a97 100644 --- a/material_maker/panels/preview_3d/preview_3d.gd +++ b/material_maker/panels/preview_3d/preview_3d.gd @@ -202,7 +202,7 @@ func on_gui_input(event) -> void: if event.pressed and lpressed != rpressed: # xor Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) - _mouse_start_position = event.global_position/get_window().content_scale_factor + _mouse_start_position = event.global_position moving = true elif not lpressed and not rpressed: Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN) # allow and hide cursor warp diff --git a/material_maker/panels/preview_3d/preview_3d_panel.gd b/material_maker/panels/preview_3d/preview_3d_panel.gd index fbeb47c6e..094dfc926 100644 --- a/material_maker/panels/preview_3d/preview_3d_panel.gd +++ b/material_maker/panels/preview_3d/preview_3d_panel.gd @@ -23,7 +23,6 @@ func on_right_click(): #todo $MaterialPreview.keep_3d_linear = true await get_tree().process_frame await get_tree().process_frame - await get_tree().process_frame # Pick position in image var texture : ViewportTexture = $MaterialPreview.get_texture() var image : Image = texture.get_image() @@ -36,7 +35,7 @@ func on_right_click(): # Reset normal rendering current_object.set_surface_override_material(0, material_save) $TextureRect.visible = false - $PopupMenu.popup(Rect2(get_local_mouse_position()+get_screen_position(), Vector2(0, 0))) + $PopupMenu.popup(Rect2(mm_globals.screen_space_mouse_position(self), Vector2(0, 0))) func _on_PopupMenu_id_pressed(id): var pivot = get_node("MaterialPreview/Preview3d/ObjectsPivot/Objects") diff --git a/material_maker/panels/reference/reference_panel.gd b/material_maker/panels/reference/reference_panel.gd index 3f29f27e1..8affa365e 100644 --- a/material_maker/panels/reference/reference_panel.gd +++ b/material_maker/panels/reference/reference_panel.gd @@ -73,7 +73,7 @@ func _on_Image_gui_input(event) -> void: elif event.button_index == MOUSE_BUTTON_MIDDLE: dragging = true elif event.button_index == MOUSE_BUTTON_RIGHT: - $ContextMenu.popup(Rect2(get_local_mouse_position()+get_screen_position(), Vector2(0, 0))) + $ContextMenu.popup(Rect2(mm_globals.screen_space_mouse_position(self), Vector2(0, 0))) elif event.button_index == MOUSE_BUTTON_MIDDLE: dragging = false elif event.button_index == MOUSE_BUTTON_LEFT: diff --git a/material_maker/windows/sdf_builder/sdf_builder.gd b/material_maker/windows/sdf_builder/sdf_builder.gd index 94f43e65a..894a3ce36 100644 --- a/material_maker/windows/sdf_builder/sdf_builder.gd +++ b/material_maker/windows/sdf_builder/sdf_builder.gd @@ -290,7 +290,7 @@ func show_menu(current_item : TreeItem): add_child(menu) menu.id_pressed.connect(self._on_menu.bind(current_item)) menu.popup_hide.connect(menu.queue_free) - menu.popup(Rect2($VBoxContainer.get_local_mouse_position()+$VBoxContainer.get_screen_position(), Vector2(0, 0))) + menu.popup(Rect2(mm_globals.screen_space_mouse_position($VBoxContainer), Vector2(0, 0))) func _on_Tree_gui_input(event : InputEvent): if event is InputEventMouseButton: