mirror of
https://github.com/Relintai/GraphicsEditor.git
synced 2025-02-19 15:54:23 +01:00
got action do/undo working, added brush'
This commit is contained in:
parent
cfd5d9e6ba
commit
f1787477fc
@ -1,4 +1,5 @@
|
|||||||
extends Control
|
extends Control
|
||||||
|
class_name GECanvas
|
||||||
tool
|
tool
|
||||||
|
|
||||||
export var pixel_size: int = 16 setget set_pixel_size
|
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):
|
func get_pixel(x: int, y: int):
|
||||||
var idx = GEUtils.to_1D(x, y, canvas_width)
|
var idx = GEUtils.to_1D(x, y, canvas_width)
|
||||||
if active_layer:
|
if active_layer:
|
||||||
if not active_layer.pixels.has(idx):
|
if active_layer.pixels.size() <= idx:
|
||||||
return null
|
return null
|
||||||
return active_layer.pixels[idx]
|
return active_layer.pixels[idx]
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ func _enter_tree():
|
|||||||
|
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
return
|
set_brush(Tools.PAINT)
|
||||||
|
|
||||||
|
|
||||||
func _input(event):
|
func _input(event):
|
||||||
@ -83,7 +83,7 @@ func _input(event):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
var brush_mode = Tools.PAINT
|
var brush_mode
|
||||||
|
|
||||||
var mouse_position = Vector2()
|
var mouse_position = Vector2()
|
||||||
var canvas_position = Vector2()
|
var canvas_position = Vector2()
|
||||||
@ -177,10 +177,20 @@ func brush_process():
|
|||||||
if Input.is_mouse_button_pressed(BUTTON_LEFT):
|
if Input.is_mouse_button_pressed(BUTTON_LEFT):
|
||||||
# var arr = GEUtils.get_pixels_in_line(cell_mouse_position, last_cell_mouse_position)
|
# var arr = GEUtils.get_pixels_in_line(cell_mouse_position, last_cell_mouse_position)
|
||||||
# paint_canvas.set_pixel_arr(arr, selected_color)
|
# 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
|
return
|
||||||
else:
|
else:
|
||||||
commit_action()
|
if _current_action and _current_action.can_commit():
|
||||||
|
commit_action()
|
||||||
|
|
||||||
return
|
return
|
||||||
if Input.is_mouse_button_pressed(BUTTON_LEFT):
|
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):
|
func do_action(data: Array):
|
||||||
if _current_action == null:
|
if _current_action == null:
|
||||||
_redo_history.clear()
|
_redo_history.clear()
|
||||||
_current_action = GEPencil.new()
|
|
||||||
_current_action.do_action(paint_canvas, data)
|
_current_action.do_action(paint_canvas, data)
|
||||||
|
|
||||||
|
|
||||||
func commit_action():
|
func commit_action():
|
||||||
if not _current_action:
|
if not _current_action:
|
||||||
return
|
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
|
action.action_data = _current_action.action_data
|
||||||
if not "action_data" in action:
|
if not "action_data" in action:
|
||||||
print(action.get_class())
|
print(action.get_class())
|
||||||
return
|
return
|
||||||
action.action_data = _current_action.action_data.duplicate(true)
|
|
||||||
_actions_history.push_back(action)
|
|
||||||
_current_action.commit_action(paint_canvas)
|
|
||||||
_current_action = null
|
_current_action = null
|
||||||
|
|
||||||
|
|
||||||
@ -420,13 +426,24 @@ func undo_action():
|
|||||||
if not action:
|
if not action:
|
||||||
return
|
return
|
||||||
action.undo_action(paint_canvas)
|
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
|
# Brushes
|
||||||
#---------------------------------------
|
#---------------------------------------
|
||||||
|
|
||||||
|
|
||||||
func set_brush(new_mode):
|
func set_brush(new_mode):
|
||||||
if brush_mode == new_mode:
|
if brush_mode == new_mode:
|
||||||
return
|
return
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
[ext_resource path="res://icon.png" type="Texture" id=13]
|
[ext_resource path="res://icon.png" type="Texture" id=13]
|
||||||
|
|
||||||
[sub_resource type="StyleBoxFlat" id=1]
|
[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_left = 1
|
||||||
border_width_top = 1
|
border_width_top = 1
|
||||||
border_width_right = 1
|
border_width_right = 1
|
||||||
|
46
addons/graphics_editor/actions/Brush.gd
Normal file
46
addons/graphics_editor/actions/Brush.gd
Normal 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])
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -11,8 +11,11 @@ func do_action(canvas, data: Array):
|
|||||||
action_data.undo["cells"] = []
|
action_data.undo["cells"] = []
|
||||||
action_data.undo["colors"] = []
|
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:
|
for pixel in pixels:
|
||||||
canvas.set_pixel_v(pixel, data[2])
|
canvas.set_pixel_v(pixel, data[2])
|
||||||
action_data.do.cells.append(pixel)
|
action_data.do.cells.append(pixel)
|
||||||
|
@ -19,6 +19,16 @@ _global_script_classes=[ {
|
|||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://addons/graphics_editor/actions/Action.gd"
|
"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",
|
"base": "Reference",
|
||||||
"class": "GELayer",
|
"class": "GELayer",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
@ -37,6 +47,8 @@ _global_script_classes=[ {
|
|||||||
_global_script_class_icons={
|
_global_script_class_icons={
|
||||||
"BrushPrefabs": "",
|
"BrushPrefabs": "",
|
||||||
"GEAction": "",
|
"GEAction": "",
|
||||||
|
"GEBrush": "",
|
||||||
|
"GECanvas": "",
|
||||||
"GELayer": "",
|
"GELayer": "",
|
||||||
"GEPencil": "",
|
"GEPencil": "",
|
||||||
"GEUtils": ""
|
"GEUtils": ""
|
||||||
|
Loading…
Reference in New Issue
Block a user