2019-08-18 09:28:38 +00:00
|
|
|
|
extends Node2D
|
|
|
|
|
class_name Canvas
|
|
|
|
|
|
|
|
|
|
var layers := []
|
|
|
|
|
var current_layer_index := 0
|
|
|
|
|
var location := Vector2.ZERO
|
|
|
|
|
var size := Vector2(64, 64)
|
2019-11-10 01:25:25 +00:00
|
|
|
|
var frame := 0 setget frame_changed
|
2019-09-09 22:57:46 +00:00
|
|
|
|
var frame_button : VBoxContainer
|
|
|
|
|
var frame_texture_rect : TextureRect
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
2019-11-20 22:11:21 +00:00
|
|
|
|
var current_pixel := Vector2.ZERO #pretty much same as mouse_pos, but can be accessed externally
|
2019-08-18 09:28:38 +00:00
|
|
|
|
var previous_mouse_pos := Vector2.ZERO
|
2019-12-15 12:07:28 +00:00
|
|
|
|
var previous_mouse_pos_for_lines := Vector2.ZERO
|
2019-12-26 19:36:56 +00:00
|
|
|
|
var can_undo := true
|
2019-12-06 23:08:23 +00:00
|
|
|
|
var cursor_inside_canvas := false
|
2019-10-29 21:22:38 +00:00
|
|
|
|
var previous_action := "None"
|
2019-08-18 09:28:38 +00:00
|
|
|
|
var mouse_inside_canvas := false #used for undo
|
|
|
|
|
var sprite_changed_this_frame := false #for optimization purposes
|
2019-11-12 00:51:47 +00:00
|
|
|
|
var lighten_darken_pixels := [] #Cleared after mouse release
|
2019-09-09 22:57:46 +00:00
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
|
var is_making_line := false
|
2019-12-19 13:50:41 +00:00
|
|
|
|
var made_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
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
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)
|
|
|
|
|
sprite.lock()
|
2019-12-25 18:27:25 +00:00
|
|
|
|
|
2019-09-09 22:57:46 +00:00
|
|
|
|
var tex := ImageTexture.new()
|
|
|
|
|
tex.create_from_image(sprite, 0)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-12-24 21:51:08 +00:00
|
|
|
|
#Store [Image, ImageTexture, Layer Name, Visibity boolean, Opacity]
|
|
|
|
|
layers.append([sprite, tex, "Layer 0", true, 1])
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
|
generate_layer_panels()
|
2019-10-29 21:22:38 +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-11-06 16:39:23 +00:00
|
|
|
|
frame_button.get_node("FrameButton").pressed = true
|
2019-09-09 23:30:43 +00:00
|
|
|
|
frame_button.get_node("FrameID").text = str(frame + 1)
|
2019-12-17 22:53:06 +00:00
|
|
|
|
frame_button.get_node("FrameID").add_color_override("font_color", Color("#3c5d75"))
|
2019-09-09 22:57:46 +00:00
|
|
|
|
Global.frame_container.add_child(frame_button)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-09-09 22:57:46 +00:00
|
|
|
|
frame_texture_rect = Global.find_node_by_name(frame_button, "FrameTexture")
|
|
|
|
|
frame_texture_rect.texture = layers[0][1] #ImageTexture current_layer_index
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-12-08 23:39:59 +00:00
|
|
|
|
#Only handle camera zoom settings & offset on the first frame
|
|
|
|
|
if Global.canvases[0] == self:
|
|
|
|
|
camera_zoom()
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
2019-09-25 19:59:48 +00:00
|
|
|
|
func camera_zoom() -> void:
|
|
|
|
|
#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
|
2019-12-08 22:17:05 +00:00
|
|
|
|
Global.camera_preview.zoom_max = zoom_max
|
2019-09-25 19:59:48 +00:00
|
|
|
|
else:
|
|
|
|
|
Global.camera.zoom_max = Vector2.ONE
|
|
|
|
|
Global.camera2.zoom_max = Vector2.ONE
|
2019-12-08 22:17:05 +00:00
|
|
|
|
Global.camera_preview.zoom_max = Vector2.ONE
|
|
|
|
|
|
2019-09-25 19:59:48 +00:00
|
|
|
|
Global.camera.zoom = Vector2(bigger, bigger) * 0.002
|
|
|
|
|
Global.camera2.zoom = Vector2(bigger, bigger) * 0.002
|
2019-12-05 14:49:27 +00:00
|
|
|
|
Global.camera_preview.zoom = Vector2(bigger, bigger) * 0.007
|
2019-11-21 17:10:03 +00:00
|
|
|
|
Global.zoom_level_label.text = str(round(100 / Global.camera.zoom.x)) + " %"
|
2019-09-25 19:59:48 +00:00
|
|
|
|
|
2019-11-19 21:23:43 +00:00
|
|
|
|
#Set camera offset to the center of canvas
|
|
|
|
|
Global.camera.offset = size / 2
|
|
|
|
|
Global.camera2.offset = size / 2
|
2019-12-05 14:49:27 +00:00
|
|
|
|
Global.camera_preview.offset = size / 2
|
2019-11-19 21:23:43 +00:00
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
|
# warning-ignore:unused_argument
|
2019-12-26 19:36:56 +00:00
|
|
|
|
func _input(event) -> void:
|
2019-08-18 09:28:38 +00:00
|
|
|
|
sprite_changed_this_frame = false
|
2019-12-26 19:36:56 +00:00
|
|
|
|
if Global.current_frame == frame:
|
|
|
|
|
update()
|
2019-11-20 22:11:21 +00:00
|
|
|
|
current_pixel = get_local_mouse_position() - location
|
|
|
|
|
var mouse_pos := current_pixel
|
2019-09-18 14:47:28 +00:00
|
|
|
|
var mouse_pos_floored := mouse_pos.floor()
|
|
|
|
|
var mouse_pos_ceiled := mouse_pos.ceil()
|
2019-11-03 21:07:57 +00:00
|
|
|
|
var mouse_in_canvas := point_in_rectangle(mouse_pos, location, location + size)
|
2019-08-18 09:28:38 +00:00
|
|
|
|
var current_mouse_button := "None"
|
|
|
|
|
var current_action := "None"
|
2019-12-26 19:36:56 +00:00
|
|
|
|
var fill_area := 0 # For the bucket tool
|
|
|
|
|
# For the LightenDarken tool
|
2019-12-03 23:01:37 +00:00
|
|
|
|
var ld := 0
|
|
|
|
|
var ld_amount := 0.1
|
2019-08-18 09:28:38 +00:00
|
|
|
|
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
|
2019-12-03 22:14:14 +00:00
|
|
|
|
fill_area = Global.left_fill_area
|
2019-12-03 23:01:37 +00:00
|
|
|
|
ld = Global.left_ld
|
|
|
|
|
ld_amount = Global.left_ld_amount
|
2019-08-18 09:28:38 +00:00
|
|
|
|
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-12-03 22:14:14 +00:00
|
|
|
|
fill_area = Global.right_fill_area
|
2019-12-03 23:01:37 +00:00
|
|
|
|
ld = Global.right_ld
|
|
|
|
|
ld_amount = Global.right_ld_amount
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-11-10 01:25:25 +00:00
|
|
|
|
if Global.current_frame == frame:
|
2019-12-05 22:27:47 +00:00
|
|
|
|
if mouse_in_canvas && Global.has_focus:
|
|
|
|
|
Global.cursor_position_label.text = "[%s×%s] %s, %s" % [size.x, size.y, mouse_pos_floored.x, mouse_pos_floored.y]
|
2019-12-06 23:08:23 +00:00
|
|
|
|
if !cursor_inside_canvas:
|
|
|
|
|
cursor_inside_canvas = true
|
2019-12-10 17:56:16 +00:00
|
|
|
|
Input.set_custom_mouse_cursor(load("res://Assets/Graphics/Cursor.png"), 0, Vector2(15, 15))
|
|
|
|
|
Global.left_cursor.visible = true
|
|
|
|
|
Global.right_cursor.visible = true
|
2019-12-05 22:27:47 +00:00
|
|
|
|
else:
|
2019-09-09 22:57:46 +00:00
|
|
|
|
if !Input.is_mouse_button_pressed(BUTTON_LEFT) && !Input.is_mouse_button_pressed(BUTTON_RIGHT):
|
|
|
|
|
if mouse_inside_canvas:
|
|
|
|
|
mouse_inside_canvas = false
|
2019-11-21 17:10:03 +00:00
|
|
|
|
Global.cursor_position_label.text = "[%s×%s]" % [size.x, size.y]
|
2019-12-06 23:08:23 +00:00
|
|
|
|
if cursor_inside_canvas:
|
|
|
|
|
cursor_inside_canvas = false
|
2019-12-10 17:56:16 +00:00
|
|
|
|
Global.left_cursor.visible = false
|
|
|
|
|
Global.right_cursor.visible = false
|
2019-12-06 23:08:23 +00:00
|
|
|
|
Input.set_custom_mouse_cursor(null)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-12-26 19:36:56 +00:00
|
|
|
|
# Handle Undo/Redo
|
2019-12-19 13:50:41 +00:00
|
|
|
|
var can_handle : bool = mouse_in_canvas && Global.can_draw && Global.has_focus && !made_line
|
2019-11-03 21:07:57 +00:00
|
|
|
|
var mouse_pressed : bool = (Input.is_action_just_pressed("left_mouse") && !Input.is_action_pressed("right_mouse")) || (Input.is_action_just_pressed("right_mouse") && !Input.is_action_pressed("left_mouse"))
|
|
|
|
|
|
2019-12-26 19:36:56 +00:00
|
|
|
|
# If we're already pressing a mouse button and we haven't handled undo yet,...
|
|
|
|
|
#. .. it means that the cursor was outside the canvas. Then, ...
|
|
|
|
|
# simulate "just pressed" logic the moment the cursor gets inside the canvas
|
2019-11-03 21:07:57 +00:00
|
|
|
|
if Input.is_action_pressed("left_mouse") || Input.is_action_pressed("right_mouse"):
|
|
|
|
|
if mouse_in_canvas && Global.undos < Global.undo_redo.get_version():
|
|
|
|
|
mouse_pressed = true
|
|
|
|
|
|
|
|
|
|
if mouse_pressed:
|
2019-11-05 01:14:03 +00:00
|
|
|
|
if can_handle && Global.current_frame == frame:
|
2019-12-05 22:27:47 +00:00
|
|
|
|
if current_action != "None" && current_action != "ColorPicker":
|
2019-10-29 21:22:38 +00:00
|
|
|
|
if current_action == "RectSelect":
|
|
|
|
|
handle_undo("Rectangle Select")
|
|
|
|
|
else:
|
|
|
|
|
handle_undo("Draw")
|
2019-11-03 21:07:57 +00:00
|
|
|
|
elif (Input.is_action_just_released("left_mouse") && !Input.is_action_pressed("right_mouse")) || (Input.is_action_just_released("right_mouse") && !Input.is_action_pressed("left_mouse")):
|
2019-12-19 13:50:41 +00:00
|
|
|
|
made_line = false
|
2019-11-12 00:51:47 +00:00
|
|
|
|
lighten_darken_pixels.clear()
|
2019-11-05 01:14:03 +00:00
|
|
|
|
if (can_handle || Global.undos == Global.undo_redo.get_version()) && Global.current_frame == frame:
|
2019-12-05 22:27:47 +00:00
|
|
|
|
if previous_action != "None" && previous_action != "RectSelect" && current_action != "ColorPicker":
|
2019-10-29 21:22:38 +00:00
|
|
|
|
handle_redo("Draw")
|
|
|
|
|
|
2019-12-26 19:36:56 +00:00
|
|
|
|
match current_action: # Handle current tool
|
2019-08-18 09:28:38 +00:00
|
|
|
|
"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-11-19 21:23:43 +00:00
|
|
|
|
"Bucket":
|
2019-12-05 22:27:47 +00:00
|
|
|
|
if can_handle && Global.current_frame == frame:
|
2019-12-25 00:53:45 +00:00
|
|
|
|
if fill_area == 0: # Paint the specific area of the same color
|
2019-12-03 22:14:14 +00:00
|
|
|
|
var current_color : Color
|
|
|
|
|
var horizontal_mirror := false
|
|
|
|
|
var vertical_mirror := false
|
|
|
|
|
var mirror_x := size.x - mouse_pos.x - 1
|
|
|
|
|
var mirror_y := size.y - mouse_pos.y - 1
|
|
|
|
|
if current_mouse_button == "left_mouse":
|
|
|
|
|
current_color = Global.left_color_picker.color
|
|
|
|
|
horizontal_mirror = Global.left_horizontal_mirror
|
|
|
|
|
vertical_mirror = Global.left_vertical_mirror
|
|
|
|
|
elif current_mouse_button == "right_mouse":
|
|
|
|
|
current_color = Global.right_color_picker.color
|
|
|
|
|
horizontal_mirror = Global.right_horizontal_mirror
|
|
|
|
|
vertical_mirror = Global.right_vertical_mirror
|
|
|
|
|
|
|
|
|
|
flood_fill(mouse_pos, layers[current_layer_index][0].get_pixelv(mouse_pos), current_color)
|
|
|
|
|
if horizontal_mirror:
|
|
|
|
|
var pos := Vector2(mirror_x, mouse_pos.y)
|
|
|
|
|
flood_fill(pos, layers[current_layer_index][0].get_pixelv(pos), current_color)
|
|
|
|
|
if vertical_mirror:
|
|
|
|
|
var pos := Vector2(mouse_pos.x, mirror_y)
|
|
|
|
|
flood_fill(pos, layers[current_layer_index][0].get_pixelv(pos), current_color)
|
|
|
|
|
if horizontal_mirror && vertical_mirror:
|
|
|
|
|
var pos := Vector2(mirror_x, mirror_y)
|
|
|
|
|
flood_fill(pos, layers[current_layer_index][0].get_pixelv(pos), current_color)
|
|
|
|
|
|
2019-12-25 00:53:45 +00:00
|
|
|
|
else: # Paint all pixels of the same color
|
|
|
|
|
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 = 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-12-03 22:14:14 +00:00
|
|
|
|
var current_color : Color
|
|
|
|
|
if current_mouse_button == "left_mouse":
|
|
|
|
|
current_color = Global.left_color_picker.color
|
|
|
|
|
elif current_mouse_button == "right_mouse":
|
|
|
|
|
current_color = Global.right_color_picker.color
|
|
|
|
|
|
|
|
|
|
var pixel_color : Color = layers[current_layer_index][0].get_pixelv(mouse_pos)
|
2019-12-25 00:53:45 +00:00
|
|
|
|
for xx in range(west_limit, east_limit):
|
|
|
|
|
for yy in range(north_limit, south_limit):
|
2019-12-03 22:14:14 +00:00
|
|
|
|
var c : Color = layers[current_layer_index][0].get_pixel(xx, yy)
|
|
|
|
|
if c == pixel_color:
|
|
|
|
|
layers[current_layer_index][0].set_pixel(xx, yy, current_color)
|
|
|
|
|
sprite_changed_this_frame = true
|
2019-10-22 23:54:29 +00:00
|
|
|
|
"LightenDarken":
|
2019-12-05 22:27:47 +00:00
|
|
|
|
if can_handle && Global.current_frame == frame:
|
2019-10-22 23:54:29 +00:00
|
|
|
|
var pixel_color : Color = layers[current_layer_index][0].get_pixelv(mouse_pos)
|
2019-12-03 23:01:37 +00:00
|
|
|
|
var color_changed : Color
|
|
|
|
|
if ld == 0: #Lighten
|
|
|
|
|
color_changed = pixel_color.lightened(ld_amount)
|
|
|
|
|
else: #Darken
|
|
|
|
|
color_changed = pixel_color.darkened(ld_amount)
|
2019-11-12 00:51:47 +00:00
|
|
|
|
pencil_and_eraser(mouse_pos, color_changed, current_mouse_button, current_action)
|
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-12-04 15:22:21 +00:00
|
|
|
|
"ColorPicker":
|
2019-12-05 22:27:47 +00:00
|
|
|
|
if can_handle && Global.current_frame == frame:
|
2019-12-04 15:22:21 +00:00
|
|
|
|
var pixel_color : Color = layers[current_layer_index][0].get_pixelv(mouse_pos)
|
|
|
|
|
if current_mouse_button == "left_mouse":
|
|
|
|
|
Global.left_color_picker.color = pixel_color
|
2019-12-10 23:00:26 +00:00
|
|
|
|
Global.update_left_custom_brush()
|
2019-12-04 15:22:21 +00:00
|
|
|
|
elif current_mouse_button == "right_mouse":
|
|
|
|
|
Global.right_color_picker.color = pixel_color
|
2019-12-10 23:00:26 +00:00
|
|
|
|
Global.update_right_custom_brush()
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-12-19 13:50:41 +00:00
|
|
|
|
if Global.can_draw && Global.has_focus && Input.is_action_just_pressed("shift") && (["Pencil", "Eraser", "LightenDarken"].has(Global.current_left_tool) || ["Pencil", "Eraser", "LightenDarken"].has(Global.current_right_tool)):
|
2019-12-15 06:59:19 +00:00
|
|
|
|
line_2d = Line2D.new()
|
|
|
|
|
line_2d.width = 0.5
|
|
|
|
|
line_2d.default_color = Color.darkgray
|
2019-12-15 12:07:28 +00:00
|
|
|
|
line_2d.add_point(previous_mouse_pos_for_lines)
|
2019-12-15 06:59:19 +00:00
|
|
|
|
line_2d.add_point(mouse_pos)
|
|
|
|
|
add_child(line_2d)
|
|
|
|
|
is_making_line = true
|
|
|
|
|
elif Input.is_action_just_released("shift"):
|
|
|
|
|
is_making_line = false
|
2019-12-15 15:19:58 +00:00
|
|
|
|
if is_instance_valid(line_2d):
|
|
|
|
|
line_2d.queue_free()
|
2019-12-19 13:50:41 +00:00
|
|
|
|
|
2019-12-15 06:59:19 +00:00
|
|
|
|
if is_making_line:
|
2019-12-19 13:50:41 +00:00
|
|
|
|
var point0 : Vector2 = line_2d.points[0]
|
|
|
|
|
var angle := stepify(rad2deg(mouse_pos.angle_to_point(point0)), 0.01)
|
|
|
|
|
if Input.is_action_pressed("ctrl"):
|
|
|
|
|
angle = round(angle / 15) * 15
|
|
|
|
|
var distance : float = point0.distance_to(mouse_pos)
|
|
|
|
|
line_2d.set_point_position(1, point0 + Vector2.RIGHT.rotated(deg2rad(angle)) * distance)
|
|
|
|
|
else:
|
|
|
|
|
line_2d.set_point_position(1, mouse_pos)
|
|
|
|
|
|
2019-12-19 01:15:23 +00:00
|
|
|
|
if angle < 0:
|
|
|
|
|
angle = 360 + angle
|
|
|
|
|
Global.cursor_position_label.text += " %s°" % str(angle)
|
2019-12-15 11:44:53 +00:00
|
|
|
|
|
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
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-09-18 14:47:28 +00:00
|
|
|
|
if start_pos.y > end_pos.y:
|
|
|
|
|
var temp = end_pos.y
|
|
|
|
|
end_pos.y = start_pos.y
|
|
|
|
|
start_pos.y = temp
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-09-18 14:47:28 +00:00
|
|
|
|
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)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-09-18 14:47:28 +00:00
|
|
|
|
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-10-29 21:22:38 +00:00
|
|
|
|
handle_redo("Rectangle Select")
|
|
|
|
|
|
|
|
|
|
previous_action = current_action
|
2019-12-15 12:07:28 +00:00
|
|
|
|
previous_mouse_pos = current_pixel
|
|
|
|
|
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)
|
2019-08-18 09:28:38 +00:00
|
|
|
|
if sprite_changed_this_frame:
|
2019-12-07 17:34:54 +00:00
|
|
|
|
update_texture(current_layer_index, (Input.is_action_just_released("left_mouse") || Input.is_action_just_released("right_mouse")))
|
2019-10-23 21:36:22 +00:00
|
|
|
|
|
2019-10-29 21:22:38 +00:00
|
|
|
|
func handle_undo(action : String) -> void:
|
2019-12-26 19:36:56 +00:00
|
|
|
|
if !can_undo:
|
|
|
|
|
return
|
2019-11-05 16:19:41 +00:00
|
|
|
|
var canvases := []
|
|
|
|
|
var layer_index := -1
|
2019-12-26 19:36:56 +00:00
|
|
|
|
if Global.animation_timer.is_stopped(): # if we're not animating, store only the current canvas
|
2019-11-05 16:19:41 +00:00
|
|
|
|
canvases = [self]
|
|
|
|
|
layer_index = current_layer_index
|
2019-12-26 19:36:56 +00:00
|
|
|
|
else: # If we're animating, store all canvases
|
2019-11-05 16:19:41 +00:00
|
|
|
|
canvases = Global.canvases
|
2019-10-31 23:41:02 +00:00
|
|
|
|
Global.undos += 1
|
2019-10-29 21:22:38 +00:00
|
|
|
|
Global.undo_redo.create_action(action)
|
2019-11-05 16:19:41 +00:00
|
|
|
|
for c in canvases:
|
2019-12-26 19:36:56 +00:00
|
|
|
|
# I'm not sure why I have to unlock it, but...
|
|
|
|
|
# ...if I don't, it doesn't work properly
|
2019-11-05 16:19:41 +00:00
|
|
|
|
c.layers[c.current_layer_index][0].unlock()
|
|
|
|
|
var data = c.layers[c.current_layer_index][0].data
|
|
|
|
|
c.layers[c.current_layer_index][0].lock()
|
|
|
|
|
Global.undo_redo.add_undo_property(c.layers[c.current_layer_index][0], "data", data)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
if action == "Rectangle Select":
|
|
|
|
|
var selected_pixels = Global.selected_pixels.duplicate()
|
|
|
|
|
Global.undo_redo.add_undo_property(Global.selection_rectangle, "polygon", Global.selection_rectangle.polygon)
|
|
|
|
|
Global.undo_redo.add_undo_property(Global, "selected_pixels", selected_pixels)
|
2019-11-05 16:19:41 +00:00
|
|
|
|
Global.undo_redo.add_undo_method(Global, "undo", canvases, layer_index)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-12-26 19:36:56 +00:00
|
|
|
|
can_undo = false
|
|
|
|
|
|
2019-10-29 21:22:38 +00:00
|
|
|
|
func handle_redo(action : String) -> void:
|
2019-10-31 23:41:02 +00:00
|
|
|
|
if Global.undos < Global.undo_redo.get_version():
|
|
|
|
|
return
|
2019-11-05 16:19:41 +00:00
|
|
|
|
var canvases := []
|
|
|
|
|
var layer_index := -1
|
2019-11-19 21:23:43 +00:00
|
|
|
|
if Global.animation_timer.is_stopped():
|
2019-11-05 16:19:41 +00:00
|
|
|
|
canvases = [self]
|
|
|
|
|
layer_index = current_layer_index
|
|
|
|
|
else:
|
|
|
|
|
canvases = Global.canvases
|
|
|
|
|
for c in canvases:
|
|
|
|
|
Global.undo_redo.add_do_property(c.layers[c.current_layer_index][0], "data", c.layers[c.current_layer_index][0].data)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
if action == "Rectangle Select":
|
|
|
|
|
Global.undo_redo.add_do_property(Global.selection_rectangle, "polygon", Global.selection_rectangle.polygon)
|
|
|
|
|
Global.undo_redo.add_do_property(Global, "selected_pixels", Global.selected_pixels)
|
2019-11-05 16:19:41 +00:00
|
|
|
|
Global.undo_redo.add_do_method(Global, "redo", canvases, layer_index)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
Global.undo_redo.commit_action()
|
|
|
|
|
|
2019-12-26 19:36:56 +00:00
|
|
|
|
can_undo = true
|
|
|
|
|
|
2019-12-07 17:34:54 +00:00
|
|
|
|
func update_texture(layer_index : int, update_frame_tex := true) -> void:
|
2019-08-18 09:28:38 +00:00
|
|
|
|
layers[layer_index][1].create_from_image(layers[layer_index][0], 0)
|
2019-10-31 19:34:42 +00:00
|
|
|
|
var layer_container := get_layer_container(layer_index)
|
|
|
|
|
if layer_container:
|
2019-12-08 01:12:34 +00:00
|
|
|
|
layer_container.get_child(1).get_child(0).texture = layers[layer_index][1]
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-12-07 17:34:54 +00:00
|
|
|
|
if update_frame_tex:
|
|
|
|
|
#This code is used to update the texture in the animation timeline frame button
|
|
|
|
|
#but blend_rect causes major performance issues on large images
|
|
|
|
|
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
|
|
|
|
|
2019-11-10 01:25:25 +00:00
|
|
|
|
func frame_changed(value : int) -> void:
|
|
|
|
|
frame = value
|
|
|
|
|
if frame_button:
|
|
|
|
|
frame_button.get_node("FrameButton").frame = frame
|
|
|
|
|
frame_button.get_node("FrameID").text = str(frame + 1)
|
|
|
|
|
|
2019-11-06 16:39:23 +00:00
|
|
|
|
func get_layer_container(layer_index : int) -> LayerContainer:
|
2019-08-18 09:28:38 +00:00
|
|
|
|
for container in Global.vbox_layer_container.get_children():
|
2019-11-06 16:39:23 +00:00
|
|
|
|
if container is LayerContainer && container.i == layer_index:
|
2019-08-18 09:28:38 +00:00
|
|
|
|
return container
|
|
|
|
|
return null
|
|
|
|
|
|
|
|
|
|
func _draw() -> void:
|
2019-12-21 02:56:48 +00:00
|
|
|
|
draw_texture_rect(Global.transparent_background, Rect2(location, size), true) #Draw transparent background
|
2019-09-14 19:55:33 +00:00
|
|
|
|
#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)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-09-14 19:55:33 +00:00
|
|
|
|
#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)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-09-14 19:55:33 +00:00
|
|
|
|
#Draw current frame layers
|
2019-08-18 09:28:38 +00:00
|
|
|
|
for texture in layers:
|
2019-12-24 21:51:08 +00:00
|
|
|
|
var modulate_color := Color(1, 1, 1, texture[4])
|
2019-08-18 09:28:38 +00:00
|
|
|
|
if texture[3]: #if it's visible
|
2019-12-24 21:51:08 +00:00
|
|
|
|
draw_texture(texture[1], location, modulate_color)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-09-18 14:47:28 +00:00
|
|
|
|
if Global.tile_mode:
|
2019-12-24 21:51:08 +00:00
|
|
|
|
draw_texture(texture[1], Vector2(location.x, location.y + size.y), modulate_color) #Down
|
|
|
|
|
draw_texture(texture[1], Vector2(location.x - size.x, location.y + size.y), modulate_color) #Down Left
|
|
|
|
|
draw_texture(texture[1], Vector2(location.x - size.x, location.y), modulate_color) #Left
|
|
|
|
|
draw_texture(texture[1], location - size, modulate_color) #Up left
|
|
|
|
|
draw_texture(texture[1], Vector2(location.x, location.y - size.y), modulate_color) #Up
|
|
|
|
|
draw_texture(texture[1], Vector2(location.x + size.x, location.y - size.y), modulate_color) #Up right
|
|
|
|
|
draw_texture(texture[1], Vector2(location.x + size.x, location.y), modulate_color) #Right
|
|
|
|
|
draw_texture(texture[1], location + size, modulate_color) #Down right
|
2019-10-29 21:22: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-12-07 17:34:54 +00:00
|
|
|
|
for x in range(0, size.x, Global.grid_width):
|
|
|
|
|
draw_line(Vector2(x, location.y), Vector2(x, size.y), Global.grid_color, true)
|
|
|
|
|
for y in range(0, size.y, Global.grid_height):
|
|
|
|
|
draw_line(Vector2(location.x, y), Vector2(size.x, y), Global.grid_color, true)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
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-12-25 18:27:25 +00:00
|
|
|
|
if Global.left_square_indicator_visible && Global.can_draw:
|
|
|
|
|
if Global.current_left_brush_type == Global.BRUSH_TYPES.PIXEL || Global.current_left_tool == "LightenDarken":
|
2019-12-10 23:00:26 +00:00
|
|
|
|
if Global.current_left_tool == "Pencil" || Global.current_left_tool == "Eraser" || Global.current_left_tool == "LightenDarken":
|
|
|
|
|
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)
|
2019-12-25 18:27:25 +00:00
|
|
|
|
elif Global.current_left_brush_type == Global.BRUSH_TYPES.CIRCLE:
|
|
|
|
|
if Global.current_left_tool == "Pencil" || Global.current_left_tool == "Eraser":
|
2019-12-25 19:17:29 +00:00
|
|
|
|
draw_set_transform(mouse_pos, rotation, scale)
|
2019-12-25 18:27:25 +00:00
|
|
|
|
for rect in Global.left_circle_points:
|
|
|
|
|
draw_rect(Rect2(rect, Vector2.ONE), Color.blue, false)
|
2019-12-25 19:17:29 +00:00
|
|
|
|
draw_set_transform(position, rotation, scale)
|
2019-12-03 19:51:13 +00:00
|
|
|
|
else:
|
|
|
|
|
if Global.current_left_tool == "Pencil" || Global.current_left_tool == "Eraser":
|
2019-10-03 16:37:31 +00:00
|
|
|
|
var custom_brush_size = Global.custom_left_brush_image.get_size() - Vector2.ONE
|
|
|
|
|
var dst := rectangle_center(mouse_pos, custom_brush_size)
|
2019-09-27 17:05:24 +00:00
|
|
|
|
draw_texture(Global.custom_left_brush_texture, dst)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-12-25 18:27:25 +00:00
|
|
|
|
if Global.right_square_indicator_visible && Global.can_draw:
|
|
|
|
|
if Global.current_right_brush_type == Global.BRUSH_TYPES.PIXEL || Global.current_right_tool == "LightenDarken":
|
|
|
|
|
if Global.current_right_tool == "Pencil" || Global.current_right_tool == "Eraser" || Global.current_right_tool == "LightenDarken":
|
2019-12-10 23:00:26 +00:00
|
|
|
|
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)
|
2019-12-25 18:27:25 +00:00
|
|
|
|
elif Global.current_right_brush_type == Global.BRUSH_TYPES.CIRCLE:
|
|
|
|
|
if Global.current_right_tool == "Pencil" || Global.current_right_tool == "Eraser":
|
2019-12-25 19:17:29 +00:00
|
|
|
|
draw_set_transform(mouse_pos, rotation, scale)
|
2019-12-25 18:27:25 +00:00
|
|
|
|
for rect in Global.right_circle_points:
|
|
|
|
|
draw_rect(Rect2(rect, Vector2.ONE), Color.red, false)
|
2019-12-25 19:17:29 +00:00
|
|
|
|
draw_set_transform(position, rotation, scale)
|
2019-12-03 19:51:13 +00:00
|
|
|
|
else:
|
|
|
|
|
if Global.current_right_tool == "Pencil" || Global.current_right_tool == "Eraser":
|
2019-10-03 16:37:31 +00:00
|
|
|
|
var custom_brush_size = Global.custom_right_brush_image.get_size() - Vector2.ONE
|
|
|
|
|
var dst := rectangle_center(mouse_pos, custom_brush_size)
|
2019-09-27 17:05:24 +00:00
|
|
|
|
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():
|
2019-11-06 16:39:23 +00:00
|
|
|
|
if child is LayerContainer:
|
2019-08-18 09:28:38 +00:00
|
|
|
|
child.queue_free()
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
|
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-10-29 21:22:38 +00:00
|
|
|
|
|
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-11-06 16:39:23 +00:00
|
|
|
|
if !layers[i][2]:
|
|
|
|
|
layers[i][2] = "Layer %s" % i
|
2019-08-18 09:28:38 +00:00
|
|
|
|
layer_container.i = i
|
2019-12-08 01:12:34 +00:00
|
|
|
|
layer_container.get_child(1).get_child(0).texture = layers[i][1]
|
|
|
|
|
layer_container.get_child(1).get_child(1).text = layers[i][2]
|
|
|
|
|
layer_container.get_child(1).get_child(2).text = layers[i][2]
|
2019-12-24 21:51:08 +00:00
|
|
|
|
layers[i][3] = true # Set visible
|
2019-08-18 09:28:38 +00:00
|
|
|
|
Global.vbox_layer_container.add_child(layer_container)
|
|
|
|
|
|
2019-11-12 00:51:47 +00:00
|
|
|
|
func pencil_and_eraser(mouse_pos : Vector2, color : Color, current_mouse_button : String, current_action := "None") -> void:
|
2019-12-19 13:50:41 +00:00
|
|
|
|
if made_line:
|
|
|
|
|
return
|
2019-12-15 06:59:19 +00:00
|
|
|
|
if is_making_line:
|
2019-12-19 13:50:41 +00:00
|
|
|
|
fill_gaps(line_2d.points[1], previous_mouse_pos_for_lines, color, current_mouse_button, current_action)
|
|
|
|
|
draw_pixel(line_2d.points[1], color, current_mouse_button, current_action)
|
|
|
|
|
made_line = true
|
2019-08-18 09:28:38 +00:00
|
|
|
|
else:
|
2019-12-15 06:59:19 +00:00
|
|
|
|
if point_in_rectangle(mouse_pos, location, location + size):
|
|
|
|
|
mouse_inside_canvas = true
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# Draw
|
2019-12-15 06:59:19 +00:00
|
|
|
|
draw_pixel(mouse_pos, color, current_mouse_button, current_action)
|
2019-12-15 12:07:28 +00:00
|
|
|
|
fill_gaps(mouse_pos, previous_mouse_pos, color, current_mouse_button, current_action) #Fill the gaps
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# If mouse is not inside bounds but it used to be, fill the gaps
|
2019-12-15 06:59:19 +00:00
|
|
|
|
elif point_in_rectangle(previous_mouse_pos, location, location + size):
|
2019-12-15 12:07:28 +00:00
|
|
|
|
fill_gaps(mouse_pos, previous_mouse_pos, color, current_mouse_button, current_action)
|
2019-10-23 21:34:08 +00:00
|
|
|
|
|
2019-11-12 00:51:47 +00:00
|
|
|
|
func draw_pixel(pos : Vector2, color : Color, current_mouse_button : String, current_action := "None") -> void:
|
2019-10-23 21:34:08 +00:00
|
|
|
|
if Global.can_draw && Global.has_focus && Global.current_frame == frame:
|
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-10-23 21:34:08 +00:00
|
|
|
|
var horizontal_mirror := false
|
2019-10-29 21:22:38 +00:00
|
|
|
|
var vertical_mirror := false
|
2019-12-11 00:47:54 +00:00
|
|
|
|
var ld := 0
|
|
|
|
|
var ld_amount := 0.1
|
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-12-26 19:36:56 +00:00
|
|
|
|
if brush_type != Global.BRUSH_TYPES.RANDOM_FILE:
|
|
|
|
|
custom_brush_image = Global.custom_left_brush_image
|
|
|
|
|
else: # Handle random brush
|
|
|
|
|
var brush_button = Global.file_brush_container.get_child(brush_index + 2)
|
|
|
|
|
var random_index = randi() % brush_button.random_brushes.size()
|
2019-12-26 22:00:20 +00:00
|
|
|
|
custom_brush_image = Image.new()
|
|
|
|
|
custom_brush_image.copy_from(brush_button.random_brushes[random_index])
|
2019-12-26 19:36:56 +00:00
|
|
|
|
var custom_brush_size = custom_brush_image.get_size()
|
|
|
|
|
custom_brush_image.resize(custom_brush_size.x * brush_size, custom_brush_size.y * brush_size, Image.INTERPOLATE_NEAREST)
|
|
|
|
|
custom_brush_image = Global.blend_image_with_color(custom_brush_image, color, Global.left_interpolate_spinbox.value / 100)
|
|
|
|
|
custom_brush_image.lock()
|
|
|
|
|
|
2019-10-23 21:34:08 +00:00
|
|
|
|
horizontal_mirror = Global.left_horizontal_mirror
|
|
|
|
|
vertical_mirror = Global.left_vertical_mirror
|
2019-12-11 00:47:54 +00:00
|
|
|
|
ld = Global.left_ld
|
|
|
|
|
ld_amount = Global.left_ld_amount
|
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-12-26 19:36:56 +00:00
|
|
|
|
if brush_type != Global.BRUSH_TYPES.RANDOM_FILE:
|
|
|
|
|
custom_brush_image = Global.custom_right_brush_image
|
|
|
|
|
else: # Handle random brush
|
|
|
|
|
var brush_button = Global.file_brush_container.get_child(brush_index + 2)
|
|
|
|
|
var random_index = randi() % brush_button.random_brushes.size()
|
2019-12-26 22:00:20 +00:00
|
|
|
|
custom_brush_image = Image.new()
|
|
|
|
|
custom_brush_image.copy_from(brush_button.random_brushes[random_index])
|
2019-12-26 19:36:56 +00:00
|
|
|
|
var custom_brush_size = custom_brush_image.get_size()
|
|
|
|
|
custom_brush_image.resize(custom_brush_size.x * brush_size, custom_brush_size.y * brush_size, Image.INTERPOLATE_NEAREST)
|
|
|
|
|
custom_brush_image = Global.blend_image_with_color(custom_brush_image, color, Global.right_interpolate_spinbox.value / 100)
|
|
|
|
|
custom_brush_image.lock()
|
|
|
|
|
|
2019-10-23 21:34:08 +00:00
|
|
|
|
horizontal_mirror = Global.right_horizontal_mirror
|
|
|
|
|
vertical_mirror = Global.right_vertical_mirror
|
2019-12-11 00:47:54 +00:00
|
|
|
|
ld = Global.right_ld
|
|
|
|
|
ld_amount = Global.right_ld_amount
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
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-12-25 18:27:25 +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-10-29 21:22:38 +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
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2019-11-26 23:13:02 +00:00
|
|
|
|
if brush_type == Global.BRUSH_TYPES.PIXEL || current_action == "LightenDarken":
|
|
|
|
|
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)):
|
|
|
|
|
var pos_floored := Vector2(cur_pos_x, cur_pos_y).floor()
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# Don't draw the same pixel over and over and don't re-lighten/darken it
|
2019-12-19 01:38:14 +00:00
|
|
|
|
var current_pixel_color : Color = layers[current_layer_index][0].get_pixel(cur_pos_x, cur_pos_y)
|
|
|
|
|
if current_pixel_color != color && !(pos_floored in lighten_darken_pixels):
|
2019-11-26 23:13:02 +00:00
|
|
|
|
if current_action == "LightenDarken":
|
2019-12-25 18:27:25 +00:00
|
|
|
|
if ld == 0: # Lighten
|
2019-12-19 01:38:14 +00:00
|
|
|
|
color = current_pixel_color.lightened(ld_amount)
|
2019-12-11 00:47:54 +00:00
|
|
|
|
else:
|
2019-12-19 01:38:14 +00:00
|
|
|
|
color = current_pixel_color.darkened(ld_amount)
|
2019-11-26 23:13:02 +00:00
|
|
|
|
lighten_darken_pixels.append(pos_floored)
|
2019-12-11 00:47:54 +00:00
|
|
|
|
|
|
|
|
|
layers[current_layer_index][0].set_pixel(cur_pos_x, cur_pos_y, color)
|
2019-11-26 23:13:02 +00:00
|
|
|
|
sprite_changed_this_frame = true
|
|
|
|
|
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# Handle mirroring
|
2019-11-26 23:13:02 +00:00
|
|
|
|
var mirror_x := east_limit + west_limit - cur_pos_x - 1
|
|
|
|
|
var mirror_y := south_limit + north_limit - cur_pos_y - 1
|
|
|
|
|
if horizontal_mirror:
|
2019-12-19 01:38:14 +00:00
|
|
|
|
current_pixel_color = layers[current_layer_index][0].get_pixel(mirror_x, cur_pos_y)
|
2019-12-25 18:27:25 +00:00
|
|
|
|
if current_pixel_color != color: # don't draw the same pixel over and over
|
2019-12-11 00:47:54 +00:00
|
|
|
|
if current_action == "LightenDarken":
|
2019-12-25 18:27:25 +00:00
|
|
|
|
if ld == 0: # Lighten
|
2019-12-19 01:38:14 +00:00
|
|
|
|
color = current_pixel_color.lightened(ld_amount)
|
2019-12-11 00:47:54 +00:00
|
|
|
|
else:
|
2019-12-19 01:38:14 +00:00
|
|
|
|
color = current_pixel_color.darkened(ld_amount)
|
2019-12-11 00:47:54 +00:00
|
|
|
|
lighten_darken_pixels.append(pos_floored)
|
|
|
|
|
|
2019-11-26 23:13:02 +00:00
|
|
|
|
layers[current_layer_index][0].set_pixel(mirror_x, cur_pos_y, color)
|
|
|
|
|
sprite_changed_this_frame = true
|
|
|
|
|
if vertical_mirror:
|
2019-12-19 01:38:14 +00:00
|
|
|
|
current_pixel_color = layers[current_layer_index][0].get_pixel(cur_pos_x, mirror_y)
|
2019-12-25 18:27:25 +00:00
|
|
|
|
if current_pixel_color != color: # don't draw the same pixel over and over
|
2019-12-11 00:47:54 +00:00
|
|
|
|
if current_action == "LightenDarken":
|
2019-12-25 18:27:25 +00:00
|
|
|
|
if ld == 0: # Lighten
|
2019-12-19 01:38:14 +00:00
|
|
|
|
color = current_pixel_color.lightened(ld_amount)
|
2019-12-11 00:47:54 +00:00
|
|
|
|
else:
|
2019-12-19 01:38:14 +00:00
|
|
|
|
color = current_pixel_color.darkened(ld_amount)
|
2019-12-11 00:47:54 +00:00
|
|
|
|
lighten_darken_pixels.append(pos_floored)
|
|
|
|
|
|
2019-11-26 23:13:02 +00:00
|
|
|
|
layers[current_layer_index][0].set_pixel(cur_pos_x, mirror_y, color)
|
|
|
|
|
sprite_changed_this_frame = true
|
|
|
|
|
if horizontal_mirror && vertical_mirror:
|
2019-12-19 01:38:14 +00:00
|
|
|
|
current_pixel_color = layers[current_layer_index][0].get_pixel(mirror_x, mirror_y)
|
2019-12-25 18:27:25 +00:00
|
|
|
|
if current_pixel_color != color: # don't draw the same pixel over and over
|
2019-12-11 00:47:54 +00:00
|
|
|
|
if current_action == "LightenDarken":
|
2019-12-25 18:27:25 +00:00
|
|
|
|
if ld == 0: # Lighten
|
2019-12-19 01:38:14 +00:00
|
|
|
|
color = current_pixel_color.lightened(ld_amount)
|
2019-12-11 00:47:54 +00:00
|
|
|
|
else:
|
2019-12-19 01:38:14 +00:00
|
|
|
|
color = current_pixel_color.darkened(ld_amount)
|
2019-12-11 00:47:54 +00:00
|
|
|
|
lighten_darken_pixels.append(pos_floored)
|
|
|
|
|
|
2019-11-26 23:13:02 +00:00
|
|
|
|
layers[current_layer_index][0].set_pixel(mirror_x, mirror_y, color)
|
|
|
|
|
sprite_changed_this_frame = true
|
|
|
|
|
|
2019-12-25 18:27:25 +00:00
|
|
|
|
elif brush_type == Global.BRUSH_TYPES.CIRCLE:
|
|
|
|
|
plot_circle(layers[current_layer_index][0], pos.x, pos.y, brush_size, color)
|
|
|
|
|
|
|
|
|
|
# Handle mirroring
|
|
|
|
|
var mirror_x := east_limit + west_limit - pos.x
|
|
|
|
|
var mirror_y := south_limit + north_limit - pos.y
|
|
|
|
|
if horizontal_mirror:
|
|
|
|
|
plot_circle(layers[current_layer_index][0], mirror_x, pos.y, brush_size, color)
|
|
|
|
|
if vertical_mirror:
|
|
|
|
|
plot_circle(layers[current_layer_index][0], pos.x, mirror_y, brush_size, color)
|
|
|
|
|
if horizontal_mirror && vertical_mirror:
|
|
|
|
|
plot_circle(layers[current_layer_index][0], mirror_x, mirror_y, brush_size, color)
|
|
|
|
|
|
|
|
|
|
sprite_changed_this_frame = true
|
|
|
|
|
|
2019-11-26 23:13:02 +00:00
|
|
|
|
else:
|
|
|
|
|
var custom_brush_size := custom_brush_image.get_size() - Vector2.ONE
|
|
|
|
|
pos = pos.floor()
|
|
|
|
|
var dst := rectangle_center(pos, custom_brush_size)
|
|
|
|
|
var src_rect := Rect2(Vector2.ZERO, custom_brush_size + Vector2.ONE)
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# Rectangle with the same size as the brush, but at cursor's position
|
2019-11-26 23:13:02 +00:00
|
|
|
|
var pos_rect := Rect2(dst, custom_brush_size + Vector2.ONE)
|
|
|
|
|
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# The selection rectangle
|
|
|
|
|
# If there's no rectangle, the whole canvas is considered a selection
|
2019-11-26 23:13:02 +00:00
|
|
|
|
var selection_rect := Rect2()
|
|
|
|
|
selection_rect.position = Vector2(west_limit, north_limit)
|
|
|
|
|
selection_rect.end = Vector2(east_limit, south_limit)
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# Intersection of the position rectangle and selection
|
2019-11-26 23:13:02 +00:00
|
|
|
|
var pos_rect_clipped := pos_rect.clip(selection_rect)
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# If the size is 0, that means that the brush wasn't positioned inside the selection
|
2019-11-26 23:13:02 +00:00
|
|
|
|
if pos_rect_clipped.size == Vector2.ZERO:
|
|
|
|
|
return
|
|
|
|
|
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# Re-position src_rect and dst based on the clipped position
|
2019-11-26 23:13:02 +00:00
|
|
|
|
var pos_difference := (pos_rect.position - pos_rect_clipped.position).abs()
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# Obviously, if pos_rect and pos_rect_clipped are the same, pos_difference is Vector2.ZERO
|
2019-11-26 23:13:02 +00:00
|
|
|
|
src_rect.position = pos_difference
|
|
|
|
|
dst += pos_difference
|
|
|
|
|
src_rect.end -= pos_rect.end - pos_rect_clipped.end
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# If the selection rectangle is smaller than the brush, ...
|
|
|
|
|
# ... make sure pixels aren't being drawn outside the selection by adjusting src_rect's size
|
2019-11-26 23:13:02 +00:00
|
|
|
|
src_rect.size.x = min(src_rect.size.x, selection_rect.size.x)
|
|
|
|
|
src_rect.size.y = min(src_rect.size.y, selection_rect.size.y)
|
|
|
|
|
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# Handle mirroring
|
2019-11-26 23:13:02 +00:00
|
|
|
|
var mirror_x := east_limit + west_limit - pos.x - (pos.x - dst.x)
|
|
|
|
|
var mirror_y := south_limit + north_limit - pos.y - (pos.y - dst.y)
|
|
|
|
|
if int(pos_rect_clipped.size.x) % 2 != 0:
|
|
|
|
|
mirror_x -= 1
|
|
|
|
|
if int(pos_rect_clipped.size.y) % 2 != 0:
|
|
|
|
|
mirror_y -= 1
|
2019-12-15 12:07:28 +00:00
|
|
|
|
# Use custom blend function cause of godot's issue #31124
|
2019-12-25 18:27:25 +00:00
|
|
|
|
if color.a > 0: # If it's the pencil
|
2019-12-04 23:38:10 +00:00
|
|
|
|
blend_rect(layers[current_layer_index][0], custom_brush_image, src_rect, dst)
|
2019-11-26 23:13:02 +00:00
|
|
|
|
if horizontal_mirror:
|
2019-12-04 23:38:10 +00:00
|
|
|
|
blend_rect(layers[current_layer_index][0], custom_brush_image, src_rect, Vector2(mirror_x, dst.y))
|
2019-11-26 23:13:02 +00:00
|
|
|
|
if vertical_mirror:
|
2019-12-04 23:38:10 +00:00
|
|
|
|
blend_rect(layers[current_layer_index][0], custom_brush_image, src_rect, Vector2(dst.x, mirror_y))
|
2019-11-26 23:13:02 +00:00
|
|
|
|
if horizontal_mirror && vertical_mirror:
|
2019-12-04 23:38:10 +00:00
|
|
|
|
blend_rect(layers[current_layer_index][0], custom_brush_image, src_rect, Vector2(mirror_x, mirror_y))
|
2019-11-26 23:13:02 +00:00
|
|
|
|
|
2019-12-25 18:27:25 +00:00
|
|
|
|
else: # if it's transparent - if it's the eraser
|
2019-11-26 23:13:02 +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)
|
|
|
|
|
|
|
|
|
|
layers[current_layer_index][0].blit_rect_mask(custom_brush_blended, custom_brush, src_rect, dst)
|
|
|
|
|
if horizontal_mirror:
|
|
|
|
|
layers[current_layer_index][0].blit_rect_mask(custom_brush_blended, custom_brush, src_rect, Vector2(mirror_x, dst.y))
|
|
|
|
|
if vertical_mirror:
|
|
|
|
|
layers[current_layer_index][0].blit_rect_mask(custom_brush_blended, custom_brush, src_rect, Vector2(dst.x, mirror_y))
|
|
|
|
|
if horizontal_mirror && vertical_mirror:
|
|
|
|
|
layers[current_layer_index][0].blit_rect_mask(custom_brush_blended, custom_brush, src_rect, Vector2(mirror_x, mirror_y))
|
|
|
|
|
|
|
|
|
|
layers[current_layer_index][0].lock()
|
|
|
|
|
sprite_changed_this_frame = true
|
2019-12-15 11:44:53 +00:00
|
|
|
|
|
2019-12-19 01:38:14 +00:00
|
|
|
|
previous_mouse_pos_for_lines = pos.floor() + Vector2(0.5, 0.5)
|
2019-12-15 12:07:28 +00:00
|
|
|
|
previous_mouse_pos_for_lines.x = clamp(previous_mouse_pos_for_lines.x, location.x, location.x + size.x)
|
|
|
|
|
previous_mouse_pos_for_lines.y = clamp(previous_mouse_pos_for_lines.y, location.y, location.y + size.y)
|
2019-12-15 06:59:19 +00:00
|
|
|
|
if is_making_line:
|
2019-12-15 12:07:28 +00:00
|
|
|
|
line_2d.set_point_position(0, previous_mouse_pos_for_lines)
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# Bresenham's Algorithm
|
|
|
|
|
# Thanks to https://godotengine.org/qa/35276/tile-based-line-drawing-algorithm-efficiency
|
2019-12-15 12:07:28 +00:00
|
|
|
|
func fill_gaps(mouse_pos : Vector2, prev_mouse_pos : Vector2, color : Color, current_mouse_button : String, current_action := "None") -> void:
|
|
|
|
|
var previous_mouse_pos_floored = prev_mouse_pos.floor()
|
2019-08-18 09:28:38 +00:00
|
|
|
|
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-11-12 00:51:47 +00:00
|
|
|
|
draw_pixel(Vector2(x, y), color, current_mouse_button, current_action)
|
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
|
|
|
|
|
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# Thanks to https://en.wikipedia.org/wiki/Flood_fill
|
2019-08-18 09:28:38 +00:00
|
|
|
|
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-10-29 21:22:38 +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-10-29 21:22:38 +00:00
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
|
var q = [pos]
|
|
|
|
|
for n in q:
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# If the difference in colors is very small, break the loop (thanks @azagaya on GitHub!)
|
2019-11-30 12:26:58 +00:00
|
|
|
|
if target_color == replace_color:
|
2019-11-30 01:22:56 +00:00
|
|
|
|
break
|
2019-08-18 09:28:38 +00:00
|
|
|
|
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-12-25 18:27:25 +00:00
|
|
|
|
# Draw
|
2019-09-25 19:59:48 +00:00
|
|
|
|
layers[current_layer_index][0].set_pixelv(p, replace_color)
|
2019-11-30 12:26:58 +00:00
|
|
|
|
replace_color = layers[current_layer_index][0].get_pixelv(p)
|
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-11-30 00:45:45 +00:00
|
|
|
|
sprite_changed_this_frame = true
|
2019-09-25 19:59:48 +00:00
|
|
|
|
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# Algorithm based on http://members.chello.at/easyfilter/bresenham.html
|
|
|
|
|
func plot_circle(sprite : Image, xm : int, ym : int, r : int, color : Color) -> void:
|
|
|
|
|
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 = 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)
|
|
|
|
|
|
|
|
|
|
var x := -r
|
|
|
|
|
var y := 0
|
|
|
|
|
var err := 2 - r * 2 # II. Quadrant
|
|
|
|
|
while x < 0:
|
|
|
|
|
var quadrant_1 := Vector2(xm - x, ym + y)
|
|
|
|
|
var quadrant_2 := Vector2(xm - y, ym - x)
|
|
|
|
|
var quadrant_3 := Vector2(xm + x, ym - y)
|
|
|
|
|
var quadrant_4 := Vector2(xm + y, ym + x)
|
|
|
|
|
if point_in_rectangle(quadrant_1, Vector2(west_limit - 1, north_limit - 1), Vector2(east_limit, south_limit)):
|
|
|
|
|
sprite.set_pixelv(quadrant_1, color)
|
|
|
|
|
if point_in_rectangle(quadrant_2, Vector2(west_limit - 1, north_limit - 1), Vector2(east_limit, south_limit)):
|
|
|
|
|
sprite.set_pixelv(quadrant_2, color)
|
|
|
|
|
if point_in_rectangle(quadrant_3, Vector2(west_limit - 1, north_limit - 1), Vector2(east_limit, south_limit)):
|
|
|
|
|
sprite.set_pixelv(quadrant_3, color)
|
|
|
|
|
if point_in_rectangle(quadrant_4, Vector2(west_limit - 1, north_limit - 1), Vector2(east_limit, south_limit)):
|
|
|
|
|
sprite.set_pixelv(quadrant_4, color)
|
|
|
|
|
r = err
|
|
|
|
|
if r <= y:
|
|
|
|
|
y += 1
|
|
|
|
|
err += y * 2 + 1
|
|
|
|
|
if r > x || err > y:
|
|
|
|
|
x += 1
|
|
|
|
|
err += x * 2 + 1
|
|
|
|
|
|
|
|
|
|
# Checks if a point is inside a rectangle
|
2019-09-25 19:59:48 +00:00
|
|
|
|
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-12-25 18:27:25 +00:00
|
|
|
|
# Returns the position in the middle of a rectangle
|
2019-11-20 12:42:52 +00:00
|
|
|
|
func rectangle_center(rect_position : Vector2, rect_size : Vector2) -> Vector2:
|
|
|
|
|
return (rect_position - rect_size / 2).floor()
|
2019-10-03 16:37:31 +00:00
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
|
func _on_Timer_timeout() -> void:
|
2019-10-23 21:36:22 +00:00
|
|
|
|
Global.can_draw = true
|
2019-12-03 15:53:42 +00:00
|
|
|
|
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# Custom blend rect function, needed because Godot's issue #31124
|
2019-12-04 23:38:10 +00:00
|
|
|
|
func blend_rect(bg : Image, brush : Image, src_rect : Rect2, dst : Vector2) -> void:
|
2019-12-03 15:53:42 +00:00
|
|
|
|
var brush_size := brush.get_size()
|
2019-12-04 23:38:10 +00:00
|
|
|
|
var clipped_src_rect := Rect2(Vector2.ZERO, brush_size).clip(src_rect)
|
|
|
|
|
if clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0:
|
|
|
|
|
return
|
|
|
|
|
var src_underscan := Vector2(min(0, src_rect.position.x), min(0, src_rect.position.y))
|
|
|
|
|
var dest_rect := Rect2(0, 0, bg.get_width(), bg.get_height()).clip(Rect2(dst - src_underscan, clipped_src_rect.size))
|
|
|
|
|
|
|
|
|
|
for x in range(0, dest_rect.size.x):
|
|
|
|
|
for y in range(0, dest_rect.size.y):
|
|
|
|
|
var src_x := clipped_src_rect.position.x + x;
|
|
|
|
|
var src_y := clipped_src_rect.position.y + y;
|
|
|
|
|
|
|
|
|
|
var dst_x := dest_rect.position.x + x;
|
|
|
|
|
var dst_y := dest_rect.position.y + y;
|
|
|
|
|
|
2019-12-03 15:53:42 +00:00
|
|
|
|
var out_color := Color()
|
2019-12-04 23:38:10 +00:00
|
|
|
|
var brush_color := brush.get_pixel(src_x, src_y)
|
|
|
|
|
var bg_color := bg.get_pixel(dst_x, dst_y)
|
2019-12-03 15:53:42 +00:00
|
|
|
|
out_color.a = brush_color.a + bg_color.a * (1 - brush_color.a)
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# Blend the colors
|
2019-12-03 15:53:42 +00:00
|
|
|
|
if out_color.a != 0:
|
|
|
|
|
out_color.r = (brush_color.r * brush_color.a + bg_color.r * bg_color.a * (1 - brush_color.a)) / out_color.a
|
|
|
|
|
out_color.g = (brush_color.g * brush_color.a + bg_color.g * bg_color.a * (1 - brush_color.a)) / out_color.a
|
|
|
|
|
out_color.b = (brush_color.b * brush_color.a + bg_color.b * bg_color.a * (1 - brush_color.a)) / out_color.a
|
2019-12-04 23:38:10 +00:00
|
|
|
|
bg.set_pixel(dst_x, dst_y, out_color)
|