2019-08-18 09:28:38 +00:00
|
|
|
extends Node2D
|
|
|
|
class_name Canvas
|
|
|
|
|
|
|
|
var layers := []
|
|
|
|
var current_layer_index := 0
|
|
|
|
var trans_background : ImageTexture
|
|
|
|
var location := Vector2.ZERO
|
|
|
|
var size := Vector2(64, 64)
|
2019-09-09 22:57:46 +00:00
|
|
|
var frame := 0
|
|
|
|
var frame_button : VBoxContainer
|
|
|
|
var frame_texture_rect : TextureRect
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
|
|
var previous_mouse_pos := Vector2.ZERO
|
|
|
|
var mouse_inside_canvas := false #used for undo
|
|
|
|
var sprite_changed_this_frame := false #for optimization purposes
|
2019-09-09 22:57:46 +00:00
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
var is_making_line := false
|
2019-09-18 14:47:28 +00:00
|
|
|
var is_making_selection := "None"
|
2019-08-18 09:28:38 +00:00
|
|
|
var line_2d : Line2D
|
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready() -> void:
|
|
|
|
Global.can_draw = false
|
|
|
|
#Background
|
|
|
|
trans_background = ImageTexture.new()
|
2019-09-25 19:59:48 +00:00
|
|
|
trans_background.create_from_image(load("res://Assets/Graphics/Transparent Background.png"), 0)
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
|
|
#The sprite itself
|
2019-09-09 22:57:46 +00:00
|
|
|
if layers.empty():
|
|
|
|
var sprite := Image.new()
|
|
|
|
sprite.create(size.x, size.y, false, Image.FORMAT_RGBA8)
|
2019-08-18 09:28:38 +00:00
|
|
|
|
2019-09-09 22:57:46 +00:00
|
|
|
sprite.lock()
|
|
|
|
var tex := ImageTexture.new()
|
|
|
|
tex.create_from_image(sprite, 0)
|
|
|
|
|
|
|
|
#Store [Image, ImageTexture, Layer Name, Visibity boolean]
|
|
|
|
layers.append([sprite, tex, "Layer 0", true])
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
|
|
generate_layer_panels()
|
2019-09-09 22:57:46 +00:00
|
|
|
|
2019-09-25 19:59:48 +00:00
|
|
|
frame_button = load("res://Prefabs/FrameButton.tscn").instance()
|
2019-09-09 22:57:46 +00:00
|
|
|
frame_button.name = "Frame_%s" % frame
|
|
|
|
frame_button.get_node("FrameButton").frame = frame
|
2019-09-09 23:30:43 +00:00
|
|
|
frame_button.get_node("FrameID").text = str(frame + 1)
|
2019-09-09 22:57:46 +00:00
|
|
|
Global.frame_container.add_child(frame_button)
|
|
|
|
|
|
|
|
frame_texture_rect = Global.find_node_by_name(frame_button, "FrameTexture")
|
|
|
|
frame_texture_rect.texture = layers[0][1] #ImageTexture current_layer_index
|
|
|
|
|
|
|
|
camera_zoom()
|
2019-08-18 09:28:38 +00:00
|
|
|
|
2019-09-25 19:59:48 +00:00
|
|
|
func camera_zoom() -> void:
|
|
|
|
#Set camera offset to the center of canvas
|
|
|
|
Global.camera.offset = size / 2
|
|
|
|
Global.camera2.offset = size / 2
|
|
|
|
#Set camera zoom based on the sprite size
|
|
|
|
var bigger = max(size.x, size.y)
|
|
|
|
var zoom_max := Vector2(bigger, bigger) * 0.01
|
|
|
|
if zoom_max > Vector2.ONE:
|
|
|
|
Global.camera.zoom_max = zoom_max
|
|
|
|
Global.camera2.zoom_max = zoom_max
|
|
|
|
else:
|
|
|
|
Global.camera.zoom_max = Vector2.ONE
|
|
|
|
Global.camera2.zoom_max = Vector2.ONE
|
|
|
|
Global.camera.zoom = Vector2(bigger, bigger) * 0.002
|
|
|
|
Global.camera2.zoom = Vector2(bigger, bigger) * 0.002
|
|
|
|
Global.zoom_level_label.text = "Zoom: x%s" % [stepify(1 / Global.camera.zoom.x, 0.01)]
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
# warning-ignore:unused_argument
|
|
|
|
func _process(delta) -> void:
|
|
|
|
sprite_changed_this_frame = false
|
|
|
|
update()
|
|
|
|
var mouse_pos := get_local_mouse_position() - location
|
2019-09-18 14:47:28 +00:00
|
|
|
var mouse_pos_floored := mouse_pos.floor()
|
|
|
|
var mouse_pos_ceiled := mouse_pos.ceil()
|
2019-08-18 09:28:38 +00:00
|
|
|
var current_mouse_button := "None"
|
|
|
|
var current_action := "None"
|
|
|
|
if Input.is_mouse_button_pressed(BUTTON_LEFT):
|
2019-09-18 14:47:28 +00:00
|
|
|
current_mouse_button = "left_mouse"
|
2019-08-18 09:28:38 +00:00
|
|
|
current_action = Global.current_left_tool
|
|
|
|
elif Input.is_mouse_button_pressed(BUTTON_RIGHT):
|
2019-09-18 14:47:28 +00:00
|
|
|
current_mouse_button = "right_mouse"
|
2019-08-18 09:28:38 +00:00
|
|
|
current_action = Global.current_right_tool
|
|
|
|
|
2019-09-09 22:57:46 +00:00
|
|
|
if visible:
|
|
|
|
if !point_in_rectangle(mouse_pos, location, location + size):
|
|
|
|
if !Input.is_mouse_button_pressed(BUTTON_LEFT) && !Input.is_mouse_button_pressed(BUTTON_RIGHT):
|
|
|
|
if mouse_inside_canvas:
|
|
|
|
mouse_inside_canvas = false
|
|
|
|
Global.cursor_position_label.text = "[%sx%s]" % [size.x, size.y]
|
|
|
|
else:
|
|
|
|
Global.cursor_position_label.text = "[%sx%s] %s, %s" % [size.x, size.y, mouse_pos_floored.x, mouse_pos_floored.y]
|
2019-09-18 14:47:28 +00:00
|
|
|
#Handle current tool
|
2019-08-18 09:28:38 +00:00
|
|
|
match current_action:
|
|
|
|
"Pencil":
|
|
|
|
var current_color : Color
|
2019-09-18 14:47:28 +00:00
|
|
|
if current_mouse_button == "left_mouse":
|
2019-08-18 09:28:38 +00:00
|
|
|
current_color = Global.left_color_picker.color
|
2019-09-18 14:47:28 +00:00
|
|
|
elif current_mouse_button == "right_mouse":
|
2019-08-18 09:28:38 +00:00
|
|
|
current_color = Global.right_color_picker.color
|
2019-09-03 19:51:14 +00:00
|
|
|
pencil_and_eraser(mouse_pos, current_color, current_mouse_button)
|
2019-08-18 09:28:38 +00:00
|
|
|
"Eraser":
|
2019-09-03 19:51:14 +00:00
|
|
|
pencil_and_eraser(mouse_pos, Color(0, 0, 0, 0), current_mouse_button)
|
2019-08-18 09:28:38 +00:00
|
|
|
"Fill":
|
2019-09-09 22:57:46 +00:00
|
|
|
if point_in_rectangle(mouse_pos, location, location + size) && Global.can_draw && Global.has_focus && Global.current_frame == frame:
|
2019-08-18 09:28:38 +00:00
|
|
|
var current_color : Color
|
2019-09-18 14:47:28 +00:00
|
|
|
if current_mouse_button == "left_mouse":
|
2019-08-18 09:28:38 +00:00
|
|
|
current_color = Global.left_color_picker.color
|
2019-09-18 14:47:28 +00:00
|
|
|
elif current_mouse_button == "right_mouse":
|
2019-08-18 09:28:38 +00:00
|
|
|
current_color = Global.right_color_picker.color
|
|
|
|
flood_fill(mouse_pos, layers[current_layer_index][0].get_pixelv(mouse_pos), current_color)
|
2019-09-18 14:47:28 +00:00
|
|
|
"RectSelect":
|
2019-09-18 21:10:23 +00:00
|
|
|
#Check SelectionRectangle.gd for more code on Rectangle Selection
|
|
|
|
if Global.can_draw && Global.has_focus && Global.current_frame == frame:
|
2019-09-18 14:47:28 +00:00
|
|
|
#If we're creating a new selection
|
2019-09-18 21:10:23 +00:00
|
|
|
if Global.selected_pixels.size() == 0 || !point_in_rectangle(mouse_pos_floored, Global.selection_rectangle.polygon[0] - Vector2.ONE, Global.selection_rectangle.polygon[2]):
|
2019-09-18 14:47:28 +00:00
|
|
|
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)
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
|
|
if !is_making_line:
|
|
|
|
previous_mouse_pos = mouse_pos
|
|
|
|
previous_mouse_pos.x = clamp(previous_mouse_pos.x, location.x, location.x + size.x)
|
|
|
|
previous_mouse_pos.y = clamp(previous_mouse_pos.y, location.y, location.y + size.y)
|
|
|
|
else:
|
|
|
|
line_2d.set_point_position(1, mouse_pos)
|
|
|
|
|
2019-09-18 14:47:28 +00:00
|
|
|
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):
|
2019-09-25 19:59:48 +00:00
|
|
|
Global.selected_pixels.append(Vector2(xx, yy))
|
2019-09-18 14:47:28 +00:00
|
|
|
is_making_selection = "None"
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
if sprite_changed_this_frame:
|
|
|
|
update_texture(current_layer_index)
|
|
|
|
|
2019-09-09 22:57:46 +00:00
|
|
|
func update_texture(layer_index : int) -> void:
|
2019-08-18 09:28:38 +00:00
|
|
|
layers[layer_index][1].create_from_image(layers[layer_index][0], 0)
|
|
|
|
get_layer_container(layer_index).get_child(0).get_child(1).texture = layers[layer_index][1]
|
2019-09-09 22:57:46 +00:00
|
|
|
|
2019-09-14 19:55:33 +00:00
|
|
|
#This code is used to update the texture in the animation timeline frame button
|
|
|
|
#but blend_rect causes major performance issues on large images
|
2019-09-09 22:57:46 +00:00
|
|
|
var whole_image := Image.new()
|
|
|
|
whole_image.create(size.x, size.y, false, Image.FORMAT_RGBA8)
|
|
|
|
for layer in layers:
|
|
|
|
whole_image.blend_rect(layer[0], Rect2(position, size), Vector2.ZERO)
|
|
|
|
layer[0].lock()
|
|
|
|
var whole_image_texture := ImageTexture.new()
|
|
|
|
whole_image_texture.create_from_image(whole_image, 0)
|
|
|
|
frame_texture_rect.texture = whole_image_texture
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
|
|
func get_layer_container(layer_index : int) -> PanelContainer:
|
|
|
|
for container in Global.vbox_layer_container.get_children():
|
|
|
|
if container is PanelContainer && container.i == layer_index:
|
|
|
|
return container
|
|
|
|
return null
|
|
|
|
|
|
|
|
func _draw() -> void:
|
2019-09-14 19:55:33 +00:00
|
|
|
draw_texture_rect(trans_background, Rect2(location, size), true) #Draw transparent background
|
|
|
|
|
|
|
|
#Onion Skinning
|
|
|
|
#Past
|
|
|
|
if Global.onion_skinning_past_rate > 0:
|
|
|
|
var color : Color
|
|
|
|
if Global.onion_skinning_blue_red:
|
|
|
|
color = Color.blue
|
|
|
|
else:
|
|
|
|
color = Color.white
|
|
|
|
for i in range(1, Global.onion_skinning_past_rate + 1):
|
|
|
|
if Global.current_frame >= i:
|
|
|
|
for texture in Global.canvases[Global.current_frame - i].layers:
|
|
|
|
color.a = 0.6/i
|
|
|
|
draw_texture(texture[1], location, color)
|
|
|
|
|
|
|
|
#Future
|
|
|
|
if Global.onion_skinning_future_rate > 0:
|
|
|
|
var color : Color
|
|
|
|
if Global.onion_skinning_blue_red:
|
|
|
|
color = Color.red
|
|
|
|
else:
|
|
|
|
color = Color.white
|
|
|
|
for i in range(1, Global.onion_skinning_future_rate + 1):
|
|
|
|
if Global.current_frame < Global.canvases.size() - i:
|
|
|
|
for texture in Global.canvases[Global.current_frame + i].layers:
|
|
|
|
color.a = 0.6/i
|
|
|
|
draw_texture(texture[1], location, color)
|
|
|
|
|
|
|
|
#Draw current frame layers
|
2019-08-18 09:28:38 +00:00
|
|
|
for texture in layers:
|
|
|
|
if texture[3]: #if it's visible
|
|
|
|
draw_texture(texture[1], location)
|
2019-09-18 14:47:28 +00:00
|
|
|
|
|
|
|
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
|
2019-08-18 09:28:38 +00:00
|
|
|
|
2019-09-03 19:51:14 +00:00
|
|
|
#Idea taken from flurick (on GitHub)
|
2019-09-09 22:57:46 +00:00
|
|
|
if Global.draw_grid:
|
2019-08-18 09:28:38 +00:00
|
|
|
for x in size.x:
|
2019-09-03 19:51:14 +00:00
|
|
|
draw_line(Vector2(x, location.y), Vector2(x, size.y), Color.black, true)
|
|
|
|
for y in size.y:
|
|
|
|
draw_line(Vector2(location.x, y), Vector2(size.x, y), Color.black, true)
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
|
|
#Draw rectangle to indicate the pixel currently being hovered on
|
2019-09-27 17:05:24 +00:00
|
|
|
var mouse_pos := get_local_mouse_position() + location
|
2019-08-18 09:28:38 +00:00
|
|
|
if point_in_rectangle(mouse_pos, location, location + size):
|
|
|
|
mouse_pos = mouse_pos.floor()
|
2019-09-09 22:57:46 +00:00
|
|
|
if Global.left_square_indicator_visible:
|
2019-09-27 17:05:24 +00:00
|
|
|
match Global.current_left_brush_type:
|
|
|
|
Global.BRUSH_TYPES.PIXEL:
|
|
|
|
var start_pos_x = mouse_pos.x - (Global.left_brush_size >> 1)
|
|
|
|
var start_pos_y = mouse_pos.y - (Global.left_brush_size >> 1)
|
|
|
|
draw_rect(Rect2(start_pos_x, start_pos_y, Global.left_brush_size, Global.left_brush_size), Color.blue, false)
|
|
|
|
Global.BRUSH_TYPES.CUSTOM:
|
|
|
|
var custom_brush_size = Global.custom_left_brush_image.get_size()
|
|
|
|
var dst : Vector2 = mouse_pos - custom_brush_size / 4
|
|
|
|
draw_texture(Global.custom_left_brush_texture, dst)
|
|
|
|
|
2019-09-09 22:57:46 +00:00
|
|
|
if Global.right_square_indicator_visible:
|
2019-09-27 17:05:24 +00:00
|
|
|
match Global.current_right_brush_type:
|
|
|
|
Global.BRUSH_TYPES.PIXEL:
|
|
|
|
var start_pos_x = mouse_pos.x - (Global.right_brush_size >> 1)
|
|
|
|
var start_pos_y = mouse_pos.y - (Global.right_brush_size >> 1)
|
|
|
|
draw_rect(Rect2(start_pos_x, start_pos_y, Global.right_brush_size, Global.right_brush_size), Color.red, false)
|
|
|
|
Global.BRUSH_TYPES.CUSTOM:
|
|
|
|
var custom_brush_size = Global.custom_right_brush_image.get_size()
|
|
|
|
var dst : Vector2 = mouse_pos - custom_brush_size / 4
|
|
|
|
draw_texture(Global.custom_right_brush_texture, dst)
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
|
|
func generate_layer_panels() -> void:
|
|
|
|
for child in Global.vbox_layer_container.get_children():
|
|
|
|
if child is PanelContainer:
|
|
|
|
child.queue_free()
|
|
|
|
|
|
|
|
current_layer_index = layers.size() - 1
|
|
|
|
if layers.size() == 1:
|
|
|
|
Global.remove_layer_button.disabled = true
|
2019-09-04 17:50:05 +00:00
|
|
|
Global.remove_layer_button.mouse_default_cursor_shape = Control.CURSOR_FORBIDDEN
|
2019-08-18 09:28:38 +00:00
|
|
|
else:
|
|
|
|
Global.remove_layer_button.disabled = false
|
2019-09-04 17:50:05 +00:00
|
|
|
Global.remove_layer_button.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
|
|
for i in range(layers.size() -1, -1, -1):
|
2019-09-25 19:59:48 +00:00
|
|
|
var layer_container = load("res://Prefabs/LayerContainer.tscn").instance()
|
2019-08-18 09:28:38 +00:00
|
|
|
layers[i][2] = "Layer %s" % i
|
|
|
|
layer_container.i = i
|
|
|
|
layer_container.get_child(0).get_child(2).text = layers[i][2]
|
|
|
|
layers[i][3] = true #set visible
|
|
|
|
layer_container.get_child(0).get_child(1).texture = layers[i][1]
|
|
|
|
Global.vbox_layer_container.add_child(layer_container)
|
|
|
|
|
2019-09-03 19:51:14 +00:00
|
|
|
func pencil_and_eraser(mouse_pos : Vector2, color : Color, current_mouse_button : String) -> void:
|
2019-08-18 09:28:38 +00:00
|
|
|
if Input.is_key_pressed(KEY_SHIFT):
|
|
|
|
if !is_making_line:
|
|
|
|
line_2d = Line2D.new()
|
|
|
|
line_2d.width = 0.5
|
|
|
|
line_2d.default_color = Color.darkgray
|
|
|
|
line_2d.add_point(previous_mouse_pos)
|
|
|
|
line_2d.add_point(mouse_pos)
|
|
|
|
add_child(line_2d)
|
|
|
|
is_making_line = true
|
|
|
|
else:
|
2019-09-03 19:51:14 +00:00
|
|
|
var brush_size := 1
|
2019-09-25 19:59:48 +00:00
|
|
|
var brush_type = Global.BRUSH_TYPES.PIXEL
|
|
|
|
var brush_index := -1
|
2019-09-27 17:05:24 +00:00
|
|
|
var custom_brush_image : Image
|
2019-09-18 14:47:28 +00:00
|
|
|
if current_mouse_button == "left_mouse":
|
2019-09-09 22:57:46 +00:00
|
|
|
brush_size = Global.left_brush_size
|
2019-09-25 19:59:48 +00:00
|
|
|
brush_type = Global.current_left_brush_type
|
|
|
|
brush_index = Global.custom_left_brush_index
|
2019-09-27 17:05:24 +00:00
|
|
|
custom_brush_image = Global.custom_left_brush_image
|
2019-09-18 14:47:28 +00:00
|
|
|
elif current_mouse_button == "right_mouse":
|
2019-09-09 22:57:46 +00:00
|
|
|
brush_size = Global.right_brush_size
|
2019-09-25 19:59:48 +00:00
|
|
|
brush_type = Global.current_right_brush_type
|
|
|
|
brush_index = Global.custom_right_brush_index
|
2019-09-27 17:05:24 +00:00
|
|
|
custom_brush_image = Global.custom_right_brush_image
|
2019-09-03 19:51:14 +00:00
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
if is_making_line:
|
2019-09-27 17:05:24 +00:00
|
|
|
fill_gaps(mouse_pos, color, brush_size, brush_type, brush_index, custom_brush_image)
|
2019-08-18 09:28:38 +00:00
|
|
|
is_making_line = false
|
|
|
|
line_2d.queue_free()
|
|
|
|
else:
|
|
|
|
if point_in_rectangle(mouse_pos, location, location + size):
|
|
|
|
mouse_inside_canvas = true
|
|
|
|
#Draw
|
2019-09-27 17:05:24 +00:00
|
|
|
draw_pixel(mouse_pos, color, brush_size, brush_type, brush_index, custom_brush_image)
|
|
|
|
fill_gaps(mouse_pos, color, brush_size, brush_type, brush_index, custom_brush_image) #Fill the gaps
|
2019-08-18 09:28:38 +00:00
|
|
|
#If mouse is not inside bounds but it used to be, fill the gaps
|
|
|
|
elif point_in_rectangle(previous_mouse_pos, location, location + size):
|
2019-09-27 17:05:24 +00:00
|
|
|
fill_gaps(mouse_pos, color, brush_size, brush_type, brush_index, custom_brush_image)
|
2019-08-18 09:28:38 +00:00
|
|
|
|
2019-09-27 17:05:24 +00:00
|
|
|
func draw_pixel(pos : Vector2, color : Color, brush_size : int, brush_type : int, brush_index : int, custom_brush_image : Image) -> void:
|
2019-09-09 22:57:46 +00:00
|
|
|
if Global.can_draw && Global.has_focus && Global.current_frame == frame:
|
2019-09-18 14:47:28 +00:00
|
|
|
var west_limit := location.x
|
|
|
|
var east_limit := location.x + size.x
|
|
|
|
var north_limit := location.y
|
|
|
|
var south_limit := location.y + size.y
|
2019-09-25 19:59:48 +00:00
|
|
|
if Global.selected_pixels.size() != 0: #If there is a selection and current pixel position is not in it
|
2019-09-18 21:10:23 +00:00
|
|
|
west_limit = max(west_limit, Global.selection_rectangle.polygon[0].x)
|
|
|
|
east_limit = min(east_limit, Global.selection_rectangle.polygon[2].x)
|
|
|
|
north_limit = max(north_limit, Global.selection_rectangle.polygon[0].y)
|
|
|
|
south_limit = min(south_limit, Global.selection_rectangle.polygon[2].y)
|
2019-09-18 14:47:28 +00:00
|
|
|
|
2019-09-25 19:59:48 +00:00
|
|
|
var start_pos_x
|
|
|
|
var start_pos_y
|
|
|
|
var end_pos_x
|
|
|
|
var end_pos_y
|
|
|
|
|
|
|
|
match(brush_type):
|
|
|
|
Global.BRUSH_TYPES.PIXEL:
|
|
|
|
start_pos_x = pos.x - (brush_size >> 1)
|
|
|
|
start_pos_y = pos.y - (brush_size >> 1)
|
|
|
|
end_pos_x = start_pos_x + brush_size
|
|
|
|
end_pos_y = start_pos_y + brush_size
|
|
|
|
for cur_pos_x in range(start_pos_x, end_pos_x):
|
|
|
|
for cur_pos_y in range(start_pos_y, end_pos_y):
|
|
|
|
if point_in_rectangle(Vector2(cur_pos_x, cur_pos_y), Vector2(west_limit - 1, north_limit - 1), Vector2(east_limit, south_limit)):
|
|
|
|
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)
|
|
|
|
sprite_changed_this_frame = true
|
|
|
|
|
|
|
|
Global.BRUSH_TYPES.CUSTOM:
|
2019-09-27 17:05:24 +00:00
|
|
|
var custom_brush_size = custom_brush_image.get_size()
|
2019-09-25 19:59:48 +00:00
|
|
|
var dst : Vector2 = pos - custom_brush_size / 4
|
|
|
|
var src_rect := Rect2(Vector2.ZERO, custom_brush_size)
|
|
|
|
#src_rect = src_rect.clip(Rect2(west_limit - dst.x, north_limit - dst.y, east_limit - custom_brush_size.x, south_limit - custom_brush_size.y))
|
|
|
|
|
|
|
|
if color.a > 0: #If it's the pencil
|
2019-09-27 17:05:24 +00:00
|
|
|
layers[current_layer_index][0].blend_rect(custom_brush_image, src_rect, dst)
|
2019-09-25 19:59:48 +00:00
|
|
|
else: #if it's transparent - if it's the eraser
|
2019-09-27 17:05:24 +00:00
|
|
|
var custom_brush := Image.new()
|
|
|
|
custom_brush.copy_from(Global.custom_brushes[brush_index])
|
|
|
|
custom_brush_size = custom_brush.get_size()
|
|
|
|
custom_brush.resize(custom_brush_size.x * brush_size, custom_brush_size.y * brush_size, Image.INTERPOLATE_NEAREST)
|
|
|
|
var custom_brush_blended = Global.blend_image_with_color(custom_brush, color, 1)
|
2019-09-25 19:59:48 +00:00
|
|
|
layers[current_layer_index][0].blit_rect_mask(custom_brush_blended, custom_brush, src_rect, dst)
|
|
|
|
layers[current_layer_index][0].lock()
|
|
|
|
update_texture(current_layer_index)
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
|
|
#Bresenham's Algorithm
|
|
|
|
#Thanks to https://godotengine.org/qa/35276/tile-based-line-drawing-algorithm-efficiency
|
2019-09-27 17:05:24 +00:00
|
|
|
func fill_gaps(mouse_pos : Vector2, color : Color, brush_size : int, brush_type : int, brush_index : int, custom_brush_image : Image) -> void:
|
2019-08-18 09:28:38 +00:00
|
|
|
var previous_mouse_pos_floored = previous_mouse_pos.floor()
|
|
|
|
var mouse_pos_floored = mouse_pos.floor()
|
|
|
|
mouse_pos_floored.x = clamp(mouse_pos_floored.x, location.x - 1, location.x + size.x)
|
|
|
|
mouse_pos_floored.y = clamp(mouse_pos_floored.y, location.y - 1, location.y + size.y)
|
|
|
|
var dx := int(abs(mouse_pos_floored.x - previous_mouse_pos_floored.x))
|
|
|
|
var dy := int(-abs(mouse_pos_floored.y - previous_mouse_pos_floored.y))
|
|
|
|
var err := dx + dy
|
|
|
|
var e2 := err << 1 #err * 2
|
|
|
|
var sx = 1 if previous_mouse_pos_floored.x < mouse_pos_floored.x else -1
|
|
|
|
var sy = 1 if previous_mouse_pos_floored.y < mouse_pos_floored.y else -1
|
|
|
|
var x = previous_mouse_pos_floored.x
|
|
|
|
var y = previous_mouse_pos_floored.y
|
|
|
|
while !(x == mouse_pos_floored.x && y == mouse_pos_floored.y):
|
2019-09-27 17:05:24 +00:00
|
|
|
draw_pixel(Vector2(x, y), color, brush_size, brush_type, brush_index, custom_brush_image)
|
2019-08-18 09:28:38 +00:00
|
|
|
e2 = err << 1
|
|
|
|
if e2 >= dy:
|
|
|
|
err += dy
|
|
|
|
x += sx
|
|
|
|
if e2 <= dx:
|
|
|
|
err += dx
|
|
|
|
y += sy
|
|
|
|
|
|
|
|
#Thanks to https://en.wikipedia.org/wiki/Flood_fill
|
|
|
|
func flood_fill(pos : Vector2, target_color : Color, replace_color : Color) -> void:
|
|
|
|
pos = pos.floor()
|
|
|
|
var pixel = layers[current_layer_index][0].get_pixelv(pos)
|
|
|
|
if target_color == replace_color:
|
|
|
|
return
|
|
|
|
elif pixel != target_color:
|
|
|
|
return
|
|
|
|
else:
|
2019-09-18 14:47:28 +00:00
|
|
|
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:
|
2019-09-18 21:10:23 +00:00
|
|
|
west_limit = max(west_limit, Global.selection_rectangle.polygon[0].x)
|
|
|
|
east_limit = min(east_limit, Global.selection_rectangle.polygon[2].x)
|
|
|
|
north_limit = max(north_limit, Global.selection_rectangle.polygon[0].y)
|
|
|
|
south_limit = min(south_limit, Global.selection_rectangle.polygon[2].y)
|
2019-09-18 14:47:28 +00:00
|
|
|
|
2019-09-18 21:10:23 +00:00
|
|
|
if !point_in_rectangle(pos, Vector2(west_limit - 1, north_limit - 1), Vector2(east_limit, south_limit)):
|
2019-09-18 14:47:28 +00:00
|
|
|
return
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
var q = [pos]
|
|
|
|
for n in q:
|
|
|
|
var west : Vector2 = n
|
|
|
|
var east : Vector2 = n
|
2019-09-18 14:47:28 +00:00
|
|
|
while west.x >= west_limit && layers[current_layer_index][0].get_pixelv(west) == target_color:
|
2019-08-18 09:28:38 +00:00
|
|
|
west += Vector2.LEFT
|
2019-09-18 14:47:28 +00:00
|
|
|
while east.x < east_limit && layers[current_layer_index][0].get_pixelv(east) == target_color:
|
2019-08-18 09:28:38 +00:00
|
|
|
east += Vector2.RIGHT
|
|
|
|
for px in range(west.x + 1, east.x):
|
|
|
|
var p := Vector2(px, n.y)
|
2019-09-25 19:59:48 +00:00
|
|
|
#Draw
|
|
|
|
layers[current_layer_index][0].set_pixelv(p, replace_color)
|
2019-08-18 09:28:38 +00:00
|
|
|
var north := p + Vector2.UP
|
|
|
|
var south := p + Vector2.DOWN
|
2019-09-18 14:47:28 +00:00
|
|
|
if north.y >= north_limit && layers[current_layer_index][0].get_pixelv(north) == target_color:
|
2019-08-18 09:28:38 +00:00
|
|
|
q.append(north)
|
2019-09-18 14:47:28 +00:00
|
|
|
if south.y < south_limit && layers[current_layer_index][0].get_pixelv(south) == target_color:
|
2019-08-18 09:28:38 +00:00
|
|
|
q.append(south)
|
2019-09-25 19:59:48 +00:00
|
|
|
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
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
func _on_Timer_timeout() -> void:
|
|
|
|
Global.can_draw = true
|