2020-06-04 11:11:24 +00:00
|
|
|
class_name Drawer
|
2020-05-01 17:47:10 +00:00
|
|
|
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
class ColorOp:
|
|
|
|
var strength := 1.0
|
|
|
|
|
|
|
|
|
|
|
|
func process(src: Color, _dst: Color) -> Color:
|
|
|
|
return src
|
|
|
|
|
|
|
|
|
2020-06-04 11:11:24 +00:00
|
|
|
class SimpleDrawer:
|
2020-07-09 12:22:17 +00:00
|
|
|
func set_pixel(image: Image, position: Vector2, color: Color, op : ColorOp) -> void:
|
|
|
|
var color_old := image.get_pixelv(position)
|
|
|
|
var color_new := op.process(color, color_old)
|
|
|
|
if not color_new.is_equal_approx(color_old):
|
|
|
|
image.set_pixelv(position, color_new)
|
2020-04-27 15:09:54 +00:00
|
|
|
|
|
|
|
|
2020-06-04 11:11:24 +00:00
|
|
|
class PixelPerfectDrawer:
|
2020-04-27 15:09:54 +00:00
|
|
|
const neighbours = [Vector2(0, 1), Vector2(1, 0), Vector2(-1, 0), Vector2(0, -1)]
|
|
|
|
const corners = [Vector2(1, 1), Vector2(-1, -1), Vector2(-1, 1), Vector2(1, -1)]
|
|
|
|
var last_pixels = [null, null]
|
|
|
|
|
2020-05-01 17:47:10 +00:00
|
|
|
|
2020-05-07 14:02:35 +00:00
|
|
|
func reset() -> void:
|
2020-04-27 15:09:54 +00:00
|
|
|
last_pixels = [null, null]
|
|
|
|
|
2020-05-01 17:47:10 +00:00
|
|
|
|
2020-07-09 12:22:17 +00:00
|
|
|
func set_pixel(image: Image, position: Vector2, color: Color, op : ColorOp) -> void:
|
|
|
|
var color_old = image.get_pixelv(position)
|
|
|
|
last_pixels.push_back([position, color_old])
|
|
|
|
image.set_pixelv(position, op.process(color, color_old))
|
2020-04-27 15:09:54 +00:00
|
|
|
|
|
|
|
var corner = last_pixels.pop_front()
|
|
|
|
var neighbour = last_pixels[0]
|
|
|
|
|
|
|
|
if corner == null or neighbour == null:
|
|
|
|
return
|
|
|
|
|
2020-07-09 12:22:17 +00:00
|
|
|
if position - corner[0] in corners and position - neighbour[0] in neighbours:
|
|
|
|
image.set_pixel(neighbour[0].x, neighbour[0].y, neighbour[1])
|
2020-04-27 15:09:54 +00:00
|
|
|
last_pixels[0] = corner
|
2020-06-04 11:11:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
var pixel_perfect := false setget set_pixel_perfect
|
2020-07-09 12:22:17 +00:00
|
|
|
var horizontal_mirror := false
|
|
|
|
var vertical_mirror := false
|
|
|
|
var color_op := ColorOp.new()
|
2020-06-04 11:11:24 +00:00
|
|
|
|
|
|
|
var simple_drawer := SimpleDrawer.new()
|
|
|
|
var pixel_perfect_drawers = [PixelPerfectDrawer.new(), PixelPerfectDrawer.new(), PixelPerfectDrawer.new(), PixelPerfectDrawer.new()]
|
|
|
|
var drawers = [simple_drawer, simple_drawer, simple_drawer, simple_drawer]
|
|
|
|
|
|
|
|
|
|
|
|
func reset() -> void:
|
|
|
|
for drawer in pixel_perfect_drawers:
|
|
|
|
drawer.reset()
|
|
|
|
|
|
|
|
|
|
|
|
func set_pixel_perfect(value: bool) -> void:
|
|
|
|
pixel_perfect = value
|
|
|
|
if pixel_perfect:
|
|
|
|
drawers = pixel_perfect_drawers.duplicate()
|
|
|
|
else:
|
|
|
|
drawers = [simple_drawer, simple_drawer, simple_drawer, simple_drawer]
|
|
|
|
|
|
|
|
|
2020-07-09 12:22:17 +00:00
|
|
|
func set_pixel(image: Image, position: Vector2, color: Color) -> void:
|
|
|
|
var mirror_x = Global.current_project.x_max + Global.current_project.x_min - position.x - 1
|
|
|
|
var mirror_y = Global.current_project.y_max + Global.current_project.y_min - position.y - 1
|
2020-06-04 11:11:24 +00:00
|
|
|
|
2020-07-09 12:22:17 +00:00
|
|
|
drawers[0].set_pixel(image, position, color, color_op)
|
|
|
|
if horizontal_mirror:
|
|
|
|
drawers[1].set_pixel(image, Vector2(mirror_x, position.y), color, color_op)
|
|
|
|
if vertical_mirror:
|
|
|
|
drawers[2].set_pixel(image, Vector2(mirror_x, mirror_y), color, color_op)
|
|
|
|
if vertical_mirror:
|
|
|
|
drawers[3].set_pixel(image, Vector2(position.x, mirror_y), color, color_op)
|