1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-03-01 08:53:14 +00:00
Pixelorama/src/UI/Dialogs/PreviewDialog.gd
Emmanouil Papadeas 91bfef16b3
Port to Godot 4 (#900)
* Initial conversion

* Hide some dialogs

* Update addons

* Fix errors in scripts

Temporarily commented out adding new file import types and certain Control methods that are not available in the Window class, such as get_global_mouse_position()

* Update shaders

* Fix some more errors and signals, rename "pressed" to "button_pressed"

* Even more error fixes and renaming corrections

* Fixed more errors, Pixelorama almost runs

* Update ValueSlider.gd

* Remove lock() and unlock(), more ImageTexture.create_from_image() static usage

* More static function using

* Re-add some of the dialog signals, fix window transparency

* Change instances of popup_hide to visibility_changed

* More more errors and warnings

* Fix more errors and warnings

* Get rid of errors in the output when opening Pixelorama in Godot

* Properly connect most signals without using strings

* Fix some scenes

* Don't load Main.tscn

* Emit signals directly instead of using strings

* Fix Keychain menu nodes

* Get rid of self. on most instances, as setters and getters are now always called

* Some more static typing

* Disable texture filters

* Fix zooming

* Fix int as enum warnings

* Fix tools and rename doubleclick to double_click

* Update tool scenes

* Fix tabs

* Fix create new image

* Use static typing on flood fill to speed it up

* Update static-checks.yml

* Reverts #729 for a speedup, hopefully the bug won't get re-introduced

* Fix TransparentChecker warning

* Re-add Default template

* Fix 3D cels

* Fix rotation

Project converted bug. Should be fixed by https://github.com/godotengine/godot/pull/79264

* Fix UITransparency alignment issue, thanks Variable

* Add missing OptionButton items

Hopefully that should be all of them

* Fix the appearance of CollapsibleContainer

* Change instances of world to world_3d

* Fix tool button backgrounds

* Fix Splash dialog

* Fix brush selection

* Update Main.gd

* Fix About Dialog

* Fix more zooming issues

* Fix canvas preview zooming

* Use signals for queue_redraw on project change

* Fix layer button's look

* Fix gradients

* Some gradient fixes and code cleanups, dithering is still broken

* Fix bucket

* Fix the rest of the undo_redo.add_(un)do_method() cases

* Fix guides

* Fix guide text

* Some small changes in Main

* Update Tools.gd

* Fix palette importing

* Get rid of TODOGODOT4s

* Fix the rest of the dialogs

* Update the rest of the scenes

* Fix onion skinning and frame tag dialogs

* Fix file brushes being imported twice

* Fix palette swatch crashing on double click

* Use nearest filter for some of the windows

* Remove old .tres font files

* Fix language switching

* Get rid of Keychain.action_get_first_key() on the extra shortcuts of tools

* Get rid of Keychain.MenuInputActions and directly set shortcuts to the menu items

This temporarily removes echoing support for undo and redo, this will be re-added once https://github.com/godotengine/godot/pull/36493 or https://github.com/godotengine/godot/pull/64317 is merged.

* Clean shortcut-related duplicate code in TopMenuContainer

* Remove DroidSansFallback now that system fonts can be used as fallback

* Remove 3.x settings from project.godot

* Format

* Format gdgifexporter

* Reset Keychain to its original state

* Remove textures from the dark and gray themes

* Remove all textures from the dark theme

* Better static typing in DrawingAlgos

* Use Vector2i for project size

* [Risky commit] Use Vector2i instead of Vector2 for tools

I tested it and everything seems to be working the same as before, but more testing would be appreciated.

* Format after previous commit

* Fix line angle constraint being rotated 180 degrees

This is not a regression from the previous commit(s), Godot 4 probably reversed the logic of `angle_to_point()`.

* Fix input map action not found errors when pressing Shift or Control

* Make AnimatePanel bigger, add spring interpolation

* Fix some layouts/extensions/preferences loading errors

* Fix dithering

* Update layout resources

Probably doesn't change anything at all, but I suppose it might be a good thing to do

* Small changes

* Disable filter in ResizeCanvas dialog

* Fix some preferences default button states

* Fix tile mode always having masking on

* Use integers in tile mode

* Fix checkboxes in preferences not working

* More statically typed arrays!

No need to have these # Array of X comments anymore!

* Fix "apply all" for multiple preview dialogs

* Update theme.tres

* Add HeaderSmall theme type variation

* Fix dynamics buttons

* Don't allow sub-zero zoom values

* Let zoom_out_max always remain Vector2(0.01, 0.01)

This fixes zooming on large canvases

* Bump version to v1.0-dev

* Fix ambient light not working on 3D cels

* Fix .obj loading

* Don't allow greater than max values in the zoom slider

* Set maximum zoom value to always be (500, 500)

* Set zoom slider minimum value to 1

* Some UI changes, mostly related to buttons and the timeline

* Change window titles to what they were before

* [COMPATIBILITY BROKEN WITH v0.x] Fix loading 3D cels

* Avoid changing Cel3DObject's file_path if it's the same

* Make preferences window bigger

* Fix png exporting

* Fix reference image initial size and filter setting

* Fix perspective line reverse scaling on zoom and facing 180 degrees away from cursor

* Format and some linting

* Remove most Images from the rest of the themes

* Remove all textures from all themes

* Fix drawing when the mouse gets released outside the canvas boundaries

* Format Keychain

* Implement #890

* Fix recorder

* Fix layout deletion

* Better static typing and fix empty_clicked signal-connected methods not having arguments

* Fix layout and extension directory creation if they don't already exist

* Change all instances of "HTML5" to "Web"

OS.get_name() now returns "Web" instead of "HTML5" in Godot 4

* Fix JavaScript detection

Opening files in the Web version does not yet work for some reason

* Fix formatting

* Fix lint errors

* Remove unneeded lines from rotation shaders

* Clean some rotation shader related code

* Remove ErrorManager from #891, as it's no longer needed in Godot 4

* Some docstrings

* More Vector2i and Recti replacing their float counterparts

* Remove the hardcoded shortcut from ValueSlider

Note that the brush size shortcut may not change properly because Keychain isn't updated to support Godot 4's input map properly yet.

* Fix bugs from the rebase, integer zooming is currently broken

* Format

* Fix bug where some imported images would fail to load when using smart slice

* Fix integer zooming (I think)

* Fix errors after #898

* Fix some UI issues with PreviewDialog

* Use ctrl/command instead of just ctrl in the shortcuts of the InputMap - gets rid of the need for _use_osx_shortcuts() in Main.gd

* Update Keychain and addons/README.md

* Update CI to Godot 4.1.1 (probably will not work)

* Remove XDGDataPaths.gd

* Make windows non-exclusive

* Attempt to fix macOS CI

* Attempt to fix CI

* Attempt to fix CI

* Minor fix in the dark theme, more will follow

* Silence enumerator/integer warning

* Attempt to fix macOS CI

* Another attempt to fix macOS CI

* Attempt to fix Windows & macOS CI

* fix: Recorder directory create (#903)

* Update Keychain so that the brush size shortcuts can be changed

This update generally lets users use modifier buttons (control, alt, shift, meta) with mouse buttons

* Change OSX to macOS

* Detect if multi-threading is enabled when exporting gifs

* Fix color picker not working on the top color mode

* Make some public methods private in Export.gd

* Remove Global.window_title variable

* Fix frame UI in the timeline breaking after 100 frames

* Static typing improvements for the timeline

* Better static typing for grids

* Fix typo

* Fix pixel grid not appearing

* Move preference updating code to Global using setters

This should make the code a bit more readable, as the logic for each property update can be found directly under the variable declaration, and not hidden in PreferencesDialog's preference_update() method. This also allows for changing these properties outside the preferences, if that will ever be needed. In theory it's also faster as we don't have to do all these string comparisons anymore, but I doubt this will be noticeable in practice.

* Remove RestoreDefaultButton.tscn

* Implement changing font size in the preferences

* Resize HeaderSmall font size along with the default font size

* A step towards fixing image loading in the Web version

Doesn't completely fix the issue, it requires a fix from Godot's side as well

* Implement missing input event actions for buttons

TODO: Add default shortcuts

* Do not change language and theme if they are already the defaults

Reduces the initial loading time a bit

* Remove update_hint_tooltips() as it's no longer needed, only keep it for tools

This was needed in order for hint tooltips to be successfully translated while taking the shortcuts into account. Godot 4 already it correctly for us now. Tools still need it because they contain multiple shortcut data in their tooltips.

* Change ExportDialog's PathDialog's file mode to be "open directory" and make them bigger

* Fix Vector2i + Vector2 errors in grid center snapping

* Update tooltips when the shortcut profile changes

* Fix copy-paste mistake

* Update tooltips during startup if the shortcut profile is not the default

* Fix gif warning label size in ExportDialog

* Fix BBCode in ExportDialog

* Fix some Godot 4.2 warnings

* Some CI fixes

* Static typing improvements and more inline functions

* Format

* Even more static typing, inline methods, docstrings etc

* Some more static typing improvements and inline setters

* Remove unneeded project type specifying

* Fix splash dialog error

* Fix enumerator warning

* Don't preload the font in the rules and guides

* Fix some integer division warnings

Sometimes we indeed need them to be floats

* Change some Rect2s to Rect2is

* Minor static typing improvements

* Update README, CHANGELOG, Translations

* Only load translation files when needed, reduces loading time a bit

* Update Keychain so it doesn't load languages during startup

* Lazy load all tool scenes, breaks compatibility with the extension API

Decreases initial loading time

* Format

* Very minor loading time speedups

* Remove unneeded project type specifying

* Even more static typing and docstring improvements

* Fix extension loading

* Palette docstrings

---------

Co-authored-by: ppphp <kevinniub@gmail.com>
2023-09-04 16:29:06 +03:00

480 lines
17 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name PreviewDialog
extends ConfirmationDialog
enum ImageImportOptions {
NEW_TAB,
SPRITESHEET_TAB,
SPRITESHEET_LAYER,
NEW_FRAME,
REPLACE_CEL,
NEW_LAYER,
NEW_REFERENCE_IMAGE,
PALETTE,
BRUSH,
PATTERN
}
enum BrushTypes { FILE, PROJECT, RANDOM }
var path: String
var image: Image
var current_import_option := ImageImportOptions.NEW_TAB
var smart_slice := false
var recycle_last_slice_result := false # Should we recycle the current sliced_rects
var sliced_rects: RegionUnpacker.RectData
var spritesheet_horizontal := 1
var spritesheet_vertical := 1
var brush_type := BrushTypes.FILE
var opened_once := false
var is_main := false
var hiding := false
@onready var texture_rect: TextureRect = $VBoxContainer/AspectRatioContainer/TextureRect
@onready var image_size_label: Label = $VBoxContainer/SizeContainer/ImageSizeLabel
@onready var frame_size_label: Label = $VBoxContainer/SizeContainer/FrameSizeLabel
@onready var smart_slice_checkbox := %SmartSliceButton as CheckBox
@onready var merge_threshold := %SmartOptions/Threshold as ValueSlider
@onready var merge_dist := %SmartOptions/MergeDist as ValueSlider
@onready
var spritesheet_manual_tab_options = $VBoxContainer/HBoxContainer/SpritesheetTabOptions/Manual
@onready var spritesheet_smart_tab_options := %SmartOptions as HBoxContainer
@onready var spritesheet_tab_options = $VBoxContainer/HBoxContainer/SpritesheetTabOptions
@onready var spritesheet_lay_opt = $VBoxContainer/HBoxContainer/SpritesheetLayerOptions
@onready var new_frame_options = $VBoxContainer/HBoxContainer/NewFrameOptions
@onready var replace_cel_options = $VBoxContainer/HBoxContainer/ReplaceCelOptions
@onready var new_layer_options = $VBoxContainer/HBoxContainer/NewLayerOptions
@onready var new_brush_options = $VBoxContainer/HBoxContainer/NewBrushOptions
@onready var new_brush_name = $VBoxContainer/HBoxContainer/NewBrushOptions/BrushName
@onready var import_options: OptionButton = $VBoxContainer/HBoxContainer/ImportOption
@onready var apply_all: CheckBox = $VBoxContainer/ApplyAll
func _on_PreviewDialog_about_to_show() -> void:
if opened_once:
return
opened_once = true
# # order as in ImageImportOptions enum
import_options.add_item("New project")
import_options.add_item("Spritesheet (new project)")
import_options.add_item("Spritesheet (new layer)")
import_options.add_item("New frame")
import_options.add_item("Replace cel")
import_options.add_item("New layer")
import_options.add_item("New reference image")
import_options.add_item("New palette")
import_options.add_item("New brush")
import_options.add_item("New pattern")
# Select the option that the preview dialog before it had selected
import_options.select(OpenSave.last_dialog_option)
import_options.item_selected.emit(OpenSave.last_dialog_option)
var img_texture := ImageTexture.create_from_image(image)
texture_rect.texture = img_texture
spritesheet_manual_tab_options.get_node("HorizontalFrames").max_value = min(
spritesheet_manual_tab_options.get_node("HorizontalFrames").max_value, image.get_size().x
)
spritesheet_manual_tab_options.get_node("VerticalFrames").max_value = min(
spritesheet_manual_tab_options.get_node("VerticalFrames").max_value, image.get_size().y
)
image_size_label.text = (
tr("Image Size") + ": " + str(image.get_size().x) + "×" + str(image.get_size().y)
)
frame_size_label.text = (
tr("Frame Size") + ": " + str(image.get_size().x) + "×" + str(image.get_size().y)
)
if OpenSave.preview_dialogs.size() > 1:
apply_all.visible = true
func _on_visibility_changed() -> void:
if visible:
return
if hiding: # if the popup is hiding because of main
return
elif is_main: # if the main dialog is closed then close others too
for child in Global.control.get_children():
if child is PreviewDialog:
OpenSave.preview_dialogs.erase(child)
child.queue_free()
else: # dialogs being closed separately
OpenSave.preview_dialogs.erase(self)
queue_free()
# Call Global.dialog_open() only if it's the only preview dialog opened
if OpenSave.preview_dialogs.size() != 0:
return
Global.dialog_open(false)
func _on_PreviewDialog_confirmed() -> void:
if is_main: # if the main dialog is confirmed then confirm others too
is_main = false
synchronize()
for child in Global.control.get_children():
if child is PreviewDialog:
child.confirmed.emit()
else:
if current_import_option == ImageImportOptions.NEW_TAB:
OpenSave.open_image_as_new_tab(path, image)
elif current_import_option == ImageImportOptions.SPRITESHEET_TAB:
if smart_slice:
if !recycle_last_slice_result:
obtain_sliced_data()
OpenSave.open_image_as_spritesheet_tab_smart(
path, image, sliced_rects.rects, sliced_rects.frame_size
)
else:
OpenSave.open_image_as_spritesheet_tab(
path, image, spritesheet_horizontal, spritesheet_vertical
)
elif current_import_option == ImageImportOptions.SPRITESHEET_LAYER:
var frame_index: int = spritesheet_lay_opt.get_node("AtFrameSpinbox").value - 1
if smart_slice:
if !recycle_last_slice_result:
obtain_sliced_data()
OpenSave.open_image_as_spritesheet_layer_smart(
path,
image,
path.get_basename().get_file(),
sliced_rects["rects"],
frame_index,
sliced_rects["frame_size"]
)
else:
OpenSave.open_image_as_spritesheet_layer(
path,
image,
path.get_basename().get_file(),
spritesheet_horizontal,
spritesheet_vertical,
frame_index
)
elif current_import_option == ImageImportOptions.NEW_FRAME:
var layer_index: int = new_frame_options.get_node("AtLayerOption").get_selected_id()
OpenSave.open_image_as_new_frame(image, layer_index)
elif current_import_option == ImageImportOptions.REPLACE_CEL:
var layer_index: int = replace_cel_options.get_node("AtLayerOption").get_selected_id()
var frame_index: int = replace_cel_options.get_node("AtFrameSpinbox").value - 1
OpenSave.open_image_at_cel(image, layer_index, frame_index)
elif current_import_option == ImageImportOptions.NEW_LAYER:
var frame_index: int = new_layer_options.get_node("AtFrameSpinbox").value - 1
OpenSave.open_image_as_new_layer(image, path.get_basename().get_file(), frame_index)
elif current_import_option == ImageImportOptions.NEW_REFERENCE_IMAGE:
if OS.get_name() == "Web":
OpenSave.import_reference_image_from_image(image)
else:
OpenSave.import_reference_image_from_path(path)
elif current_import_option == ImageImportOptions.PALETTE:
Palettes.import_palette_from_path(path)
elif current_import_option == ImageImportOptions.BRUSH:
add_brush()
elif current_import_option == ImageImportOptions.PATTERN:
var file_name_ext: String = path.get_file()
file_name_ext = file_name_replace(file_name_ext, "Patterns")
var file_name: String = file_name_ext.get_basename()
image.convert(Image.FORMAT_RGBA8)
Global.patterns_popup.add(image, file_name)
# Copy the image file into the "pixelorama/Patterns" directory
var location := "Patterns".path_join(file_name_ext)
var dir := DirAccess.open(path)
dir.copy(path, Global.home_data_directory.path_join(location))
func _on_ApplyAll_toggled(pressed: bool) -> void:
is_main = pressed
# below 4 (and the last) line is needed for correct popup placement
var old_rect := Rect2i(position, size)
visibility_changed.disconnect(_on_visibility_changed)
hide()
visibility_changed.connect(_on_visibility_changed)
for child in Global.control.get_children():
if child != self and child is PreviewDialog:
child.hiding = pressed
if pressed:
child.hide()
synchronize()
else:
child.popup_centered()
popup(old_rect) # needed for correct popup_order
func synchronize() -> void:
for child in Global.control.get_children():
if child != self and child is PreviewDialog:
var dialog := child as PreviewDialog
#sync modes
var id := current_import_option
dialog.import_options.select(id)
dialog.import_options.item_selected.emit(id)
#sync properties (if any)
if (
id == ImageImportOptions.SPRITESHEET_TAB
or id == ImageImportOptions.SPRITESHEET_LAYER
):
dialog.spritesheet_manual_tab_options.get_node("HorizontalFrames").value = min(
spritesheet_manual_tab_options.get_node("HorizontalFrames").value,
image.get_size().x
)
dialog.spritesheet_manual_tab_options.get_node("VerticalFrames").value = min(
spritesheet_manual_tab_options.get_node("VerticalFrames").value,
image.get_size().y
)
if id == ImageImportOptions.SPRITESHEET_LAYER:
dialog.spritesheet_lay_opt.get_node("AtFrameSpinbox").value = (
spritesheet_lay_opt.get_node("AtFrameSpinbox").value
)
elif id == ImageImportOptions.NEW_FRAME:
dialog.new_frame_options.get_node("AtLayerOption").selected = (
new_frame_options.get_node("AtLayerOption").selected
)
elif id == ImageImportOptions.REPLACE_CEL:
dialog.replace_cel_options.get_node("AtLayerOption").selected = (
replace_cel_options.get_node("AtLayerOption").selected
)
dialog.replace_cel_options.get_node("AtFrameSpinbox").value = (
replace_cel_options.get_node("AtFrameSpinbox").value
)
elif id == ImageImportOptions.NEW_LAYER:
dialog.new_layer_options.get_node("AtFrameSpinbox").value = (
new_layer_options.get_node("AtFrameSpinbox").value
)
elif id == ImageImportOptions.BRUSH:
var type = new_brush_options.get_node("BrushTypeOption").selected
dialog.new_brush_options.get_node("BrushTypeOption").select(type)
dialog.new_brush_options.get_node("BrushTypeOption").item_selected.emit(type)
func _on_ImportOption_item_selected(id: ImageImportOptions) -> void:
current_import_option = id
OpenSave.last_dialog_option = current_import_option
smart_slice_checkbox.button_pressed = false
apply_all.disabled = false
smart_slice = false
smart_slice_checkbox.visible = false
spritesheet_tab_options.visible = false
spritesheet_lay_opt.visible = false
new_frame_options.visible = false
replace_cel_options.visible = false
new_layer_options.visible = false
new_brush_options.visible = false
texture_rect.get_child(0).visible = false
texture_rect.get_child(1).visible = false
if id == ImageImportOptions.SPRITESHEET_TAB:
frame_size_label.visible = true
smart_slice_checkbox.visible = true
spritesheet_tab_options.visible = true
texture_rect.get_child(0).visible = true
texture_rect.get_child(1).visible = true
elif id == ImageImportOptions.SPRITESHEET_LAYER:
frame_size_label.visible = true
smart_slice_checkbox.visible = true
spritesheet_lay_opt.visible = true
spritesheet_tab_options.visible = true
texture_rect.get_child(0).visible = true
texture_rect.get_child(1).visible = true
elif id == ImageImportOptions.NEW_FRAME:
new_frame_options.visible = true
# Fill the at layer option button:
var at_layer_option: OptionButton = new_frame_options.get_node("AtLayerOption")
at_layer_option.clear()
var layers := Global.current_project.layers.duplicate()
layers.reverse()
var i := 0
for l in layers:
if not l is PixelLayer:
continue
at_layer_option.add_item(l.name, l.index)
at_layer_option.set_item_tooltip(i, l.get_layer_path())
i += 1
at_layer_option.selected = at_layer_option.get_item_count() - 1
elif id == ImageImportOptions.REPLACE_CEL:
replace_cel_options.visible = true
# Fill the at layer option button:
var at_layer_option: OptionButton = replace_cel_options.get_node("AtLayerOption")
at_layer_option.clear()
var layers := Global.current_project.layers.duplicate()
layers.reverse()
var i := 0
for l in layers:
if not l is PixelLayer:
continue
at_layer_option.add_item(l.name, l.index)
at_layer_option.set_item_tooltip(i, l.get_layer_path())
i += 1
at_layer_option.selected = at_layer_option.get_item_count() - 1
var at_frame_spinbox: SpinBox = replace_cel_options.get_node("AtFrameSpinbox")
at_frame_spinbox.max_value = Global.current_project.frames.size()
elif id == ImageImportOptions.NEW_LAYER:
new_layer_options.visible = true
new_layer_options.get_node("AtFrameSpinbox").max_value = (
Global.current_project.frames.size()
)
elif id == ImageImportOptions.BRUSH:
new_brush_options.visible = true
_call_queue_redraw()
func _on_smart_slice_toggled(button_pressed: bool) -> void:
setup_smart_slice(button_pressed)
func setup_smart_slice(enabled: bool) -> void:
spritesheet_smart_tab_options.visible = enabled
spritesheet_manual_tab_options.visible = !enabled
if is_main: # Disable apply all (the algorithm is not fast enough for this)
apply_all.button_pressed = false
apply_all.disabled = enabled
smart_slice = enabled
if !recycle_last_slice_result and enabled:
slice_preview()
_call_queue_redraw()
func obtain_sliced_data() -> void:
var unpak := RegionUnpacker.new(merge_threshold.value, merge_dist.value)
sliced_rects = unpak.get_used_rects(texture_rect.texture.get_image())
func slice_preview():
sliced_rects = null
obtain_sliced_data()
recycle_last_slice_result = true
var frame_size := sliced_rects.frame_size
frame_size_label.text = tr("Frame Size") + ": " + str(frame_size.x) + "×" + str(frame_size.y)
func _on_threshold_value_changed(_value: float) -> void:
recycle_last_slice_result = false
func _on_merge_dist_value_changed(_value: float) -> void:
recycle_last_slice_result = false
func _on_slice_pressed() -> void:
if !recycle_last_slice_result:
slice_preview()
_call_queue_redraw()
func _on_HorizontalFrames_value_changed(value: int) -> void:
spritesheet_horizontal = value
spritesheet_frame_value_changed()
func _on_VerticalFrames_value_changed(value: int) -> void:
spritesheet_vertical = value
spritesheet_frame_value_changed()
func spritesheet_frame_value_changed() -> void:
var frame_width := floori(image.get_size().x / spritesheet_horizontal)
var frame_height := floori(image.get_size().y / spritesheet_vertical)
frame_size_label.text = tr("Frame Size") + ": " + str(frame_width) + "×" + str(frame_height)
_call_queue_redraw()
func _on_BrushTypeOption_item_selected(index: BrushTypes) -> void:
brush_type = index
new_brush_name.visible = false
if brush_type == BrushTypes.RANDOM:
new_brush_name.visible = true
func add_brush() -> void:
image.convert(Image.FORMAT_RGBA8)
if brush_type == BrushTypes.FILE:
var file_name_ext: String = path.get_file()
file_name_ext = file_name_replace(file_name_ext, "Brushes")
var file_name: String = file_name_ext.get_basename()
Brushes.add_file_brush([image], file_name)
# Copy the image file into the "pixelorama/Brushes" directory
var location := "Brushes".path_join(file_name_ext)
var dir := DirAccess.open(path)
dir.copy(path, Global.home_data_directory.path_join(location))
elif brush_type == BrushTypes.PROJECT:
var file_name: String = path.get_file().get_basename()
Global.current_project.brushes.append(image)
Brushes.add_project_brush(image, file_name)
elif brush_type == BrushTypes.RANDOM:
var brush_name = new_brush_name.get_node("BrushNameLineEdit").text.to_lower()
if !brush_name.is_valid_filename():
return
var dir := DirAccess.open(Global.home_data_directory.path_join("Brushes"))
if !dir.dir_exists(brush_name):
dir.make_dir(brush_name)
dir = DirAccess.open(Global.home_data_directory.path_join("Brushes").path_join(brush_name))
var random_brushes := []
dir.list_dir_begin()
var curr_file := dir.get_next()
while curr_file != "":
if curr_file.begins_with("~") and brush_name in curr_file:
random_brushes.append(curr_file)
curr_file = dir.get_next()
dir.list_dir_end()
var file_ext := path.get_file().get_extension()
var index := random_brushes.size() + 1
var file_name = "~" + brush_name + str(index) + "." + file_ext
var location := "Brushes".path_join(brush_name).path_join(file_name)
dir.copy(path, Global.home_data_directory.path_join(location))
## Checks if the file already exists
## If it does, add a number to its name, for example
## "Brush_Name" will become "Brush_Name (2)", "Brush_Name (3)", etc.
func file_name_replace(file_name: String, folder: String) -> String:
var i := 1
var file_ext := file_name.get_extension()
var temp_name := file_name
var dir := DirAccess.open(Global.home_data_directory.path_join(folder))
while dir.file_exists(temp_name):
i += 1
temp_name = file_name.get_basename() + " (%s)" % i
temp_name += "." + file_ext
file_name = temp_name
return file_name
func _call_queue_redraw() -> void:
var empty_array: Array[Rect2i] = []
$"%SmartSlice".show_preview(empty_array)
$"%RowColumnLines".show_preview(1, 1)
if (
current_import_option == ImageImportOptions.SPRITESHEET_TAB
or current_import_option == ImageImportOptions.SPRITESHEET_LAYER
):
if smart_slice:
if is_instance_valid(sliced_rects) and not sliced_rects.rects.is_empty():
$"%SmartSlice".show_preview(sliced_rects.rects)
else:
$"%RowColumnLines".show_preview(spritesheet_vertical, spritesheet_horizontal)
func _on_size_changed() -> void:
_call_queue_redraw()