From 244078993ad487012a0c9e7e1eab4e9b0ce06e39 Mon Sep 17 00:00:00 2001 From: tafode Date: Fri, 22 Mar 2024 23:05:17 +0100 Subject: [PATCH] Created "Reset to Center" and "Zoom to Drawing" Functions Added Toolbar buttons for the new functions --- lorien/Assets/I18n/en.txt | 2 + lorien/Assets/Textures/reset_to_center.png | Bin 0 -> 257 bytes .../Textures/reset_to_center.png.import | 35 ++++++++++ lorien/Assets/Textures/zoom_to_drawing.png | Bin 0 -> 254 bytes .../Textures/zoom_to_drawing.png.import | 35 ++++++++++ lorien/InfiniteCanvas/InfiniteCanvas.gd | 10 +++ lorien/InfiniteCanvas/PanZoomCamera.gd | 65 ++++++++++++++++++ lorien/InfiniteCanvas/Tools/CanvasTool.gd | 1 + lorien/Main.gd | 17 +++++ lorien/UI/Toolbar.gd | 4 ++ lorien/UI/Toolbar.tscn | 38 +++++++++- lorien/project.godot | 10 +++ 12 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 lorien/Assets/Textures/reset_to_center.png create mode 100644 lorien/Assets/Textures/reset_to_center.png.import create mode 100644 lorien/Assets/Textures/zoom_to_drawing.png create mode 100644 lorien/Assets/Textures/zoom_to_drawing.png.import diff --git a/lorien/Assets/I18n/en.txt b/lorien/Assets/I18n/en.txt index c059e19a..9b54fc8a 100644 --- a/lorien/Assets/I18n/en.txt +++ b/lorien/Assets/I18n/en.txt @@ -174,6 +174,8 @@ ACTION_canvas_pan_up Pan up ACTION_canvas_pan_down Pan down ACTION_canvas_pan_right Pan right ACTION_canvas_pan_left Pan left +ACTION_canvas_reset_to_center Reset to center +ACTION_canvas_zoom_to_drawing Zoom to drawing # ----------------------------------------------------------------------------- # Kebindings dialog messages diff --git a/lorien/Assets/Textures/reset_to_center.png b/lorien/Assets/Textures/reset_to_center.png new file mode 100644 index 0000000000000000000000000000000000000000..443708d16cf3aa61fe3658fddfc9f1867acdd411 GIT binary patch literal 257 zcmeAS@N?(olHy`uVBq!ia0vp^LLkh+3?vf;>QaGJLV!<*E09)FFmcteNwYNU53{~s zsCIKf+PlNEet$BX`7|JFpMm+5Jnu~XAa4~T8%ulp5`%=M=)8x?v;W7ReRBHtpa1{= z&tAK|rLH|QD=RWAsLZKrjgisK@Z{^&yZWblb>$oUPTq4KXoo^ckY6wZP&GaQaGJLV!<*E09)FFmcteNwYNU53{~s zsCIKf+PlNEet$BX`7|JFpMm+5Jnu~XAa4~T8%ulp5`%=M=)8x?v;W7ReRBHtpa1{= z&tAK|rLH|QD=RWAsLZKrjgisK@Z{^&yZWblb>$oUPTq4KXoo^ckY6wZP&Ga2Wn*SboFyt=akR{0QcQ&@&Et; literal 0 HcmV?d00001 diff --git a/lorien/Assets/Textures/zoom_to_drawing.png.import b/lorien/Assets/Textures/zoom_to_drawing.png.import new file mode 100644 index 00000000..dc60f566 --- /dev/null +++ b/lorien/Assets/Textures/zoom_to_drawing.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/zoom_to_drawing.png-eea4c42eb958f91b5b39ab225a46cbb9.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://Assets/Textures/zoom_to_drawing.png" +dest_files=[ "res://.import/zoom_to_drawing.png-eea4c42eb958f91b5b39ab225a46cbb9.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/lorien/InfiniteCanvas/InfiniteCanvas.gd b/lorien/InfiniteCanvas/InfiniteCanvas.gd index 8372e41d..909478f9 100644 --- a/lorien/InfiniteCanvas/InfiniteCanvas.gd +++ b/lorien/InfiniteCanvas/InfiniteCanvas.gd @@ -97,6 +97,16 @@ func center_to_mouse() -> void: var screen_space_cursor_pos := _viewport.get_mouse_position() _camera.do_center(screen_space_cursor_pos) +# ------------------------------------------------------------------------------------------------- +func reset_to_center(): + if _active_tool != null: + _camera.reset_to_center() + +# ------------------------------------------------------------------------------------------------- +func zoom_to_drawing(): + if _active_tool != null: + _camera.zoom_to_drawing() + # ------------------------------------------------------------------------------------------------- func use_tool(tool_type: int) -> void: var prev_tool := _active_tool diff --git a/lorien/InfiniteCanvas/PanZoomCamera.gd b/lorien/InfiniteCanvas/PanZoomCamera.gd index 4a7eb5c8..70089f92 100644 --- a/lorien/InfiniteCanvas/PanZoomCamera.gd +++ b/lorien/InfiniteCanvas/PanZoomCamera.gd @@ -16,6 +16,9 @@ var _zoom_active := false var _current_zoom_level := 1.0 var _start_mouse_pos := Vector2(0.0, 0.0) +onready var _topbar : VBoxContainer = get_node("/root/Main/Topbar") +onready var _statusbar : Panel = get_node("/root/Main/Statusbar") + # ------------------------------------------------------------------------------------------------- func set_zoom_level(zoom_level: float) -> void: _current_zoom_level = _to_nearest_zoom_step(zoom_level) @@ -27,7 +30,68 @@ func do_center(screen_space_center_point: Vector2) -> void: var delta := screen_space_center - screen_space_center_point get_viewport().warp_mouse(screen_space_center) _do_pan(delta) + +# ------------------------------------------------------------------------------------------------- +func reset_to_center(): + offset = Vector2(0.0,0.0) + emit_signal("position_changed", offset) + _zoom_canvas(1.0, Vector2(0.0,0.0)) +# ------------------------------------------------------------------------------------------------- +func zoom_to_drawing(): + var max_dim : Vector2 = BrushStroke.MIN_VECTOR2 + var min_dim : Vector2 = BrushStroke.MAX_VECTOR2 + var project: Project = ProjectManager.get_active_project() + var zoom_steps_x : int = 0 + var zoom_steps_y : int = 0 + var zoom_x : float = 1 + var zoom_y :float = 1 + var final_zoom : float + var zoomed_viewport_size : Vector2 = get_viewport().size + + if project.strokes.empty(): + reset_to_center() + else: + #subtract UI size from viewport if it is visible + if _topbar.visible && _statusbar.visible: + zoomed_viewport_size.y = zoomed_viewport_size.y-(_topbar.rect_size.y+_statusbar.rect_size.y) + + for stroke in project.strokes: + min_dim.x = min(min_dim.x, stroke.top_left_pos.x) + min_dim.y = min(min_dim.y, stroke.top_left_pos.y) + max_dim.x = max(max_dim.x, stroke.bottom_right_pos.x) + max_dim.y = max(max_dim.y, stroke.bottom_right_pos.y) + + var diff_max_dim = Vector2(abs(max_dim.x-min_dim.x), abs(max_dim.y-min_dim.y)) + + while diff_max_dim.x > zoomed_viewport_size.x: + zoomed_viewport_size.x = zoomed_viewport_size.x * 1.1 + zoom_x = zoom_x*1.1 + zoom_steps_x = zoom_steps_x + 1 + + while diff_max_dim.y > zoomed_viewport_size.y: + zoomed_viewport_size.y = zoomed_viewport_size.y * 1.1 + zoom_y = zoom_y*1.1 + zoom_steps_y = zoom_steps_y +1 + + if zoom_steps_x > zoom_steps_y: + final_zoom = zoom_x + else: + final_zoom = zoom_y + + var anchor = Vector2(min_dim.x+get_viewport().size.x/2, min_dim.y+get_viewport().size.y/2) + + final_zoom = final_zoom * 1.1 + var center_offset : Vector2 + center_offset.x = (final_zoom*get_viewport().size.x)-diff_max_dim.x + center_offset.y = (final_zoom*(get_viewport().size.y))-diff_max_dim.y + + _zoom_canvas(final_zoom, anchor) + offset = min_dim + offset.y = offset.y-((center_offset.y)/2) + offset.x = offset.x-(center_offset.x/2) + emit_signal("position_changed", offset) + # ------------------------------------------------------------------------------------------------- func tool_event(event: InputEvent) -> void: if _is_input_enabled: @@ -132,3 +196,4 @@ func disable_input() -> void: # ------------------------------------------------------------------------------------------------- func xform(pos: Vector2) -> Vector2: return (pos * zoom) + offset + diff --git a/lorien/InfiniteCanvas/Tools/CanvasTool.gd b/lorien/InfiniteCanvas/Tools/CanvasTool.gd index 37473855..b72683ac 100644 --- a/lorien/InfiniteCanvas/Tools/CanvasTool.gd +++ b/lorien/InfiniteCanvas/Tools/CanvasTool.gd @@ -90,6 +90,7 @@ func end_stroke() -> void: # ------------------------------------------------------------------------------------------------- func xform_vector2(v: Vector2) -> Vector2: + print(_canvas.get_camera().xform(v * _canvas.get_canvas_scale())) return _canvas.get_camera().xform(v * _canvas.get_canvas_scale()) # ------------------------------------------------------------------------------------------------- diff --git a/lorien/Main.gd b/lorien/Main.gd index 852bf655..195893f6 100644 --- a/lorien/Main.gd +++ b/lorien/Main.gd @@ -47,6 +47,8 @@ func _ready(): _toolbar.connect("save_project", self, "_on_save_project") _toolbar.connect("brush_size_changed", self, "_on_brush_size_changed") _toolbar.connect("tool_changed", self, "_on_tool_changed") + _toolbar.connect("reset_to_center_action", self, "_on_reset_to_center_action") + _toolbar.connect("zoom_to_drawing_action", self, "_on_zoom_to_drawing_action") _menubar.connect("create_new_project", self, "_on_create_new_project") _menubar.connect("project_selected", self, "_on_project_selected") @@ -164,6 +166,10 @@ func _unhandled_input(event): _toggle_distraction_free_mode() elif Utils.event_pressed_bug_workaround("toggle_fullscreen", event): _toggle_fullscreen() + elif Utils.event_pressed_bug_workaround("canvas_reset_to_center", event): + _on_reset_to_center_action() + elif Utils.event_pressed_bug_workaround("canvas_zoom_to_drawing", event): + _on_zoom_to_drawing_action() # ------------------------------------------------------------------------------------------------- func _toggle_player() -> void: @@ -561,3 +567,14 @@ func _get_general_ui_scale() -> float: elif smallest_dimension >= 1700: return Config.DEFAULT_UI_SCALE * 1.5 return Config.DEFAULT_UI_SCALE + +# -------------------------------------------------------------------------------------------------- +func _on_reset_to_center_action() -> void: + print("On Reset To Camera Action") + _canvas.reset_to_center() + +# -------------------------------------------------------------------------------------------------- +func _on_zoom_to_drawing_action() -> void: + print("On Zoom To Drawing Action") + _canvas.zoom_to_drawing() + diff --git a/lorien/UI/Toolbar.gd b/lorien/UI/Toolbar.gd index 0f077f29..deb22187 100644 --- a/lorien/UI/Toolbar.gd +++ b/lorien/UI/Toolbar.gd @@ -10,6 +10,8 @@ signal redo_action signal toggle_brush_color_picker signal brush_size_changed(size) signal tool_changed(t) +signal reset_to_center_action +signal zoom_to_drawing_action # ------------------------------------------------------------------------------------------------- const BUTTON_HOVER_COLOR = Color("50ffd6") @@ -51,6 +53,8 @@ func _on_NewFileButton_pressed(): emit_signal("new_project") func _on_ClearCanvasButton_pressed(): emit_signal("clear_canvas") func _on_UndoButton_pressed(): emit_signal("undo_action") func _on_RedoButton_pressed(): emit_signal("redo_action") +func _on_ResetToCenterButton_pressed(): emit_signal("reset_to_center_action") +func _on_ZoomToDrawingButton_pressed(): emit_signal("zoom_to_drawing_action") # ------------------------------------------------------------------------------------------------- func enable_tool(tool_type: int) -> void: diff --git a/lorien/UI/Toolbar.tscn b/lorien/UI/Toolbar.tscn index fae5d6b5..cff3fd8d 100644 --- a/lorien/UI/Toolbar.tscn +++ b/lorien/UI/Toolbar.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=21 format=2] +[gd_scene load_steps=23 format=2] [ext_resource path="res://Assets/Icons/save_file.png" type="Texture" id=1] [ext_resource path="res://Assets/Icons/open_file.png" type="Texture" id=2] @@ -11,10 +11,12 @@ [ext_resource path="res://Assets/Icons/eraser_tool.png" type="Texture" id=9] [ext_resource path="res://Assets/Icons/rectangle_tool.png" type="Texture" id=10] [ext_resource path="res://Assets/Icons/line_tool.png" type="Texture" id=11] +[ext_resource path="res://Assets/Textures/reset_to_center.png" type="Texture" id=12] [ext_resource path="res://UI/Components/FlatTextureButton.gd" type="Script" id=13] [ext_resource path="res://Assets/Fonts/font_small.tres" type="DynamicFont" id=14] [ext_resource path="res://UI/Themes/theme_dark.tres" type="Theme" id=15] [ext_resource path="res://Assets/Icons/selection_tool.png" type="Texture" id=16] +[ext_resource path="res://Assets/Textures/zoom_to_drawing.png" type="Texture" id=17] [ext_resource path="res://Assets/Icons/fullscreen.png" type="Texture" id=18] [ext_resource path="res://Assets/Icons/circle_tool.png" type="Texture" id=19] @@ -297,6 +299,38 @@ margin_bottom = 27.0 custom_fonts/font = ExtResource( 14 ) text = "12" +[node name="VSeparator5" type="VSeparator" parent="Console/Left"] +margin_left = 618.0 +margin_right = 622.0 +margin_bottom = 40.0 +custom_styles/separator = SubResource( 3 ) + +[node name="ResetToCenterButton" type="TextureButton" parent="Console/Left"] +margin_left = 634.0 +margin_top = 11.0 +margin_right = 652.0 +margin_bottom = 29.0 +hint_tooltip = "TOOLBAR_TOOLTIP_UNDO" +size_flags_horizontal = 4 +size_flags_vertical = 4 +texture_normal = ExtResource( 12 ) +script = ExtResource( 13 ) +hover_tint = Color( 0.662745, 0.945098, 0.87451, 1 ) +pressed_tint = Color( 0.572549, 1, 0.894118, 1 ) + +[node name="ZoomToDrawingButton" type="TextureButton" parent="Console/Left"] +margin_left = 664.0 +margin_top = 11.0 +margin_right = 682.0 +margin_bottom = 29.0 +hint_tooltip = "TOOLBAR_TOOLTIP_UNDO" +size_flags_horizontal = 4 +size_flags_vertical = 4 +texture_normal = ExtResource( 17 ) +script = ExtResource( 13 ) +hover_tint = Color( 0.662745, 0.945098, 0.87451, 1 ) +pressed_tint = Color( 0.572549, 1, 0.894118, 1 ) + [node name="Right" type="HBoxContainer" parent="Console"] margin_left = 962.0 margin_right = 1920.0 @@ -345,4 +379,6 @@ custom_styles/separator = SubResource( 2 ) [connection signal="pressed" from="Console/Left/SelectionToolButton" to="." method="_on_SelectToolButton_pressed"] [connection signal="pressed" from="Console/Left/ColorButton" to="." method="_on_ColorButton_pressed"] [connection signal="value_changed" from="Console/Left/BrushSizeSlider" to="." method="_on_BrushSizeSlider_value_changed"] +[connection signal="pressed" from="Console/Left/ResetToCenterButton" to="." method="_on_ResetToCenterButton_pressed"] +[connection signal="pressed" from="Console/Left/ZoomToDrawingButton" to="." method="_on_ZoomToDrawingButton_pressed"] [connection signal="toggled" from="Console/Right/FullscreenButton" to="." method="_on_FullscreenButton_toggled"] diff --git a/lorien/project.godot b/lorien/project.godot index f229db77..8023685a 100644 --- a/lorien/project.godot +++ b/lorien/project.godot @@ -490,6 +490,16 @@ canvas_pan_right={ "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"physical_scancode":0,"unicode":0,"echo":false,"script":null) ] } +canvas_reset_to_center={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":46,"physical_scancode":0,"unicode":0,"echo":false,"script":null) + ] +} +canvas_zoom_to_drawing={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":44,"physical_scancode":0,"unicode":0,"echo":false,"script":null) + ] +} [locale]