Skip to content

Commit

Permalink
[WIP] Added super eraser tool
Browse files Browse the repository at this point in the history
This commit introduces super eraser tool which removes the entire stroke
upon contact with cursor. Undo/Redo works fine. However, I still need to
handle stroke size somehow.
  • Loading branch information
hansemro committed Jan 17, 2022
1 parent 1e11cd7 commit 65953e2
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 16 deletions.
1 change: 1 addition & 0 deletions lorien/Assets/I18n/en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ TOOLBAR_TOOLTIP_RECTANGLE_TOOL Rectangle Tool (R)
TOOLBAR_TOOLTIP_LINE_TOOL Line Tool (L)
TOOLBAR_TOOLTIP_ERASER_TOOL Eraser (E)
TOOLBAR_TOOLTIP_SELECT_TOOL Selection Tool (S)
TOOLBAR_TOOLTIP_SUPER_ERASER_TOOL Super Eraser
TOOLBAR_TOOLTIP_BRUSH_COLOR Brush Color
TOOLBAR_TOOLTIP_BRUSH_SIZE Brush Size
TOOLBAR_TOOLTIP_CANVAS_COLOR Canvas Color
Expand Down
Binary file added lorien/Assets/Icons/super_eraser_tool.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/Icons/super_eraser_tool.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/super_eraser_tool.png-07ce97550fe2bbcde89ee994efb82315.stex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://Assets/Icons/super_eraser_tool.png"
dest_files=[ "res://.import/super_eraser_tool.png-07ce97550fe2bbcde89ee994efb82315.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
4 changes: 4 additions & 0 deletions lorien/InfiniteCanvas/InfiniteCanvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const ERASER_SIZE_FACTOR = 1.25

# -------------------------------------------------------------------------------------------------
onready var _brush_tool: BrushTool = $BrushTool
onready var _super_eraser_tool: SuperEraserTool = $SuperEraserTool
onready var _rectangle_tool: RectangleTool = $RectangleTool
onready var _line_tool: LineTool = $LineTool
onready var _selection_tool: SelectionTool = $SelectionTool
Expand Down Expand Up @@ -87,6 +88,9 @@ func use_tool(tool_type: int) -> void:
_brush_tool.mode = BrushTool.Mode.ERASE
_active_tool = _brush_tool
_use_optimizer = true
Types.Tool.SUPERERASER:
_active_tool = _super_eraser_tool
_use_optimizer = false
Types.Tool.SELECT:
_active_tool = _selection_tool
_use_optimizer = false
Expand Down
6 changes: 6 additions & 0 deletions lorien/InfiniteCanvas/InfiniteCanvas.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[ext_resource path="res://InfiniteCanvas/Tools/RectangleTool.gd" type="Script" id=11]
[ext_resource path="res://InfiniteCanvas/InfiniteCanvasGrid.gd" type="Script" id=12]
[ext_resource path="res://InfiniteCanvas/grid.shader" type="Shader" id=15]
[ext_resource path="res://InfiniteCanvas/Tools/SuperEraserTool.gd" type="Script" id=16]

[sub_resource type="ShaderMaterial" id=1]
shader = ExtResource( 10 )
Expand Down Expand Up @@ -50,6 +51,11 @@ script = ExtResource( 7 )
cursor_path = NodePath("../Viewport/SelectionCursor")
selection_rectangle_path = NodePath("../SelectionRectangle")

[node name="SuperEraserTool" type="Node" parent="."]
script = ExtResource( 16 )
cursor_path = NodePath("../Viewport/BrushCursor")
selection_rectangle_path = NodePath("../SelectionRectangle")

[node name="SelectionRectangle" type="Node2D" parent="."]
material = SubResource( 1 )
script = ExtResource( 9 )
Expand Down
63 changes: 63 additions & 0 deletions lorien/InfiniteCanvas/Tools/SuperEraserTool.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class_name SuperEraserTool
extends CanvasTool

# -------------------------------------------------------------------------------------------------
export var pressure_curve: Curve
var _last_mouse_motion: InputEventMouseMotion
var _removed_strokes := [] # BrushStroke -> Vector2

# -------------------------------------------------------------------------------------------------
func _input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
_last_mouse_motion = event
_cursor.global_position = xform_vector2(event.global_position)

if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT:
if event.pressed && _last_mouse_motion != null:
_last_mouse_motion.global_position = event.global_position
_last_mouse_motion.position = event.position
performing_stroke = true
elif !event.pressed:
performing_stroke = false

# -------------------------------------------------------------------------------------------------
func _process(delta: float) -> void:
if performing_stroke && _last_mouse_motion != null:
_remove_stroke(_last_mouse_motion.global_position)
_add_undoredo_action_for_erased_strokes()
_last_mouse_motion = null

# ------------------------------------------------------------------------------------------------
func _stroke_intersects_circle(stroke, circle_position: Vector2, circle_radius: float) -> bool:
for i in stroke.points.size() - 1:
var start = _calc_abs_stroke_point(stroke.points[i], stroke)
var end = _calc_abs_stroke_point(stroke.points[i + 1], stroke)
if Geometry.segment_intersects_circle(start, end, circle_position, circle_radius) >= 0:
return true
return false

# -------------------------------------------------------------------------------------------------
func _remove_stroke(brush_position: Vector2) -> void:
for stroke in _canvas.get_strokes_in_camera_frustrum():
# check if brush intersects stroke (and not already being removed)
if !_removed_strokes.has(stroke) && _stroke_intersects_circle(stroke, brush_position,
float(_cursor._brush_size)/2):
# Add stroke to remove to _removed_strokes
_removed_strokes.append(stroke)

# ------------------------------------------------------------------------------------------------
func _calc_abs_stroke_point(p: Vector2, stroke: BrushStroke) -> Vector2:
return (p + stroke.position - _canvas.get_camera_offset()) / _canvas.get_camera_zoom()

# ------------------------------------------------------------------------------------------------
func _add_undoredo_action_for_erased_strokes() -> void:
var project: Project = ProjectManager.get_active_project()
if _removed_strokes.size():
project.undo_redo.create_action("Erase Stroke")
for stroke in _removed_strokes:
_removed_strokes.erase(stroke)
project.undo_redo.add_do_method(_canvas, "_do_delete_stroke", stroke)
project.undo_redo.add_undo_method(_canvas, "_undo_delete_stroke", stroke)
project.undo_redo.commit_action()
project.dirty = true
6 changes: 4 additions & 2 deletions lorien/Main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ __meta__ = {
}

[node name="ColorPicker" type="ColorPicker" parent="BackgroundColorPickerPopup/PanelContainer"]
margin_right = 309.0
margin_bottom = 404.0
margin_left = 4.0
margin_top = 4.0
margin_right = 313.0
margin_bottom = 408.0
edit_alpha = false
presets_enabled = false
presets_visible = false
Expand Down
1 change: 1 addition & 0 deletions lorien/Misc/Types.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum Tool {
RECTANGLE,
LINE,
ERASER,
SUPERERASER
SELECT,
}

Expand Down
7 changes: 7 additions & 0 deletions lorien/UI/Toolbar.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ onready var _tool_btn_brush: TextureButton = $Left/BrushToolButton
onready var _tool_btn_rectangle: TextureButton = $Left/RectangleToolButton
onready var _tool_btn_line: TextureButton = $Left/LineToolButton
onready var _tool_btn_eraser: TextureButton = $Left/EraserToolButton
onready var _tool_btn_super_eraser: TextureButton = $Left/SuperEraserToolButton
onready var _tool_btn_selection: TextureButton = $Left/SelectionToolButton

var _last_active_tool_button: TextureButton
Expand Down Expand Up @@ -66,6 +67,7 @@ func enable_tool(tool_type: int) -> void:
Types.Tool.BRUSH: btn = _tool_btn_brush
Types.Tool.LINE: btn = _tool_btn_line
Types.Tool.ERASER: btn = _tool_btn_eraser
Types.Tool.SUPERERASER: btn = _tool_btn_super_eraser
Types.Tool.SELECT: btn = _tool_btn_selection
Types.Tool.RECTANGLE: btn = _tool_btn_rectangle

Expand Down Expand Up @@ -144,6 +146,11 @@ func _on_EraserToolButton_pressed():
_change_active_tool_button(_tool_btn_eraser)
emit_signal("tool_changed", Types.Tool.ERASER)

# -------------------------------------------------------------------------------------------------
func _on_SuperEraserToolButton_pressed():
_change_active_tool_button(_tool_btn_super_eraser)
emit_signal("tool_changed", Types.Tool.SUPERERASER)

# -------------------------------------------------------------------------------------------------
func _on_SelectToolButton_pressed():
_change_active_tool_button(_tool_btn_selection)
Expand Down
46 changes: 32 additions & 14 deletions lorien/UI/Toolbar.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=23 format=2]
[gd_scene load_steps=24 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 @@ -18,6 +18,7 @@
[ext_resource path="res://Assets/Icons/selection_tool.png" type="Texture" id=16]
[ext_resource path="res://Assets/Icons/grid.png" type="Texture" id=17]
[ext_resource path="res://Assets/Icons/fullscreen.png" type="Texture" id=18]
[ext_resource path="res://Assets/Icons/super_eraser_tool.png" type="Texture" id=19]

[sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0.207843, 0.211765, 0.227451, 1 )
Expand Down Expand Up @@ -222,6 +223,7 @@ margin_left = 314.0
margin_top = 12.0
margin_right = 330.0
margin_bottom = 28.0
rect_pivot_offset = Vector2( -362, -18 )
hint_tooltip = "TOOLBAR_TOOLTIP_SELECT_TOOL"
size_flags_horizontal = 4
size_flags_vertical = 4
Expand All @@ -231,19 +233,34 @@ script = ExtResource( 13 )
hover_tint = Color( 0.662745, 0.945098, 0.87451, 1 )
pressed_tint = Color( 0.572549, 1, 0.894118, 1 )

[node name="VSeparator4" type="VSeparator" parent="Left"]
[node name="SuperEraserToolButton" type="TextureButton" parent="Left"]
margin_left = 342.0
margin_right = 346.0
margin_top = 11.0
margin_right = 360.0
margin_bottom = 29.0
rect_pivot_offset = Vector2( -362, -18 )
hint_tooltip = "TOOLBAR_TOOLTIP_SUPER_ERASER_TOOL"
size_flags_horizontal = 4
size_flags_vertical = 4
toggle_mode = true
texture_normal = ExtResource( 19 )
script = ExtResource( 13 )
hover_tint = Color( 0.662745, 0.945098, 0.87451, 1 )
pressed_tint = Color( 0.572549, 1, 0.894118, 1 )

[node name="VSeparator4" type="VSeparator" parent="Left"]
margin_left = 372.0
margin_right = 376.0
margin_bottom = 40.0
custom_styles/separator = SubResource( 3 )
__meta__ = {
"_edit_use_anchors_": false
}

[node name="ColorButton" type="Button" parent="Left"]
margin_left = 358.0
margin_left = 388.0
margin_top = 10.0
margin_right = 418.0
margin_right = 448.0
margin_bottom = 30.0
rect_min_size = Vector2( 60, 20 )
hint_tooltip = "TOOLBAR_TOOLTIP_BRUSH_COLOR"
Expand All @@ -265,9 +282,9 @@ __meta__ = {
}

[node name="BrushSizeSlider" type="HSlider" parent="Left"]
margin_left = 430.0
margin_left = 460.0
margin_top = 10.0
margin_right = 550.0
margin_right = 580.0
margin_bottom = 30.0
rect_min_size = Vector2( 120, 20 )
hint_tooltip = "TOOLBAR_TOOLTIP_BRUSH_SIZE"
Expand All @@ -278,9 +295,9 @@ max_value = 50.0
value = 5.0

[node name="BrushSizeLabel" type="Label" parent="Left"]
margin_left = 562.0
margin_left = 592.0
margin_top = 13.0
margin_right = 578.0
margin_right = 608.0
margin_bottom = 27.0
custom_fonts/font = ExtResource( 14 )
text = "12"
Expand Down Expand Up @@ -329,9 +346,9 @@ pressed_tint = Color( 0.572549, 1, 0.894118, 1 )

[node name="FullscreenButton" type="TextureButton" parent="Right"]
margin_left = 56.0
margin_top = 12.0
margin_right = 72.0
margin_bottom = 28.0
margin_top = 11.0
margin_right = 74.0
margin_bottom = 29.0
hint_tooltip = "TOOLBAR_FULLSCREEN_TOGGLE"
size_flags_horizontal = 4
size_flags_vertical = 4
Expand All @@ -343,8 +360,8 @@ hover_tint = Color( 0.662745, 0.945098, 0.87451, 1 )
pressed_tint = Color( 0.572549, 1, 0.894118, 1 )

[node name="VSeparator" type="VSeparator" parent="Right"]
margin_left = 84.0
margin_right = 88.0
margin_left = 86.0
margin_right = 90.0
margin_bottom = 40.0
custom_styles/separator = SubResource( 2 )

Expand All @@ -359,6 +376,7 @@ custom_styles/separator = SubResource( 2 )
[connection signal="pressed" from="Left/LineToolButton" to="." method="_on_LineToolButton_pressed"]
[connection signal="pressed" from="Left/EraserToolButton" to="." method="_on_EraserToolButton_pressed"]
[connection signal="pressed" from="Left/SelectionToolButton" to="." method="_on_SelectToolButton_pressed"]
[connection signal="pressed" from="Left/SuperEraserToolButton" to="." method="_on_SuperEraserToolButton_pressed"]
[connection signal="pressed" from="Left/ColorButton" to="." method="_on_ColorButton_pressed"]
[connection signal="value_changed" from="Left/BrushSizeSlider" to="." method="_on_BrushSizeSlider_value_changed"]
[connection signal="pressed" from="Right/BackgroundColorButton" to="." method="_on_BackgroundColorButton_pressed"]
Expand Down
6 changes: 6 additions & 0 deletions lorien/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://UI/Statusbar.gd"
}, {
"base": "CanvasTool",
"class": "SuperEraserTool",
"language": "GDScript",
"path": "res://InfiniteCanvas/Tools/SuperEraserTool.gd"
}, {
"base": "Panel",
"class": "Toolbar",
"language": "GDScript",
Expand Down Expand Up @@ -188,6 +193,7 @@ _global_script_class_icons={
"SelectionTool": "",
"Serializer": "",
"Statusbar": "",
"SuperEraserTool": "",
"Toolbar": "",
"Types": ""
}
Expand Down

0 comments on commit 65953e2

Please sign in to comment.