Skip to content

Commit

Permalink
Fix for painting undo/redo
Browse files Browse the repository at this point in the history
  • Loading branch information
RodZill4 committed Oct 11, 2024
1 parent 97e04bc commit 59bdda7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 37 deletions.
3 changes: 1 addition & 2 deletions material_maker/panels/paint/layer_types/layer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ static func save_layers(layers_array : Array, path : String) -> Array:
return layers_data

func set_state(s):
print(s)
for c in s.keys():
if c in get_channels():
set(c, s[c])
get(c).set_image(s[c])
else:
print("Useless channel %s in layer state" % c)
37 changes: 21 additions & 16 deletions material_maker/panels/paint/paint.gd
Original file line number Diff line number Diff line change
Expand Up @@ -952,22 +952,22 @@ func set_environment(index) -> void:

var stroke_history = { layers={} }

var redo_index : int = 0

func undoredo_command(command : Dictionary) -> void:
match command.type:
"reload_layer_state":
var layer = command.layer
var state = stroke_history.layers[layer].history[command.index]
if false:
for c in state.keys():
state[c].save_png("d:/redo_%d_%s.png" % [ redo_index, c ])
redo_index += 1
layer.set_state(state)
if layer == layers.selected_layer:
painter.set_state(state)
layers.select_layer(layers.selected_layer, true, false)
else:
layer.set_state(state)
await get_tree().process_frame
await get_tree().process_frame
await get_tree().process_frame
await get_tree().process_frame
await get_tree().process_frame
await get_tree().process_frame
layers._on_layers_changed()
layers._on_layers_changed()
stroke_history.layers[layer].current = command.index

func initialize_layer_history(layer):
Expand All @@ -979,12 +979,10 @@ func initialize_layer_history(layer):
await get_tree().process_frame
await get_tree().process_frame
for c in layer.get_channels():
var texture = layer.get_channel_texture(c)
if texture is ViewportTexture:
var image = texture.get_image()
texture = ImageTexture.new()
texture.set_image(image)
channels[c] = texture
var texture : Texture = layer.get_channel_texture(c)
var image : Image = Image.new()
image.copy_from(texture.get_image())
channels[c] = image
stroke_history.layers[layer] = { history=[channels], current=0 }

func initialize_layers_history(layer_list = null):
Expand All @@ -996,6 +994,8 @@ func initialize_layers_history(layer_list = null):
initialize_layer_history(l)
initialize_layers_history(l.layers)

var undo_index : int = 0

# Undo/Redo for strokes
func _on_Painter_end_of_stroke(stroke_state):
var layer = layers.selected_layer
Expand All @@ -1006,7 +1006,12 @@ func _on_Painter_end_of_stroke(stroke_state):
# Copy relevant channels into stroke state
for c in stroke_state.keys():
if c in layer.get_channels():
new_history_item[c] = stroke_state[c]
var channel_image : Image = Image.new()
channel_image.copy_from(stroke_state[c].get_texture().get_image())
if false:
channel_image.save_png("d:/undo_%d_%s.png" % [ undo_index, c ])
new_history_item[c] = channel_image
undo_index += 1
layer_history.history.push_back(new_history_item)
var undo_command = { type="reload_layer_state", layer=layer, index=layer_history.current }
layer_history.current += 1
Expand Down
1 change: 1 addition & 0 deletions material_maker/panels/paint/paint.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ scroll_offset = Vector2(0, -308)
shader_context_defs = "uniform sampler2D mesh_normal_tex;
"

[connection signal="end_of_stroke" from="Painter" to="." method="_on_Painter_end_of_stroke"]
[connection signal="painted" from="Painter" to="PaintLayers" method="_on_Painter_painted"]
[connection signal="layer_selected" from="PaintLayers" to="." method="_on_PaintLayers_layer_selected"]
[connection signal="dragged" from="VSplitContainer" to="." method="_on_v_split_container_dragged"]
Expand Down
6 changes: 3 additions & 3 deletions material_maker/panels/paint/paint_layers.gd
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ func get_occlusion_texture():
func _on_Tree_selection_changed(_old_selected : TreeItem, new_selected : TreeItem) -> void:
select_layer(new_selected.get_meta("layer"))

func select_layer(layer : MMLayer) -> void:
if layer == selected_layer:
func select_layer(layer : MMLayer, force : bool = false, update_last : bool = true) -> void:
if layer == selected_layer and not force:
return
if painter_node == null:
painter_node = get_node(painter)
if selected_layer != null:
if selected_layer != null and update_last:
for c in selected_layer.get_channels():
var old_texture : Texture2D = selected_layer.get(c)
var new_texture = ImageTexture.new()
Expand Down
23 changes: 7 additions & 16 deletions material_maker/tools/painter/painter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ func update_view(p : Projection, t : Transform3D, s : Vector2i):
brush_params.view_back = Vector3(0.0, 0.0, 1.0) * transform.basis.orthonormalized()
brush_params.view_right = Vector3(1.0, 0.0, 0.0) * transform.basis.orthonormalized()
brush_params.view_up = Vector3(0.0, 1.0, 0.0) * transform.basis.orthonormalized()
print("back: "+str(brush_params.view_back))
update_view_textures()
update_brush()

Expand Down Expand Up @@ -572,16 +573,14 @@ func on_dep_update_value(buffer_name : String, parameter_name : String, value) -
return false

func paint(shader_params : Dictionary, end_of_stroke : bool = false, emit_end_of_stroke : bool = true, on_mask : bool = false) -> void:
var active_viewports : Array = []
if on_mask:
active_viewports.push_back("mask")
var channels_infos : Array[Dictionary] = PAINT_CHANNELS_MASK if on_mask else PAINT_CHANNELS
for p in shader_params.keys():
var n : String
if PARAMETER_RENAMES.has(p):
n = PARAMETER_RENAMES[p]
else:
n = p
paint_shader.set_parameter(n, shader_params[p])
paint_shader.set_parameter(n, shader_params[p], true)
await paint_shader.render_ext(paint_textures, Vector2i(texture_size, texture_size))
for i in range(paint_textures.size()/3):
if OS.is_debug_build():
Expand All @@ -600,20 +599,12 @@ func paint(shader_params : Dictionary, end_of_stroke : bool = false, emit_end_of
await init_shader.render_ext([ paint_textures[i*3+1] ], Vector2i(texture_size, texture_size))
paint_textures[i*3+1].get_texture()
var stroke_state = {}
for v in active_viewports:
pass
# TODO: this is used for undo/redo
# stroke_state[v] = viewports[v].get_current_state()
for c in channels_infos.size():
var channel_name : String = channels_infos[c].name
if has_channel[channel_name]:
stroke_state[channel_name] = get_paint_channel(c).next_texture
emit_signal("end_of_stroke", stroke_state)

func set_state(s):
for c in s.keys():
pass
# TODO: initialize layers
#if viewports.has(c):
# viewports[c].init(Color(1, 1, 1, 1), s[c])
emit_signal("painted")

func fill(erase : bool, reset : bool = false, emit_end_of_stroke : bool = true) -> void:
paint({ brush_pos=Vector2(0, 0), brush_ppos=Vector2(0, 0), erase=erase, pressure=1.0, fill=true, reset=reset }, true, emit_end_of_stroke)

Expand Down

0 comments on commit 59bdda7

Please sign in to comment.