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
Re-Implementation for Godot 4.3
  • Loading branch information
tafode committed Sep 2, 2024
2 parents 47e74be + 47e74be commit d3ae17e
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lorien/Assets/I18n/en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ TOOLBAR_TOOLTIP_BRUSH_COLOR Brush Color
TOOLBAR_TOOLTIP_BRUSH_SIZE Brush Size
TOOLBAR_TOOLTIP_CANVAS_COLOR Canvas Color
TOOLBAR_FULLSCREEN_TOGGLE Toggle fullscreen
TOOLBAR_TOOLTIP_ZOOM_TO_DRAWING Zoom to Drawing
TOOLBAR_TOOLTIP_RESET_TO_CENTER Reset Camera to Center

# -----------------------------------------------------------------------------
# Color Palette Picker
Expand Down Expand Up @@ -160,6 +162,8 @@ ACTION_toggle_zen_mode Toggle Zen Mode
ACTION_toggle_player Toggle Easteregg
ACTION_toggle_fullscreen Toggle Fullscreen
ACTION_canvas_pan_key Pan Key
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/Icons/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.
34 changes: 34 additions & 0 deletions lorien/Assets/Icons/reset_to_center.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://dcxctfytoiq28"
path="res://.godot/imported/reset_to_center.png-f0c4ce74002102472b04bab039768552.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://Assets/Icons/reset_to_center.png"
dest_files=["res://.godot/imported/reset_to_center.png-f0c4ce74002102472b04bab039768552.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
Binary file added lorien/Assets/Icons/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.
34 changes: 34 additions & 0 deletions lorien/Assets/Icons/zoom_to_drawing.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://dug7uaervy2ik"
path="res://.godot/imported/zoom_to_drawing.png-0adbb90ff26abb26c463d52713a19708.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://Assets/Icons/zoom_to_drawing.png"
dest_files=["res://.godot/imported/zoom_to_drawing.png-0adbb90ff26abb26c463d52713a19708.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
11 changes: 11 additions & 0 deletions lorien/InfiniteCanvas/InfiniteCanvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ 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
63 changes: 63 additions & 0 deletions lorien/InfiniteCanvas/PanZoomCamera.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ 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 @@ -33,6 +35,67 @@ func do_center(screen_space_center_point: Vector2) -> void:
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

print(project.strokes.size())
if project.strokes.is_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.get_rect().size.y+_statusbar.get_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 = ((1/final_zoom)*get_viewport().size.x)-diff_max_dim.x
center_offset.y = ((1/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
16 changes: 16 additions & 0 deletions lorien/Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func _ready() -> void:
_toolbar.save_project.connect(_on_save_project)
_toolbar.brush_size_changed.connect(_on_brush_size_changed)
_toolbar.tool_changed.connect(_on_tool_changed)
_toolbar.reset_to_center_action.connect(_on_reset_to_center_action)
_toolbar.zoom_to_drawing_action.connect(_on_zoom_to_drawing_action)


_menubar.create_new_project.connect(_on_create_new_project)
_menubar.project_selected.connect(_on_project_selected)
Expand Down Expand Up @@ -171,6 +174,10 @@ func _unhandled_input(event: InputEvent) -> void:
_toggle_zen_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 @@ -574,3 +581,12 @@ 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:
_canvas.reset_to_center()

# --------------------------------------------------------------------------------------------------
func _on_zoom_to_drawing_action() -> void:
_canvas.zoom_to_drawing()

6 changes: 6 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: float)
signal tool_changed(t: Types.Tool)
signal reset_to_center_action
signal zoom_to_drawing_action

# -------------------------------------------------------------------------------------------------
const BUTTON_HOVER_COLOR = Color("50ffd6")
Expand All @@ -25,6 +27,8 @@ const BUTTON_NORMAL_COLOR = Color.WHITE
@onready var _undo_button: FlatTextureButton = $Console/Left/UndoButton
@onready var _redo_button: FlatTextureButton = $Console/Left/RedoButton
@onready var _color_button: Button = $Console/Left/ColorButton
@onready var _reset_to_center_button: FlatTextureButton = $Console/Left/ResetToCenterButton
@onready var _zoom_to_drawing_button: FlatTextureButton = $Console/Left/ZoomToDrawingButton
@onready var _brush_size_label: Label = $Console/Left/BrushSizeLabel
@onready var _brush_size_slider: HSlider = $Console/Left/BrushSizeSlider
@onready var _tool_btn_brush: FlatTextureButton = $Console/Left/BrushToolButton
Expand Down Expand Up @@ -58,6 +62,8 @@ func _ready() -> void:
_open_button.pressed.connect(_on_open_project_pressed)
_save_button.pressed.connect(func() -> void: save_project.emit())
_color_button.pressed.connect(func() -> void: toggle_brush_color_picker.emit())
_reset_to_center_button.pressed.connect(func() -> void: reset_to_center_action.emit())
_zoom_to_drawing_button.pressed.connect(func() -> void: zoom_to_drawing_action.emit())
_brush_size_slider.value_changed.connect(_on_brush_size_changed)
_tool_btn_brush.pressed.connect(_on_brush_tool_pressed)
_tool_btn_rectangle.pressed.connect(_on_rectangle_tool_pressed)
Expand Down
30 changes: 29 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=19 format=3 uid="uid://c0ral10lvpo7f"]
[gd_scene load_steps=21 format=3 uid="uid://c0ral10lvpo7f"]

[ext_resource type="Texture2D" uid="uid://c4kv3i7fmom58" path="res://Assets/Icons/save_file.png" id="1"]
[ext_resource type="Texture2D" uid="uid://dxi6gc6npiskq" path="res://Assets/Icons/open_file.png" id="2"]
Expand All @@ -12,6 +12,8 @@
[ext_resource type="Texture2D" uid="uid://bn1qdw0v30np0" path="res://Assets/Icons/line_tool.png" id="11"]
[ext_resource type="Script" path="res://UI/Components/FlatTextureButton.gd" id="13"]
[ext_resource type="Theme" uid="uid://u5qnpgxqykiv" path="res://UI/Themes/theme_dark.tres" id="15"]
[ext_resource type="Texture2D" uid="uid://dug7uaervy2ik" path="res://Assets/Icons/zoom_to_drawing.png" id="15_b47ao"]
[ext_resource type="Texture2D" uid="uid://dcxctfytoiq28" path="res://Assets/Icons/reset_to_center.png" id="15_r3wm2"]
[ext_resource type="Texture2D" uid="uid://ddxis8f7tvg66" path="res://Assets/Icons/selection_tool.png" id="16"]
[ext_resource type="Texture2D" uid="uid://0qicbkag5jd3" path="res://Assets/Icons/circle_tool.png" id="19"]

Expand Down Expand Up @@ -219,6 +221,32 @@ value = 5.0
layout_mode = 2
text = "12"

[node name="VSeparator5" type="VSeparator" parent="Console/Left"]
layout_mode = 2
theme_override_styles/separator = SubResource("3")

[node name="ResetToCenterButton" type="TextureButton" parent="Console/Left"]
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 4
tooltip_text = "TOOLBAR_TOOLTIP_RESET_TO_CENTER"
toggle_mode = true
texture_normal = ExtResource("15_r3wm2")
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"]
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 4
tooltip_text = "TOOLBAR_TOOLTIP_ZOOM_TO_DRAWING"
toggle_mode = true
texture_normal = ExtResource("15_b47ao")
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"]
layout_mode = 2
size_flags_horizontal = 3
Expand Down
10 changes: 10 additions & 0 deletions lorien/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ canvas_pan_key={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":32,"physical_keycode":0,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null)
]
}
canvas_reset_to_center={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
canvas_zoom_to_drawing={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}

[locale]

Expand Down

0 comments on commit d3ae17e

Please sign in to comment.