2020-04-09 20:54:05 +00:00
|
|
|
extends Node
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
var current_save_paths := [] # Array of strings
|
2020-04-30 17:33:24 +00:00
|
|
|
# Stores a filename of a backup file in user:// until user saves manually
|
2021-11-25 12:48:30 +00:00
|
|
|
var backup_save_paths := [] # Array of strings
|
|
|
|
var preview_dialog_tscn = preload("res://src/UI/Dialogs/PreviewDialog.tscn")
|
2020-04-09 20:54:05 +00:00
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
onready var autosave_timer: Timer
|
2020-04-21 01:29:39 +00:00
|
|
|
|
2020-05-01 15:40:16 +00:00
|
|
|
|
|
|
|
func _ready() -> void:
|
2020-04-30 17:33:24 +00:00
|
|
|
autosave_timer = Timer.new()
|
|
|
|
autosave_timer.one_shot = false
|
|
|
|
autosave_timer.process_mode = Timer.TIMER_PROCESS_IDLE
|
|
|
|
autosave_timer.connect("timeout", self, "_on_Autosave_timeout")
|
|
|
|
add_child(autosave_timer)
|
2020-05-31 20:04:59 +00:00
|
|
|
update_autosave()
|
2020-04-30 17:33:24 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func handle_loading_files(files: PoolStringArray) -> void:
|
2020-06-11 22:11:58 +00:00
|
|
|
for file in files:
|
2020-06-17 00:56:46 +00:00
|
|
|
file = file.replace("\\", "/")
|
2021-11-25 12:48:30 +00:00
|
|
|
var file_ext: String = file.get_extension().to_lower()
|
|
|
|
if file_ext == "pxo": # Pixelorama project file
|
2020-06-11 22:11:58 +00:00
|
|
|
open_pxo_file(file)
|
2022-02-16 16:23:32 +00:00
|
|
|
|
|
|
|
elif file_ext == "tres": # Godot resource file
|
|
|
|
var resource = load(file)
|
|
|
|
if resource is Palette:
|
|
|
|
Palettes.import_palette(resource, file.get_file())
|
|
|
|
else:
|
|
|
|
var file_name: String = file.get_file()
|
|
|
|
Global.error_dialog.set_text(tr("Can't load file '%s'.") % [file_name])
|
|
|
|
Global.error_dialog.popup_centered()
|
|
|
|
Global.dialog_open(true)
|
|
|
|
|
|
|
|
elif file_ext == "gpl" or file_ext == "pal" or file_ext == "json":
|
|
|
|
Palettes.import_palette_from_path(file)
|
|
|
|
|
Implement a basic extension system
Importing .pck or .zip Godot resource pack files into Pixelorama is now possible. This needs to be documented properly, but here's the basic idea, for now at least. This is super early work and I haven't tested it with a proper extension yet, so all of this could be a subject of change. I tested it with a custom theme extension though and it seems to be working perfectly.
Importing resource pack files, either by dragging and dropping them into the app window or by going to Edit>Preferences>Extensions>Add Extension, copies the files into user://extensions/. Extensions can be enabled/disabled and uninstalled. Uninstalling them deletes the resource pack files from user://extensions/.
The extension project source files need to be in a folder inside src/Extensions/ with the same name as the .pck or .zip file. **This is required for now, otherwise it will not work.** Inside that folder there also needs to be an extension.json file, with a structure similar to this:
{
"name": "ExtensionName",
"display_name": "Extension Name",
"description": "A Pixelorama extension",
"author": "Orama Interactive",
"version": "0.1",
"license": "MIT",
"nodes": [
"ExtensionExample.tscn"
]
}
The `nodes` array leads to the packed scene files with the nodes that are to be instantiated. **The root nodes of these scenes need to have the same name as the .tscn files they belong to.** The scripts of these nodes should have _enter_tree() and _exit_tree() methods to handle the extension enabling/disabling (or even uninstalling) logic. Note that .json files need to be included in the export options while exporting the extension from Godot.
Enabling an extension means that the scenes found in the extension.json's "nodes" array get instantiated, and disabling gets rid of these nodes from Pixelorama's SceneTree.
2022-02-19 01:21:08 +00:00
|
|
|
elif file_ext in ["pck", "zip"]: # Godot resource pack file
|
|
|
|
Global.preferences_dialog.extensions.install_extension(file)
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
else: # Image files
|
2020-06-13 17:58:43 +00:00
|
|
|
var image := Image.new()
|
|
|
|
var err := image.load(file)
|
2021-11-25 12:48:30 +00:00
|
|
|
if err != OK: # An error occured
|
|
|
|
var file_name: String = file.get_file()
|
|
|
|
Global.error_dialog.set_text(
|
|
|
|
tr("Can't load file '%s'.\nError code: %s") % [file_name, str(err)]
|
|
|
|
)
|
2020-06-13 17:58:43 +00:00
|
|
|
Global.error_dialog.popup_centered()
|
|
|
|
Global.dialog_open(true)
|
|
|
|
continue
|
2020-06-30 19:29:24 +00:00
|
|
|
handle_loading_image(file, image)
|
2020-06-13 17:58:43 +00:00
|
|
|
|
2020-06-30 19:29:24 +00:00
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func handle_loading_image(file: String, image: Image) -> void:
|
|
|
|
var preview_dialog: ConfirmationDialog = preview_dialog_tscn.instance()
|
2020-06-30 19:29:24 +00:00
|
|
|
preview_dialog.path = file
|
|
|
|
preview_dialog.image = image
|
|
|
|
Global.control.add_child(preview_dialog)
|
|
|
|
preview_dialog.popup_centered()
|
|
|
|
Global.dialog_open(true)
|
2020-06-11 22:11:58 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func open_pxo_file(path: String, untitled_backup: bool = false, replace_empty: bool = true) -> void:
|
2020-04-09 20:54:05 +00:00
|
|
|
var file := File.new()
|
2020-04-09 22:06:24 +00:00
|
|
|
var err := file.open_compressed(path, File.READ, File.COMPRESSION_ZSTD)
|
|
|
|
if err == ERR_FILE_UNRECOGNIZED:
|
2021-11-25 12:48:30 +00:00
|
|
|
err = file.open(path, File.READ) # If the file is not compressed open it raw (pre-v0.7)
|
2020-04-09 22:06:24 +00:00
|
|
|
|
|
|
|
if err != OK:
|
2020-10-25 16:02:51 +00:00
|
|
|
Global.error_dialog.set_text(tr("File failed to open. Error code %s") % err)
|
|
|
|
Global.error_dialog.popup_centered()
|
|
|
|
Global.dialog_open(true)
|
2020-04-09 20:54:05 +00:00
|
|
|
file.close()
|
|
|
|
return
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
var empty_project: bool = Global.current_project.is_empty() and replace_empty
|
|
|
|
var new_project: Project
|
2020-06-06 22:48:35 +00:00
|
|
|
if empty_project:
|
|
|
|
new_project = Global.current_project
|
|
|
|
new_project.frames = []
|
|
|
|
new_project.layers = []
|
|
|
|
new_project.animation_tags.clear()
|
|
|
|
new_project.name = path.get_file()
|
|
|
|
else:
|
|
|
|
new_project = Project.new([], path.get_file())
|
2020-06-05 15:54:11 +00:00
|
|
|
|
2020-06-10 01:17:39 +00:00
|
|
|
var first_line := file.get_line()
|
|
|
|
var dict := JSON.parse(first_line)
|
|
|
|
if dict.error != OK:
|
|
|
|
open_old_pxo_file(file, new_project, first_line)
|
|
|
|
else:
|
|
|
|
if typeof(dict.result) != TYPE_DICTIONARY:
|
|
|
|
print("Error, json parsed result is: %s" % typeof(dict.result))
|
|
|
|
file.close()
|
|
|
|
return
|
|
|
|
|
|
|
|
new_project.deserialize(dict.result)
|
|
|
|
for frame in new_project.frames:
|
|
|
|
for cel in frame.cels:
|
|
|
|
var buffer := file.get_buffer(new_project.size.x * new_project.size.y * 4)
|
2021-11-25 12:48:30 +00:00
|
|
|
cel.image.create_from_data(
|
|
|
|
new_project.size.x, new_project.size.y, false, Image.FORMAT_RGBA8, buffer
|
|
|
|
)
|
|
|
|
cel.image = cel.image # Just to call image_changed
|
2020-06-10 01:17:39 +00:00
|
|
|
|
|
|
|
if dict.result.has("brushes"):
|
|
|
|
for brush in dict.result.brushes:
|
|
|
|
var b_width = brush.size_x
|
|
|
|
var b_height = brush.size_y
|
|
|
|
var buffer := file.get_buffer(b_width * b_height * 4)
|
|
|
|
var image := Image.new()
|
|
|
|
image.create_from_data(b_width, b_height, false, Image.FORMAT_RGBA8, buffer)
|
|
|
|
new_project.brushes.append(image)
|
2020-07-09 12:22:17 +00:00
|
|
|
Brushes.add_project_brush(image)
|
2020-06-10 01:17:39 +00:00
|
|
|
|
|
|
|
file.close()
|
|
|
|
if !empty_project:
|
|
|
|
Global.projects.append(new_project)
|
|
|
|
Global.tabs.current_tab = Global.tabs.get_tab_count() - 1
|
|
|
|
else:
|
2021-09-07 17:31:32 +00:00
|
|
|
if dict.error == OK and dict.result.has("fps"):
|
2021-08-25 13:37:09 +00:00
|
|
|
Global.animation_timeline.fps_spinbox.value = dict.result.fps
|
2021-11-25 12:48:30 +00:00
|
|
|
new_project.frames = new_project.frames # Just to call frames_changed
|
|
|
|
new_project.layers = new_project.layers # Just to call layers_changed
|
2020-06-10 01:17:39 +00:00
|
|
|
Global.canvas.camera_zoom()
|
|
|
|
|
|
|
|
if not untitled_backup:
|
|
|
|
# Untitled backup should not change window title and save path
|
|
|
|
current_save_paths[Global.current_project_index] = path
|
|
|
|
Global.window_title = path.get_file() + " - Pixelorama " + Global.current_version
|
2020-06-11 22:11:58 +00:00
|
|
|
Global.save_sprites_dialog.current_path = path
|
|
|
|
# Set last opened project path and save
|
|
|
|
Global.config_cache.set_value("preferences", "last_project_path", path)
|
|
|
|
Global.config_cache.save("user://cache.ini")
|
2020-07-31 20:26:52 +00:00
|
|
|
Export.file_name = path.get_file().trim_suffix(".pxo")
|
|
|
|
Export.directory_path = path.get_base_dir()
|
2020-10-29 20:35:20 +00:00
|
|
|
new_project.directory_path = Export.directory_path
|
|
|
|
new_project.file_name = Export.file_name
|
2020-07-31 20:26:52 +00:00
|
|
|
Export.was_exported = false
|
New selection system (#474)
* Basic move tool
* Added marching ants effect on the selection borders
* Rename SelectionRectangle to SelectionShape, make it have non-rectangular shape and multiple SelectionShapes can exist
- Create multiple selection rectangles
- Merge them together if they intersect
- Move the selections (without contents as of right now)
- Gizmos are being drawn but they are not functional yet
Code is very ugly.
* Sort vectors counter-clockwise to be used as polygon borders
I did this, no idea if it works properly, probably won't be used but I thought I'd keep it saved somewhere
* More experiments I may or may not need
Trying to generate a polygon from the individual selected pixels
* Change default rectangle select behavior and ability to clip polygons using Control
* Fix rectangle selection clipping
* Split polygon into two with selection subtracting
* Move selection with contents with the move tool
Code is still a mess, don't bother looking.
* Move some methods from SelectionShape.gd to Selection.gd
The purpose of this is to generalize some selection code, so that it applies to all polygons, the entire selection. More will follow.
* UndoRedo for border moving
Nothing else in the selections system works properly in UndoRedo right now. Needs:
- UR support for creating selections
- UR support for modifying selections (merging and cutting selections together)
- UR support for removing selection
- UR support for moving content
& for all the rest of the remaining features
* Moving all of the selection shape logic to Selection.gd
Handle all of the polygons there instead of having them as individual nodes. Should be easier to handle undo/redo this way. This commit probably breaks move tool + selection tool and undo/redo. Code is still a mess. For your sanity, I hope you are not reading this. I promise I will clean up.
* Move tool works again
Buggy and messy, of course.
* Remove unneeded code and restore selection move undoredo logic
* Made Selection.gd have one big preview_image for when moving content, instead of each polygon having its own image
Could be further optimized for some specific cases. We could also remove selected_pixels from SelectionPolygon.
* UndoRedo support for creating, deleting, merging and clipping selections
UndoRedo support for moving content not added in this commit. Should work but needs more testing. This PR also removes selected_pixels from the SelectionPolygon class.
* Confirm & cancel selection movement, should support undoredo properly too
Press Enter or do any editing to confirm movement, Escape to cancel. I will most likely add UI buttons for confirm and cancel too.
* Mirror View affects selection
* Restore Cut, Copy, Paste and Clear Selection
Pasting now no longer requires a pre-existing selection and instead copies the selections themselves too.
* Created a new Select menu, which has Select All and Clear Selection as options
Clear Selection now also confirms content moving. TopMenuContainer code has changed to no longer rely on Global for the menu buttons.
* Draw gizmos as rectangles
No functionality yet. They may need to be turned to nodes, so that they can easily resize based on zoom level and check for mouse enter/exit events.
* Made gizmos get drawn in the sides and corners of the big bounding rectangle instead of individual selection parts
Still no functionality yet.
* Restore label text
* Minor optimization when clipping selections
This will execute the for loop less times
* Made a Gizmo class, cursor change on hover, has_focus = false on mouse click
Now I should actually make them resize when dragged, aye?
* Very basic gizmo resizing, still a WIP, does not work properly yet
* Start replacing the array of selected pixels with a BitMap
This should optimize the selection making a lot, and it also allows for easy border drawing without having to deal with polygons, thanks to the MarchingAntsOutline.shader
Still commit is still a WIP, image effects and brushes may not work properly yet.
Because the BitMap has a fixed size, the size of the project, moving the selection outside of canvas boundaries has proven to be a bit tricky. I did implement a hacky way of handling it, but it may be buggy and problematic. I'm still unsure whether this is the best way to handle the situation.
* Selection works with mirror view
* Draw a black rectangle when the user is making a rectangular selection
After they release the mouse, the black rectangle becomes the selection
* Make Selection.gd update when undoing/redoing
* Fix brushes not working properly with non-rectangular selections
* Added invert selection
* Cache has_selection as a variable for a speedup
* Fix conflict issues with the shape tools
* Made the bitmap image squared so the marching ants effect will be the same on both dimensions
There may be a better way to fix the issue, perhaps inside the shader itself.
* Some optimizations to call selection_bitmap_changed() less times
* Restored almost all of the image effects
Left to do:
- Change gradient's behavior. Unsure of how it will work with non-rectangular selections yet, but it should not generate pixels outside of the selection.
- Restore rotation
- Resize bitmap on image resize
- Remove the `pixels` array from the ImageEffect
* Fix Selection.gd not updating when changing project
* Resize the selection bitmap along with image resize
* Restored rotation's old behavior and finally got rid of the selected_pixels array
The rotation does not yet work properly with selections, but at least it now "works".
* Resize selection too when using gizmos
Left to do for gizmos:
- Proper cancel transformation
- Begin transformation
(currently named move_content_start but it should be renamed to something more general) when resizing gizmos
- Keep the original image and selection in memory and resize them. Meaning, gizmos should not resize the already resized data, but only resize the original. This is less destructive as there is no danger of data loss.
- Always resize on InputEventMouseMotion. This is going to be worse for performance, but it will look better for the user.
* Image and bitmap resizing now uses the original data and begin transformation on gizmo click
No matter how many times the user resizes on the current transformation, the original data will not be lost until they either confirm or cancel, so there is no data loss before confirmation/cancel.
* Cancel transformation now works properly when the selection has been resized
* Made gizmos resize on mouse motion, fix issues with negative bounding rectangle and when combined with the move tool
* Resizing can now get out of positive bounds, clearing and inverting now gets limited to the canvas bounds
Resizing currently does not work properly with negative (left & up) canvas boundaries
* Flip image when resizing and the bounding rectangle gets flipped
* Call move_content_confirm() when inverting selection
* Attempt to implement selection resizing that goes outside of the canvas boundaries (not working properly yet)
* Flip selection when resizing to negative bounding rectangle sizes
And fix preview_image vertical flipping
* Fix rotation so that it works (almost) properly with selections
Rotation algorithms now accept and only work with a given image, and the pivot has been added as a parameter
* Experimental gizmo rotation - does not work properly yet
Transforming the selection outside of the canvas is still broken.
* Fix some issues with moving selection out of canvas bounds
* Fix more issues with selection getting resized outside of canvas bounds
* Update marching ants effect properly when switching between projects
And make sure the frequency of the marching ants effect always looks roughly the same on all project sizes
* Made the rotation gizmo part of the gizmos array and resize them based on camera zoom
* Remove unneeded parameter from move_bitmap_values()
* Remove more unneeded parameters
* Move the selection only if the cursor is above it and neither shift nor control are currently pressed
* Gradient generation now works on non-rectangular selections
Although this behavior might not be the intended one
* Copy/paste marching ants effect offset
Useful for when the selection is in negative coords
* Fix issue with clear selection & UndoRedo
* Restore the ability to move selection when it's in negative coords
* Made the marching ants offset a Project variable
This fixes the issue of project switching and keeping the previous project's offset. Again, this is only relevant for when the selection is in negative coords.
* Made the "from current selection" palette preset work with the new selection system
* Fix out of bounds error when using the rectangular select tool on negative coords
* Some code cleanup
* Comment out the rotation gizmo for now, since it does not work properly
* Update marching ants shader params and gizmo sizes when the bitmap changes
* Move some methods around in Selection.gd
2021-04-17 18:30:12 +00:00
|
|
|
Global.top_menu_container.file_menu.set_item_text(4, tr("Save") + " %s" % path.get_file())
|
|
|
|
Global.top_menu_container.file_menu.set_item_text(6, tr("Export"))
|
2020-10-27 21:03:43 +00:00
|
|
|
|
2021-11-22 15:37:06 +00:00
|
|
|
save_project_to_recent_list(path)
|
2020-06-10 01:17:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
# For pxo files older than v0.8
|
2021-11-25 12:48:30 +00:00
|
|
|
func open_old_pxo_file(file: File, new_project: Project, first_line: String) -> void:
|
2020-06-10 01:17:39 +00:00
|
|
|
# var file_version := file.get_line() # Example, "v0.7.10-beta"
|
|
|
|
var file_version := first_line
|
2020-05-09 19:58:09 +00:00
|
|
|
var file_ver_splitted := file_version.split("-")
|
|
|
|
var file_ver_splitted_numbers := file_ver_splitted[0].split(".")
|
|
|
|
|
|
|
|
# In the above example, the major version would return "0",
|
|
|
|
# the minor version would return "7", the patch "10"
|
|
|
|
# and the status would return "beta"
|
|
|
|
var file_major_version = int(file_ver_splitted_numbers[0].replace("v", ""))
|
|
|
|
var file_minor_version = int(file_ver_splitted_numbers[1])
|
2020-06-02 23:14:24 +00:00
|
|
|
var file_patch_version := 0
|
2020-05-09 19:58:09 +00:00
|
|
|
|
|
|
|
if file_ver_splitted_numbers.size() > 2:
|
2020-06-02 23:14:24 +00:00
|
|
|
file_patch_version = int(file_ver_splitted_numbers[2])
|
2020-04-09 20:54:05 +00:00
|
|
|
|
|
|
|
if file_major_version == 0 and file_minor_version < 5:
|
2021-11-25 12:48:30 +00:00
|
|
|
Global.notification_label(
|
|
|
|
"File is from an older version of Pixelorama, as such it might not work properly"
|
|
|
|
)
|
2020-04-09 20:54:05 +00:00
|
|
|
|
2020-06-02 23:14:24 +00:00
|
|
|
var new_guides := true
|
|
|
|
if file_major_version == 0:
|
|
|
|
if file_minor_version < 7 or (file_minor_version == 7 and file_patch_version == 0):
|
|
|
|
new_guides = false
|
|
|
|
|
2020-04-09 20:54:05 +00:00
|
|
|
var frame := 0
|
2020-05-10 22:56:02 +00:00
|
|
|
|
2020-05-09 00:51:23 +00:00
|
|
|
var linked_cels := []
|
2020-04-09 20:54:05 +00:00
|
|
|
if file_major_version >= 0 and file_minor_version > 6:
|
|
|
|
var global_layer_line := file.get_line()
|
|
|
|
while global_layer_line == ".":
|
|
|
|
var layer_name := file.get_line()
|
|
|
|
var layer_visibility := file.get_8()
|
|
|
|
var layer_lock := file.get_8()
|
2020-05-09 00:51:23 +00:00
|
|
|
var layer_new_cels_linked := file.get_8()
|
|
|
|
linked_cels.append(file.get_var())
|
2020-04-09 20:54:05 +00:00
|
|
|
|
2021-12-23 17:58:07 +00:00
|
|
|
var l := Layer.new(layer_name, layer_visibility, layer_lock, layer_new_cels_linked, [])
|
2020-06-05 15:54:11 +00:00
|
|
|
new_project.layers.append(l)
|
2020-04-09 20:54:05 +00:00
|
|
|
global_layer_line = file.get_line()
|
|
|
|
|
|
|
|
var frame_line := file.get_line()
|
2021-11-25 12:48:30 +00:00
|
|
|
while frame_line == "--": # Load frames
|
2020-06-02 23:14:24 +00:00
|
|
|
var frame_class := Frame.new()
|
2020-04-09 20:54:05 +00:00
|
|
|
var width := file.get_16()
|
|
|
|
var height := file.get_16()
|
|
|
|
|
2020-05-09 00:51:23 +00:00
|
|
|
var layer_i := 0
|
2020-04-09 20:54:05 +00:00
|
|
|
var layer_line := file.get_line()
|
2021-11-25 12:48:30 +00:00
|
|
|
while layer_line == "-": # Load layers
|
2020-04-09 20:54:05 +00:00
|
|
|
var buffer := file.get_buffer(width * height * 4)
|
|
|
|
if file_major_version == 0 and file_minor_version < 7:
|
|
|
|
var layer_name_old_version = file.get_line()
|
|
|
|
if frame == 0:
|
2020-06-01 13:42:53 +00:00
|
|
|
var l := Layer.new(layer_name_old_version)
|
2020-06-05 15:54:11 +00:00
|
|
|
new_project.layers.append(l)
|
2020-06-02 23:14:24 +00:00
|
|
|
var cel_opacity := 1.0
|
2020-04-09 20:54:05 +00:00
|
|
|
if file_major_version >= 0 and file_minor_version > 5:
|
2020-06-02 23:14:24 +00:00
|
|
|
cel_opacity = file.get_float()
|
2020-04-09 20:54:05 +00:00
|
|
|
var image := Image.new()
|
|
|
|
image.create_from_data(width, height, false, Image.FORMAT_RGBA8, buffer)
|
2020-06-02 23:14:24 +00:00
|
|
|
frame_class.cels.append(Cel.new(image, cel_opacity))
|
2020-05-09 19:58:09 +00:00
|
|
|
if file_major_version >= 0 and file_minor_version >= 7:
|
|
|
|
if frame in linked_cels[layer_i]:
|
2021-11-25 12:48:30 +00:00
|
|
|
var linked_cel: Cel = new_project.layers[layer_i].linked_cels[0].cels[layer_i]
|
2020-06-05 15:54:11 +00:00
|
|
|
new_project.layers[layer_i].linked_cels.append(frame_class)
|
2021-11-25 12:48:30 +00:00
|
|
|
frame_class.cels[layer_i].image = linked_cel.image
|
|
|
|
frame_class.cels[layer_i].image_texture = linked_cel.image_texture
|
2020-05-09 00:51:23 +00:00
|
|
|
|
|
|
|
layer_i += 1
|
2020-04-09 20:54:05 +00:00
|
|
|
layer_line = file.get_line()
|
|
|
|
|
2020-06-02 23:14:24 +00:00
|
|
|
if !new_guides:
|
2021-11-25 12:48:30 +00:00
|
|
|
var guide_line := file.get_line() # "guideline" no pun intended
|
|
|
|
while guide_line == "|": # Load guides
|
2020-06-02 23:14:24 +00:00
|
|
|
var guide := Guide.new()
|
|
|
|
guide.type = file.get_8()
|
|
|
|
if guide.type == guide.Types.HORIZONTAL:
|
|
|
|
guide.add_point(Vector2(-99999, file.get_16()))
|
|
|
|
guide.add_point(Vector2(99999, file.get_16()))
|
|
|
|
else:
|
|
|
|
guide.add_point(Vector2(file.get_16(), -99999))
|
|
|
|
guide.add_point(Vector2(file.get_16(), 99999))
|
|
|
|
guide.has_focus = false
|
|
|
|
Global.canvas.add_child(guide)
|
2020-06-10 01:17:39 +00:00
|
|
|
new_project.guides.append(guide)
|
2020-06-02 23:14:24 +00:00
|
|
|
guide_line = file.get_line()
|
|
|
|
|
2020-06-05 15:54:11 +00:00
|
|
|
new_project.size = Vector2(width, height)
|
|
|
|
new_project.frames.append(frame_class)
|
2020-06-02 23:14:24 +00:00
|
|
|
frame_line = file.get_line()
|
|
|
|
frame += 1
|
|
|
|
|
|
|
|
if new_guides:
|
2021-11-25 12:48:30 +00:00
|
|
|
var guide_line := file.get_line() # "guideline" no pun intended
|
|
|
|
while guide_line == "|": # Load guides
|
2020-04-09 20:54:05 +00:00
|
|
|
var guide := Guide.new()
|
|
|
|
guide.type = file.get_8()
|
|
|
|
if guide.type == guide.Types.HORIZONTAL:
|
|
|
|
guide.add_point(Vector2(-99999, file.get_16()))
|
|
|
|
guide.add_point(Vector2(99999, file.get_16()))
|
|
|
|
else:
|
|
|
|
guide.add_point(Vector2(file.get_16(), -99999))
|
|
|
|
guide.add_point(Vector2(file.get_16(), 99999))
|
|
|
|
guide.has_focus = false
|
2020-06-02 23:14:24 +00:00
|
|
|
Global.canvas.add_child(guide)
|
2020-06-10 01:17:39 +00:00
|
|
|
new_project.guides.append(guide)
|
2020-04-09 20:54:05 +00:00
|
|
|
guide_line = file.get_line()
|
|
|
|
|
|
|
|
# Load tool options
|
2020-07-09 12:22:17 +00:00
|
|
|
file.get_var()
|
|
|
|
file.get_var()
|
|
|
|
file.get_8()
|
|
|
|
file.get_8()
|
2020-04-09 20:54:05 +00:00
|
|
|
if file_major_version == 0 and file_minor_version < 7:
|
2020-07-09 12:22:17 +00:00
|
|
|
file.get_var()
|
|
|
|
file.get_var()
|
2020-04-09 20:54:05 +00:00
|
|
|
|
|
|
|
# Load custom brushes
|
|
|
|
var brush_line := file.get_line()
|
|
|
|
while brush_line == "/":
|
|
|
|
var b_width := file.get_16()
|
|
|
|
var b_height := file.get_16()
|
|
|
|
var buffer := file.get_buffer(b_width * b_height * 4)
|
|
|
|
var image := Image.new()
|
|
|
|
image.create_from_data(b_width, b_height, false, Image.FORMAT_RGBA8, buffer)
|
2020-06-05 15:54:11 +00:00
|
|
|
new_project.brushes.append(image)
|
2020-07-09 12:22:17 +00:00
|
|
|
Brushes.add_project_brush(image)
|
2020-04-09 20:54:05 +00:00
|
|
|
brush_line = file.get_line()
|
|
|
|
|
|
|
|
if file_major_version >= 0 and file_minor_version > 6:
|
|
|
|
var tag_line := file.get_line()
|
|
|
|
while tag_line == ".T/":
|
|
|
|
var tag_name := file.get_line()
|
2021-11-25 12:48:30 +00:00
|
|
|
var tag_color: Color = file.get_var()
|
2020-04-09 20:54:05 +00:00
|
|
|
var tag_from := file.get_8()
|
|
|
|
var tag_to := file.get_8()
|
2021-11-25 12:48:30 +00:00
|
|
|
new_project.animation_tags.append(
|
|
|
|
AnimationTag.new(tag_name, tag_color, tag_from, tag_to)
|
|
|
|
)
|
|
|
|
new_project.animation_tags = new_project.animation_tags # To execute animation_tags_changed()
|
2020-04-09 20:54:05 +00:00
|
|
|
tag_line = file.get_line()
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func save_pxo_file(
|
|
|
|
path: String,
|
|
|
|
autosave: bool,
|
|
|
|
use_zstd_compression := true,
|
|
|
|
project: Project = Global.current_project
|
|
|
|
) -> void:
|
2021-05-11 11:51:14 +00:00
|
|
|
if !autosave:
|
|
|
|
project.name = path.get_file()
|
2020-10-20 00:27:38 +00:00
|
|
|
var serialized_data = project.serialize()
|
|
|
|
if !serialized_data:
|
2021-11-25 12:48:30 +00:00
|
|
|
Global.error_dialog.set_text(
|
|
|
|
tr("File failed to save. Converting project data to dictionary failed.")
|
|
|
|
)
|
2020-10-25 16:02:51 +00:00
|
|
|
Global.error_dialog.popup_centered()
|
|
|
|
Global.dialog_open(true)
|
2020-10-20 00:27:38 +00:00
|
|
|
return
|
|
|
|
var to_save = JSON.print(serialized_data)
|
|
|
|
if !to_save:
|
2021-11-25 12:48:30 +00:00
|
|
|
Global.error_dialog.set_text(
|
|
|
|
tr("File failed to save. Converting dictionary to JSON failed.")
|
|
|
|
)
|
2020-10-25 16:02:51 +00:00
|
|
|
Global.error_dialog.popup_centered()
|
|
|
|
Global.dialog_open(true)
|
2020-10-20 00:27:38 +00:00
|
|
|
return
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
var file: File = File.new()
|
2020-07-10 23:09:17 +00:00
|
|
|
var err
|
|
|
|
if use_zstd_compression:
|
|
|
|
err = file.open_compressed(path, File.WRITE, File.COMPRESSION_ZSTD)
|
|
|
|
else:
|
|
|
|
err = file.open(path, File.WRITE)
|
|
|
|
|
2020-10-20 00:27:38 +00:00
|
|
|
if err != OK:
|
2020-10-25 16:02:51 +00:00
|
|
|
Global.error_dialog.set_text(tr("File failed to save. Error code %s") % err)
|
|
|
|
Global.error_dialog.popup_centered()
|
|
|
|
Global.dialog_open(true)
|
2020-10-20 00:27:38 +00:00
|
|
|
file.close()
|
|
|
|
return
|
2020-06-10 01:17:39 +00:00
|
|
|
|
2020-10-20 00:27:38 +00:00
|
|
|
if !autosave:
|
|
|
|
current_save_paths[Global.current_project_index] = path
|
2020-04-09 20:54:05 +00:00
|
|
|
|
2020-10-20 00:27:38 +00:00
|
|
|
file.store_line(to_save)
|
|
|
|
for frame in project.frames:
|
|
|
|
for cel in frame.cels:
|
|
|
|
file.store_buffer(cel.image.get_data())
|
2020-04-09 20:54:05 +00:00
|
|
|
|
2020-10-20 00:27:38 +00:00
|
|
|
for brush in project.brushes:
|
|
|
|
file.store_buffer(brush.get_data())
|
2020-04-30 17:33:24 +00:00
|
|
|
|
2020-10-20 00:27:38 +00:00
|
|
|
file.close()
|
2020-06-11 22:11:58 +00:00
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
if OS.get_name() == "HTML5" and OS.has_feature("JavaScript") and !autosave:
|
2020-10-20 00:27:38 +00:00
|
|
|
err = file.open(path, File.READ)
|
|
|
|
if !err:
|
|
|
|
var file_data = Array(file.get_buffer(file.get_len()))
|
2021-11-10 18:16:25 +00:00
|
|
|
JavaScript.download_buffer(file_data, path.get_file())
|
2020-06-10 01:17:39 +00:00
|
|
|
file.close()
|
2020-10-20 00:27:38 +00:00
|
|
|
# Remove the .pxo file from memory, as we don't need it anymore
|
|
|
|
var dir = Directory.new()
|
|
|
|
dir.remove(path)
|
|
|
|
|
|
|
|
if autosave:
|
|
|
|
Global.notification_label("File autosaved")
|
|
|
|
else:
|
|
|
|
# First remove backup then set current save path
|
|
|
|
if project.has_changed:
|
|
|
|
project.has_changed = false
|
|
|
|
remove_backup(Global.current_project_index)
|
|
|
|
Global.notification_label("File saved")
|
|
|
|
Global.window_title = path.get_file() + " - Pixelorama " + Global.current_version
|
|
|
|
|
|
|
|
# Set last opened project path and save
|
|
|
|
Global.config_cache.set_value("preferences", "last_project_path", path)
|
|
|
|
Global.config_cache.save("user://cache.ini")
|
|
|
|
Export.file_name = path.get_file().trim_suffix(".pxo")
|
|
|
|
Export.directory_path = path.get_base_dir()
|
|
|
|
Export.was_exported = false
|
2020-10-24 21:58:37 +00:00
|
|
|
project.was_exported = false
|
New selection system (#474)
* Basic move tool
* Added marching ants effect on the selection borders
* Rename SelectionRectangle to SelectionShape, make it have non-rectangular shape and multiple SelectionShapes can exist
- Create multiple selection rectangles
- Merge them together if they intersect
- Move the selections (without contents as of right now)
- Gizmos are being drawn but they are not functional yet
Code is very ugly.
* Sort vectors counter-clockwise to be used as polygon borders
I did this, no idea if it works properly, probably won't be used but I thought I'd keep it saved somewhere
* More experiments I may or may not need
Trying to generate a polygon from the individual selected pixels
* Change default rectangle select behavior and ability to clip polygons using Control
* Fix rectangle selection clipping
* Split polygon into two with selection subtracting
* Move selection with contents with the move tool
Code is still a mess, don't bother looking.
* Move some methods from SelectionShape.gd to Selection.gd
The purpose of this is to generalize some selection code, so that it applies to all polygons, the entire selection. More will follow.
* UndoRedo for border moving
Nothing else in the selections system works properly in UndoRedo right now. Needs:
- UR support for creating selections
- UR support for modifying selections (merging and cutting selections together)
- UR support for removing selection
- UR support for moving content
& for all the rest of the remaining features
* Moving all of the selection shape logic to Selection.gd
Handle all of the polygons there instead of having them as individual nodes. Should be easier to handle undo/redo this way. This commit probably breaks move tool + selection tool and undo/redo. Code is still a mess. For your sanity, I hope you are not reading this. I promise I will clean up.
* Move tool works again
Buggy and messy, of course.
* Remove unneeded code and restore selection move undoredo logic
* Made Selection.gd have one big preview_image for when moving content, instead of each polygon having its own image
Could be further optimized for some specific cases. We could also remove selected_pixels from SelectionPolygon.
* UndoRedo support for creating, deleting, merging and clipping selections
UndoRedo support for moving content not added in this commit. Should work but needs more testing. This PR also removes selected_pixels from the SelectionPolygon class.
* Confirm & cancel selection movement, should support undoredo properly too
Press Enter or do any editing to confirm movement, Escape to cancel. I will most likely add UI buttons for confirm and cancel too.
* Mirror View affects selection
* Restore Cut, Copy, Paste and Clear Selection
Pasting now no longer requires a pre-existing selection and instead copies the selections themselves too.
* Created a new Select menu, which has Select All and Clear Selection as options
Clear Selection now also confirms content moving. TopMenuContainer code has changed to no longer rely on Global for the menu buttons.
* Draw gizmos as rectangles
No functionality yet. They may need to be turned to nodes, so that they can easily resize based on zoom level and check for mouse enter/exit events.
* Made gizmos get drawn in the sides and corners of the big bounding rectangle instead of individual selection parts
Still no functionality yet.
* Restore label text
* Minor optimization when clipping selections
This will execute the for loop less times
* Made a Gizmo class, cursor change on hover, has_focus = false on mouse click
Now I should actually make them resize when dragged, aye?
* Very basic gizmo resizing, still a WIP, does not work properly yet
* Start replacing the array of selected pixels with a BitMap
This should optimize the selection making a lot, and it also allows for easy border drawing without having to deal with polygons, thanks to the MarchingAntsOutline.shader
Still commit is still a WIP, image effects and brushes may not work properly yet.
Because the BitMap has a fixed size, the size of the project, moving the selection outside of canvas boundaries has proven to be a bit tricky. I did implement a hacky way of handling it, but it may be buggy and problematic. I'm still unsure whether this is the best way to handle the situation.
* Selection works with mirror view
* Draw a black rectangle when the user is making a rectangular selection
After they release the mouse, the black rectangle becomes the selection
* Make Selection.gd update when undoing/redoing
* Fix brushes not working properly with non-rectangular selections
* Added invert selection
* Cache has_selection as a variable for a speedup
* Fix conflict issues with the shape tools
* Made the bitmap image squared so the marching ants effect will be the same on both dimensions
There may be a better way to fix the issue, perhaps inside the shader itself.
* Some optimizations to call selection_bitmap_changed() less times
* Restored almost all of the image effects
Left to do:
- Change gradient's behavior. Unsure of how it will work with non-rectangular selections yet, but it should not generate pixels outside of the selection.
- Restore rotation
- Resize bitmap on image resize
- Remove the `pixels` array from the ImageEffect
* Fix Selection.gd not updating when changing project
* Resize the selection bitmap along with image resize
* Restored rotation's old behavior and finally got rid of the selected_pixels array
The rotation does not yet work properly with selections, but at least it now "works".
* Resize selection too when using gizmos
Left to do for gizmos:
- Proper cancel transformation
- Begin transformation
(currently named move_content_start but it should be renamed to something more general) when resizing gizmos
- Keep the original image and selection in memory and resize them. Meaning, gizmos should not resize the already resized data, but only resize the original. This is less destructive as there is no danger of data loss.
- Always resize on InputEventMouseMotion. This is going to be worse for performance, but it will look better for the user.
* Image and bitmap resizing now uses the original data and begin transformation on gizmo click
No matter how many times the user resizes on the current transformation, the original data will not be lost until they either confirm or cancel, so there is no data loss before confirmation/cancel.
* Cancel transformation now works properly when the selection has been resized
* Made gizmos resize on mouse motion, fix issues with negative bounding rectangle and when combined with the move tool
* Resizing can now get out of positive bounds, clearing and inverting now gets limited to the canvas bounds
Resizing currently does not work properly with negative (left & up) canvas boundaries
* Flip image when resizing and the bounding rectangle gets flipped
* Call move_content_confirm() when inverting selection
* Attempt to implement selection resizing that goes outside of the canvas boundaries (not working properly yet)
* Flip selection when resizing to negative bounding rectangle sizes
And fix preview_image vertical flipping
* Fix rotation so that it works (almost) properly with selections
Rotation algorithms now accept and only work with a given image, and the pivot has been added as a parameter
* Experimental gizmo rotation - does not work properly yet
Transforming the selection outside of the canvas is still broken.
* Fix some issues with moving selection out of canvas bounds
* Fix more issues with selection getting resized outside of canvas bounds
* Update marching ants effect properly when switching between projects
And make sure the frequency of the marching ants effect always looks roughly the same on all project sizes
* Made the rotation gizmo part of the gizmos array and resize them based on camera zoom
* Remove unneeded parameter from move_bitmap_values()
* Remove more unneeded parameters
* Move the selection only if the cursor is above it and neither shift nor control are currently pressed
* Gradient generation now works on non-rectangular selections
Although this behavior might not be the intended one
* Copy/paste marching ants effect offset
Useful for when the selection is in negative coords
* Fix issue with clear selection & UndoRedo
* Restore the ability to move selection when it's in negative coords
* Made the marching ants offset a Project variable
This fixes the issue of project switching and keeping the previous project's offset. Again, this is only relevant for when the selection is in negative coords.
* Made the "from current selection" palette preset work with the new selection system
* Fix out of bounds error when using the rectangular select tool on negative coords
* Some code cleanup
* Comment out the rotation gizmo for now, since it does not work properly
* Update marching ants shader params and gizmo sizes when the bitmap changes
* Move some methods around in Selection.gd
2021-04-17 18:30:12 +00:00
|
|
|
Global.top_menu_container.file_menu.set_item_text(4, tr("Save") + " %s" % path.get_file())
|
2020-10-27 21:03:43 +00:00
|
|
|
|
2021-11-22 15:37:06 +00:00
|
|
|
save_project_to_recent_list(path)
|
2020-04-30 17:33:24 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func open_image_as_new_tab(path: String, image: Image) -> void:
|
2020-07-20 19:15:34 +00:00
|
|
|
var project = Project.new([], path.get_file(), image.get_size())
|
2020-06-11 22:11:58 +00:00
|
|
|
project.layers.append(Layer.new())
|
|
|
|
Global.projects.append(project)
|
|
|
|
|
|
|
|
var frame := Frame.new()
|
|
|
|
image.convert(Image.FORMAT_RGBA8)
|
|
|
|
frame.cels.append(Cel.new(image, 1))
|
|
|
|
|
|
|
|
project.frames.append(frame)
|
2020-06-16 14:59:56 +00:00
|
|
|
set_new_tab(project, path)
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func open_image_as_spritesheet_tab(path: String, image: Image, horiz: int, vert: int) -> void:
|
2020-06-16 14:59:56 +00:00
|
|
|
var project = Project.new([], path.get_file())
|
|
|
|
project.layers.append(Layer.new())
|
|
|
|
Global.projects.append(project)
|
2021-11-25 12:48:30 +00:00
|
|
|
horiz = min(horiz, image.get_size().x)
|
|
|
|
vert = min(vert, image.get_size().y)
|
|
|
|
var frame_width := image.get_size().x / horiz
|
|
|
|
var frame_height := image.get_size().y / vert
|
|
|
|
for yy in range(vert):
|
|
|
|
for xx in range(horiz):
|
2020-06-16 14:59:56 +00:00
|
|
|
var frame := Frame.new()
|
|
|
|
var cropped_image := Image.new()
|
2021-11-25 12:48:30 +00:00
|
|
|
cropped_image = image.get_rect(
|
|
|
|
Rect2(frame_width * xx, frame_height * yy, frame_width, frame_height)
|
|
|
|
)
|
2020-06-16 14:59:56 +00:00
|
|
|
project.size = cropped_image.get_size()
|
|
|
|
cropped_image.convert(Image.FORMAT_RGBA8)
|
|
|
|
frame.cels.append(Cel.new(cropped_image, 1))
|
|
|
|
|
|
|
|
for _i in range(1, project.layers.size()):
|
|
|
|
var empty_sprite := Image.new()
|
|
|
|
empty_sprite.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
|
|
|
|
empty_sprite.fill(Color(0, 0, 0, 0))
|
|
|
|
frame.cels.append(Cel.new(empty_sprite, 1))
|
|
|
|
|
|
|
|
project.frames.append(frame)
|
|
|
|
|
|
|
|
set_new_tab(project, path)
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func open_image_as_spritesheet_layer(
|
|
|
|
_path: String, image: Image, file_name: String, horizontal: int, vertical: int, start_frame: int
|
|
|
|
) -> void:
|
2021-02-07 14:43:44 +00:00
|
|
|
# data needed to slice images
|
|
|
|
horizontal = min(horizontal, image.get_size().x)
|
|
|
|
vertical = min(vertical, image.get_size().y)
|
|
|
|
var frame_width := image.get_size().x / horizontal
|
|
|
|
var frame_height := image.get_size().y / vertical
|
|
|
|
|
|
|
|
# resize canvas to if "frame_width" or "frame_height" is too large
|
2021-12-28 21:45:46 +00:00
|
|
|
var project: Project = Global.current_project
|
|
|
|
var project_width: int = max(frame_width, project.size.x)
|
|
|
|
var project_height: int = max(frame_height, project.size.y)
|
|
|
|
if project.size < Vector2(project_width, project_height):
|
|
|
|
DrawingAlgos.resize_canvas(project_width, project_height, 0, 0)
|
2021-02-07 14:43:44 +00:00
|
|
|
|
2021-12-28 21:45:46 +00:00
|
|
|
#initialize undo mechanism
|
|
|
|
project.undos += 1
|
|
|
|
project.undo_redo.create_action("Add Spritesheet Layer")
|
|
|
|
var new_layers: Array = project.layers.duplicate()
|
|
|
|
var new_frames: Array = project.frames.duplicate()
|
|
|
|
|
|
|
|
#Create new frames (if needed)
|
|
|
|
var new_frames_size = start_frame + (vertical * horizontal)
|
|
|
|
if new_frames_size > project.frames.size():
|
|
|
|
var required_frames = new_frames_size - project.frames.size()
|
|
|
|
for i in required_frames:
|
|
|
|
var frame: Frame = project.new_empty_frame()
|
|
|
|
new_frames.insert(project.current_frame + 1, frame)
|
|
|
|
#Create new layer for spritesheet
|
|
|
|
var layer := Layer.new(file_name)
|
|
|
|
new_layers.append(layer)
|
|
|
|
for f in new_frames:
|
|
|
|
var new_layer := Image.new()
|
|
|
|
new_layer.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
|
|
|
|
f.cels.append(Cel.new(new_layer, 1))
|
|
|
|
|
|
|
|
# slice spritesheet
|
2021-11-25 12:48:30 +00:00
|
|
|
var image_no: int = 0
|
2021-02-07 14:43:44 +00:00
|
|
|
for yy in range(vertical):
|
|
|
|
for xx in range(horizontal):
|
|
|
|
var cropped_image := Image.new()
|
2021-11-25 12:48:30 +00:00
|
|
|
cropped_image = image.get_rect(
|
|
|
|
Rect2(frame_width * xx, frame_height * yy, frame_width, frame_height)
|
|
|
|
)
|
2021-12-28 21:45:46 +00:00
|
|
|
cropped_image.crop(project.size.x, project.size.y)
|
|
|
|
var layer_index = new_layers.size() - 1
|
|
|
|
var frame_index = start_frame + image_no
|
|
|
|
|
|
|
|
for i in new_frames.size():
|
|
|
|
if i == frame_index:
|
|
|
|
cropped_image.convert(Image.FORMAT_RGBA8)
|
|
|
|
new_frames[i].cels[layer_index] = (Cel.new(cropped_image, 1))
|
2021-02-07 14:43:44 +00:00
|
|
|
image_no += 1
|
|
|
|
|
2021-12-28 21:45:46 +00:00
|
|
|
project.undo_redo.add_do_property(project, "current_frame", new_frames.size() - 1)
|
|
|
|
project.undo_redo.add_do_property(project, "current_layer", project.layers.size())
|
|
|
|
project.undo_redo.add_do_property(project, "layers", new_layers)
|
|
|
|
project.undo_redo.add_do_property(project, "frames", new_frames)
|
|
|
|
project.undo_redo.add_undo_property(project, "current_layer", project.current_layer)
|
|
|
|
project.undo_redo.add_undo_property(project, "current_frame", project.current_frame)
|
|
|
|
project.undo_redo.add_undo_property(project, "layers", project.layers)
|
|
|
|
project.undo_redo.add_undo_property(project, "frames", project.frames)
|
|
|
|
project.undo_redo.add_do_method(Global, "undo_or_redo", false)
|
|
|
|
project.undo_redo.add_undo_method(Global, "undo_or_redo", true)
|
|
|
|
project.undo_redo.commit_action()
|
|
|
|
|
2021-02-07 14:43:44 +00:00
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func open_image_at_frame(image: Image, layer_index := 0, frame_index := 0) -> void:
|
2021-02-07 14:43:44 +00:00
|
|
|
var project = Global.current_project
|
|
|
|
image.crop(project.size.x, project.size.y)
|
|
|
|
|
|
|
|
project.undos += 1
|
|
|
|
project.undo_redo.create_action("Replaced Frame")
|
2021-03-15 01:41:02 +00:00
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
var frames: Array = []
|
2021-02-07 14:43:44 +00:00
|
|
|
# create a duplicate of "project.frames"
|
|
|
|
for i in project.frames.size():
|
|
|
|
var frame := Frame.new()
|
|
|
|
frame.cels = project.frames[i].cels.duplicate(true)
|
|
|
|
frames.append(frame)
|
2021-03-15 01:41:02 +00:00
|
|
|
|
2021-02-07 14:43:44 +00:00
|
|
|
for i in project.frames.size():
|
|
|
|
if i == frame_index:
|
|
|
|
image.convert(Image.FORMAT_RGBA8)
|
|
|
|
frames[i].cels[layer_index] = (Cel.new(image, 1))
|
|
|
|
project.undo_redo.add_do_property(project.frames[i], "cels", frames[i].cels)
|
|
|
|
project.undo_redo.add_undo_property(project.frames[i], "cels", project.frames[i].cels)
|
|
|
|
|
|
|
|
project.undo_redo.add_do_property(project, "frames", frames)
|
|
|
|
project.undo_redo.add_do_property(project, "current_frame", frame_index)
|
|
|
|
|
|
|
|
project.undo_redo.add_undo_property(project, "frames", project.frames)
|
|
|
|
project.undo_redo.add_undo_property(project, "current_frame", project.current_frame)
|
2021-03-15 01:41:02 +00:00
|
|
|
|
2021-12-02 00:22:32 +00:00
|
|
|
project.undo_redo.add_do_method(Global, "undo_or_redo", false)
|
|
|
|
project.undo_redo.add_undo_method(Global, "undo_or_redo", true)
|
2021-02-07 14:43:44 +00:00
|
|
|
project.undo_redo.commit_action()
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func open_image_as_new_frame(image: Image, layer_index := 0) -> void:
|
2020-06-21 18:02:03 +00:00
|
|
|
var project = Global.current_project
|
|
|
|
image.crop(project.size.x, project.size.y)
|
2021-11-25 12:48:30 +00:00
|
|
|
var new_frames: Array = project.frames.duplicate()
|
2020-06-21 18:02:03 +00:00
|
|
|
|
|
|
|
var frame := Frame.new()
|
|
|
|
for i in project.layers.size():
|
|
|
|
if i == layer_index:
|
|
|
|
image.convert(Image.FORMAT_RGBA8)
|
|
|
|
frame.cels.append(Cel.new(image, 1))
|
|
|
|
else:
|
|
|
|
var empty_image := Image.new()
|
|
|
|
empty_image.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
|
|
|
|
frame.cels.append(Cel.new(empty_image, 1))
|
|
|
|
|
2020-06-21 18:39:16 +00:00
|
|
|
new_frames.append(frame)
|
|
|
|
|
2020-06-22 12:57:42 +00:00
|
|
|
project.undos += 1
|
|
|
|
project.undo_redo.create_action("Add Frame")
|
2021-12-02 00:22:32 +00:00
|
|
|
project.undo_redo.add_do_method(Global, "undo_or_redo", false)
|
|
|
|
project.undo_redo.add_undo_method(Global, "undo_or_redo", true)
|
2020-06-21 18:39:16 +00:00
|
|
|
|
2020-06-22 12:57:42 +00:00
|
|
|
project.undo_redo.add_do_property(project, "frames", new_frames)
|
|
|
|
project.undo_redo.add_do_property(project, "current_frame", new_frames.size() - 1)
|
|
|
|
project.undo_redo.add_do_property(project, "current_layer", layer_index)
|
2020-06-21 18:39:16 +00:00
|
|
|
|
2020-06-22 12:57:42 +00:00
|
|
|
project.undo_redo.add_undo_property(project, "frames", project.frames)
|
|
|
|
project.undo_redo.add_undo_property(project, "current_frame", project.current_frame)
|
|
|
|
project.undo_redo.add_undo_property(project, "current_layer", project.current_layer)
|
|
|
|
project.undo_redo.commit_action()
|
2020-06-21 18:20:39 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func open_image_as_new_layer(image: Image, file_name: String, frame_index := 0) -> void:
|
2020-06-21 18:20:39 +00:00
|
|
|
var project = Global.current_project
|
|
|
|
image.crop(project.size.x, project.size.y)
|
2021-11-25 12:48:30 +00:00
|
|
|
var new_layers: Array = Global.current_project.layers.duplicate()
|
2020-06-21 18:20:39 +00:00
|
|
|
var layer := Layer.new(file_name)
|
2020-06-22 12:57:42 +00:00
|
|
|
|
|
|
|
Global.current_project.undos += 1
|
|
|
|
Global.current_project.undo_redo.create_action("Add Layer")
|
2020-06-21 18:20:39 +00:00
|
|
|
for i in project.frames.size():
|
2021-11-25 12:48:30 +00:00
|
|
|
var new_cels: Array = project.frames[i].cels.duplicate(true)
|
2020-06-21 18:20:39 +00:00
|
|
|
if i == frame_index:
|
|
|
|
image.convert(Image.FORMAT_RGBA8)
|
2020-06-22 12:57:42 +00:00
|
|
|
new_cels.append(Cel.new(image, 1))
|
2020-06-21 18:20:39 +00:00
|
|
|
else:
|
|
|
|
var empty_image := Image.new()
|
|
|
|
empty_image.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
|
2020-06-22 12:57:42 +00:00
|
|
|
new_cels.append(Cel.new(empty_image, 1))
|
|
|
|
|
|
|
|
project.undo_redo.add_do_property(project.frames[i], "cels", new_cels)
|
|
|
|
project.undo_redo.add_undo_property(project.frames[i], "cels", project.frames[i].cels)
|
2020-06-21 18:20:39 +00:00
|
|
|
|
2020-06-21 18:39:16 +00:00
|
|
|
new_layers.append(layer)
|
|
|
|
|
2020-06-22 12:57:42 +00:00
|
|
|
project.undo_redo.add_do_property(project, "current_layer", new_layers.size() - 1)
|
|
|
|
project.undo_redo.add_do_property(project, "layers", new_layers)
|
|
|
|
project.undo_redo.add_do_property(project, "current_frame", frame_index)
|
2020-06-21 18:39:16 +00:00
|
|
|
|
2020-06-22 12:57:42 +00:00
|
|
|
project.undo_redo.add_undo_property(project, "current_layer", project.current_layer)
|
|
|
|
project.undo_redo.add_undo_property(project, "layers", project.layers)
|
|
|
|
project.undo_redo.add_undo_property(project, "current_frame", project.current_frame)
|
2020-06-21 18:39:16 +00:00
|
|
|
|
2021-12-02 00:22:32 +00:00
|
|
|
project.undo_redo.add_undo_method(Global, "undo_or_redo", true)
|
|
|
|
project.undo_redo.add_do_method(Global, "undo_or_redo", false)
|
2020-06-22 12:57:42 +00:00
|
|
|
project.undo_redo.commit_action()
|
2020-06-21 18:02:03 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func set_new_tab(project: Project, path: String) -> void:
|
2020-06-11 22:11:58 +00:00
|
|
|
Global.tabs.current_tab = Global.tabs.get_tab_count() - 1
|
|
|
|
Global.canvas.camera_zoom()
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
Global.window_title = (
|
|
|
|
path.get_file()
|
|
|
|
+ " ("
|
|
|
|
+ tr("imported")
|
|
|
|
+ ") - Pixelorama "
|
|
|
|
+ Global.current_version
|
|
|
|
)
|
2020-06-11 22:11:58 +00:00
|
|
|
if project.has_changed:
|
|
|
|
Global.window_title = Global.window_title + "(*)"
|
|
|
|
var file_name := path.get_basename().get_file()
|
|
|
|
var directory_path := path.get_basename().replace(file_name, "")
|
2020-10-24 21:58:37 +00:00
|
|
|
project.directory_path = directory_path
|
|
|
|
project.file_name = file_name
|
2020-07-31 20:26:52 +00:00
|
|
|
Export.directory_path = directory_path
|
|
|
|
Export.file_name = file_name
|
2020-06-11 22:11:58 +00:00
|
|
|
|
|
|
|
|
2020-05-31 20:04:59 +00:00
|
|
|
func update_autosave() -> void:
|
|
|
|
autosave_timer.stop()
|
2021-11-25 12:48:30 +00:00
|
|
|
# Interval parameter is in minutes, wait_time is seconds
|
|
|
|
autosave_timer.wait_time = Global.autosave_interval * 60
|
2020-05-31 20:04:59 +00:00
|
|
|
if Global.enable_autosave:
|
2020-04-30 17:33:24 +00:00
|
|
|
autosave_timer.start()
|
|
|
|
|
|
|
|
|
|
|
|
func _on_Autosave_timeout() -> void:
|
2020-06-05 23:16:53 +00:00
|
|
|
for i in range(backup_save_paths.size()):
|
|
|
|
if backup_save_paths[i] == "":
|
|
|
|
# Create a new backup file if it doesn't exist yet
|
|
|
|
backup_save_paths[i] = "user://backup-" + String(OS.get_unix_time()) + "-%s" % i
|
2020-04-30 17:33:24 +00:00
|
|
|
|
2020-06-05 23:16:53 +00:00
|
|
|
store_backup_path(i)
|
2020-07-10 23:09:17 +00:00
|
|
|
save_pxo_file(backup_save_paths[i], true, true, Global.projects[i])
|
2020-04-30 17:33:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Backup paths are stored in two ways:
|
|
|
|
# 1) User already manually saved and defined a save path -> {current_save_path, backup_save_path}
|
|
|
|
# 2) User didn't manually saved, "untitled" backup is stored -> {backup_save_path, backup_save_path}
|
2021-11-25 12:48:30 +00:00
|
|
|
func store_backup_path(i: int) -> void:
|
2020-06-05 23:16:53 +00:00
|
|
|
if current_save_paths[i] != "":
|
2020-04-30 17:33:24 +00:00
|
|
|
# Remove "untitled" backup if it existed on this project instance
|
2020-06-05 23:16:53 +00:00
|
|
|
if Global.config_cache.has_section_key("backups", backup_save_paths[i]):
|
|
|
|
Global.config_cache.erase_section_key("backups", backup_save_paths[i])
|
2020-04-30 17:33:24 +00:00
|
|
|
|
2020-06-05 23:16:53 +00:00
|
|
|
Global.config_cache.set_value("backups", current_save_paths[i], backup_save_paths[i])
|
2020-04-30 17:33:24 +00:00
|
|
|
else:
|
2020-06-05 23:16:53 +00:00
|
|
|
Global.config_cache.set_value("backups", backup_save_paths[i], backup_save_paths[i])
|
2020-04-30 17:33:24 +00:00
|
|
|
|
|
|
|
Global.config_cache.save("user://cache.ini")
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func remove_backup(i: int) -> void:
|
2020-04-30 17:33:24 +00:00
|
|
|
# Remove backup file
|
2020-06-05 23:16:53 +00:00
|
|
|
if backup_save_paths[i] != "":
|
|
|
|
if current_save_paths[i] != "":
|
|
|
|
remove_backup_by_path(current_save_paths[i], backup_save_paths[i])
|
2020-04-30 17:33:24 +00:00
|
|
|
else:
|
|
|
|
# If manual save was not yet done - remove "untitled" backup
|
2020-06-05 23:16:53 +00:00
|
|
|
remove_backup_by_path(backup_save_paths[i], backup_save_paths[i])
|
|
|
|
backup_save_paths[i] = ""
|
2020-04-30 17:33:24 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func remove_backup_by_path(project_path: String, backup_path: String) -> void:
|
2020-04-30 17:33:24 +00:00
|
|
|
Directory.new().remove(backup_path)
|
2020-09-05 18:55:12 +00:00
|
|
|
if Global.config_cache.has_section_key("backups", project_path):
|
|
|
|
Global.config_cache.erase_section_key("backups", project_path)
|
|
|
|
elif Global.config_cache.has_section_key("backups", backup_path):
|
|
|
|
Global.config_cache.erase_section_key("backups", backup_path)
|
2020-04-30 17:33:24 +00:00
|
|
|
Global.config_cache.save("user://cache.ini")
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func reload_backup_file(project_paths: Array, backup_paths: Array) -> void:
|
2021-01-30 17:30:02 +00:00
|
|
|
assert(project_paths.size() == backup_paths.size())
|
2020-09-05 19:27:11 +00:00
|
|
|
# Clear non-existant backups
|
2021-01-30 17:30:02 +00:00
|
|
|
var existing_backups_count := 0
|
2020-09-05 19:27:11 +00:00
|
|
|
var dir := Directory.new()
|
2021-01-30 17:30:02 +00:00
|
|
|
for i in range(backup_paths.size()):
|
|
|
|
if dir.file_exists(backup_paths[i]):
|
|
|
|
project_paths[existing_backups_count] = project_paths[i]
|
|
|
|
backup_paths[existing_backups_count] = backup_paths[i]
|
|
|
|
existing_backups_count += 1
|
|
|
|
else:
|
|
|
|
if Global.config_cache.has_section_key("backups", backup_paths[i]):
|
|
|
|
Global.config_cache.erase_section_key("backups", backup_paths[i])
|
2020-09-05 19:27:11 +00:00
|
|
|
Global.config_cache.save("user://cache.ini")
|
2021-01-30 17:30:02 +00:00
|
|
|
project_paths.resize(existing_backups_count)
|
|
|
|
backup_paths.resize(existing_backups_count)
|
2020-09-05 19:27:11 +00:00
|
|
|
|
|
|
|
# Load the backup files
|
2020-06-05 23:16:53 +00:00
|
|
|
for i in range(project_paths.size()):
|
2021-01-30 18:33:00 +00:00
|
|
|
open_pxo_file(backup_paths[i], project_paths[i] == backup_paths[i], i == 0)
|
2020-09-05 19:27:11 +00:00
|
|
|
backup_save_paths[i] = backup_paths[i]
|
2020-04-30 17:33:24 +00:00
|
|
|
|
2020-09-05 19:27:11 +00:00
|
|
|
# If project path is the same as backup save path -> the backup was untitled
|
2021-11-25 12:48:30 +00:00
|
|
|
if project_paths[i] != backup_paths[i]: # If the user has saved
|
2020-06-06 22:48:35 +00:00
|
|
|
current_save_paths[i] = project_paths[i]
|
2021-11-25 12:48:30 +00:00
|
|
|
Global.window_title = (
|
|
|
|
project_paths[i].get_file()
|
|
|
|
+ " - Pixelorama(*) "
|
|
|
|
+ Global.current_version
|
|
|
|
)
|
2020-06-05 23:16:53 +00:00
|
|
|
Global.current_project.has_changed = true
|
2020-04-30 17:33:24 +00:00
|
|
|
|
|
|
|
Global.notification_label("Backup reloaded")
|
2021-11-22 15:37:06 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func save_project_to_recent_list(path: String) -> void:
|
|
|
|
var top_menu_container: Panel = Global.top_menu_container
|
2021-11-22 15:37:06 +00:00
|
|
|
if path.get_file().substr(0, 7) == "backup-" or path == "":
|
|
|
|
return
|
|
|
|
|
|
|
|
if top_menu_container.recent_projects.has(path):
|
|
|
|
return
|
|
|
|
|
|
|
|
if top_menu_container.recent_projects.size() >= 5:
|
|
|
|
top_menu_container.recent_projects.pop_front()
|
|
|
|
top_menu_container.recent_projects.push_back(path)
|
|
|
|
|
|
|
|
Global.config_cache.set_value("data", "recent_projects", top_menu_container.recent_projects)
|
|
|
|
|
2021-11-23 00:36:22 +00:00
|
|
|
Global.top_menu_container.recent_projects_submenu.clear()
|
2021-11-22 15:37:06 +00:00
|
|
|
top_menu_container.update_recent_projects_submenu()
|