1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-30 23:19:49 +00:00

Move pixel perfect and mirror code to drawers class (#255)

This commit is contained in:
Kinwailo 2020-06-04 19:11:24 +08:00 committed by GitHub
parent 7371cd79e4
commit 30f8891f21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 64 deletions

View file

@ -25,6 +25,11 @@ _global_script_classes=[ {
"path": "res://src/Classes/Cel.gd"
}, {
"base": "Reference",
"class": "Drawer",
"language": "GDScript",
"path": "res://src/Classes/Drawers.gd"
}, {
"base": "Reference",
"class": "Frame",
"language": "GDScript",
"path": "res://src/Classes/Frame.gd"
@ -58,6 +63,7 @@ _global_script_class_icons={
"AnimationTag": "",
"Canvas": "",
"Cel": "",
"Drawer": "",
"Frame": "",
"Guide": "",
"Layer": "",

View file

@ -1,21 +1,18 @@
extends Node
const Drawer = preload("res://src/Classes/Drawers.gd").Drawer
const SimpleDrawer = preload("res://src/Classes/Drawers.gd").SimpleDrawer
const PixelPerfectDrawer = preload("res://src/Classes/Drawers.gd").PixelPerfectDrawer
var pixel_perfect_drawer := PixelPerfectDrawer.new()
var pixel_perfect_drawer_h_mirror := PixelPerfectDrawer.new()
var pixel_perfect_drawer_v_mirror := PixelPerfectDrawer.new()
var pixel_perfect_drawer_hv_mirror := PixelPerfectDrawer.new()
var simple_drawer := SimpleDrawer.new()
var drawer := Drawer.new()
var mouse_press_pixels := [] # Cleared after mouse release
var mouse_press_pressure_values := [] # Cleared after mouse release
func draw_pixel_blended(sprite : Image, pos : Vector2, color : Color, pen_pressure : float, current_mouse_button := -1, current_action := -1, drawer : Drawer = simple_drawer) -> void:
func reset() -> void:
drawer.reset()
mouse_press_pixels.clear()
mouse_press_pressure_values.clear()
func draw_pixel_blended(sprite : Image, pos : Vector2, color : Color, pen_pressure : float, current_mouse_button := -1, current_action := -1) -> void:
var west_limit = Global.canvas.west_limit
var east_limit = Global.canvas.east_limit
var north_limit = Global.canvas.north_limit
@ -64,7 +61,12 @@ func draw_brush(sprite : Image, pos : Vector2, color : Color, current_mouse_butt
var horizontal_mirror : bool = Global.horizontal_mirror[current_mouse_button]
var vertical_mirror : bool = Global.vertical_mirror[current_mouse_button]
var pixel_perfect : bool = Global.pixel_perfect[current_mouse_button]
drawer.pixel_perfect = pixel_perfect if brush_size == 1 else false
drawer.h_mirror = horizontal_mirror
drawer.v_mirror = vertical_mirror
if brush_type == Global.Brush_Types.PIXEL || current_action == Global.Tools.LIGHTENDARKEN:
var start_pos_x = pos.x - (brush_size >> 1)
var start_pos_y = pos.y - (brush_size >> 1)
@ -72,43 +74,12 @@ func draw_brush(sprite : Image, pos : Vector2, color : Color, current_mouse_butt
var 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):
var pixel_perfect : bool = Global.pixel_perfect[current_mouse_button]
# warning-ignore:incompatible_ternary
var drawer : Drawer = pixel_perfect_drawer if pixel_perfect else simple_drawer
draw_pixel_blended(sprite, Vector2(cur_pos_x, cur_pos_y), color, pen_pressure, current_mouse_button, current_action, drawer)
# Handle mirroring
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:
# warning-ignore:incompatible_ternary
var drawer_h_mirror : Drawer = pixel_perfect_drawer_h_mirror if pixel_perfect else simple_drawer
draw_pixel_blended(sprite, Vector2(mirror_x, cur_pos_y), color, pen_pressure, current_mouse_button, current_action, drawer_h_mirror)
if vertical_mirror:
# warning-ignore:incompatible_ternary
var drawer_v_mirror : Drawer = pixel_perfect_drawer_v_mirror if pixel_perfect else simple_drawer
draw_pixel_blended(sprite, Vector2(cur_pos_x, mirror_y), color, pen_pressure, current_mouse_button, current_action, drawer_v_mirror)
if horizontal_mirror && vertical_mirror:
# warning-ignore:incompatible_ternary
var drawer_hv_mirror : Drawer = pixel_perfect_drawer_hv_mirror if pixel_perfect else simple_drawer
draw_pixel_blended(sprite, Vector2(mirror_x, mirror_y), color, pen_pressure, current_mouse_button, current_action, drawer_hv_mirror)
for cur_pos_y in range(start_pos_y, end_pos_y):
draw_pixel_blended(sprite, Vector2(cur_pos_x, cur_pos_y), color, pen_pressure, current_mouse_button, current_action)
Global.canvas.sprite_changed_this_frame = true
elif brush_type == Global.Brush_Types.CIRCLE || brush_type == Global.Brush_Types.FILLED_CIRCLE:
plot_circle(sprite, pos.x, pos.y, brush_size, color, brush_type == Global.Brush_Types.FILLED_CIRCLE)
# 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(sprite, mirror_x, pos.y, brush_size, color, brush_type == Global.Brush_Types.FILLED_CIRCLE)
if vertical_mirror:
plot_circle(sprite, pos.x, mirror_y, brush_size, color, brush_type == Global.Brush_Types.FILLED_CIRCLE)
if horizontal_mirror && vertical_mirror:
plot_circle(sprite, mirror_x, mirror_y, brush_size, color, brush_type == Global.Brush_Types.FILLED_CIRCLE)
Global.canvas.sprite_changed_this_frame = true
else:

View file

@ -99,12 +99,7 @@ func _input(event : InputEvent) -> void:
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
DrawingAlgos.mouse_press_pixels.clear()
DrawingAlgos.mouse_press_pressure_values.clear()
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()
DrawingAlgos.reset()
can_undo = true
current_pixel = get_local_mouse_position() + location

View file

@ -1,21 +1,11 @@
class Drawer:
func reset() -> void:
pass
func set_pixel(_sprite: Image, _pos: Vector2, _new_color: Color) -> void:
pass
class SimpleDrawer extends Drawer:
func reset() -> void:
pass
class_name Drawer
class SimpleDrawer:
func set_pixel(_sprite: Image, _pos: Vector2, _new_color: Color) -> void:
_sprite.set_pixel(_pos.x, _pos.y, _new_color)
class PixelPerfectDrawer extends Drawer:
class PixelPerfectDrawer:
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]
@ -38,3 +28,38 @@ class PixelPerfectDrawer extends Drawer:
if _pos - corner[0] in corners and _pos - neighbour[0] in neighbours:
_sprite.set_pixel(neighbour[0].x, neighbour[0].y, neighbour[1])
last_pixels[0] = corner
var pixel_perfect := false setget set_pixel_perfect
var h_mirror := false
var v_mirror := false
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]
func set_pixel(_sprite: Image, _pos: Vector2, _new_color: Color) -> void:
var mirror_x = Global.canvas.east_limit + Global.canvas.west_limit - _pos.x - 1
var mirror_y = Global.canvas.south_limit + Global.canvas.north_limit - _pos.y - 1
drawers[0].set_pixel(_sprite, _pos, _new_color)
if h_mirror:
drawers[1].set_pixel(_sprite, Vector2(mirror_x, _pos.y), _new_color)
if v_mirror:
drawers[2].set_pixel(_sprite, Vector2(mirror_x, mirror_y), _new_color)
if v_mirror:
drawers[3].set_pixel(_sprite, Vector2(_pos.x, mirror_y), _new_color)