Initial commit

This commit is contained in:
OverloadedOrama 2019-08-18 12:28:38 +03:00
commit f647a42752
71 changed files with 1548 additions and 0 deletions

25
Scripts/CameraMovement.gd Normal file
View file

@ -0,0 +1,25 @@
extends Camera2D
var zoom_min := Vector2(0.005, 0.005)
var zoom_max := Vector2(0.8, 0.8)
var drag := false
func _input(event) -> void:
if Global.can_draw && Global.has_focus:
if event.is_action_pressed("camera_drag"):
drag = true
elif event.is_action_released("camera_drag"):
drag = false
elif event.is_action_pressed("zoom_in"): # Wheel Up Event
zoom_camera(-1)
elif event.is_action_pressed("zoom_out"): # Wheel Down Event
zoom_camera(1)
elif event is InputEventMouseMotion && drag:
offset = offset - event.relative * zoom
# Zoom Camera
func zoom_camera(dir : int) -> void:
var zoom_margin = zoom * dir / 10
if zoom + zoom_margin > zoom_min && zoom + zoom_margin < zoom_max:
zoom += zoom_margin

233
Scripts/Canvas.gd Normal file
View file

@ -0,0 +1,233 @@
extends Node2D
class_name Canvas
var layers := []
var current_layer_index := 0
var trans_background : ImageTexture
var current_sprite : Image
var location := Vector2.ZERO
var size := Vector2(64, 64)
var previous_mouse_pos := Vector2.ZERO
var mouse_inside_canvas := false #used for undo
var sprite_changed_this_frame := false #for optimization purposes
var is_making_line := false
var line_2d : Line2D
var draw_grid := false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
Global.can_draw = false
#Background
trans_background = ImageTexture.new()
trans_background.create_from_image(load("res://Transparent Background.png"), 0)
#The sprite itself
if !current_sprite:
current_sprite = Image.new()
current_sprite.create(size.x, size.y, false, Image.FORMAT_RGBA8)
current_sprite.lock()
var tex := ImageTexture.new()
tex.create_from_image(current_sprite, 0)
#Store [Image, ImageTexture, Layer Name, Visibity boolean]
layers.append([current_sprite, tex, "Layer 0", true])
generate_layer_panels()
#Set camera offset to the center of canvas
$"../Camera2D".offset = size / 2
#Set camera zoom based on the sprite size
var bigger = max(size.x, size.y)
$"../Camera2D".zoom_max = Vector2(bigger, bigger) * 0.01
$"../Camera2D".zoom = Vector2(bigger, bigger) * 0.002
# warning-ignore:unused_argument
func _process(delta) -> void:
sprite_changed_this_frame = false
update()
var mouse_pos := get_local_mouse_position() - location
var current_mouse_button := "None"
var current_action := "None"
if Input.is_mouse_button_pressed(BUTTON_LEFT):
current_mouse_button = "L"
current_action = Global.current_left_tool
elif Input.is_mouse_button_pressed(BUTTON_RIGHT):
current_mouse_button = "R"
current_action = Global.current_right_tool
if !point_in_rectangle(mouse_pos, location, location + size):
if !Input.is_mouse_button_pressed(BUTTON_LEFT) && !Input.is_mouse_button_pressed(BUTTON_RIGHT):
if mouse_inside_canvas:
mouse_inside_canvas = false
match current_action:
"Pencil":
var current_color : Color
if current_mouse_button == "L":
current_color = Global.left_color_picker.color
elif current_mouse_button == "R":
current_color = Global.right_color_picker.color
pencil_and_eraser(mouse_pos, current_color)
"Eraser":
pencil_and_eraser(mouse_pos, Color(0, 0, 0, 0))
"Fill":
if point_in_rectangle(mouse_pos, location, location + size) && Global.can_draw && Global.has_focus:
var current_color : Color
if current_mouse_button == "L":
current_color = Global.left_color_picker.color
elif current_mouse_button == "R":
current_color = Global.right_color_picker.color
flood_fill(mouse_pos, layers[current_layer_index][0].get_pixelv(mouse_pos), current_color)
if !is_making_line:
previous_mouse_pos = mouse_pos
previous_mouse_pos.x = clamp(previous_mouse_pos.x, location.x, location.x + size.x)
previous_mouse_pos.y = clamp(previous_mouse_pos.y, location.y, location.y + size.y)
else:
line_2d.set_point_position(1, mouse_pos)
if sprite_changed_this_frame:
update_texture(current_layer_index)
func update_texture(layer_index : int):
layers[layer_index][1].create_from_image(layers[layer_index][0], 0)
get_layer_container(layer_index).get_child(0).get_child(1).texture = layers[layer_index][1]
func get_layer_container(layer_index : int) -> PanelContainer:
for container in Global.vbox_layer_container.get_children():
if container is PanelContainer && container.i == layer_index:
return container
return null
func _draw() -> void:
draw_texture_rect(trans_background, Rect2(location, size), true)
#for texture in layer_textures:
for texture in layers:
if texture[3]: #if it's visible
draw_texture(texture[1], location)
#Draw grid (causes lag - unused. If you wanna test it just set draw_grid = true)
if draw_grid:
for x in size.x:
for y in size.y:
draw_rect(Rect2(location.x + x, location.y + y, 1, 1), Color.black, false)
#Draw rectangle to indicate the pixel currently being hovered on
var mouse_pos := get_local_mouse_position() - location
if point_in_rectangle(mouse_pos, location, location + size):
mouse_pos = mouse_pos.floor()
draw_rect(Rect2(mouse_pos.x, mouse_pos.y, 1, 1), Color.red, false)
func generate_layer_panels() -> void:
for child in Global.vbox_layer_container.get_children():
if child is PanelContainer:
child.queue_free()
current_layer_index = layers.size() - 1
if layers.size() == 1:
Global.remove_layer_button.disabled = true
else:
Global.remove_layer_button.disabled = false
for i in range(layers.size() -1, -1, -1):
var layer_container = load("res://LayerContainer.tscn").instance()
#layer_names.insert(i, "Layer %s" % i)
layers[i][2] = "Layer %s" % i
layer_container.i = i
#layer_container.get_child(0).get_child(2).text = layer_names[i]
layer_container.get_child(0).get_child(2).text = layers[i][2]
layers[i][3] = true #set visible
layer_container.get_child(0).get_child(1).texture = layers[i][1]
Global.vbox_layer_container.add_child(layer_container)
func pencil_and_eraser(mouse_pos : Vector2, color : Color) -> void:
if Input.is_key_pressed(KEY_SHIFT):
if !is_making_line:
line_2d = Line2D.new()
line_2d.width = 0.5
line_2d.default_color = Color.darkgray
line_2d.add_point(previous_mouse_pos)
line_2d.add_point(mouse_pos)
add_child(line_2d)
is_making_line = true
else:
if is_making_line:
fill_gaps(mouse_pos, color)
is_making_line = false
line_2d.queue_free()
else:
if point_in_rectangle(mouse_pos, location, location + size):
mouse_inside_canvas = true
#Draw
draw_pixel(mouse_pos, color)
fill_gaps(mouse_pos, color) #Fill the gaps
#If mouse is not inside bounds but it used to be, fill the gaps
elif point_in_rectangle(previous_mouse_pos, location, location + size):
fill_gaps(mouse_pos, color)
func draw_pixel(pos : Vector2, color : Color) -> void:
if layers[current_layer_index][0].get_pixelv(pos) != color: #don't draw the same pixel over and over
if Global.can_draw && Global.has_focus:
#sprite.lock()
layers[current_layer_index][0].set_pixelv(pos, color)
#sprite.unlock()
sprite_changed_this_frame = true
func point_in_rectangle(p : Vector2, coord1 : Vector2, coord2 : Vector2) -> bool:
return p.x > coord1.x && p.y > coord1.y && p.x < coord2.x && p.y < coord2.y
#Bresenham's Algorithm
#Thanks to https://godotengine.org/qa/35276/tile-based-line-drawing-algorithm-efficiency
func fill_gaps(mouse_pos : Vector2, color : Color) -> void:
var previous_mouse_pos_floored = previous_mouse_pos.floor()
var mouse_pos_floored = mouse_pos.floor()
mouse_pos_floored.x = clamp(mouse_pos_floored.x, location.x - 1, location.x + size.x)
mouse_pos_floored.y = clamp(mouse_pos_floored.y, location.y - 1, location.y + size.y)
var dx := int(abs(mouse_pos_floored.x - previous_mouse_pos_floored.x))
var dy := int(-abs(mouse_pos_floored.y - previous_mouse_pos_floored.y))
var err := dx + dy
var e2 := err << 1 #err * 2
var sx = 1 if previous_mouse_pos_floored.x < mouse_pos_floored.x else -1
var sy = 1 if previous_mouse_pos_floored.y < mouse_pos_floored.y else -1
var x = previous_mouse_pos_floored.x
var y = previous_mouse_pos_floored.y
while !(x == mouse_pos_floored.x && y == mouse_pos_floored.y):
draw_pixel(Vector2(x, y), color)
e2 = err << 1
if e2 >= dy:
err += dy
x += sx
if e2 <= dx:
err += dx
y += sy
#Thanks to https://en.wikipedia.org/wiki/Flood_fill
func flood_fill(pos : Vector2, target_color : Color, replace_color : Color) -> void:
pos = pos.floor()
var pixel = layers[current_layer_index][0].get_pixelv(pos)
if target_color == replace_color:
return
elif pixel != target_color:
return
else:
var q = [pos]
for n in q:
var west : Vector2 = n
var east : Vector2 = n
while west.x >= location.x && layers[current_layer_index][0].get_pixelv(west) == target_color:
west += Vector2.LEFT
while east.x < location.x + size.x && layers[current_layer_index][0].get_pixelv(east) == target_color:
east += Vector2.RIGHT
for px in range(west.x + 1, east.x):
var p := Vector2(px, n.y)
draw_pixel(p, replace_color)
var north := p + Vector2.UP
var south := p + Vector2.DOWN
if north.y >= location.y && layers[current_layer_index][0].get_pixelv(north) == target_color:
q.append(north)
if south.y < location.y + size.y && layers[current_layer_index][0].get_pixelv(south) == target_color:
q.append(south)
func _on_Timer_timeout() -> void:
Global.can_draw = true

51
Scripts/Global.gd Normal file
View file

@ -0,0 +1,51 @@
extends Node
# warning-ignore:unused_class_variable
var can_draw := false
# warning-ignore:unused_class_variable
var has_focus := true
var canvas : Canvas
var canvas_parent
var left_color_picker : ColorPickerButton
var right_color_picker : ColorPickerButton
var file_menu : MenuButton
var edit_menu : MenuButton
var left_indicator : Sprite
var right_indicator : Sprite
var vbox_layer_container : VBoxContainer
var remove_layer_button : Button
var move_up_layer_button : Button
var move_down_layer_button : Button
var merge_down_layer_button : Button
# warning-ignore:unused_class_variable
var current_left_tool := "Pencil"
# warning-ignore:unused_class_variable
var current_right_tool := "Eraser"
func _ready() -> void:
var root = get_tree().get_root()
canvas = find_node_by_name(root, "Canvas")
canvas_parent = canvas.get_parent()
left_color_picker = find_node_by_name(root, "LeftColorPickerButton")
right_color_picker = find_node_by_name(root, "RightColorPickerButton")
file_menu = find_node_by_name(root, "FileMenu")
edit_menu = find_node_by_name(root, "EditMenu")
left_indicator = find_node_by_name(root, "LeftIndicator")
right_indicator = find_node_by_name(root, "RightIndicator")
vbox_layer_container = find_node_by_name(root, "VBoxLayerContainer")
remove_layer_button = find_node_by_name(root, "RemoveLayerButton")
move_up_layer_button = find_node_by_name(root, "MoveUpLayer")
move_down_layer_button = find_node_by_name(root, "MoveDownLayer")
merge_down_layer_button = find_node_by_name(root, "MergeDownLayer")
#Thanks to https://godotengine.org/qa/17524/how-to-find-an-instanced-scene-by-its-name
func find_node_by_name(root, node_name) -> Node:
if root.get_name() == node_name:
return root
for child in root.get_children():
if child.get_name() == node_name:
return child
var found = find_node_by_name(child, node_name)
if found:
return found
return null

60
Scripts/LayerContainer.gd Normal file
View file

@ -0,0 +1,60 @@
extends PanelContainer
var i
var currently_selected := false
var visibility_toggled := false
func _ready() -> void:
var stylebox = StyleBoxFlat.new()
stylebox.bg_color = Color("3d3b45")
add_stylebox_override("panel", stylebox)
changed_selection()
# warning-ignore:unused_argument
func _process(delta) -> void:
var mouse_pos := get_local_mouse_position() + rect_position
if point_in_rectangle(mouse_pos, rect_position, rect_position + rect_size) && !visibility_toggled:
if Input.is_action_just_pressed("left_mouse"):
Global.canvas.current_layer_index = i
changed_selection()
func changed_selection() -> void:
var parent = get_parent()
for child in parent.get_children():
if child is PanelContainer:
if Global.canvas.current_layer_index == child.i:
child.currently_selected = true
child.get_stylebox("panel").bg_color = Color("282532")
if Global.canvas.current_layer_index < Global.canvas.layers.size() - 1:
Global.move_up_layer_button.disabled = false
else:
Global.move_up_layer_button.disabled = true
if Global.canvas.current_layer_index > 0:
Global.move_down_layer_button.disabled = false
Global.merge_down_layer_button.disabled = false
else:
Global.move_down_layer_button.disabled = true
Global.merge_down_layer_button.disabled = true
else:
child.currently_selected = false
child.get_stylebox("panel").bg_color = Color("3d3b45")
func point_in_rectangle(p : Vector2, coord1 : Vector2, coord2 : Vector2) -> bool:
return p.x > coord1.x && p.y > coord1.y && p.x < coord2.x && p.y < coord2.y
func _on_VisibilityButton_pressed() -> void:
if Global.canvas.layers[i][3]:
Global.canvas.layers[i][3] = false
get_child(0).get_child(0).text = "I"
else:
Global.canvas.layers[i][3] = true
get_child(0).get_child(0).text = "V"
func _on_VisibilityButton_button_down() -> void:
visibility_toggled = true
func _on_VisibilityButton_button_up() -> void:
visibility_toggled = false

214
Scripts/Main.gd Normal file
View file

@ -0,0 +1,214 @@
extends Control
var current_path := ""
var opensprite_file_selected := false
var pencil_tool
var eraser_tool
var fill_tool
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
var file_menu_items := {
"New..." : KEY_MASK_CTRL + KEY_N,
"Import..." : KEY_MASK_CTRL + KEY_O,
"Export..." : KEY_MASK_CTRL + KEY_S,
"Export as..." : KEY_MASK_SHIFT + KEY_MASK_CTRL + KEY_S,
"Quit" : KEY_MASK_CTRL + KEY_Q
}
# var edit_menu_items := {
# "Undo" : KEY_MASK_CTRL + KEY_Z,
# "Redo" : KEY_MASK_SHIFT + KEY_MASK_CTRL + KEY_Z,
# "Scale Image" : 0
# }
var file_menu : PopupMenu = Global.file_menu.get_popup()
var edit_menu : PopupMenu = Global.edit_menu.get_popup()
var i = 0
for item in file_menu_items.keys():
file_menu.add_item(item, i, file_menu_items[item])
i += 1
# i = 0
# for item in edit_menu_items.keys():
# edit_menu.add_item(item, i, edit_menu_items[item])
# i += 1
file_menu.connect("id_pressed", self, "file_menu_id_pressed")
#edit_menu.connect("id_pressed", self, "edit_menu_id_pressed")
pencil_tool = $UI/ToolPanel/VBoxContainer/ToolsContainer/Pencil
eraser_tool = $UI/ToolPanel/VBoxContainer/ToolsContainer/Eraser
fill_tool = $UI/ToolPanel/VBoxContainer/ToolsContainer/Fill
pencil_tool.connect("pressed", self, "_on_Tool_pressed", [pencil_tool])
eraser_tool.connect("pressed", self, "_on_Tool_pressed", [eraser_tool])
fill_tool.connect("pressed", self, "_on_Tool_pressed", [fill_tool])
pencil_tool.hint_tooltip = "P for left mouse button, Alt + P for right mouse button"
eraser_tool.hint_tooltip = "E for left mouse button, Alt + E for right mouse button"
fill_tool.hint_tooltip = "B for left mouse button, Alt + B for right mouse button"
func _input(event):
#Handle tool shortcuts
if event.is_action_pressed("right_pencil_tool"):
_on_Tool_pressed(pencil_tool, false, false)
elif event.is_action_pressed("left_pencil_tool"):
_on_Tool_pressed(pencil_tool, false, true)
elif event.is_action_pressed("right_eraser_tool"):
_on_Tool_pressed(eraser_tool, false, false)
elif event.is_action_pressed("left_eraser_tool"):
_on_Tool_pressed(eraser_tool, false, true)
elif event.is_action_pressed("right_fill_tool"):
_on_Tool_pressed(fill_tool, false, false)
elif event.is_action_pressed("left_fill_tool"):
_on_Tool_pressed(fill_tool, false, true)
func file_menu_id_pressed(id : int) -> void:
match id:
0: #New
$CreateNewImage.popup_centered()
Global.can_draw = false
1: #Import
$OpenSprite.popup_centered()
Global.can_draw = false
opensprite_file_selected = false
2: #Export
if current_path == "":
$SaveSprite.popup_centered()
Global.can_draw = false
else:
save_sprite()
3: #Export as
$SaveSprite.popup_centered()
Global.can_draw = false
4: #Quit
get_tree().quit()
func _on_CreateNewImage_confirmed() -> void:
var width = float($CreateNewImage/VBoxContainer/WidthCont/LineEdit.text)
var height = float($CreateNewImage/VBoxContainer/HeightCont/LineEdit.text)
width = clamp(width, 1, 16384)
height = clamp(height, 1, 16384)
new_canvas(Vector2(width, height).floor())
func _on_OpenSprite_file_selected(path : String) -> void:
var image = Image.new()
var err = image.load(path)
if err == OK:
opensprite_file_selected = true
new_canvas(image.get_size(), image)
else:
OS.alert("Can't load file")
func new_canvas(size : Vector2, sprite : Image = null) -> void:
for child in Global.vbox_layer_container.get_children():
if child is PanelContainer:
child.queue_free()
Global.canvas.queue_free()
Global.canvas = load("res://Canvas.tscn").instance()
Global.canvas.size = size
if sprite:
Global.canvas.current_sprite = sprite
Global.canvas.current_sprite.convert(Image.FORMAT_RGBA8)
Global.canvas_parent.add_child(Global.canvas)
func _on_SaveSprite_file_selected(path : String) -> void:
current_path = path
save_sprite()
func save_sprite() -> void:
var whole_image := Image.new()
whole_image.create(Global.canvas.size.x, Global.canvas.size.y, false, Image.FORMAT_RGBA8)
for layer in Global.canvas.layers:
whole_image.blend_rect(layer[0], Rect2(Global.canvas.position, Global.canvas.size), Vector2.ZERO)
layer[0].lock()
#var err = Global.canvas.current_sprite.save_png(current_path)
var err = whole_image.save_png(current_path)
if err != OK:
OS.alert("Can't save file")
func _on_OpenSprite_popup_hide() -> void:
if !opensprite_file_selected:
Global.can_draw = true
print(Global.can_draw)
func _on_ViewportContainer_mouse_entered() -> void:
Global.has_focus = true
func _on_ViewportContainer_mouse_exited() -> void:
Global.has_focus = false
func _can_draw_true() -> void:
Global.can_draw = true
func _can_draw_false() -> void:
Global.can_draw = false
func _on_Tool_pressed(tool_pressed : BaseButton, mouse_press := true, key_for_left := true) -> void:
var current_action := tool_pressed.name
if (mouse_press && Input.is_action_just_released("left_mouse")) || (!mouse_press && key_for_left):
Global.current_left_tool = current_action
Global.left_indicator.get_parent().remove_child(Global.left_indicator)
tool_pressed.add_child(Global.left_indicator)
elif (mouse_press && Input.is_action_just_released("right_mouse")) || (!mouse_press && !key_for_left):
Global.current_right_tool = current_action
Global.right_indicator.get_parent().remove_child(Global.right_indicator)
tool_pressed.add_child(Global.right_indicator)
func _on_ScaleImage_confirmed() -> void:
var width = float($ScaleImage/VBoxContainer/WidthCont/LineEdit.text)
var height = float($ScaleImage/VBoxContainer/HeightCont/LineEdit.text)
width = clamp(width, 1, 16384)
height = clamp(height, 1, 16384)
#var sprites := []
for i in range(Global.canvas.layers.size() - 1, -1, -1):
var sprite = Image.new()
sprite = Global.canvas.layers[i][1].get_data()
sprite.resize(width, height)
Global.canvas.layers[i][0] = sprite
Global.canvas.layers[i][0].lock()
Global.canvas.update_texture(i)
Global.canvas.size = Vector2(width, height).floor()
func add_layer(is_new := true) -> void:
var new_layer := Image.new()
if is_new:
new_layer.create(Global.canvas.size.x, Global.canvas.size.y, false, Image.FORMAT_RGBA8)
else: #clone layer
new_layer.copy_from(Global.canvas.layers[Global.canvas.current_layer_index][0])
new_layer.lock()
var new_layer_tex := ImageTexture.new()
new_layer_tex.create_from_image(new_layer, 0)
Global.canvas.layers.append([new_layer, new_layer_tex, null, true])
Global.canvas.generate_layer_panels()
func _on_AddLayerButton_pressed() -> void:
add_layer()
func _on_RemoveLayerButton_pressed() -> void:
Global.canvas.layers.remove(Global.canvas.current_layer_index)
Global.canvas.generate_layer_panels()
func _on_MoveUpLayer_pressed() -> void:
change_layer_order(1)
func _on_MoveDownLayer_pressed() -> void:
change_layer_order(-1)
func change_layer_order(rate : int) -> void:
var change = Global.canvas.current_layer_index + rate
var temp = Global.canvas.layers[Global.canvas.current_layer_index]
Global.canvas.layers[Global.canvas.current_layer_index] = Global.canvas.layers[change]
Global.canvas.layers[change] = temp
Global.canvas.generate_layer_panels()
Global.canvas.current_layer_index = change
Global.canvas.get_layer_container(Global.canvas.current_layer_index).changed_selection()
func _on_CloneLayer_pressed() -> void:
add_layer(false)
func _on_MergeLayer_pressed() -> void:
var selected_layer = Global.canvas.layers[Global.canvas.current_layer_index][0]
Global.canvas.layers[Global.canvas.current_layer_index - 1][0].blend_rect(selected_layer, Rect2(Global.canvas.position, Global.canvas.size), Vector2.ZERO)
Global.canvas.layers[Global.canvas.current_layer_index - 1][0].lock()
Global.canvas.update_texture(Global.canvas.current_layer_index - 1)
_on_RemoveLayerButton_pressed()