got action do/undo working, added brush'

This commit is contained in:
cobrapitz 2020-10-26 20:31:41 +01:00
parent cfd5d9e6ba
commit f1787477fc
6 changed files with 99 additions and 20 deletions

View File

@ -1,4 +1,5 @@
extends Control
class_name GECanvas
tool
export var pixel_size: int = 16 setget set_pixel_size
@ -232,7 +233,7 @@ func get_pixel_v(pos: Vector2):
func get_pixel(x: int, y: int):
var idx = GEUtils.to_1D(x, y, canvas_width)
if active_layer:
if not active_layer.pixels.has(idx):
if active_layer.pixels.size() <= idx:
return null
return active_layer.pixels[idx]

View File

@ -72,7 +72,7 @@ func _enter_tree():
func _ready():
return
set_brush(Tools.PAINT)
func _input(event):
@ -83,7 +83,7 @@ func _input(event):
var brush_mode = Tools.PAINT
var brush_mode
var mouse_position = Vector2()
var canvas_position = Vector2()
@ -177,10 +177,20 @@ func brush_process():
if Input.is_mouse_button_pressed(BUTTON_LEFT):
# var arr = GEUtils.get_pixels_in_line(cell_mouse_position, last_cell_mouse_position)
# paint_canvas.set_pixel_arr(arr, selected_color)
do_action([cell_mouse_position, last_cell_mouse_position, selected_color])
if _current_action == null:
_current_action = get_action()
match brush_mode:
Tools.PAINT:
do_action([cell_mouse_position, last_cell_mouse_position, selected_color])
Tools.BRUSH:
do_action([cell_mouse_position, last_cell_mouse_position, selected_color, selected_brush_prefab])
return
else:
commit_action()
if _current_action and _current_action.can_commit():
commit_action()
return
if Input.is_mouse_button_pressed(BUTTON_LEFT):
@ -383,31 +393,27 @@ func _on_Save_pressed():
#---------------------------------------
func get_action():
match brush_mode:
Tools.PAINT:
return GEPencil.new()
return null
func do_action(data: Array):
if _current_action == null:
_redo_history.clear()
_current_action = GEPencil.new()
_current_action.do_action(paint_canvas, data)
func commit_action():
if not _current_action:
return
var action = GEPencil.new()
print("commit action")
_current_action.commit_action(paint_canvas)
var action = get_action()
action.action_data = _current_action.action_data.duplicate(true)
_actions_history.push_back(action)
_current_action = null
return
action.action_data = _current_action.action_data
if not "action_data" in action:
print(action.get_class())
return
action.action_data = _current_action.action_data.duplicate(true)
_actions_history.push_back(action)
_current_action.commit_action(paint_canvas)
_current_action = null
@ -420,13 +426,24 @@ func undo_action():
if not action:
return
action.undo_action(paint_canvas)
print("undo action")
func get_action():
match brush_mode:
Tools.PAINT:
return GEPencil.new()
Tools.BRUSH:
return GEBrush.new()
_:
print("no tool!")
return null
#---------------------------------------
# Brushes
#---------------------------------------
func set_brush(new_mode):
if brush_mode == new_mode:
return

View File

@ -14,7 +14,7 @@
[ext_resource path="res://icon.png" type="Texture" id=13]
[sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0.75, 0.75, 0.75, 1 )
bg_color = Color( 0.25, 0.25, 0.25, 1 )
border_width_left = 1
border_width_top = 1
border_width_right = 1

View File

@ -0,0 +1,46 @@
extends GEAction
class_name GEBrush
func do_action(canvas: GECanvas, data: Array):
if not "cells" in action_data.do:
action_data.do["cells"] = []
action_data.do["colors"] = []
if not "cells" in action_data.undo:
action_data.undo["cells"] = []
action_data.undo["colors"] = []
if "layer" in action_data.do:
action_data.do["layer"] = canvas.active_layer
action_data.undo["layer"] = canvas.active_layer
for pixel in GEUtils.get_pixels_in_line(data[0], data[1]):
for off in BrushPrefabs.list[data[3]]:
var p = pixel + off
if p in action_data.undo.cells or canvas.get_pixel_v(p) == null:
continue
action_data.undo.colors.append(canvas.get_pixel_v(p))
action_data.undo.cells.append(p)
canvas.set_pixel_v(p, data[2])
action_data.do.cells.append(p)
action_data.do.colors.append(data[2])
func commit_action(canvas):
var cells = action_data.do.cells
var colors = action_data.do.colors
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
for idx in range(cells.size()):
canvas.set_pixel_v(cells[idx], colors[idx])

View File

@ -11,8 +11,11 @@ func do_action(canvas, data: Array):
action_data.undo["cells"] = []
action_data.undo["colors"] = []
var pixels = GEUtils.get_pixels_in_line(data[0], data[1])
if "layer" in action_data.do:
action_data.do["layer"] = canvas.active_layer
action_data.undo["layer"] = canvas.active_layer
var pixels = GEUtils.get_pixels_in_line(data[0], data[1])
for pixel in pixels:
canvas.set_pixel_v(pixel, data[2])
action_data.do.cells.append(pixel)

View File

@ -19,6 +19,16 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://addons/graphics_editor/actions/Action.gd"
}, {
"base": "GEAction",
"class": "GEBrush",
"language": "GDScript",
"path": "res://addons/graphics_editor/actions/Brush.gd"
}, {
"base": "Control",
"class": "GECanvas",
"language": "GDScript",
"path": "res://addons/graphics_editor/Canvas.gd"
}, {
"base": "Reference",
"class": "GELayer",
"language": "GDScript",
@ -37,6 +47,8 @@ _global_script_classes=[ {
_global_script_class_icons={
"BrushPrefabs": "",
"GEAction": "",
"GEBrush": "",
"GECanvas": "",
"GELayer": "",
"GEPencil": "",
"GEUtils": ""