2019-08-18 09:28:38 +00:00
|
|
|
|
class_name Canvas
|
2020-02-10 22:06:24 +00:00
|
|
|
|
extends Node2D
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
2020-05-01 17:47:10 +00:00
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
|
var layers := []
|
|
|
|
|
var current_layer_index := 0
|
|
|
|
|
var location := Vector2.ZERO
|
|
|
|
|
var size := Vector2(64, 64)
|
2020-01-10 19:47:44 +00:00
|
|
|
|
var fill_color := Color(0, 0, 0, 0)
|
2020-03-08 20:03:31 +00:00
|
|
|
|
var frame := 0
|
2019-12-28 13:14:54 +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
|
2020-05-31 13:04:33 +00:00
|
|
|
|
var previous_action := -1
|
2019-12-28 13:14:54 +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
|
|
|
|
|
var mouse_inside_canvas := false # used for undo
|
|
|
|
|
var sprite_changed_this_frame := false # for optimization purposes
|
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
|
2020-05-31 15:46:47 +00:00
|
|
|
|
var is_making_selection := -1
|
2019-08-18 09:28:38 +00:00
|
|
|
|
var line_2d : Line2D
|
2020-02-08 23:34:37 +00:00
|
|
|
|
var pen_pressure := 1.0 # For tablet pressure sensitivity
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
2020-04-27 15:09:54 +00:00
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
|
func _ready() -> void:
|
2020-01-18 19:06:47 +00:00
|
|
|
|
var fill_layers := layers.empty()
|
2020-05-09 00:51:23 +00:00
|
|
|
|
var layer_i := 0
|
2020-03-03 01:05:48 +00:00
|
|
|
|
for l in Global.layers:
|
2020-01-18 19:06:47 +00:00
|
|
|
|
if fill_layers:
|
|
|
|
|
# The sprite itself
|
|
|
|
|
var sprite := Image.new()
|
|
|
|
|
if Global.is_default_image:
|
|
|
|
|
if Global.config_cache.has_section_key("preferences", "default_width"):
|
|
|
|
|
size.x = Global.config_cache.get_value("preferences", "default_width")
|
|
|
|
|
if Global.config_cache.has_section_key("preferences", "default_height"):
|
|
|
|
|
size.y = Global.config_cache.get_value("preferences", "default_height")
|
|
|
|
|
if Global.config_cache.has_section_key("preferences", "default_fill_color"):
|
|
|
|
|
fill_color = Global.config_cache.get_value("preferences", "default_fill_color")
|
|
|
|
|
Global.is_default_image = !Global.is_default_image
|
|
|
|
|
|
|
|
|
|
sprite.create(size.x, size.y, false, Image.FORMAT_RGBA8)
|
|
|
|
|
sprite.fill(fill_color)
|
|
|
|
|
sprite.lock()
|
|
|
|
|
|
|
|
|
|
var tex := ImageTexture.new()
|
|
|
|
|
tex.create_from_image(sprite, 0)
|
|
|
|
|
|
2020-02-29 22:57:47 +00:00
|
|
|
|
# Store [Image, ImageTexture, Opacity]
|
2020-01-18 19:06:47 +00:00
|
|
|
|
layers.append([sprite, tex, 1])
|
|
|
|
|
|
2020-03-18 01:24:15 +00:00
|
|
|
|
if self in l[5]:
|
2020-04-21 18:34:45 +00:00
|
|
|
|
# If the linked button is pressed, set as the Image & ImageTexture
|
|
|
|
|
# to be the same as the first linked cel
|
2020-05-09 00:51:23 +00:00
|
|
|
|
layers[layer_i][0] = l[5][0].layers[layer_i][0]
|
|
|
|
|
layers[layer_i][1] = l[5][0].layers[layer_i][1]
|
|
|
|
|
|
|
|
|
|
layer_i += 1
|
2020-03-18 01:24:15 +00:00
|
|
|
|
|
2019-12-30 20:05:09 +00:00
|
|
|
|
# Only handle camera zoom settings & offset on the first frame
|
2019-12-08 23:39:59 +00:00
|
|
|
|
if Global.canvases[0] == self:
|
|
|
|
|
camera_zoom()
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
2020-01-15 20:31:02 +00:00
|
|
|
|
line_2d = Line2D.new()
|
|
|
|
|
line_2d.width = 0.5
|
|
|
|
|
line_2d.default_color = Color.darkgray
|
|
|
|
|
line_2d.add_point(previous_mouse_pos_for_lines)
|
|
|
|
|
line_2d.add_point(previous_mouse_pos_for_lines)
|
|
|
|
|
add_child(line_2d)
|
|
|
|
|
|
2020-05-01 17:47:10 +00:00
|
|
|
|
|
2020-02-10 22:06:24 +00:00
|
|
|
|
func _draw() -> void:
|
2020-01-18 19:06:47 +00:00
|
|
|
|
# Onion Skinning
|
2020-03-26 18:56:30 +00:00
|
|
|
|
if Global.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:
|
|
|
|
|
var layer_i := 0
|
|
|
|
|
for layer in Global.canvases[Global.current_frame - i].layers:
|
|
|
|
|
if Global.layers[layer_i][1]: # If it's visible
|
|
|
|
|
color.a = 0.6 / i
|
|
|
|
|
draw_texture(layer[1], location, color)
|
|
|
|
|
layer_i += 1
|
2019-12-08 22:17:05 +00:00
|
|
|
|
|
2020-03-26 18:56:30 +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:
|
|
|
|
|
var layer_i := 0
|
|
|
|
|
for layer in Global.canvases[Global.current_frame + i].layers:
|
|
|
|
|
if Global.layers[layer_i][1]: # If it's visible
|
|
|
|
|
color.a = 0.6 / i
|
|
|
|
|
draw_texture(layer[1], location, color)
|
|
|
|
|
layer_i += 1
|
2020-02-10 22:06:24 +00:00
|
|
|
|
|
2020-01-18 19:06:47 +00:00
|
|
|
|
# Draw current frame layers
|
|
|
|
|
for i in range(layers.size()):
|
|
|
|
|
var modulate_color := Color(1, 1, 1, layers[i][2])
|
|
|
|
|
if Global.layers[i][1]: # if it's visible
|
|
|
|
|
draw_texture(layers[i][1], location, modulate_color)
|
2020-02-10 22:06:24 +00:00
|
|
|
|
|
|
|
|
|
if Global.tile_mode:
|
2020-03-09 00:17:49 +00:00
|
|
|
|
draw_texture(layers[i][1], Vector2(location.x, location.y + size.y), modulate_color) # Down
|
|
|
|
|
draw_texture(layers[i][1], Vector2(location.x - size.x, location.y + size.y), modulate_color) # Down Left
|
|
|
|
|
draw_texture(layers[i][1], Vector2(location.x - size.x, location.y), modulate_color) # Left
|
|
|
|
|
draw_texture(layers[i][1], location - size, modulate_color) # Up left
|
|
|
|
|
draw_texture(layers[i][1], Vector2(location.x, location.y - size.y), modulate_color) # Up
|
|
|
|
|
draw_texture(layers[i][1], Vector2(location.x + size.x, location.y - size.y), modulate_color) # Up right
|
|
|
|
|
draw_texture(layers[i][1], Vector2(location.x + size.x, location.y), modulate_color) # Right
|
|
|
|
|
draw_texture(layers[i][1], location + size, modulate_color) # Down right
|
2020-01-18 19:06:47 +00:00
|
|
|
|
|
|
|
|
|
# Idea taken from flurick (on GitHub)
|
2020-02-10 22:06:24 +00:00
|
|
|
|
if Global.draw_grid:
|
2020-03-12 02:17:48 +00:00
|
|
|
|
if Global.grid_type == Global.Grid_Types.CARTESIAN || Global.grid_type == Global.Grid_Types.ALL:
|
|
|
|
|
for x in range(Global.grid_width, size.x, Global.grid_width):
|
|
|
|
|
draw_line(Vector2(x, location.y), Vector2(x, size.y), Global.grid_color, true)
|
|
|
|
|
|
|
|
|
|
for y in range(Global.grid_height, size.y, Global.grid_height):
|
|
|
|
|
draw_line(Vector2(location.x, y), Vector2(size.x, y), Global.grid_color, true)
|
|
|
|
|
|
|
|
|
|
if Global.grid_type == Global.Grid_Types.ISOMETRIC || Global.grid_type == Global.Grid_Types.ALL:
|
|
|
|
|
var prev_x := 0
|
|
|
|
|
var prev_y := 0
|
|
|
|
|
for y in range(0, size.y + 1, Global.grid_width):
|
|
|
|
|
var yy1 = y + size.y * tan(deg2rad(26.565)) # 30 degrees
|
|
|
|
|
if yy1 <= (size.y + 0.01):
|
|
|
|
|
draw_line(Vector2(location.x, y), Vector2(size.x, yy1),Global.grid_color)
|
|
|
|
|
else:
|
|
|
|
|
var xx1 = (size.x - y) * tan(deg2rad(90 - 26.565)) # 60 degrees
|
|
|
|
|
draw_line(Vector2(location.x, y), Vector2(xx1, size.y), Global.grid_color)
|
|
|
|
|
for y in range(0, size.y + 1, Global.grid_height):
|
|
|
|
|
var xx2 = y * tan(deg2rad(90 - 26.565)) # 60 degrees
|
|
|
|
|
if xx2 <= (size.x + 0.01):
|
|
|
|
|
draw_line(Vector2(location.x, y), Vector2(xx2, location.y), Global.grid_color)
|
|
|
|
|
prev_y = location.y
|
|
|
|
|
else:
|
|
|
|
|
var distance = (xx2 - prev_x) / 2
|
|
|
|
|
#var yy2 = (size.y - y) * tan(deg2rad(26.565)) # 30 degrees
|
|
|
|
|
var yy2 = prev_y + distance
|
|
|
|
|
draw_line(Vector2(location.x, y), Vector2(size.x, yy2), Global.grid_color)
|
|
|
|
|
prev_y = yy2
|
|
|
|
|
|
|
|
|
|
prev_x = xx2
|
|
|
|
|
|
|
|
|
|
for x in range(0, size.x, Global.grid_width * 2):
|
|
|
|
|
if x == 0:
|
|
|
|
|
continue
|
|
|
|
|
var yy1 = (size.x - x) * tan(deg2rad(26.565)) # 30 degrees
|
|
|
|
|
draw_line(Vector2(x, location.y), Vector2(size.x, yy1), Global.grid_color)
|
|
|
|
|
for x in range(0, size.x, Global.grid_height * 2):
|
|
|
|
|
var yy2 = (size.x - x) * tan(deg2rad(26.565)) # 30 degrees
|
|
|
|
|
draw_line(Vector2(x, size.y), Vector2(size.x, size.y - yy2), Global.grid_color)
|
2020-02-10 22:06:24 +00:00
|
|
|
|
|
2020-01-18 19:06:47 +00:00
|
|
|
|
# Draw rectangle to indicate the pixel currently being hovered on
|
2020-02-10 22:06:24 +00:00
|
|
|
|
var mouse_pos := current_pixel
|
2020-05-30 22:07:08 +00:00
|
|
|
|
mouse_pos = mouse_pos.floor()
|
|
|
|
|
if Global.left_square_indicator_visible && Global.can_draw:
|
2020-05-31 15:40:47 +00:00
|
|
|
|
if Global.current_brush_types[0] == Global.Brush_Types.PIXEL || Global.current_left_tool == Global.Tools.LIGHTENDARKEN:
|
2020-05-31 13:04:33 +00:00
|
|
|
|
if Global.current_left_tool == Global.Tools.PENCIL || Global.current_left_tool == Global.Tools.ERASER || Global.current_left_tool == Global.Tools.LIGHTENDARKEN:
|
2020-05-30 22:07:08 +00:00
|
|
|
|
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)
|
2020-05-31 15:40:47 +00:00
|
|
|
|
elif Global.current_brush_types[0] == Global.Brush_Types.CIRCLE || Global.current_brush_types[0] == Global.Brush_Types.FILLED_CIRCLE:
|
2020-05-31 13:04:33 +00:00
|
|
|
|
if Global.current_left_tool == Global.Tools.PENCIL || Global.current_left_tool == Global.Tools.ERASER:
|
2020-05-30 22:07:08 +00:00
|
|
|
|
draw_set_transform(mouse_pos, rotation, scale)
|
|
|
|
|
for rect in Global.left_circle_points:
|
|
|
|
|
draw_rect(Rect2(rect, Vector2.ONE), Color.blue, false)
|
|
|
|
|
draw_set_transform(position, rotation, scale)
|
|
|
|
|
else:
|
2020-05-31 13:04:33 +00:00
|
|
|
|
if Global.current_left_tool == Global.Tools.PENCIL || Global.current_left_tool == Global.Tools.ERASER:
|
2020-05-31 15:40:47 +00:00
|
|
|
|
var custom_brush_size = Global.custom_brush_images[0].get_size() - Vector2.ONE
|
2020-05-30 22:07:08 +00:00
|
|
|
|
var dst := rectangle_center(mouse_pos, custom_brush_size)
|
2020-05-31 15:40:47 +00:00
|
|
|
|
draw_texture(Global.custom_brush_textures[0], dst)
|
2020-05-30 22:07:08 +00:00
|
|
|
|
|
|
|
|
|
if Global.right_square_indicator_visible && Global.can_draw:
|
2020-05-31 15:40:47 +00:00
|
|
|
|
if Global.current_brush_types[1] == Global.Brush_Types.PIXEL || Global.current_right_tool == Global.Tools.LIGHTENDARKEN:
|
2020-05-31 13:04:33 +00:00
|
|
|
|
if Global.current_right_tool == Global.Tools.PENCIL || Global.current_right_tool == Global.Tools.ERASER || Global.current_right_tool == Global.Tools.LIGHTENDARKEN:
|
2020-05-30 22:07:08 +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)
|
2020-05-31 15:40:47 +00:00
|
|
|
|
elif Global.current_brush_types[1] == Global.Brush_Types.CIRCLE || Global.current_brush_types[1] == Global.Brush_Types.FILLED_CIRCLE:
|
2020-05-31 13:04:33 +00:00
|
|
|
|
if Global.current_right_tool == Global.Tools.PENCIL || Global.current_right_tool == Global.Tools.ERASER:
|
2020-05-30 22:07:08 +00:00
|
|
|
|
draw_set_transform(mouse_pos, rotation, scale)
|
|
|
|
|
for rect in Global.right_circle_points:
|
|
|
|
|
draw_rect(Rect2(rect, Vector2.ONE), Color.red, false)
|
|
|
|
|
draw_set_transform(position, rotation, scale)
|
|
|
|
|
else:
|
2020-05-31 13:04:33 +00:00
|
|
|
|
if Global.current_right_tool == Global.Tools.PENCIL || Global.current_right_tool == Global.Tools.ERASER:
|
2020-05-31 15:40:47 +00:00
|
|
|
|
var custom_brush_size = Global.custom_brush_images[1].get_size() - Vector2.ONE
|
2020-05-30 22:07:08 +00:00
|
|
|
|
var dst := rectangle_center(mouse_pos, custom_brush_size)
|
2020-05-31 15:40:47 +00:00
|
|
|
|
draw_texture(Global.custom_brush_textures[1], dst)
|
2019-09-25 19:59:48 +00:00
|
|
|
|
|
2019-11-19 21:23:43 +00:00
|
|
|
|
|
2019-12-27 15:12:19 +00:00
|
|
|
|
func _input(event : InputEvent) -> void:
|
2019-12-27 15:38:43 +00:00
|
|
|
|
# Don't process anything below if the input isn't a mouse event, or Shift/Ctrl.
|
2019-12-27 15:12:19 +00:00
|
|
|
|
# This decreases CPU/GPU usage slightly.
|
|
|
|
|
if not event is InputEventMouse:
|
2019-12-27 15:38:43 +00:00
|
|
|
|
if event is InputEventKey:
|
|
|
|
|
if event.scancode != KEY_SHIFT && event.scancode != KEY_CONTROL:
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
return
|
2019-12-27 15:12:19 +00:00
|
|
|
|
|
2020-02-11 16:42:23 +00:00
|
|
|
|
if (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")):
|
|
|
|
|
made_line = false
|
2020-05-29 00:16:44 +00:00
|
|
|
|
DrawingAlgos.mouse_press_pixels.clear()
|
|
|
|
|
DrawingAlgos.mouse_press_pressure_values.clear()
|
2020-05-29 21:46:58 +00:00
|
|
|
|
DrawingAlgos.pixel_perfect_drawer.reset()
|
|
|
|
|
DrawingAlgos.pixel_perfect_drawer_h_mirror.reset()
|
|
|
|
|
DrawingAlgos.pixel_perfect_drawer_v_mirror.reset()
|
|
|
|
|
DrawingAlgos.pixel_perfect_drawer_hv_mirror.reset()
|
2020-02-11 18:05:37 +00:00
|
|
|
|
can_undo = true
|
2020-02-11 16:42:23 +00:00
|
|
|
|
|
2020-01-09 18:49:27 +00:00
|
|
|
|
current_pixel = get_local_mouse_position() + location
|
2020-03-09 01:26:13 +00:00
|
|
|
|
if Global.current_frame != frame || Global.layers[Global.current_layer][2]:
|
2020-01-15 23:38:44 +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)
|
|
|
|
|
return
|
|
|
|
|
|
2020-01-15 20:47:56 +00:00
|
|
|
|
if Global.has_focus:
|
2019-12-26 19:36:56 +00:00
|
|
|
|
update()
|
2019-12-27 15:38:43 +00:00
|
|
|
|
|
2020-02-08 23:34:37 +00:00
|
|
|
|
# Godot 3.2 and above only code
|
|
|
|
|
if Engine.get_version_info().major == 3 && Engine.get_version_info().minor >= 2:
|
|
|
|
|
if event is InputEventMouseMotion:
|
|
|
|
|
pen_pressure = event.pressure
|
2020-05-09 18:38:28 +00:00
|
|
|
|
|
|
|
|
|
# To be removed once Godot 3.2.2 is out of beta
|
2020-02-14 16:05:53 +00:00
|
|
|
|
if event.pressure == 0.0: # Drawing with mouse
|
|
|
|
|
pen_pressure = 1 # This causes problems with tablets though
|
2020-02-08 23:34:37 +00:00
|
|
|
|
|
2019-12-27 15:38:43 +00:00
|
|
|
|
sprite_changed_this_frame = false
|
2020-01-18 19:06:47 +00:00
|
|
|
|
var sprite : Image = layers[Global.current_layer][0]
|
2019-11-20 22:11:21 +00:00
|
|
|
|
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()
|
2020-05-31 15:46:47 +00:00
|
|
|
|
var current_mouse_button := -1
|
2020-05-31 13:04:33 +00:00
|
|
|
|
var current_action := -1
|
2019-12-28 13:14:54 +00:00
|
|
|
|
var current_color : Color
|
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
|
2020-01-10 20:44:29 +00:00
|
|
|
|
var color_picker_for := 0
|
2020-04-13 02:07:52 +00:00
|
|
|
|
var zoom_mode := 0
|
2019-12-27 15:38:43 +00:00
|
|
|
|
|
2019-12-28 17:03:45 +00:00
|
|
|
|
west_limit = location.x
|
|
|
|
|
east_limit = location.x + size.x
|
|
|
|
|
north_limit = location.y
|
|
|
|
|
south_limit = location.y + size.y
|
2019-12-28 13:14:54 +00:00
|
|
|
|
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-08-18 09:28:38 +00:00
|
|
|
|
if Input.is_mouse_button_pressed(BUTTON_LEFT):
|
2020-05-31 15:46:47 +00:00
|
|
|
|
current_mouse_button = Global.Mouse_Button.LEFT
|
2019-08-18 09:28:38 +00:00
|
|
|
|
current_action = Global.current_left_tool
|
2020-05-31 15:03:44 +00:00
|
|
|
|
current_color = Global.color_pickers[0].color
|
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
|
2020-01-10 20:44:29 +00:00
|
|
|
|
color_picker_for = Global.left_color_picker_for
|
2020-04-13 02:07:52 +00:00
|
|
|
|
zoom_mode = Global.left_zoom_mode
|
|
|
|
|
|
2019-08-18 09:28:38 +00:00
|
|
|
|
elif Input.is_mouse_button_pressed(BUTTON_RIGHT):
|
2020-05-31 15:46:47 +00:00
|
|
|
|
current_mouse_button = Global.Mouse_Button.RIGHT
|
2019-08-18 09:28:38 +00:00
|
|
|
|
current_action = Global.current_right_tool
|
2020-05-31 15:03:44 +00:00
|
|
|
|
current_color = Global.color_pickers[1].color
|
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
|
2020-01-10 20:44:29 +00:00
|
|
|
|
color_picker_for = Global.right_color_picker_for
|
2020-04-13 02:07:52 +00:00
|
|
|
|
zoom_mode = Global.right_zoom_mode
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2020-05-30 22:07:08 +00:00
|
|
|
|
if Global.has_focus:
|
2020-01-15 20:47:56 +00:00
|
|
|
|
Global.cursor_position_label.text = "[%s×%s] %s, %s" % [size.x, size.y, mouse_pos_floored.x, mouse_pos_floored.y]
|
|
|
|
|
if !cursor_inside_canvas:
|
|
|
|
|
cursor_inside_canvas = true
|
2020-02-29 22:58:50 +00:00
|
|
|
|
if Global.cursor_image.get_data().get_size() != Vector2.ZERO:
|
|
|
|
|
Input.set_custom_mouse_cursor(Global.cursor_image, 0, Vector2(15, 15))
|
2020-01-15 20:47:56 +00:00
|
|
|
|
if Global.show_left_tool_icon:
|
|
|
|
|
Global.left_cursor.visible = true
|
|
|
|
|
if Global.show_right_tool_icon:
|
|
|
|
|
Global.right_cursor.visible = true
|
|
|
|
|
else:
|
|
|
|
|
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 = "[%s×%s]" % [size.x, size.y]
|
|
|
|
|
if cursor_inside_canvas:
|
|
|
|
|
cursor_inside_canvas = false
|
|
|
|
|
Global.left_cursor.visible = false
|
|
|
|
|
Global.right_cursor.visible = false
|
|
|
|
|
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
|
2020-05-30 22:07:08 +00:00
|
|
|
|
var can_handle : bool = 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"))
|
|
|
|
|
|
|
|
|
|
if mouse_pressed:
|
2020-01-15 20:47:56 +00:00
|
|
|
|
if can_handle || is_making_line:
|
2020-05-31 13:04:33 +00:00
|
|
|
|
if current_action != -1 && current_action != Global.Tools.COLORPICKER && current_action != Global.Tools.ZOOM:
|
|
|
|
|
if current_action == Global.Tools.RECTSELECT:
|
2019-10-29 21:22:38 +00:00
|
|
|
|
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")):
|
2020-01-15 20:47:56 +00:00
|
|
|
|
if can_handle || Global.undos == Global.undo_redo.get_version():
|
2020-05-31 13:04:33 +00:00
|
|
|
|
if previous_action != -1 && previous_action != Global.Tools.RECTSELECT && current_action != Global.Tools.COLORPICKER && current_action != Global.Tools.ZOOM:
|
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
|
2020-05-31 13:04:33 +00:00
|
|
|
|
Global.Tools.PENCIL:
|
2020-01-18 19:06:47 +00:00
|
|
|
|
pencil_and_eraser(sprite, mouse_pos, current_color, current_mouse_button, current_action)
|
2020-05-31 13:04:33 +00:00
|
|
|
|
Global.Tools.ERASER:
|
2020-01-18 19:06:47 +00:00
|
|
|
|
pencil_and_eraser(sprite, mouse_pos, Color(0, 0, 0, 0), current_mouse_button, current_action)
|
2020-05-31 13:04:33 +00:00
|
|
|
|
Global.Tools.BUCKET:
|
2020-01-15 20:47:56 +00:00
|
|
|
|
if can_handle:
|
2020-04-24 21:42:02 +00:00
|
|
|
|
var fill_with := 0
|
|
|
|
|
var pattern_image : Image
|
2020-04-24 23:09:34 +00:00
|
|
|
|
var pattern_offset : Vector2
|
2020-05-31 15:46:47 +00:00
|
|
|
|
if current_mouse_button == Global.Mouse_Button.LEFT:
|
2020-04-24 21:42:02 +00:00
|
|
|
|
fill_with = Global.left_fill_with
|
2020-05-31 15:40:47 +00:00
|
|
|
|
pattern_image = Global.pattern_images[0]
|
2020-04-24 23:09:34 +00:00
|
|
|
|
pattern_offset = Global.left_fill_pattern_offset
|
2020-05-31 15:46:47 +00:00
|
|
|
|
elif current_mouse_button == Global.Mouse_Button.RIGHT:
|
2020-04-24 21:42:02 +00:00
|
|
|
|
fill_with = Global.right_fill_with
|
2020-05-31 15:40:47 +00:00
|
|
|
|
pattern_image = Global.pattern_images[1]
|
2020-04-24 23:09:34 +00:00
|
|
|
|
pattern_offset = Global.right_fill_pattern_offset
|
2020-03-28 03:15:09 +00:00
|
|
|
|
|
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 horizontal_mirror := false
|
|
|
|
|
var vertical_mirror := false
|
2019-12-28 13:14:54 +00:00
|
|
|
|
var mirror_x := east_limit + west_limit - mouse_pos_floored.x - 1
|
|
|
|
|
var mirror_y := south_limit + north_limit - mouse_pos_floored.y - 1
|
2020-05-31 15:46:47 +00:00
|
|
|
|
if current_mouse_button == Global.Mouse_Button.LEFT:
|
2019-12-03 22:14:14 +00:00
|
|
|
|
horizontal_mirror = Global.left_horizontal_mirror
|
|
|
|
|
vertical_mirror = Global.left_vertical_mirror
|
2020-05-31 15:46:47 +00:00
|
|
|
|
elif current_mouse_button == Global.Mouse_Button.RIGHT:
|
2019-12-03 22:14:14 +00:00
|
|
|
|
horizontal_mirror = Global.right_horizontal_mirror
|
|
|
|
|
vertical_mirror = Global.right_vertical_mirror
|
|
|
|
|
|
2020-04-25 12:00:38 +00:00
|
|
|
|
if fill_with == 1 && pattern_image: # Pattern fill
|
2020-05-29 00:16:44 +00:00
|
|
|
|
DrawingAlgos.pattern_fill(sprite, mouse_pos, pattern_image, sprite.get_pixelv(mouse_pos), pattern_offset)
|
2020-03-28 03:15:09 +00:00
|
|
|
|
if horizontal_mirror:
|
|
|
|
|
var pos := Vector2(mirror_x, mouse_pos.y)
|
2020-05-29 00:16:44 +00:00
|
|
|
|
DrawingAlgos.pattern_fill(sprite, pos, pattern_image, sprite.get_pixelv(mouse_pos), pattern_offset)
|
2020-03-28 03:15:09 +00:00
|
|
|
|
if vertical_mirror:
|
|
|
|
|
var pos := Vector2(mouse_pos.x, mirror_y)
|
2020-05-29 00:16:44 +00:00
|
|
|
|
DrawingAlgos.pattern_fill(sprite, pos, pattern_image, sprite.get_pixelv(mouse_pos), pattern_offset)
|
2020-03-28 03:15:09 +00:00
|
|
|
|
if horizontal_mirror && vertical_mirror:
|
|
|
|
|
var pos := Vector2(mirror_x, mirror_y)
|
2020-05-29 00:16:44 +00:00
|
|
|
|
DrawingAlgos.pattern_fill(sprite, pos, pattern_image, sprite.get_pixelv(mouse_pos), pattern_offset)
|
2020-03-28 03:15:09 +00:00
|
|
|
|
else: # Flood fill
|
2020-05-29 00:16:44 +00:00
|
|
|
|
DrawingAlgos.flood_fill(sprite, mouse_pos, sprite.get_pixelv(mouse_pos), current_color)
|
2020-03-28 03:15:09 +00:00
|
|
|
|
if horizontal_mirror:
|
|
|
|
|
var pos := Vector2(mirror_x, mouse_pos.y)
|
2020-05-29 00:16:44 +00:00
|
|
|
|
DrawingAlgos.flood_fill(sprite, pos, sprite.get_pixelv(pos), current_color)
|
2020-03-28 03:15:09 +00:00
|
|
|
|
if vertical_mirror:
|
|
|
|
|
var pos := Vector2(mouse_pos.x, mirror_y)
|
2020-05-29 00:16:44 +00:00
|
|
|
|
DrawingAlgos.flood_fill(sprite, pos, sprite.get_pixelv(pos), current_color)
|
2020-03-28 03:15:09 +00:00
|
|
|
|
if horizontal_mirror && vertical_mirror:
|
|
|
|
|
var pos := Vector2(mirror_x, mirror_y)
|
2020-05-29 00:16:44 +00:00
|
|
|
|
DrawingAlgos.flood_fill(sprite, pos, sprite.get_pixelv(pos), current_color)
|
2019-12-03 22:14:14 +00:00
|
|
|
|
|
2019-12-25 00:53:45 +00:00
|
|
|
|
else: # Paint all pixels of the same color
|
2020-01-18 19:06:47 +00:00
|
|
|
|
var pixel_color : Color = sprite.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):
|
2020-01-18 19:06:47 +00:00
|
|
|
|
var c : Color = sprite.get_pixel(xx, yy)
|
2019-12-03 22:14:14 +00:00
|
|
|
|
if c == pixel_color:
|
2020-04-25 12:00:38 +00:00
|
|
|
|
if fill_with == 1 && pattern_image: # Pattern fill
|
2020-04-24 21:42:02 +00:00
|
|
|
|
pattern_image.lock()
|
|
|
|
|
var pattern_size := pattern_image.get_size()
|
2020-04-24 23:09:34 +00:00
|
|
|
|
var xxx : int = int(xx + pattern_offset.x) % int(pattern_size.x)
|
|
|
|
|
var yyy : int = int(yy + pattern_offset.y) % int(pattern_size.y)
|
2020-04-24 21:42:02 +00:00
|
|
|
|
var pattern_color : Color = pattern_image.get_pixel(xxx, yyy)
|
2020-03-28 03:15:09 +00:00
|
|
|
|
sprite.set_pixel(xx, yy, pattern_color)
|
2020-04-24 21:42:02 +00:00
|
|
|
|
pattern_image.unlock()
|
2020-03-28 03:15:09 +00:00
|
|
|
|
else:
|
|
|
|
|
sprite.set_pixel(xx, yy, current_color)
|
2019-12-03 22:14:14 +00:00
|
|
|
|
sprite_changed_this_frame = true
|
2020-05-31 13:04:33 +00:00
|
|
|
|
Global.Tools.LIGHTENDARKEN:
|
2020-01-15 20:47:56 +00:00
|
|
|
|
if can_handle:
|
2020-01-18 19:06:47 +00:00
|
|
|
|
var pixel_color : Color = sprite.get_pixelv(mouse_pos)
|
2019-12-03 23:01:37 +00:00
|
|
|
|
var color_changed : Color
|
2019-12-27 15:38:43 +00:00
|
|
|
|
if ld == 0: # Lighten
|
2019-12-03 23:01:37 +00:00
|
|
|
|
color_changed = pixel_color.lightened(ld_amount)
|
2019-12-27 15:38:43 +00:00
|
|
|
|
else: # Darken
|
2019-12-03 23:01:37 +00:00
|
|
|
|
color_changed = pixel_color.darkened(ld_amount)
|
2020-01-18 19:06:47 +00:00
|
|
|
|
pencil_and_eraser(sprite, mouse_pos, color_changed, current_mouse_button, current_action)
|
2020-05-31 13:04:33 +00:00
|
|
|
|
Global.Tools.RECTSELECT:
|
2019-12-27 15:38:43 +00:00
|
|
|
|
# Check SelectionRectangle.gd for more code on Rectangle Selection
|
2020-01-15 20:47:56 +00:00
|
|
|
|
if Global.can_draw && Global.has_focus:
|
2019-12-27 15:38:43 +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]):
|
2020-05-31 15:46:47 +00:00
|
|
|
|
var mouse_button_string := "left_mouse"
|
|
|
|
|
if current_mouse_button == Global.Mouse_Button.RIGHT:
|
|
|
|
|
mouse_button_string = "right_mouse"
|
|
|
|
|
if Input.is_action_just_pressed(mouse_button_string):
|
2019-09-18 14:47:28 +00:00
|
|
|
|
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:
|
2020-05-31 15:46:47 +00:00
|
|
|
|
if is_making_selection != -1: # If we're making a new selection...
|
2019-09-18 14:47:28 +00:00
|
|
|
|
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)
|
2020-05-31 13:04:33 +00:00
|
|
|
|
Global.Tools.COLORPICKER:
|
2020-05-31 13:24:08 +00:00
|
|
|
|
var canvas_rect := Rect2(location, size)
|
|
|
|
|
if can_handle && canvas_rect.has_point(mouse_pos):
|
2020-05-10 22:46:24 +00:00
|
|
|
|
var image_data := Image.new()
|
|
|
|
|
image_data.copy_from(sprite)
|
|
|
|
|
image_data.lock()
|
|
|
|
|
var pixel_color : Color = image_data.get_pixelv(mouse_pos)
|
2020-05-31 15:40:47 +00:00
|
|
|
|
Global.color_pickers[color_picker_for].color = pixel_color
|
|
|
|
|
Global.update_custom_brush(color_picker_for)
|
2020-05-31 13:04:33 +00:00
|
|
|
|
Global.Tools.ZOOM:
|
2020-04-13 02:07:52 +00:00
|
|
|
|
if can_handle:
|
|
|
|
|
if zoom_mode == 0:
|
|
|
|
|
Global.camera.zoom_camera(-1)
|
|
|
|
|
else:
|
|
|
|
|
Global.camera.zoom_camera(1)
|
2019-10-29 21:22:38 +00:00
|
|
|
|
|
2020-05-31 13:24:08 +00:00
|
|
|
|
if Global.can_draw && Global.has_focus && Input.is_action_just_pressed("shift") && ([Global.Tools.PENCIL, Global.Tools.ERASER, Global.Tools.LIGHTENDARKEN].has(Global.current_left_tool) || [Global.Tools.PENCIL, Global.Tools.ERASER, Global.Tools.LIGHTENDARKEN].has(Global.current_right_tool)):
|
2019-12-15 06:59:19 +00:00
|
|
|
|
is_making_line = true
|
2020-01-15 20:31:02 +00:00
|
|
|
|
line_2d.set_point_position(0, previous_mouse_pos_for_lines)
|
2019-12-15 06:59:19 +00:00
|
|
|
|
elif Input.is_action_just_released("shift"):
|
|
|
|
|
is_making_line = false
|
2020-01-15 20:31:02 +00:00
|
|
|
|
line_2d.set_point_position(1, line_2d.points[0])
|
2019-12-19 13:50:41 +00:00
|
|
|
|
|
2020-01-15 20:31:02 +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
|
|
|
|
|
2020-05-31 15:46:47 +00:00
|
|
|
|
if is_making_selection != -1: # If we're making a selection
|
|
|
|
|
var mouse_button_string := "left_mouse"
|
|
|
|
|
if is_making_selection == Global.Mouse_Button.RIGHT:
|
|
|
|
|
mouse_button_string = "right_mouse"
|
|
|
|
|
if Input.is_action_just_released(mouse_button_string): # Finish selection when button is released
|
2019-09-18 14:47:28 +00:00
|
|
|
|
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))
|
2020-05-31 15:46:47 +00:00
|
|
|
|
is_making_selection = -1
|
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
|
2019-08-18 09:28:38 +00:00
|
|
|
|
if sprite_changed_this_frame:
|
2020-03-04 23:53:48 +00:00
|
|
|
|
update_texture(Global.current_layer)
|
2019-10-23 21:36:22 +00:00
|
|
|
|
|
2020-05-01 17:47:10 +00:00
|
|
|
|
|
2020-02-10 22:06:24 +00:00
|
|
|
|
func camera_zoom() -> void:
|
|
|
|
|
# Set camera zoom based on the sprite size
|
2020-04-16 15:34:57 +00:00
|
|
|
|
var bigger_canvas_axis = max(size.x, size.y)
|
|
|
|
|
var zoom_max := Vector2(bigger_canvas_axis, bigger_canvas_axis) * 0.01
|
2020-02-10 22:06:24 +00:00
|
|
|
|
if zoom_max > Vector2.ONE:
|
|
|
|
|
Global.camera.zoom_max = zoom_max
|
|
|
|
|
Global.camera2.zoom_max = zoom_max
|
|
|
|
|
Global.camera_preview.zoom_max = zoom_max
|
|
|
|
|
else:
|
|
|
|
|
Global.camera.zoom_max = Vector2.ONE
|
|
|
|
|
Global.camera2.zoom_max = Vector2.ONE
|
|
|
|
|
Global.camera_preview.zoom_max = Vector2.ONE
|
|
|
|
|
|
2020-05-11 13:33:44 +00:00
|
|
|
|
Global.camera.fit_to_frame(size)
|
|
|
|
|
Global.camera2.fit_to_frame(size)
|
|
|
|
|
Global.camera_preview.fit_to_frame(size)
|
2020-04-20 15:52:05 +00:00
|
|
|
|
|
2020-04-20 16:12:22 +00:00
|
|
|
|
Global.transparent_checker._ready() # To update the rect size
|
|
|
|
|
|
2020-05-01 17:47:10 +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]
|
2020-01-18 19:06:47 +00:00
|
|
|
|
layer_index = Global.current_layer
|
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
|
2020-02-29 00:12:03 +00:00
|
|
|
|
c.layers[Global.current_layer][0].unlock()
|
|
|
|
|
var data = c.layers[Global.current_layer][0].data
|
|
|
|
|
c.layers[Global.current_layer][0].lock()
|
|
|
|
|
Global.undo_redo.add_undo_property(c.layers[Global.current_layer][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
|
|
|
|
|
|
2020-05-01 17:47:10 +00:00
|
|
|
|
|
2019-10-29 21:22:38 +00:00
|
|
|
|
func handle_redo(action : String) -> void:
|
2020-02-11 18:05:37 +00:00
|
|
|
|
can_undo = true
|
|
|
|
|
|
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]
|
2020-02-29 00:12:03 +00:00
|
|
|
|
layer_index = Global.current_layer
|
2019-11-05 16:19:41 +00:00
|
|
|
|
else:
|
|
|
|
|
canvases = Global.canvases
|
|
|
|
|
for c in canvases:
|
2020-02-29 00:12:03 +00:00
|
|
|
|
Global.undo_redo.add_do_property(c.layers[Global.current_layer][0], "data", c.layers[Global.current_layer][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()
|
|
|
|
|
|
2020-05-01 17:47:10 +00:00
|
|
|
|
|
2020-03-04 23:53:48 +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)
|
2020-03-04 23:53:48 +00:00
|
|
|
|
|
2020-01-18 19:06:47 +00:00
|
|
|
|
var frame_texture_rect : TextureRect
|
2020-05-01 19:17:05 +00:00
|
|
|
|
frame_texture_rect = Global.find_node_by_name(Global.layers[layer_index][3].get_child(frame), "CelTexture")
|
2020-01-18 19:06:47 +00:00
|
|
|
|
frame_texture_rect.texture = layers[layer_index][1]
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
2020-05-01 17:47:10 +00:00
|
|
|
|
|
2020-05-31 15:46:47 +00:00
|
|
|
|
func pencil_and_eraser(sprite : Image, mouse_pos : Vector2, color : Color, current_mouse_button : int, current_action := -1) -> 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:
|
2020-05-29 21:46:58 +00:00
|
|
|
|
DrawingAlgos.fill_gaps(sprite, line_2d.points[1], previous_mouse_pos_for_lines, color, current_mouse_button, pen_pressure, current_action)
|
|
|
|
|
DrawingAlgos.draw_brush(sprite, line_2d.points[1], color, current_mouse_button, pen_pressure, current_action)
|
2019-12-19 13:50:41 +00:00
|
|
|
|
made_line = true
|
2019-08-18 09:28:38 +00:00
|
|
|
|
else:
|
2020-05-30 22:07:08 +00:00
|
|
|
|
# Draw
|
|
|
|
|
DrawingAlgos.draw_brush(sprite, mouse_pos, color, current_mouse_button, pen_pressure, current_action)
|
|
|
|
|
DrawingAlgos.fill_gaps(sprite, mouse_pos, previous_mouse_pos, color, current_mouse_button, pen_pressure, current_action) # Fill the gaps
|
2019-08-18 09:28:38 +00:00
|
|
|
|
|
2020-05-01 17:47:10 +00:00
|
|
|
|
|
2019-12-25 18:27:25 +00:00
|
|
|
|
# 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
|
|
|
|
|
|
2020-05-01 17:47:10 +00:00
|
|
|
|
|
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()
|