Added rectangle selection tool, copy & paste selection and Tile Mode

- New rectangle selection tool. Hold mouse button to create selection, release to finish it. You cannot draw outside of the selection.
- The selection can be moved around, and if Shift is pressed, selected content gets moved too. Currently cannot be moved outside the canvas.
- You can copy the selection with Ctrl + C, and paste it on a new selection with Ctrl + V.
- Added tile mode. Basically draws the canvas 8 more times in all directions.
This commit is contained in:
OverloadedOrama 2019-09-18 17:47:28 +03:00
parent f83e5b44f3
commit 2b710afd3b
7 changed files with 285 additions and 28 deletions

View file

@ -15,6 +15,7 @@ var mouse_inside_canvas := false #used for undo
var sprite_changed_this_frame := false #for optimization purposes
var is_making_line := false
var is_making_selection := "None"
var line_2d : Line2D
# Called when the node enters the scene tree for the first time.
@ -54,13 +55,15 @@ func _process(delta) -> void:
sprite_changed_this_frame = false
update()
var mouse_pos := get_local_mouse_position() - location
var mouse_pos_floored := mouse_pos.floor()
var mouse_pos_ceiled := mouse_pos.ceil()
var current_mouse_button := "None"
var current_action := "None"
if Input.is_mouse_button_pressed(BUTTON_LEFT):
current_mouse_button = "L"
current_mouse_button = "left_mouse"
current_action = Global.current_left_tool
elif Input.is_mouse_button_pressed(BUTTON_RIGHT):
current_mouse_button = "R"
current_mouse_button = "right_mouse"
current_action = Global.current_right_tool
if visible:
@ -70,15 +73,14 @@ func _process(delta) -> void:
mouse_inside_canvas = false
Global.cursor_position_label.text = "[%sx%s]" % [size.x, size.y]
else:
var mouse_pos_floored := mouse_pos.floor()
Global.cursor_position_label.text = "[%sx%s] %s, %s" % [size.x, size.y, mouse_pos_floored.x, mouse_pos_floored.y]
#Handle current tool
match current_action:
"Pencil":
var current_color : Color
if current_mouse_button == "L":
if current_mouse_button == "left_mouse":
current_color = Global.left_color_picker.color
elif current_mouse_button == "R":
elif current_mouse_button == "right_mouse":
current_color = Global.right_color_picker.color
pencil_and_eraser(mouse_pos, current_color, current_mouse_button)
"Eraser":
@ -86,11 +88,35 @@ func _process(delta) -> void:
"Fill":
if point_in_rectangle(mouse_pos, location, location + size) && Global.can_draw && Global.has_focus && Global.current_frame == frame:
var current_color : Color
if current_mouse_button == "L":
if current_mouse_button == "left_mouse":
current_color = Global.left_color_picker.color
elif current_mouse_button == "R":
elif current_mouse_button == "right_mouse":
current_color = Global.right_color_picker.color
flood_fill(mouse_pos, layers[current_layer_index][0].get_pixelv(mouse_pos), current_color)
"RectSelect":
if point_in_rectangle(mouse_pos_floored, location - Vector2.ONE, location + size) && Global.can_draw && Global.has_focus && Global.current_frame == frame:
#If we're creating a new selection
if Global.selected_pixels.size() == 0 || !point_in_rectangle_equal(mouse_pos_floored, Global.selection_rectangle.polygon[0], Global.selection_rectangle.polygon[2]):
if Input.is_action_just_pressed(current_mouse_button):
Global.selection_rectangle.polygon[0] = mouse_pos_floored
Global.selection_rectangle.polygon[1] = mouse_pos_floored
Global.selection_rectangle.polygon[2] = mouse_pos_floored
Global.selection_rectangle.polygon[3] = mouse_pos_floored
is_making_selection = current_mouse_button
Global.selected_pixels.clear()
else:
if is_making_selection != "None": #If we're making a new selection...
var start_pos = Global.selection_rectangle.polygon[0]
if start_pos != mouse_pos_floored:
var end_pos := Vector2(mouse_pos_ceiled.x, mouse_pos_ceiled.y)
if mouse_pos.x < start_pos.x:
end_pos.x = mouse_pos_ceiled.x - 1
if mouse_pos.y < start_pos.y:
end_pos.y = mouse_pos_ceiled.y - 1
Global.selection_rectangle.polygon[1] = Vector2(end_pos.x, start_pos.y)
Global.selection_rectangle.polygon[2] = end_pos
Global.selection_rectangle.polygon[3] = Vector2(start_pos.x, end_pos.y)
if !is_making_line:
previous_mouse_pos = mouse_pos
@ -99,6 +125,30 @@ func _process(delta) -> void:
else:
line_2d.set_point_position(1, mouse_pos)
if is_making_selection != "None": #If we're making a selection
if Input.is_action_just_released(is_making_selection): #Finish selection when button is released
var start_pos = Global.selection_rectangle.polygon[0]
var end_pos = Global.selection_rectangle.polygon[2]
if start_pos.x > end_pos.x:
var temp = end_pos.x
end_pos.x = start_pos.x
start_pos.x = temp
if start_pos.y > end_pos.y:
var temp = end_pos.y
end_pos.y = start_pos.y
start_pos.y = temp
Global.selection_rectangle.polygon[0] = start_pos
Global.selection_rectangle.polygon[1] = Vector2(end_pos.x, start_pos.y)
Global.selection_rectangle.polygon[2] = end_pos
Global.selection_rectangle.polygon[3] = Vector2(start_pos.x, end_pos.y)
for xx in range(start_pos.x, end_pos.x):
for yy in range(start_pos.y, end_pos.y):
Global.selected_pixels.append(Vector2(xx, yy))
is_making_selection = "None"
if sprite_changed_this_frame:
update_texture(current_layer_index)
@ -158,6 +208,16 @@ func _draw() -> void:
for texture in layers:
if texture[3]: #if it's visible
draw_texture(texture[1], location)
if Global.tile_mode:
draw_texture(texture[1], Vector2(location.x, location.y + size.y)) #Down
draw_texture(texture[1], Vector2(location.x - size.x, location.y + size.y)) #Down Left
draw_texture(texture[1], Vector2(location.x - size.x, location.y)) #Left
draw_texture(texture[1], location - size) #Up left
draw_texture(texture[1], Vector2(location.x, location.y - size.y)) #Up
draw_texture(texture[1], Vector2(location.x + size.x, location.y - size.y)) #Up right
draw_texture(texture[1], Vector2(location.x + size.x, location.y)) #Right
draw_texture(texture[1], location + size) #Down right
#Idea taken from flurick (on GitHub)
if Global.draw_grid:
@ -228,9 +288,9 @@ func pencil_and_eraser(mouse_pos : Vector2, color : Color, current_mouse_button
is_making_line = true
else:
var brush_size := 1
if current_mouse_button == "L":
if current_mouse_button == "left_mouse":
brush_size = Global.left_brush_size
elif current_mouse_button == "R":
elif current_mouse_button == "right_mouse":
brush_size = Global.right_brush_size
if is_making_line:
@ -249,18 +309,33 @@ func pencil_and_eraser(mouse_pos : Vector2, color : Color, current_mouse_button
func draw_pixel(pos : Vector2, color : Color, brush_size : int) -> void:
if Global.can_draw && Global.has_focus && Global.current_frame == frame:
#If there is a selection and current pixel is not in it
var west_limit := location.x
var east_limit := location.x + size.x
var north_limit := location.y
var south_limit := location.y + size.y
if Global.selected_pixels.size() != 0:
west_limit = Global.selection_rectangle.polygon[0].x
east_limit = Global.selection_rectangle.polygon[2].x
north_limit = Global.selection_rectangle.polygon[0].y
south_limit = Global.selection_rectangle.polygon[2].y
var start_pos_x = pos.x - (brush_size >> 1)
var start_pos_y = pos.y - (brush_size >> 1)
for cur_pos_x in range(start_pos_x, start_pos_x + brush_size):
#layers[current_layer_index][0].set_pixel(cur_pos_x, pos.y, color)
for cur_pos_y in range(start_pos_y, start_pos_y + brush_size):
if layers[current_layer_index][0].get_pixel(cur_pos_x, cur_pos_y) != color: #don't draw the same pixel over and over
layers[current_layer_index][0].set_pixel(cur_pos_x, cur_pos_y, color)
#layers[current_layer_index][0].set_pixelv(pos, color)
sprite_changed_this_frame = true
if point_in_rectangle_equal(Vector2(cur_pos_x, cur_pos_y), Vector2(west_limit, north_limit), Vector2(east_limit - 1, south_limit - 1)):
layers[current_layer_index][0].set_pixel(cur_pos_x, cur_pos_y, color)
#layers[current_layer_index][0].set_pixelv(pos, color)
sprite_changed_this_frame = true
func point_in_rectangle(p : Vector2, coord1 : Vector2, coord2 : Vector2) -> bool:
return p.x > coord1.x && p.y > coord1.y && p.x < coord2.x && p.y < coord2.y
func point_in_rectangle_equal(p : Vector2, coord1 : Vector2, coord2 : Vector2) -> bool:
return p.x >= coord1.x && p.y >= coord1.y && p.x <= coord2.x && p.y <= coord2.y
#Bresenham's Algorithm
#Thanks to https://godotengine.org/qa/35276/tile-based-line-drawing-algorithm-efficiency
@ -296,23 +371,35 @@ func flood_fill(pos : Vector2, target_color : Color, replace_color : Color) -> v
elif pixel != target_color:
return
else:
var west_limit := location.x
var east_limit := location.x + size.x
var north_limit := location.y
var south_limit := location.y + size.y
if Global.selected_pixels.size() != 0:
west_limit = Global.selection_rectangle.polygon[0].x
east_limit = Global.selection_rectangle.polygon[2].x
north_limit = Global.selection_rectangle.polygon[0].y
south_limit = Global.selection_rectangle.polygon[2].y
if !point_in_rectangle_equal(pos, Vector2(west_limit, north_limit), Vector2(east_limit - 1, south_limit - 1)):
return
var q = [pos]
for n in q:
var west : Vector2 = n
var east : Vector2 = n
while west.x >= location.x && layers[current_layer_index][0].get_pixelv(west) == target_color:
while west.x >= west_limit && layers[current_layer_index][0].get_pixelv(west) == target_color:
west += Vector2.LEFT
while east.x < location.x + size.x && layers[current_layer_index][0].get_pixelv(east) == target_color:
while east.x < east_limit && layers[current_layer_index][0].get_pixelv(east) == target_color:
east += Vector2.RIGHT
for px in range(west.x + 1, east.x):
var p := Vector2(px, n.y)
#print(point_in_rectangle(p, location, size))
draw_pixel(p, replace_color, 1)
var north := p + Vector2.UP
var south := p + Vector2.DOWN
if north.y >= location.y && layers[current_layer_index][0].get_pixelv(north) == target_color:
if north.y >= north_limit && layers[current_layer_index][0].get_pixelv(north) == target_color:
q.append(north)
if south.y < location.y + size.y && layers[current_layer_index][0].get_pixelv(south) == target_color:
if south.y < south_limit && layers[current_layer_index][0].get_pixelv(south) == target_color:
q.append(south)
func _on_Timer_timeout() -> void:

View file

@ -12,6 +12,8 @@ var onion_skinning_future_rate := 0
# warning-ignore:unused_class_variable
var onion_skinning_blue_red := false
# warning-ignore:unused_class_variable
var tile_mode := false
# warning-ignore:unused_class_variable
var draw_grid := false
var canvases := []
var canvas : Canvas
@ -25,6 +27,9 @@ var left_brush_size := 1
# warning-ignore:unused_class_variable
var right_brush_size := 1
var camera : Camera2D
var selection_rectangle : Polygon2D
var selected_pixels := []
var image_clipboard : Image
var file_menu : MenuButton
var edit_menu : MenuButton
@ -61,6 +66,9 @@ func _ready() -> void:
canvas_parent = canvas.get_parent()
camera = find_node_by_name(canvas_parent, "Camera2D")
selection_rectangle = find_node_by_name(root, "SelectionRectangle")
image_clipboard = Image.new()
file_menu = find_node_by_name(root, "FileMenu")
edit_menu = find_node_by_name(root, "EditMenu")
left_indicator = find_node_by_name(root, "LeftIndicator")

View file

@ -6,6 +6,7 @@ var opensprite_file_selected := false
var pencil_tool
var eraser_tool
var fill_tool
var rectselect_tool
var import_as_new_frame : CheckBox
var export_all_frames : CheckBox
var export_as_single_file : CheckBox
@ -20,7 +21,6 @@ func _ready() -> void:
# This property is only available in 3.2alpha or later, so use `set()` to fail gracefully if it doesn't exist.
OS.set("min_window_size", Vector2(1024, 600))
var file_menu_items := {
"New..." : KEY_MASK_CTRL + KEY_N,
"Open..." : KEY_MASK_CTRL + KEY_O,
@ -33,7 +33,9 @@ func _ready() -> void:
}
var edit_menu_items := {
"Scale Image" : 0,
"Show Grid" : KEY_MASK_CTRL + KEY_G
"Tile Mode" : KEY_MASK_CTRL + KEY_T,
"Show Grid" : KEY_MASK_CTRL + KEY_G,
"Clear Selection" : 0
#"Undo" : KEY_MASK_CTRL + KEY_Z,
#"Redo" : KEY_MASK_SHIFT + KEY_MASK_CTRL + KEY_Z,
}
@ -53,10 +55,12 @@ func _ready() -> void:
pencil_tool = $UI/ToolPanel/Tools/MenusAndTools/ToolsContainer/Pencil
eraser_tool = $UI/ToolPanel/Tools/MenusAndTools/ToolsContainer/Eraser
fill_tool = $UI/ToolPanel/Tools/MenusAndTools/ToolsContainer/Fill
rectselect_tool = $UI/ToolPanel/Tools/MenusAndTools/ToolsContainer/RectSelect
pencil_tool.connect("pressed", self, "_on_Tool_pressed", [pencil_tool])
eraser_tool.connect("pressed", self, "_on_Tool_pressed", [eraser_tool])
fill_tool.connect("pressed", self, "_on_Tool_pressed", [fill_tool])
rectselect_tool.connect("pressed", self, "_on_Tool_pressed", [rectselect_tool])
#Options for Import
import_as_new_frame = CheckBox.new()
@ -89,6 +93,10 @@ func _input(event):
_on_Tool_pressed(fill_tool, false, false)
elif event.is_action_pressed("left_fill_tool"):
_on_Tool_pressed(fill_tool, false, true)
elif event.is_action_pressed("right_rectangle_select_tool"):
_on_Tool_pressed(rectselect_tool, false, false)
elif event.is_action_pressed("left_rectangle_select_tool"):
_on_Tool_pressed(rectselect_tool, false, true)
func file_menu_id_pressed(id : int) -> void:
@ -130,8 +138,16 @@ func edit_menu_id_pressed(id : int) -> void:
0: #Scale Image
$ScaleImage.popup_centered()
Global.can_draw = false
1: #Show grid
1: #Tile mode
Global.tile_mode = !Global.tile_mode
2: #Show grid
Global.draw_grid = !Global.draw_grid
3: #Clear selection
Global.selection_rectangle.polygon[0] = Vector2.ZERO
Global.selection_rectangle.polygon[1] = Vector2.ZERO
Global.selection_rectangle.polygon[2] = Vector2.ZERO
Global.selection_rectangle.polygon[3] = Vector2.ZERO
Global.selected_pixels.clear()
func _on_CreateNewImage_confirmed() -> void:
var width = float($CreateNewImage/VBoxContainer/WidthCont/WidthValue.value)

View file

@ -0,0 +1,107 @@
extends Polygon2D
var img : Image
var tex : ImageTexture
var is_dragging := false
var move_pixels := false
var diff_x := 0.0
var diff_y := 0.0
var orig_x := 0.0
var orig_y := 0.0
var orig_colors := []
func _ready() -> void:
img = Image.new()
#img.create(Global.canvas.size.x, Global.canvas.size.y, false, Image.FORMAT_RGBA8)
img.create(1, 1, false, Image.FORMAT_RGBA8)
img.lock()
tex = ImageTexture.new()
tex.create_from_image(img, 0)
# warning-ignore:unused_argument
func _process(delta) -> void:
var mouse_pos := get_local_mouse_position() - Global.canvas.location
var mouse_pos_floored := mouse_pos.floor()
var start_pos := polygon[0]
var end_pos := polygon[2]
var layer : Image = Global.canvas.layers[Global.canvas.current_layer_index][0]
if point_in_rectangle(mouse_pos, polygon[0], polygon[2]) && Global.selected_pixels.size() > 0 && (Global.current_left_tool == "RectSelect" || Global.current_right_tool == "RectSelect"):
get_parent().get_parent().mouse_default_cursor_shape = Input.CURSOR_MOVE
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
is_dragging = true
if Input.is_key_pressed(KEY_SHIFT):
move_pixels = true
else:
move_pixels = false
img.fill(Color(0, 0, 0, 0))
diff_x = end_pos.x - mouse_pos_floored.x
diff_y = end_pos.y - mouse_pos_floored.y
orig_x = start_pos.x - mouse_pos_floored.x
orig_y = start_pos.y - mouse_pos_floored.y
if move_pixels:
img.resize(polygon[2].x - polygon[0].x, polygon[2].y - polygon[0].y, 0)
img.lock()
for i in range(Global.selected_pixels.size()):
orig_colors.append(layer.get_pixelv(Global.selected_pixels[i]))
var px = Global.selected_pixels[i] - Global.selected_pixels[0]
img.set_pixelv(px, orig_colors[i])
layer.set_pixelv(Global.selected_pixels[i], Color(0, 0, 0, 0))
#print(layer.get_pixelv(Global.selected_pixels[i]))
Global.canvas.update_texture(Global.canvas.current_layer_index)
tex.create_from_image(img, 0)
update()
else:
get_parent().get_parent().mouse_default_cursor_shape = Input.CURSOR_CROSS
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
if orig_x + mouse_pos_floored.x >= Global.canvas.location.x && diff_x + mouse_pos_floored.x <= Global.canvas.size.x:
start_pos.x = orig_x + mouse_pos_floored.x
end_pos.x = diff_x + mouse_pos_floored.x
if orig_y + mouse_pos_floored.y >= Global.canvas.location.y && diff_y + mouse_pos_floored.y <= Global.canvas.size.y:
start_pos.y = orig_y + mouse_pos_floored.y
end_pos.y = diff_y + mouse_pos_floored.y
polygon[0] = start_pos
polygon[1] = Vector2(end_pos.x, start_pos.y)
polygon[2] = end_pos
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
is_dragging = false
if move_pixels:
for i in range(Global.selected_pixels.size()):
if orig_colors[i].a > 0:
var px = polygon[0] + Global.selected_pixels[i] - Global.selected_pixels[0]
layer.set_pixelv(px, orig_colors[i])
Global.canvas.update_texture(Global.canvas.current_layer_index)
orig_colors.clear()
Global.selected_pixels.clear()
for xx in range(start_pos.x, end_pos.x):
for yy in range(start_pos.y, end_pos.y):
Global.selected_pixels.append(Vector2(xx, yy))
#Handle copy
if Input.is_action_just_pressed("copy") && Global.selected_pixels.size() > 0:
Global.image_clipboard = layer.get_rect(Rect2(polygon[0], polygon[2]))
print(Rect2(polygon[0], polygon[2]), Global.image_clipboard.get_size())
print(Global.image_clipboard.get_data()[0])
#Handle paste
if Input.is_action_just_pressed("paste") && Global.selected_pixels.size() > 0 && !is_dragging:
layer.blend_rect(Global.image_clipboard, Rect2(Vector2.ZERO, polygon[2]-polygon[0]), polygon[0])
layer.lock()
Global.canvas.update_texture(Global.canvas.current_layer_index)
func _draw() -> void:
if img.get_size() == polygon[2] - polygon[0]:
draw_texture(tex, polygon[0], Color(1, 1, 1, 0.5))
func point_in_rectangle(p : Vector2, coord1 : Vector2, coord2 : Vector2) -> bool:
return p.x > coord1.x && p.y > coord1.y && p.x < coord2.x && p.y < coord2.y