mirror of
https://github.com/Orama-Interactive/Pixelorama.git
synced 2025-01-31 07:29:49 +00:00
Refactored Canvas.gd
This commit is contained in:
parent
9b4956b4f1
commit
5246ce23ef
526
src/Canvas.gd
526
src/Canvas.gd
|
@ -12,13 +12,12 @@ var current_pixel := Vector2.ZERO # pretty much same as mouse_pos, but can be ac
|
|||
var previous_mouse_pos := Vector2.ZERO
|
||||
var previous_mouse_pos_for_lines := Vector2.ZERO
|
||||
var can_undo := true
|
||||
var cursor_inside_canvas := false
|
||||
var cursor_image_has_changed := false
|
||||
var previous_action := -1
|
||||
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
|
||||
var is_making_line := false
|
||||
var made_line := false
|
||||
|
@ -75,39 +74,8 @@ func _ready() -> void:
|
|||
|
||||
|
||||
func _draw() -> void:
|
||||
# Onion Skinning
|
||||
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
|
||||
|
||||
# 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
|
||||
onion_skinning()
|
||||
|
||||
# Draw current frame layers
|
||||
for i in range(layers.size()):
|
||||
|
@ -125,86 +93,33 @@ func _draw() -> void:
|
|||
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
|
||||
|
||||
# Idea taken from flurick (on GitHub)
|
||||
if Global.draw_grid:
|
||||
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)
|
||||
draw_grid(Global.grid_type)
|
||||
|
||||
# Draw rectangle to indicate the pixel currently being hovered on
|
||||
var mouse_pos := current_pixel
|
||||
mouse_pos = mouse_pos.floor()
|
||||
if Global.left_square_indicator_visible && Global.can_draw:
|
||||
if Global.current_brush_types[0] == Global.Brush_Types.PIXEL || Global.current_tools[0] == Global.Tools.LIGHTENDARKEN:
|
||||
if Global.current_tools[0] == Global.Tools.PENCIL || Global.current_tools[0] == Global.Tools.ERASER || Global.current_tools[0] == Global.Tools.LIGHTENDARKEN:
|
||||
var start_pos_x = mouse_pos.x - (Global.brush_sizes[0] >> 1)
|
||||
var start_pos_y = mouse_pos.y - (Global.brush_sizes[0] >> 1)
|
||||
draw_rect(Rect2(start_pos_x, start_pos_y, Global.brush_sizes[0], Global.brush_sizes[0]), Color.blue, false)
|
||||
elif Global.current_brush_types[0] == Global.Brush_Types.CIRCLE || Global.current_brush_types[0] == Global.Brush_Types.FILLED_CIRCLE:
|
||||
if Global.current_tools[0] == Global.Tools.PENCIL || Global.current_tools[0] == Global.Tools.ERASER:
|
||||
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:
|
||||
if Global.current_tools[0] == Global.Tools.PENCIL || Global.current_tools[0] == Global.Tools.ERASER:
|
||||
var custom_brush_size = Global.custom_brush_images[0].get_size() - Vector2.ONE
|
||||
var dst := rectangle_center(mouse_pos, custom_brush_size)
|
||||
draw_texture(Global.custom_brush_textures[0], dst)
|
||||
if Global.can_draw:
|
||||
var mouse_pos := current_pixel
|
||||
mouse_pos = mouse_pos.floor()
|
||||
var visible_indicators := [Global.left_square_indicator_visible, Global.right_square_indicator_visible]
|
||||
|
||||
if Global.right_square_indicator_visible && Global.can_draw:
|
||||
if Global.current_brush_types[1] == Global.Brush_Types.PIXEL || Global.current_tools[1] == Global.Tools.LIGHTENDARKEN:
|
||||
if Global.current_tools[1] == Global.Tools.PENCIL || Global.current_tools[1] == Global.Tools.ERASER || Global.current_tools[1] == Global.Tools.LIGHTENDARKEN:
|
||||
var start_pos_x = mouse_pos.x - (Global.brush_sizes[1] >> 1)
|
||||
var start_pos_y = mouse_pos.y - (Global.brush_sizes[1] >> 1)
|
||||
draw_rect(Rect2(start_pos_x, start_pos_y, Global.brush_sizes[1], Global.brush_sizes[1]), Color.red, false)
|
||||
elif Global.current_brush_types[1] == Global.Brush_Types.CIRCLE || Global.current_brush_types[1] == Global.Brush_Types.FILLED_CIRCLE:
|
||||
if Global.current_tools[1] == Global.Tools.PENCIL || Global.current_tools[1] == Global.Tools.ERASER:
|
||||
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:
|
||||
if Global.current_tools[1] == Global.Tools.PENCIL || Global.current_tools[1] == Global.Tools.ERASER:
|
||||
var custom_brush_size = Global.custom_brush_images[1].get_size() - Vector2.ONE
|
||||
var dst := rectangle_center(mouse_pos, custom_brush_size)
|
||||
draw_texture(Global.custom_brush_textures[1], dst)
|
||||
for i in range(0, 1):
|
||||
if visible_indicators[i]:
|
||||
if Global.current_brush_types[i] == Global.Brush_Types.PIXEL || Global.current_tools[i] == Global.Tools.LIGHTENDARKEN:
|
||||
if Global.current_tools[i] == Global.Tools.PENCIL || Global.current_tools[i] == Global.Tools.ERASER || Global.current_tools[i] == Global.Tools.LIGHTENDARKEN:
|
||||
var start_pos_x = mouse_pos.x - (Global.brush_sizes[i] >> 1)
|
||||
var start_pos_y = mouse_pos.y - (Global.brush_sizes[i] >> 1)
|
||||
draw_rect(Rect2(start_pos_x, start_pos_y, Global.brush_sizes[i], Global.brush_sizes[i]), Color.blue, false)
|
||||
elif Global.current_brush_types[i] == Global.Brush_Types.CIRCLE || Global.current_brush_types[i] == Global.Brush_Types.FILLED_CIRCLE:
|
||||
if Global.current_tools[i] == Global.Tools.PENCIL || Global.current_tools[i] == Global.Tools.ERASER:
|
||||
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:
|
||||
if Global.current_tools[i] == Global.Tools.PENCIL || Global.current_tools[i] == Global.Tools.ERASER:
|
||||
var custom_brush_size = Global.custom_brush_images[i].get_size() - Vector2.ONE
|
||||
var dst := DrawingAlgos.rectangle_center(mouse_pos, custom_brush_size)
|
||||
draw_texture(Global.custom_brush_textures[i], dst)
|
||||
|
||||
|
||||
func _input(event : InputEvent) -> void:
|
||||
|
@ -247,19 +162,9 @@ func _input(event : InputEvent) -> void:
|
|||
pen_pressure = 1 # This causes problems with tablets though
|
||||
|
||||
sprite_changed_this_frame = false
|
||||
var sprite : Image = layers[Global.current_layer][0]
|
||||
var mouse_pos := current_pixel
|
||||
var mouse_pos_floored := mouse_pos.floor()
|
||||
var mouse_pos_ceiled := mouse_pos.ceil()
|
||||
var current_mouse_button := -1
|
||||
var current_action := -1
|
||||
var current_color : Color
|
||||
var fill_area := 0 # For the bucket tool
|
||||
# For the LightenDarken tool
|
||||
var ld := 0
|
||||
var ld_amount := 0.1
|
||||
var color_picker_for := 0
|
||||
var zoom_mode := 0
|
||||
|
||||
west_limit = location.x
|
||||
east_limit = location.x + size.x
|
||||
|
@ -273,28 +178,16 @@ func _input(event : InputEvent) -> void:
|
|||
|
||||
if Input.is_mouse_button_pressed(BUTTON_LEFT):
|
||||
current_mouse_button = Global.Mouse_Button.LEFT
|
||||
current_action = Global.current_tools[0]
|
||||
current_color = Global.color_pickers[0].color
|
||||
fill_area = Global.fill_areas[0]
|
||||
ld = Global.ld_modes[0]
|
||||
ld_amount = Global.ld_amounts[0]
|
||||
color_picker_for = Global.color_picker_for[0]
|
||||
zoom_mode = Global.zoom_modes[0]
|
||||
|
||||
elif Input.is_mouse_button_pressed(BUTTON_RIGHT):
|
||||
current_mouse_button = Global.Mouse_Button.RIGHT
|
||||
current_action = Global.current_tools[1]
|
||||
current_color = Global.color_pickers[1].color
|
||||
fill_area = Global.fill_areas[1]
|
||||
ld = Global.ld_modes[1]
|
||||
ld_amount = Global.ld_amounts[1]
|
||||
color_picker_for = Global.color_picker_for[1]
|
||||
zoom_mode = Global.zoom_modes[1]
|
||||
|
||||
var current_action : int = Global.current_tools[current_mouse_button] if current_mouse_button != -1 else -1
|
||||
|
||||
if Global.has_focus:
|
||||
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
|
||||
if !cursor_image_has_changed:
|
||||
cursor_image_has_changed = true
|
||||
if Global.cursor_image.get_data().get_size() != Vector2.ZERO:
|
||||
Input.set_custom_mouse_cursor(Global.cursor_image, 0, Vector2(15, 15))
|
||||
if Global.show_left_tool_icon:
|
||||
|
@ -302,12 +195,9 @@ func _input(event : InputEvent) -> void:
|
|||
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
|
||||
if cursor_image_has_changed:
|
||||
cursor_image_has_changed = false
|
||||
Global.left_cursor.visible = false
|
||||
Global.right_cursor.visible = false
|
||||
Input.set_custom_mouse_cursor(null)
|
||||
|
@ -328,128 +218,7 @@ func _input(event : InputEvent) -> void:
|
|||
if previous_action != -1 && previous_action != Global.Tools.RECTSELECT && current_action != Global.Tools.COLORPICKER && current_action != Global.Tools.ZOOM:
|
||||
handle_redo("Draw")
|
||||
|
||||
match current_action: # Handle current tool
|
||||
Global.Tools.PENCIL:
|
||||
pencil_and_eraser(sprite, mouse_pos, current_color, current_mouse_button, current_action)
|
||||
Global.Tools.ERASER:
|
||||
pencil_and_eraser(sprite, mouse_pos, Color(0, 0, 0, 0), current_mouse_button, current_action)
|
||||
Global.Tools.BUCKET:
|
||||
if can_handle:
|
||||
var fill_with := 0
|
||||
var pattern_image : Image
|
||||
var pattern_offset : Vector2
|
||||
if current_mouse_button == Global.Mouse_Button.LEFT:
|
||||
fill_with = Global.fill_with[0]
|
||||
pattern_image = Global.pattern_images[0]
|
||||
pattern_offset = Global.fill_pattern_offsets[0]
|
||||
elif current_mouse_button == Global.Mouse_Button.RIGHT:
|
||||
fill_with = Global.fill_with[1]
|
||||
pattern_image = Global.pattern_images[1]
|
||||
pattern_offset = Global.fill_pattern_offsets[1]
|
||||
|
||||
if fill_area == 0: # Paint the specific area of the same color
|
||||
var horizontal_mirror := false
|
||||
var vertical_mirror := false
|
||||
var mirror_x := east_limit + west_limit - mouse_pos_floored.x - 1
|
||||
var mirror_y := south_limit + north_limit - mouse_pos_floored.y - 1
|
||||
if current_mouse_button == Global.Mouse_Button.LEFT:
|
||||
horizontal_mirror = Global.horizontal_mirror[0]
|
||||
vertical_mirror = Global.vertical_mirror[0]
|
||||
elif current_mouse_button == Global.Mouse_Button.RIGHT:
|
||||
horizontal_mirror = Global.horizontal_mirror[1]
|
||||
vertical_mirror = Global.vertical_mirror[1]
|
||||
|
||||
if fill_with == 1 && pattern_image: # Pattern fill
|
||||
DrawingAlgos.pattern_fill(sprite, mouse_pos, pattern_image, sprite.get_pixelv(mouse_pos), pattern_offset)
|
||||
if horizontal_mirror:
|
||||
var pos := Vector2(mirror_x, mouse_pos.y)
|
||||
DrawingAlgos.pattern_fill(sprite, pos, pattern_image, sprite.get_pixelv(mouse_pos), pattern_offset)
|
||||
if vertical_mirror:
|
||||
var pos := Vector2(mouse_pos.x, mirror_y)
|
||||
DrawingAlgos.pattern_fill(sprite, pos, pattern_image, sprite.get_pixelv(mouse_pos), pattern_offset)
|
||||
if horizontal_mirror && vertical_mirror:
|
||||
var pos := Vector2(mirror_x, mirror_y)
|
||||
DrawingAlgos.pattern_fill(sprite, pos, pattern_image, sprite.get_pixelv(mouse_pos), pattern_offset)
|
||||
else: # Flood fill
|
||||
DrawingAlgos.flood_fill(sprite, mouse_pos, sprite.get_pixelv(mouse_pos), current_color)
|
||||
if horizontal_mirror:
|
||||
var pos := Vector2(mirror_x, mouse_pos.y)
|
||||
DrawingAlgos.flood_fill(sprite, pos, sprite.get_pixelv(pos), current_color)
|
||||
if vertical_mirror:
|
||||
var pos := Vector2(mouse_pos.x, mirror_y)
|
||||
DrawingAlgos.flood_fill(sprite, pos, sprite.get_pixelv(pos), current_color)
|
||||
if horizontal_mirror && vertical_mirror:
|
||||
var pos := Vector2(mirror_x, mirror_y)
|
||||
DrawingAlgos.flood_fill(sprite, pos, sprite.get_pixelv(pos), current_color)
|
||||
|
||||
else: # Paint all pixels of the same color
|
||||
var pixel_color : Color = sprite.get_pixelv(mouse_pos)
|
||||
for xx in range(west_limit, east_limit):
|
||||
for yy in range(north_limit, south_limit):
|
||||
var c : Color = sprite.get_pixel(xx, yy)
|
||||
if c == pixel_color:
|
||||
if fill_with == 1 && pattern_image: # Pattern fill
|
||||
pattern_image.lock()
|
||||
var pattern_size := pattern_image.get_size()
|
||||
var xxx : int = int(xx + pattern_offset.x) % int(pattern_size.x)
|
||||
var yyy : int = int(yy + pattern_offset.y) % int(pattern_size.y)
|
||||
var pattern_color : Color = pattern_image.get_pixel(xxx, yyy)
|
||||
sprite.set_pixel(xx, yy, pattern_color)
|
||||
pattern_image.unlock()
|
||||
else:
|
||||
sprite.set_pixel(xx, yy, current_color)
|
||||
sprite_changed_this_frame = true
|
||||
Global.Tools.LIGHTENDARKEN:
|
||||
if can_handle:
|
||||
var pixel_color : Color = sprite.get_pixelv(mouse_pos)
|
||||
var color_changed : Color
|
||||
if ld == 0: # Lighten
|
||||
color_changed = pixel_color.lightened(ld_amount)
|
||||
else: # Darken
|
||||
color_changed = pixel_color.darkened(ld_amount)
|
||||
pencil_and_eraser(sprite, mouse_pos, color_changed, current_mouse_button, current_action)
|
||||
Global.Tools.RECTSELECT:
|
||||
# Check SelectionRectangle.gd for more code on Rectangle Selection
|
||||
if Global.can_draw && Global.has_focus:
|
||||
# If we're creating a new selection
|
||||
if Global.selected_pixels.size() == 0 || !point_in_rectangle(mouse_pos_floored, Global.selection_rectangle.polygon[0] - Vector2.ONE, Global.selection_rectangle.polygon[2]):
|
||||
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):
|
||||
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 != -1: # 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)
|
||||
Global.Tools.COLORPICKER:
|
||||
var canvas_rect := Rect2(location, size)
|
||||
if can_handle && canvas_rect.has_point(mouse_pos):
|
||||
var image_data := Image.new()
|
||||
image_data.copy_from(sprite)
|
||||
image_data.lock()
|
||||
var pixel_color : Color = image_data.get_pixelv(mouse_pos)
|
||||
Global.color_pickers[color_picker_for].color = pixel_color
|
||||
Global.update_custom_brush(color_picker_for)
|
||||
Global.Tools.ZOOM:
|
||||
if can_handle:
|
||||
if zoom_mode == 0:
|
||||
Global.camera.zoom_camera(-1)
|
||||
else:
|
||||
Global.camera.zoom_camera(1)
|
||||
handle_tools(current_mouse_button, current_action, mouse_pos, can_handle)
|
||||
|
||||
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_tools[0]) || [Global.Tools.PENCIL, Global.Tools.ERASER, Global.Tools.LIGHTENDARKEN].has(Global.current_tools[1])):
|
||||
is_making_line = true
|
||||
|
@ -473,9 +242,8 @@ func _input(event : InputEvent) -> void:
|
|||
Global.cursor_position_label.text += " %s°" % str(angle)
|
||||
|
||||
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"
|
||||
var mouse_button_string := "left_mouse" if is_making_selection == Global.Mouse_Button.LEFT else "right_mouse"
|
||||
|
||||
if Input.is_action_just_released(mouse_button_string): # Finish selection when button is released
|
||||
var start_pos = Global.selection_rectangle.polygon[0]
|
||||
var end_pos = Global.selection_rectangle.polygon[2]
|
||||
|
@ -526,6 +294,141 @@ func camera_zoom() -> void:
|
|||
Global.transparent_checker._ready() # To update the rect size
|
||||
|
||||
|
||||
func handle_tools(current_mouse_button : int, current_action : int, mouse_pos : Vector2, can_handle : bool) -> void:
|
||||
var sprite : Image = layers[Global.current_layer][0]
|
||||
var mouse_pos_floored := mouse_pos.floor()
|
||||
var mouse_pos_ceiled := mouse_pos.ceil()
|
||||
|
||||
var current_color : Color = Global.color_pickers[current_mouse_button].color
|
||||
var fill_area : int = Global.fill_areas[current_mouse_button]
|
||||
var ld : int = Global.ld_modes[current_mouse_button]
|
||||
var ld_amount : float = Global.ld_amounts[current_mouse_button]
|
||||
var color_picker_for : int = Global.color_picker_for[current_mouse_button]
|
||||
var zoom_mode : int = Global.zoom_modes[current_mouse_button]
|
||||
|
||||
match current_action: # Handle current tool
|
||||
Global.Tools.PENCIL:
|
||||
pencil_and_eraser(sprite, mouse_pos, current_color, current_mouse_button, current_action)
|
||||
Global.Tools.ERASER:
|
||||
pencil_and_eraser(sprite, mouse_pos, Color(0, 0, 0, 0), current_mouse_button, current_action)
|
||||
Global.Tools.BUCKET:
|
||||
if can_handle:
|
||||
var fill_with : int = Global.fill_with[current_mouse_button]
|
||||
var pattern_image : Image = Global.pattern_images[current_mouse_button]
|
||||
var pattern_offset : Vector2 = Global.fill_pattern_offsets[current_mouse_button]
|
||||
|
||||
if fill_area == Global.Fill_Area.SAME_COLOR_AREA: # Paint the specific area of the same color
|
||||
var mirror_x := east_limit + west_limit - mouse_pos_floored.x - 1
|
||||
var mirror_y := south_limit + north_limit - mouse_pos_floored.y - 1
|
||||
var horizontal_mirror : bool = Global.horizontal_mirror[current_mouse_button]
|
||||
var vertical_mirror : bool = Global.vertical_mirror[current_mouse_button]
|
||||
|
||||
if fill_with == Global.Fill_With.PATTERN && pattern_image: # Pattern fill
|
||||
DrawingAlgos.pattern_fill(sprite, mouse_pos, pattern_image, sprite.get_pixelv(mouse_pos), pattern_offset)
|
||||
if horizontal_mirror:
|
||||
var pos := Vector2(mirror_x, mouse_pos.y)
|
||||
DrawingAlgos.pattern_fill(sprite, pos, pattern_image, sprite.get_pixelv(mouse_pos), pattern_offset)
|
||||
if vertical_mirror:
|
||||
var pos := Vector2(mouse_pos.x, mirror_y)
|
||||
DrawingAlgos.pattern_fill(sprite, pos, pattern_image, sprite.get_pixelv(mouse_pos), pattern_offset)
|
||||
if horizontal_mirror && vertical_mirror:
|
||||
var pos := Vector2(mirror_x, mirror_y)
|
||||
DrawingAlgos.pattern_fill(sprite, pos, pattern_image, sprite.get_pixelv(mouse_pos), pattern_offset)
|
||||
|
||||
else: # Flood fill
|
||||
DrawingAlgos.flood_fill(sprite, mouse_pos, sprite.get_pixelv(mouse_pos), current_color)
|
||||
if horizontal_mirror:
|
||||
var pos := Vector2(mirror_x, mouse_pos.y)
|
||||
DrawingAlgos.flood_fill(sprite, pos, sprite.get_pixelv(pos), current_color)
|
||||
if vertical_mirror:
|
||||
var pos := Vector2(mouse_pos.x, mirror_y)
|
||||
DrawingAlgos.flood_fill(sprite, pos, sprite.get_pixelv(pos), current_color)
|
||||
if horizontal_mirror && vertical_mirror:
|
||||
var pos := Vector2(mirror_x, mirror_y)
|
||||
DrawingAlgos.flood_fill(sprite, pos, sprite.get_pixelv(pos), current_color)
|
||||
|
||||
else: # Paint all pixels of the same color
|
||||
var pixel_color : Color = sprite.get_pixelv(mouse_pos)
|
||||
for xx in range(west_limit, east_limit):
|
||||
for yy in range(north_limit, south_limit):
|
||||
var c : Color = sprite.get_pixel(xx, yy)
|
||||
if c == pixel_color:
|
||||
if fill_with == Global.Fill_With.PATTERN && pattern_image: # Pattern fill
|
||||
pattern_image.lock()
|
||||
var pattern_size := pattern_image.get_size()
|
||||
var xxx : int = int(xx + pattern_offset.x) % int(pattern_size.x)
|
||||
var yyy : int = int(yy + pattern_offset.y) % int(pattern_size.y)
|
||||
var pattern_color : Color = pattern_image.get_pixel(xxx, yyy)
|
||||
sprite.set_pixel(xx, yy, pattern_color)
|
||||
pattern_image.unlock()
|
||||
else:
|
||||
sprite.set_pixel(xx, yy, current_color)
|
||||
sprite_changed_this_frame = true
|
||||
Global.Tools.LIGHTENDARKEN:
|
||||
if can_handle:
|
||||
var pixel_color : Color = sprite.get_pixelv(mouse_pos)
|
||||
var color_changed : Color
|
||||
if ld == Global.Lighten_Darken_Mode.LIGHTEN:
|
||||
color_changed = pixel_color.lightened(ld_amount)
|
||||
else: # Darken
|
||||
color_changed = pixel_color.darkened(ld_amount)
|
||||
pencil_and_eraser(sprite, mouse_pos, color_changed, current_mouse_button, current_action)
|
||||
Global.Tools.RECTSELECT:
|
||||
# Check SelectionRectangle.gd for more code on Rectangle Selection
|
||||
if Global.can_draw && Global.has_focus:
|
||||
# If we're creating a new selection
|
||||
if Global.selected_pixels.size() == 0 || !point_in_rectangle(mouse_pos_floored, Global.selection_rectangle.polygon[0] - Vector2.ONE, Global.selection_rectangle.polygon[2]):
|
||||
var mouse_button_string := "left_mouse" if current_mouse_button == Global.Mouse_Button.LEFT else "right_mouse"
|
||||
|
||||
if Input.is_action_just_pressed(mouse_button_string):
|
||||
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 != -1: # 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)
|
||||
Global.Tools.COLORPICKER:
|
||||
var canvas_rect := Rect2(location, size)
|
||||
if can_handle && canvas_rect.has_point(mouse_pos):
|
||||
var image_data := Image.new()
|
||||
image_data.copy_from(sprite)
|
||||
image_data.lock()
|
||||
var pixel_color : Color = image_data.get_pixelv(mouse_pos)
|
||||
Global.color_pickers[color_picker_for].color = pixel_color
|
||||
Global.update_custom_brush(color_picker_for)
|
||||
Global.Tools.ZOOM:
|
||||
if can_handle:
|
||||
if zoom_mode == Global.Zoom_Mode.ZOOM_IN:
|
||||
Global.camera.zoom_camera(-1)
|
||||
else:
|
||||
Global.camera.zoom_camera(1)
|
||||
|
||||
|
||||
func pencil_and_eraser(sprite : Image, mouse_pos : Vector2, color : Color, current_mouse_button : int, current_action := -1) -> void:
|
||||
if made_line:
|
||||
return
|
||||
if is_making_line:
|
||||
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)
|
||||
made_line = true
|
||||
else:
|
||||
# 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
|
||||
|
||||
|
||||
func handle_undo(action : String) -> void:
|
||||
if !can_undo:
|
||||
return
|
||||
|
@ -583,24 +486,83 @@ func update_texture(layer_index : int) -> void:
|
|||
frame_texture_rect.texture = layers[layer_index][1]
|
||||
|
||||
|
||||
func pencil_and_eraser(sprite : Image, mouse_pos : Vector2, color : Color, current_mouse_button : int, current_action := -1) -> void:
|
||||
if made_line:
|
||||
return
|
||||
if is_making_line:
|
||||
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)
|
||||
made_line = true
|
||||
else:
|
||||
# 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
|
||||
func onion_skinning() -> void:
|
||||
# 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
|
||||
|
||||
# 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
|
||||
|
||||
|
||||
func draw_grid(grid_type : int) -> void:
|
||||
if grid_type == Global.Grid_Types.CARTESIAN || 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)
|
||||
|
||||
# Doesn't work properly yet
|
||||
if grid_type == Global.Grid_Types.ISOMETRIC || 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)
|
||||
|
||||
|
||||
# Checks if a point is inside a rectangle
|
||||
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
|
||||
|
||||
|
||||
# Returns the position in the middle of a rectangle
|
||||
func rectangle_center(rect_position : Vector2, rect_size : Vector2) -> Vector2:
|
||||
return (rect_position - rect_size / 2).floor()
|
||||
|
|
Loading…
Reference in a new issue