2020-07-09 12:22:17 +00:00
|
|
|
extends "res://src/Tools/Draw.gd"
|
|
|
|
|
2021-11-11 01:21:34 +00:00
|
|
|
var _prev_mode := false
|
2020-07-09 12:22:17 +00:00
|
|
|
var _last_position := Vector2.INF
|
|
|
|
var _changed := false
|
2020-07-13 12:10:17 +00:00
|
|
|
var _overwrite := false
|
2021-05-23 21:58:19 +00:00
|
|
|
var _fill_inside := false
|
|
|
|
var _draw_points := Array()
|
2020-07-09 12:22:17 +00:00
|
|
|
|
2021-11-14 01:30:00 +00:00
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
class PencilOp:
|
|
|
|
extends Drawer.ColorOp
|
2020-07-09 12:22:17 +00:00
|
|
|
var changed := false
|
2020-07-13 12:10:17 +00:00
|
|
|
var overwrite := false
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
func process(src: Color, dst: Color) -> Color:
|
|
|
|
changed = true
|
|
|
|
src.a *= strength
|
2020-07-13 12:10:17 +00:00
|
|
|
if overwrite:
|
|
|
|
return src
|
2020-07-09 12:22:17 +00:00
|
|
|
return dst.blend(src)
|
|
|
|
|
|
|
|
|
|
|
|
func _init() -> void:
|
2020-07-13 12:10:17 +00:00
|
|
|
_drawer.color_op = PencilOp.new()
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func _on_Overwrite_toggled(button_pressed: bool):
|
2020-07-13 12:10:17 +00:00
|
|
|
_overwrite = button_pressed
|
|
|
|
update_config()
|
|
|
|
save_config()
|
|
|
|
|
|
|
|
|
2021-05-23 21:58:19 +00:00
|
|
|
func _on_FillInside_toggled(button_pressed):
|
|
|
|
_fill_inside = button_pressed
|
|
|
|
update_config()
|
|
|
|
save_config()
|
|
|
|
|
|
|
|
|
2021-11-11 01:21:34 +00:00
|
|
|
func _input(event: InputEvent) -> void:
|
2021-11-25 12:48:30 +00:00
|
|
|
var overwrite_button: CheckBox = $Overwrite
|
2021-11-11 01:21:34 +00:00
|
|
|
|
|
|
|
if event.is_action_pressed("ctrl"):
|
|
|
|
_prev_mode = overwrite_button.pressed
|
|
|
|
if event.is_action("ctrl"):
|
|
|
|
overwrite_button.pressed = !_prev_mode
|
|
|
|
_overwrite = overwrite_button.pressed
|
|
|
|
if event.is_action_released("ctrl"):
|
|
|
|
overwrite_button.pressed = _prev_mode
|
|
|
|
_overwrite = overwrite_button.pressed
|
|
|
|
|
|
|
|
|
2020-07-13 12:10:17 +00:00
|
|
|
func get_config() -> Dictionary:
|
|
|
|
var config := .get_config()
|
|
|
|
config["overwrite"] = _overwrite
|
2021-05-23 21:58:19 +00:00
|
|
|
config["fill_inside"] = _fill_inside
|
2020-07-13 12:10:17 +00:00
|
|
|
return config
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func set_config(config: Dictionary) -> void:
|
2020-07-13 12:10:17 +00:00
|
|
|
.set_config(config)
|
|
|
|
_overwrite = config.get("overwrite", _overwrite)
|
2021-05-23 21:58:19 +00:00
|
|
|
_fill_inside = config.get("fill_inside", _fill_inside)
|
2020-07-13 12:10:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
func update_config() -> void:
|
|
|
|
.update_config()
|
|
|
|
$Overwrite.pressed = _overwrite
|
2021-05-23 21:58:19 +00:00
|
|
|
$FillInside.pressed = _fill_inside
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func draw_start(position: Vector2) -> void:
|
2021-12-14 23:39:24 +00:00
|
|
|
.draw_start(position)
|
2021-11-14 01:30:00 +00:00
|
|
|
if Input.is_action_pressed("alt"):
|
|
|
|
_picking_color = true
|
|
|
|
_pick_color(position)
|
|
|
|
return
|
|
|
|
_picking_color = false
|
|
|
|
|
2021-04-23 20:37:07 +00:00
|
|
|
Global.canvas.selection.transform_content_confirm()
|
2020-07-09 12:22:17 +00:00
|
|
|
update_mask()
|
|
|
|
_changed = false
|
|
|
|
_drawer.color_op.changed = false
|
2020-07-13 12:10:17 +00:00
|
|
|
_drawer.color_op.overwrite = _overwrite
|
2021-05-23 21:58:19 +00:00
|
|
|
_draw_points = Array()
|
2020-07-09 12:22:17 +00:00
|
|
|
|
2021-12-06 16:38:54 +00:00
|
|
|
prepare_undo("Draw")
|
2020-07-09 12:22:17 +00:00
|
|
|
_drawer.reset()
|
|
|
|
|
|
|
|
_draw_line = Tools.shift
|
|
|
|
if _draw_line:
|
|
|
|
_line_start = position
|
|
|
|
_line_end = position
|
|
|
|
update_line_polylines(_line_start, _line_end)
|
|
|
|
else:
|
2021-05-23 21:58:19 +00:00
|
|
|
if _fill_inside:
|
|
|
|
_draw_points.append(position)
|
2020-07-09 12:22:17 +00:00
|
|
|
draw_tool(position)
|
|
|
|
_last_position = position
|
|
|
|
Global.canvas.sprite_changed_this_frame = true
|
|
|
|
cursor_text = ""
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func draw_move(position: Vector2) -> void:
|
2021-12-14 23:39:24 +00:00
|
|
|
.draw_move(position)
|
2021-11-25 12:48:30 +00:00
|
|
|
if _picking_color: # Still return even if we released Alt
|
2021-11-14 01:30:00 +00:00
|
|
|
if Input.is_action_pressed("alt"):
|
|
|
|
_pick_color(position)
|
|
|
|
return
|
|
|
|
|
2020-07-09 12:22:17 +00:00
|
|
|
if _draw_line:
|
|
|
|
var d = _line_angle_constraint(_line_start, position)
|
|
|
|
_line_end = d.position
|
|
|
|
cursor_text = d.text
|
|
|
|
update_line_polylines(_line_start, _line_end)
|
|
|
|
else:
|
|
|
|
draw_fill_gap(_last_position, position)
|
|
|
|
_last_position = position
|
|
|
|
cursor_text = ""
|
|
|
|
Global.canvas.sprite_changed_this_frame = true
|
2021-05-23 21:58:19 +00:00
|
|
|
if _fill_inside:
|
|
|
|
_draw_points.append(position)
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
2021-12-14 23:39:24 +00:00
|
|
|
func draw_end(position: Vector2) -> void:
|
|
|
|
.draw_end(position)
|
2021-11-14 01:30:00 +00:00
|
|
|
if _picking_color:
|
|
|
|
return
|
|
|
|
|
2020-07-09 12:22:17 +00:00
|
|
|
if _draw_line:
|
|
|
|
draw_tool(_line_start)
|
|
|
|
draw_fill_gap(_line_start, _line_end)
|
|
|
|
_draw_line = false
|
2021-05-23 21:58:19 +00:00
|
|
|
else:
|
|
|
|
if _fill_inside:
|
2021-12-14 23:39:24 +00:00
|
|
|
_draw_points.append(position)
|
2021-05-23 21:58:19 +00:00
|
|
|
if _draw_points.size() > 3:
|
|
|
|
var v = Vector2()
|
2021-06-11 22:06:13 +00:00
|
|
|
var image_size = Global.current_project.size
|
2021-05-23 21:58:19 +00:00
|
|
|
for x in image_size.x:
|
|
|
|
v.x = x
|
|
|
|
for y in image_size.y:
|
|
|
|
v.y = y
|
|
|
|
if Geometry.is_point_in_polygon(v, _draw_points):
|
|
|
|
draw_tool(v)
|
2020-07-09 12:22:17 +00:00
|
|
|
if _changed or _drawer.color_op.changed:
|
2021-12-06 16:38:54 +00:00
|
|
|
commit_undo()
|
2020-07-09 12:22:17 +00:00
|
|
|
cursor_text = ""
|
|
|
|
update_random_image()
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func _draw_brush_image(image: Image, src_rect: Rect2, dst: Vector2) -> void:
|
2020-07-09 12:22:17 +00:00
|
|
|
_changed = true
|
2021-06-11 22:06:13 +00:00
|
|
|
var images := _get_selected_draw_images()
|
2020-07-13 12:10:17 +00:00
|
|
|
if _overwrite:
|
2021-06-11 22:06:13 +00:00
|
|
|
for draw_image in images:
|
|
|
|
draw_image.blit_rect(image, src_rect, dst)
|
2020-07-13 12:10:17 +00:00
|
|
|
else:
|
2021-06-11 22:06:13 +00:00
|
|
|
for draw_image in images:
|
|
|
|
draw_image.blend_rect(image, src_rect, dst)
|