This commit is contained in:
cobrapitz 2020-11-03 19:23:53 +01:00
parent fd5907a8b4
commit 52b5363c9a
15 changed files with 113 additions and 115 deletions

View File

@ -54,6 +54,8 @@ func _enter_tree():
func _process(delta):
if not is_visible_in_tree():
return
var mouse_position = get_local_mouse_position()
var rect = Rect2(Vector2(0, 0), rect_size)
mouse_in_region = rect.has_point(mouse_position)

View File

@ -47,8 +47,6 @@ var _last_preview_draw_cell_pos = Vector2.ZERO
var _selection_cells = []
var _selection_colors = []
var _just_cut = false
var _show_cut = false
var _cut_pos = Vector2.ZERO
var _cut_size = Vector2.ZERO
@ -103,13 +101,9 @@ func _ready():
func _input(event):
if Rect2(Vector2(), paint_canvas_container_node.rect_size).has_point(
paint_canvas_container_node.get_local_mouse_position()):
mouse_in_region = true
elif mouse_in_region:
mouse_in_region = false
if not mouse_on_top or not mouse_in_region:
if not is_visible_in_tree():
return
if paint_canvas_container_node == null or paint_canvas == null:
return
if event is InputEventKey:
@ -118,74 +112,62 @@ func _input(event):
elif event.scancode == KEY_Y and event.is_pressed() and not event.is_echo():
redo_action()
_handle_zoom(event)
if is_mouse_in_canvas():
_handle_zoom(event)
if paint_canvas and \
not paint_canvas.is_active_layer_locked() and \
(paint_canvas.mouse_in_region and paint_canvas.mouse_on_top):
match brush_mode:
Tools.BUCKET:
if _current_action == null:
_current_action = get_action()
if event is InputEventMouseButton:
if paint_canvas.is_active_layer_locked():
return
if brush_mode == Tools.CUT:
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT:
if not event.pressed:
commit_action()
if (paint_canvas.mouse_in_region and paint_canvas.mouse_on_top):
if event is InputEventMouseButton:
match brush_mode:
Tools.BUCKET:
if event.button_index == BUTTON_LEFT:
if event.pressed:
do_action([cell_mouse_position, last_cell_mouse_position, selected_color])
Tools.COLORPICKER:
if event is InputEventMouseButton:
Tools.COLORPICKER:
if event.button_index == BUTTON_LEFT:
if event.pressed:
find_node("Colors").add_color_prefab(selected_color)
set_brush(_previous_tool)
Tools.PASTECUT:
if event.button_index == BUTTON_RIGHT:
if event.pressed:
commit_action()
set_brush(Tools.PAINT)
func _process(delta):
if not mouse_on_top or not mouse_in_region:
if not is_visible_in_tree():
return
update_text_info()
#It's a lot more easier to just keep updating the variables in here than just have a bunch of local variables
#in every update function and make it very messy
if paint_canvas == null:
#_check_variables()
set_process(false)
if paint_canvas_container_node == null or paint_canvas == null:
return
if mouse_on_top and mouse_in_region:
if is_mouse_in_canvas():
_handle_scroll()
#Update commonly used variables
var grid_size = paint_canvas.pixel_size
mouse_position = paint_canvas.get_local_mouse_position()
canvas_position = paint_canvas_container_node.rect_position
canvas_mouse_position = Vector2(mouse_position.x - canvas_position.x, mouse_position.y - canvas_position.y)
cell_mouse_position = Vector2(floor(canvas_mouse_position.x / grid_size), floor(canvas_mouse_position.y / grid_size))
if cell_mouse_position.x >= 0 and cell_mouse_position.y >= 0:
cell_color = paint_canvas.get_pixel(cell_mouse_position.x, cell_mouse_position.y)
#Update commonly used variables
var grid_size = paint_canvas.pixel_size
mouse_position = paint_canvas.get_local_mouse_position()
canvas_position = paint_canvas_container_node.rect_position
canvas_mouse_position = Vector2(mouse_position.x - canvas_position.x, mouse_position.y - canvas_position.y)
cell_mouse_position = Vector2(floor(canvas_mouse_position.x / grid_size), floor(canvas_mouse_position.y / grid_size))
if cell_mouse_position.x >= 0 and cell_mouse_position.y >= 0:
cell_color = paint_canvas.get_pixel(cell_mouse_position.x, cell_mouse_position.y)
update_text_info()
if (paint_canvas.mouse_in_region and paint_canvas.mouse_on_top):
if not paint_canvas.is_active_layer_locked():
if not paint_canvas.is_active_layer_locked():
if is_position_in_canvas(last_cell_mouse_position) or is_position_in_canvas(cell_mouse_position):
brush_process()
paint_canvas.tool_layer.clear()
#TODO add here brush prefab drawing
paint_canvas._set_pixel(paint_canvas.tool_layer,
cell_mouse_position.x, cell_mouse_position.y, selected_color)
paint_canvas.tool_layer.update_texture()
else:
paint_canvas.tool_layer.clear()
paint_canvas.tool_layer.update_texture()
#Canvas Shift Moving
if not mouse_position == last_mouse_position:
if paint_canvas.has_focus():
if Input.is_key_pressed(KEY_SHIFT):
if Input.is_mouse_button_pressed(BUTTON_LEFT):
var relative = mouse_position - last_mouse_position
paint_canvas.rect_position += relative
allow_drawing = false
else:
allow_drawing = true
_draw_tool_brush()
#Update last variables with the current variables
last_mouse_position = mouse_position
@ -195,6 +177,28 @@ func _process(delta):
last_cell_color = cell_color
func _draw_tool_brush():
paint_canvas.tool_layer.clear()
match brush_mode:
Tools.PASTECUT:
for idx in range(_selection_cells.size()):
var pixel = _selection_cells[idx]
if pixel.x < 0 or pixel.y < 0:
print(pixel)
var color = _selection_colors[idx]
pixel -= _cut_pos + _cut_size / 2
pixel += cell_mouse_position
paint_canvas._set_pixel_v(paint_canvas.tool_layer, pixel, color)
paint_canvas.update()
_:
paint_canvas._set_pixel(paint_canvas.tool_layer,
cell_mouse_position.x, cell_mouse_position.y, selected_color)
#TODO add here brush prefab drawing
paint_canvas.tool_layer.update_texture()
func _handle_scroll():
if Input.is_mouse_button_pressed(BUTTON_MIDDLE):
if _middle_mouse_pressed_start_pos == null:
@ -233,17 +237,9 @@ func _handle_zoom(event):
paint_canvas.rect_position.y = clamp(paint_canvas.rect_position.y, -paint_canvas.rect_size.y * 0.8, rect_size.y)
func _reset_cut_tool():
_just_cut = false
_show_cut = false
_selection_cells.clear()
_selection_colors.clear()
func _handle_cut():
if Input.is_mouse_button_pressed(BUTTON_RIGHT):
paint_canvas.clear_preview_layer()
_reset_cut_tool()
set_brush(_previous_tool)
return
@ -269,15 +265,9 @@ func _handle_cut():
func brush_process():
if _just_cut:
_handle_cut()
paint_canvas.update()
return
if Input.is_mouse_button_pressed(BUTTON_LEFT):
if _current_action == null and brush_mode != Tools.COLORPICKER:
_current_action = get_action()
match brush_mode:
Tools.PAINT:
do_action([cell_mouse_position, last_cell_mouse_position, selected_color])
@ -302,11 +292,9 @@ func brush_process():
_cut_pos, _cut_size])
Tools.RAINBOW:
do_action([cell_mouse_position, last_cell_mouse_position])
update()
paint_canvas.update()
elif Input.is_mouse_button_pressed(BUTTON_RIGHT):
update()
paint_canvas.update()
if _current_action == null:
_current_action = get_action()
@ -319,7 +307,6 @@ func brush_process():
else:
if _current_action and _current_action.can_commit():
commit_action()
update()
paint_canvas.update()
@ -377,21 +364,13 @@ func commit_action():
match brush_mode:
Tools.CUT:
if _just_cut:
continue
_cut_pos = _current_action.mouse_start_pos
_cut_size = _current_action.mouse_end_pos - _current_action.mouse_start_pos
_selection_cells = _current_action.action_data.redo.cells.duplicate()
_selection_colors = _current_action.action_data.redo.colors.duplicate()
_just_cut = true
_current_action = null
return
action.action_data = _current_action.action_data
if not "action_data" in action:
print(action.get_class())
return
_current_action = null
set_brush(Tools.PASTECUT)
_:
_current_action = null
func redo_action():
@ -438,6 +417,8 @@ func get_action():
return GEBrighten.new()
Tools.CUT:
return GECut.new()
Tools.PASTECUT:
return GEPasteCut.new()
_:
print("no tool!")
return null
@ -458,10 +439,14 @@ func set_brush(new_mode):
_previous_tool = brush_mode
brush_mode = new_mode
_current_action = get_action()
match _previous_tool:
Tools.CUT:
paint_canvas.clear_preview_layer()
_just_cut = false
Tools.PASTECUT:
_selection_cells.clear()
_selection_colors.clear()
Tools.BUCKET:
_current_action = null
print("Selected: ", Tools.keys()[brush_mode])
@ -635,3 +620,22 @@ func _on_PaintCanvasContainer_mouse_exited():
func _on_ColorPicker_popup_closed():
find_node("Colors").add_color_prefab(find_node("ColorPicker").color)
#---------------------------------------
# MISC
#---------------------------------------
func is_position_in_canvas(pos):
last_cell_mouse_position
if Rect2(Vector2(), paint_canvas_container_node.rect_size).has_point(
pos):
return true
return false
func is_mouse_in_canvas() -> bool:
if is_position_in_canvas(paint_canvas_container_node.get_local_mouse_position()):
return true #mouse_on_top # check if mouse is inside canvas
else:
return false

File diff suppressed because one or more lines are too long

View File

@ -12,25 +12,13 @@ func _enter_tree():
editor = get_parent()
func _ready():
#start_time = OS.get_ticks_msec()
return
get_node("CanvasOutlineToggle/CheckButton").pressed = canvas_outline.visible
get_node("CanvasOutlineColor/ColorPickerButton").color = canvas_outline.color
func _process(delta):
# if get_parent().paint_canvas_node != null:
# canvas_outline = get_parent().paint_canvas_node.get_node("CanvasOutline")
# end_time = OS.get_ticks_msec()
# print("[Settings] Found Editor node in %s seconds!" % [(end_time - start_time) / float(1000)])
# set_process(false)
pass
func _on_ColorPickerButton_color_changed(color):
canvas_outline.color = color
func _on_CheckButton_toggled(button_pressed):
canvas_outline.visible = button_pressed
func _on_Ok_pressed():
hide()

View File

@ -36,4 +36,6 @@ func _draw():
func _process(delta):
if not is_visible_in_tree():
return
update()

View File

@ -36,7 +36,6 @@ func commit_action(canvas):
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -30,7 +30,6 @@ func commit_action(canvas):
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -32,7 +32,6 @@ func commit_action(canvas):
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -5,6 +5,11 @@ const selection_color = Color(0.8, 0.8, 0.8, 0.5)
var mouse_start_pos = null
var mouse_end_pos = null
func can_commit() -> bool:
return false #ugly way of handling a cut
func do_action(canvas, data: Array):
.do_action(canvas, data)
@ -47,7 +52,7 @@ func commit_action(canvas):
var pos = p + Vector2(px, py)
var color = canvas.get_pixel(pos.x, pos.y)
if color == Color.transparent:
if color.a == 0.0:
continue
action_data.redo.cells.append(pos)
@ -63,7 +68,6 @@ func commit_action(canvas):
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -35,7 +35,6 @@ func commit_action(canvas):
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -40,7 +40,6 @@ func commit_action(canvas):
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -26,7 +26,6 @@ func commit_action(canvas):
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -38,7 +38,6 @@ func commit_action(canvas):
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -49,7 +49,6 @@ func commit_action(canvas):
func undo_action(canvas):
var cells = action_data.undo.cells
var colors = action_data.undo.colors
print(action_data.keys())
for idx in range(cells.size()):
canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx])

View File

@ -42,7 +42,7 @@ _global_script_classes=[ {
"base": "GEAction",
"class": "GECut",
"language": "GDScript",
"path": "res://addons/graphics_editor/actions/PasteCut.gd"
"path": "res://addons/graphics_editor/actions/Cut.gd"
}, {
"base": "GEAction",
"class": "GEDarken",
@ -60,6 +60,11 @@ _global_script_classes=[ {
"path": "res://addons/graphics_editor/actions/Line.gd"
}, {
"base": "GEAction",
"class": "GEPasteCut",
"language": "GDScript",
"path": "res://addons/graphics_editor/actions/PasteCut.gd"
}, {
"base": "GEAction",
"class": "GEPencil",
"language": "GDScript",
"path": "res://addons/graphics_editor/actions/Pencil.gd"
@ -90,6 +95,7 @@ _global_script_class_icons={
"GEDarken": "",
"GELayer": "",
"GELine": "",
"GEPasteCut": "",
"GEPencil": "",
"GERainbow": "",
"GERect": "",