From fa3b2f649be8d18705ab917992a227aa5a866ca7 Mon Sep 17 00:00:00 2001 From: OverloadedOrama <35376950+OverloadedOrama@users.noreply.github.com> Date: Fri, 27 Dec 2019 16:10:05 +0200 Subject: [PATCH] Delete the contents of a selection when pressing "Delete" --- Prefabs/PreferencesDialog.tscn | 68 +++++++++++++++++----------------- Scripts/SelectionRectangle.gd | 62 ++++++++++++++++++------------- project.godot | 5 +++ 3 files changed, 76 insertions(+), 59 deletions(-) diff --git a/Prefabs/PreferencesDialog.tscn b/Prefabs/PreferencesDialog.tscn index 2885467f7..594046d88 100644 --- a/Prefabs/PreferencesDialog.tscn +++ b/Prefabs/PreferencesDialog.tscn @@ -119,8 +119,9 @@ text = "繁體中文 [zh_TW]" [node name="Themes" type="VBoxContainer" parent="HSplitContainer/ScrollContainer/VBoxContainer"] editor/display_folded = true -margin_right = 40.0 -margin_bottom = 40.0 +margin_top = 286.0 +margin_right = 205.0 +margin_bottom = 454.0 [node name="Theme Options" type="Label" parent="HSplitContainer/ScrollContainer/VBoxContainer/Themes"] margin_right = 205.0 @@ -136,62 +137,63 @@ mouse_default_cursor_shape = 2 text = "Dark" [node name="Gray Theme" type="CheckBox" parent="HSplitContainer/ScrollContainer/VBoxContainer/Themes"] -margin_top = 32.0 +margin_top = 60.0 margin_right = 205.0 -margin_bottom = 56.0 +margin_bottom = 84.0 mouse_default_cursor_shape = 2 text = "Gray" [node name="Godot\'s Theme" type="CheckBox" parent="HSplitContainer/ScrollContainer/VBoxContainer/Themes"] -margin_top = 32.0 +margin_top = 88.0 margin_right = 205.0 -margin_bottom = 56.0 +margin_bottom = 112.0 mouse_default_cursor_shape = 2 text = "Godot" [node name="Gold Theme" type="CheckBox" parent="HSplitContainer/ScrollContainer/VBoxContainer/Themes"] -margin_top = 32.0 +margin_top = 116.0 margin_right = 205.0 -margin_bottom = 56.0 +margin_bottom = 140.0 mouse_default_cursor_shape = 2 text = "Gold" [node name="Light Theme" type="CheckBox" parent="HSplitContainer/ScrollContainer/VBoxContainer/Themes"] -margin_top = 32.0 +margin_top = 144.0 margin_right = 205.0 -margin_bottom = 56.0 +margin_bottom = 168.0 mouse_default_cursor_shape = 2 text = "Light" [node name="Grid&Guides" type="VBoxContainer" parent="HSplitContainer/ScrollContainer/VBoxContainer"] editor/display_folded = true visible = false -margin_top = 24.0 -margin_right = 151.0 -margin_bottom = 118.0 +margin_top = 458.0 +margin_right = 205.0 +margin_bottom = 576.0 [node name="GridOptionsLabel" type="Label" parent="HSplitContainer/ScrollContainer/VBoxContainer/Grid&Guides"] -margin_right = 151.0 +margin_right = 205.0 margin_bottom = 14.0 -text = "Grid options" +rect_min_size = Vector2( 0, 28 ) +text = "Grid Οptions:" [node name="GridOptions" type="GridContainer" parent="HSplitContainer/ScrollContainer/VBoxContainer/Grid&Guides"] margin_top = 18.0 -margin_right = 151.0 -margin_bottom = 94.0 +margin_right = 205.0 +margin_bottom = 118.0 custom_constants/vseparation = 4 custom_constants/hseparation = 4 columns = 2 [node name="WidthLabel" type="Label" parent="HSplitContainer/ScrollContainer/VBoxContainer/Grid&Guides/GridOptions"] margin_top = 5.0 -margin_right = 46.0 +margin_right = 78.0 margin_bottom = 19.0 text = "Width:" [node name="GridWidthValue" type="SpinBox" parent="HSplitContainer/ScrollContainer/VBoxContainer/Grid&Guides/GridOptions"] -margin_left = 50.0 -margin_right = 124.0 +margin_left = 82.0 +margin_right = 156.0 margin_bottom = 24.0 min_value = 1.0 max_value = 16384.0 @@ -200,14 +202,14 @@ suffix = "px" [node name="Height" type="Label" parent="HSplitContainer/ScrollContainer/VBoxContainer/Grid&Guides/GridOptions"] margin_top = 33.0 -margin_right = 46.0 +margin_right = 78.0 margin_bottom = 47.0 text = "Height:" [node name="GridHeightValue" type="SpinBox" parent="HSplitContainer/ScrollContainer/VBoxContainer/Grid&Guides/GridOptions"] -margin_left = 50.0 +margin_left = 82.0 margin_top = 28.0 -margin_right = 124.0 +margin_right = 156.0 margin_bottom = 52.0 min_value = 1.0 max_value = 16384.0 @@ -216,28 +218,28 @@ suffix = "px" [node name="GridColorLabel" type="Label" parent="HSplitContainer/ScrollContainer/VBoxContainer/Grid&Guides/GridOptions"] margin_top = 59.0 -margin_right = 46.0 +margin_right = 78.0 margin_bottom = 73.0 text = "Color:" [node name="GridColor" type="ColorPickerButton" parent="HSplitContainer/ScrollContainer/VBoxContainer/Grid&Guides/GridOptions"] -margin_left = 50.0 +margin_left = 82.0 margin_top = 56.0 -margin_right = 124.0 +margin_right = 156.0 margin_bottom = 76.0 rect_min_size = Vector2( 64, 20 ) [node name="GuideColorLabel" type="Label" parent="HSplitContainer/ScrollContainer/VBoxContainer/Grid&Guides/GridOptions"] -margin_top = 59.0 -margin_right = 46.0 -margin_bottom = 73.0 +margin_top = 83.0 +margin_right = 78.0 +margin_bottom = 97.0 text = "Guide color:" [node name="GuideColor" type="ColorPickerButton" parent="HSplitContainer/ScrollContainer/VBoxContainer/Grid&Guides/GridOptions"] -margin_left = 50.0 -margin_top = 56.0 -margin_right = 124.0 -margin_bottom = 76.0 +margin_left = 82.0 +margin_top = 80.0 +margin_right = 156.0 +margin_bottom = 100.0 rect_min_size = Vector2( 64, 20 ) color = Color( 0.63, 0.13, 0.94, 1 ) [connection signal="item_selected" from="HSplitContainer/Tree" to="." method="_on_Tree_item_selected"] diff --git a/Scripts/SelectionRectangle.gd b/Scripts/SelectionRectangle.gd index 3dcbad9eb..4a894b71a 100644 --- a/Scripts/SelectionRectangle.gd +++ b/Scripts/SelectionRectangle.gd @@ -35,7 +35,7 @@ func _process(delta : float) -> void: get_parent().get_parent().mouse_default_cursor_shape = Input.CURSOR_MOVE Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) if (Global.current_left_tool == "RectSelect" && Input.is_action_just_pressed("left_mouse")) || (Global.current_right_tool == "RectSelect" && Input.is_action_just_pressed("right_mouse")): - #Begin dragging + # Begin dragging is_dragging = true if Input.is_key_pressed(KEY_SHIFT): move_pixels = true @@ -52,25 +52,25 @@ func _process(delta : float) -> void: for i in range(Global.selected_pixels.size()): var curr_px = Global.selected_pixels[i] if point_in_rectangle(curr_px, Global.canvas.location - Vector2.ONE, Global.canvas.size): - orig_colors.append(layer.get_pixelv(curr_px)) #Color of pixel + orig_colors.append(layer.get_pixelv(curr_px)) # Color of pixel var px = curr_px - Global.selected_pixels[0] img.set_pixelv(px, orig_colors[i]) layer.set_pixelv(curr_px, Color(0, 0, 0, 0)) - else: #If part of selection is outside canvas + else: # If part of selection is outside canvas orig_colors.append(Color(0, 0, 0, 0)) Global.canvas.update_texture(Global.canvas.current_layer_index) tex.create_from_image(img, 0) update() # Makes line2d invisible - if is_instance_valid(Global.canvas.line_2d): # Checks to see if line_2d object still exists + if is_instance_valid(Global.canvas.line_2d): # Checks to see if line_2d object still exists Global.canvas.line_2d.default_color = Color(0, 0, 0, 0) else: get_parent().get_parent().mouse_default_cursor_shape = Input.CURSOR_ARROW if is_dragging: if (Global.current_left_tool == "RectSelect" && Input.is_action_pressed("left_mouse")) || (Global.current_right_tool == "RectSelect" && Input.is_action_pressed("right_mouse")): - #Drag + # Drag start_pos.x = orig_x + mouse_pos_floored.x end_pos.x = diff_x + mouse_pos_floored.x @@ -82,7 +82,7 @@ func _process(delta : float) -> void: polygon[3] = Vector2(start_pos.x, end_pos.y) if (Global.current_left_tool == "RectSelect" && Input.is_action_just_released("left_mouse")) || (Global.current_right_tool == "RectSelect" && Input.is_action_just_released("right_mouse")): - #Release Drag + # Release Drag is_dragging = false if move_pixels: for i in range(orig_colors.size()): @@ -101,32 +101,42 @@ func _process(delta : float) -> void: for yy in range(start_pos.y, end_pos.y): Global.selected_pixels.append(Vector2(xx, yy)) - Global.canvas.handle_redo("Rectangle Select") #Redo + Global.canvas.handle_redo("Rectangle Select") # Redo # Makes line2d visible - if is_instance_valid(Global.canvas.line_2d): # Checks to see if line_2d object still exists + if is_instance_valid(Global.canvas.line_2d): # Checks to see if line_2d object still exists Global.canvas.line_2d.default_color = Color.darkgray - #Handle copy - if Input.is_action_just_pressed("copy") && Global.selected_pixels.size() > 0: - #Save as custom brush - var brush_img := Image.new() - brush_img = layer.get_rect(Rect2(polygon[0], polygon[2] - polygon[0])) - if brush_img.is_invisible(): - return - brush_img = brush_img.get_rect(brush_img.get_used_rect()) #save only the visible pixels - Global.custom_brushes.append(brush_img) - Global.create_brush_button(brush_img) + if Global.selected_pixels.size() > 0: + # Handle copy + if Input.is_action_just_pressed("copy"): + # Save as custom brush + var brush_img := Image.new() + brush_img = layer.get_rect(Rect2(polygon[0], polygon[2] - polygon[0])) + if brush_img.is_invisible(): + return + brush_img = brush_img.get_rect(brush_img.get_used_rect()) #save only the visible pixels + Global.custom_brushes.append(brush_img) + Global.create_brush_button(brush_img) + + # Have it in the clipboard so it can be pasted later + Global.image_clipboard = layer.get_rect(Rect2(polygon[0], polygon[2] - polygon[0])) + + # Handle paste + if Input.is_action_just_pressed("paste") && Global.image_clipboard.get_size() > Vector2.ZERO: + Global.canvas.handle_undo("Draw") + layer.blend_rect(Global.image_clipboard, Rect2(Vector2.ZERO, polygon[2]-polygon[0]), polygon[0]) + layer.lock() + Global.canvas.handle_redo("Draw") + + if Input.is_action_just_pressed("delete"): + Global.canvas.handle_undo("Draw") + for xx in range(start_pos.x, end_pos.x): + for yy in range(start_pos.y, end_pos.y): + layer.set_pixel(xx, yy, Color(0, 0, 0, 0)) + Global.canvas.handle_redo("Draw") - #Have it in the clipboard so it can be pasted later - Global.image_clipboard = layer.get_rect(Rect2(polygon[0], polygon[2] - polygon[0])) - #Handle paste - if Input.is_action_just_pressed("paste") && Global.selected_pixels.size() > 0 && Global.image_clipboard.get_size() > Vector2.ZERO: - Global.canvas.handle_undo("Draw") - layer.blend_rect(Global.image_clipboard, Rect2(Vector2.ZERO, polygon[2]-polygon[0]), polygon[0]) - layer.lock() - Global.canvas.handle_redo("Draw") func _draw() -> void: if img.get_size() == polygon[2] - polygon[0]: diff --git a/project.godot b/project.godot index 130a7319b..751c61ae9 100644 --- a/project.godot +++ b/project.godot @@ -209,6 +209,11 @@ redo_secondary={ "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":true,"control":true,"meta":false,"command":true,"pressed":false,"scancode":90,"unicode":0,"echo":false,"script":null) ] } +delete={ +"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":16777224,"unicode":0,"echo":false,"script":null) + ] +} [locale]