Skip to content

Commit

Permalink
Created "Reset to Center" and "Zoom to Drawing" Functions
Browse files Browse the repository at this point in the history
Added Toolbar buttons for the new functions
  • Loading branch information
tafode committed Mar 22, 2024
1 parent a4c11c9 commit 2440789
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lorien/Assets/I18n/en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added lorien/Assets/Textures/reset_to_center.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions lorien/Assets/Textures/reset_to_center.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/reset_to_center.png-17a1d5f63ce3da5e33de26e07c6dfa15.stex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://Assets/Textures/reset_to_center.png"
dest_files=[ "res://.import/reset_to_center.png-17a1d5f63ce3da5e33de26e07c6dfa15.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
Binary file added lorien/Assets/Textures/zoom_to_drawing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions lorien/Assets/Textures/zoom_to_drawing.png.import
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions lorien/InfiniteCanvas/InfiniteCanvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions lorien/InfiniteCanvas/PanZoomCamera.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -132,3 +196,4 @@ func disable_input() -> void:
# -------------------------------------------------------------------------------------------------
func xform(pos: Vector2) -> Vector2:
return (pos * zoom) + offset

1 change: 1 addition & 0 deletions lorien/InfiniteCanvas/Tools/CanvasTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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())

# -------------------------------------------------------------------------------------------------
Expand Down
17 changes: 17 additions & 0 deletions lorien/Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()

4 changes: 4 additions & 0 deletions lorien/UI/Toolbar.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down
38 changes: 37 additions & 1 deletion lorien/UI/Toolbar.tscn
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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]

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"]
10 changes: 10 additions & 0 deletions lorien/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit 2440789

Please sign in to comment.