2021-02-02 15:48:17 +00:00
|
|
|
extends BaseTool
|
2020-07-09 12:22:17 +00:00
|
|
|
|
2022-09-18 15:43:50 +00:00
|
|
|
enum FillArea { AREA, COLORS, SELECTION }
|
|
|
|
enum FillWith { COLOR, PATTERN }
|
|
|
|
|
2022-09-18 12:56:36 +00:00
|
|
|
const COLOR_REPLACE_SHADER := preload("res://src/Shaders/ColorReplace.shader")
|
2022-09-18 16:22:06 +00:00
|
|
|
const PATTERN_FILL_SHADER := preload("res://src/Shaders/PatternFill.gdshader")
|
2022-02-15 22:15:53 +00:00
|
|
|
|
2021-11-11 01:21:34 +00:00
|
|
|
var _prev_mode := 0
|
2021-11-25 12:48:30 +00:00
|
|
|
var _pattern: Patterns.Pattern
|
2022-02-15 22:15:53 +00:00
|
|
|
var _similarity := 100
|
2022-09-18 15:43:50 +00:00
|
|
|
var _fill_area: int = FillArea.AREA
|
|
|
|
var _fill_with: int = FillWith.COLOR
|
2020-07-09 12:22:17 +00:00
|
|
|
var _offset_x := 0
|
|
|
|
var _offset_y := 0
|
2022-04-16 09:22:51 +00:00
|
|
|
# working array used as buffer for segments while flooding
|
|
|
|
var _allegro_flood_segments: Array
|
|
|
|
# results array per image while flooding
|
|
|
|
var _allegro_image_segments: Array
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
update_pattern()
|
|
|
|
|
|
|
|
|
2021-11-11 01:21:34 +00:00
|
|
|
func _input(event: InputEvent) -> void:
|
2022-09-18 15:43:50 +00:00
|
|
|
if event.is_action_pressed("change_tool_mode"):
|
|
|
|
_prev_mode = _fill_area
|
|
|
|
if event.is_action("change_tool_mode"):
|
|
|
|
if _fill_area == FillArea.SELECTION:
|
|
|
|
_fill_area = FillArea.AREA
|
|
|
|
else:
|
|
|
|
_fill_area = _prev_mode ^ 1
|
|
|
|
_select_fill_area_optionbutton()
|
|
|
|
if event.is_action_released("change_tool_mode"):
|
|
|
|
_fill_area = _prev_mode
|
|
|
|
_select_fill_area_optionbutton()
|
2022-09-18 13:36:03 +00:00
|
|
|
|
|
|
|
|
2022-09-18 15:43:50 +00:00
|
|
|
func _on_FillAreaOptions_item_selected(index: int) -> void:
|
|
|
|
_fill_area = index
|
2020-07-09 12:22:17 +00:00
|
|
|
update_config()
|
|
|
|
save_config()
|
|
|
|
|
|
|
|
|
2022-09-18 15:43:50 +00:00
|
|
|
func _select_fill_area_optionbutton() -> void:
|
2022-10-04 21:42:01 +00:00
|
|
|
$"%FillAreaOptions".selected = _fill_area
|
|
|
|
$SimilaritySlider.visible = (_fill_area == FillArea.COLORS)
|
2022-09-18 15:43:50 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func _on_FillWithOptions_item_selected(index: int) -> void:
|
2020-07-09 12:22:17 +00:00
|
|
|
_fill_with = index
|
2022-02-15 22:15:53 +00:00
|
|
|
update_config()
|
|
|
|
save_config()
|
|
|
|
|
|
|
|
|
2022-10-04 21:42:01 +00:00
|
|
|
func _on_SimilaritySlider_value_changed(value: float) -> void:
|
2022-02-15 22:15:53 +00:00
|
|
|
_similarity = value
|
2020-07-09 12:22:17 +00:00
|
|
|
update_config()
|
|
|
|
save_config()
|
|
|
|
|
|
|
|
|
2021-12-05 16:37:15 +00:00
|
|
|
func _on_PatternType_pressed() -> void:
|
|
|
|
var popup: Popup = Global.patterns_popup
|
|
|
|
if !popup.is_connected("pattern_selected", self, "_on_Pattern_selected"):
|
|
|
|
popup.connect("pattern_selected", self, "_on_Pattern_selected", [], CONNECT_ONESHOT)
|
|
|
|
popup.popup(Rect2($FillPattern/Type.rect_global_position, Vector2(226, 72)))
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func _on_Pattern_selected(pattern: Patterns.Pattern) -> void:
|
2020-07-09 12:22:17 +00:00
|
|
|
_pattern = pattern
|
|
|
|
update_pattern()
|
|
|
|
save_config()
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func _on_PatternOffsetX_value_changed(value: float) -> void:
|
2020-07-09 12:22:17 +00:00
|
|
|
_offset_x = int(value)
|
|
|
|
update_config()
|
|
|
|
save_config()
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func _on_PatternOffsetY_value_changed(value: float) -> void:
|
2020-07-09 12:22:17 +00:00
|
|
|
_offset_y = int(value)
|
|
|
|
update_config()
|
|
|
|
save_config()
|
|
|
|
|
|
|
|
|
|
|
|
func get_config() -> Dictionary:
|
2020-07-09 12:52:59 +00:00
|
|
|
if !_pattern:
|
2022-02-15 22:15:53 +00:00
|
|
|
return {"fill_area": _fill_area, "fill_with": _fill_with, "similarity": _similarity}
|
2020-07-09 12:22:17 +00:00
|
|
|
return {
|
2021-11-25 12:48:30 +00:00
|
|
|
"pattern_index": _pattern.index,
|
|
|
|
"fill_area": _fill_area,
|
|
|
|
"fill_with": _fill_with,
|
2022-02-15 22:15:53 +00:00
|
|
|
"similarity": _similarity,
|
2021-11-25 12:48:30 +00:00
|
|
|
"offset_x": _offset_x,
|
|
|
|
"offset_y": _offset_y,
|
2020-07-09 12:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func set_config(config: Dictionary) -> void:
|
2020-07-09 12:52:59 +00:00
|
|
|
if _pattern:
|
|
|
|
var index = config.get("pattern_index", _pattern.index)
|
|
|
|
_pattern = Global.patterns_popup.get_pattern(index)
|
2020-07-09 12:22:17 +00:00
|
|
|
_fill_area = config.get("fill_area", _fill_area)
|
|
|
|
_fill_with = config.get("fill_with", _fill_with)
|
2022-02-15 22:15:53 +00:00
|
|
|
_similarity = config.get("similarity", _similarity)
|
2020-07-09 12:22:17 +00:00
|
|
|
_offset_x = config.get("offset_x", _offset_x)
|
|
|
|
_offset_y = config.get("offset_y", _offset_y)
|
|
|
|
update_pattern()
|
|
|
|
|
|
|
|
|
|
|
|
func update_config() -> void:
|
2022-09-18 15:43:50 +00:00
|
|
|
_select_fill_area_optionbutton()
|
2022-10-04 21:42:01 +00:00
|
|
|
$"%FillWithOptions".selected = _fill_with
|
|
|
|
$SimilaritySlider.value = _similarity
|
2022-09-18 15:43:50 +00:00
|
|
|
$FillPattern.visible = _fill_with == FillWith.PATTERN
|
2020-07-09 12:22:17 +00:00
|
|
|
$FillPattern/XOffset/OffsetX.value = _offset_x
|
|
|
|
$FillPattern/YOffset/OffsetY.value = _offset_y
|
|
|
|
|
|
|
|
|
|
|
|
func update_pattern() -> void:
|
|
|
|
if _pattern == null:
|
2020-07-09 12:52:59 +00:00
|
|
|
if Global.patterns_popup.default_pattern == null:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
_pattern = Global.patterns_popup.default_pattern
|
2020-07-09 12:22:17 +00:00
|
|
|
var tex := ImageTexture.new()
|
2021-12-05 16:37:15 +00:00
|
|
|
if !_pattern.image.is_empty():
|
|
|
|
tex.create_from_image(_pattern.image, 0)
|
2020-07-09 12:22:17 +00:00
|
|
|
$FillPattern/Type/Texture.texture = tex
|
|
|
|
var size := _pattern.image.get_size()
|
|
|
|
$FillPattern/XOffset/OffsetX.max_value = size.x - 1
|
|
|
|
$FillPattern/YOffset/OffsetY.max_value = size.y - 1
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func draw_start(position: Vector2) -> void:
|
2021-12-14 23:39:24 +00:00
|
|
|
.draw_start(position)
|
Implement the Keychain Plugin (#700)
* Start implementing the godot_better_input plugin
* Update ShortcutEdit.gd
* Load & save preset option
* Add some groups and fix action events not being deleted on load
* Add MenuInputAction class for multiple menu accelerators
* Create a proper plugin and a BetterInput autoload
* Update menu accelerators
* Move settings to BetterInput
* Move menu enums to Global, make more MenuInputActions
* Add more menu events
* Add new groups
* Optimize BetterInput _input() method
* Remove a lot of lines of code
* Change some previous events, add ignore actions and a View menu group
* Change update_item_accelerator to update_ui
* Move MenuInputAction initialization to BetterInput.gd
* Update hint tooltips when a shortcut changes
Temporarily comment out some code regarding the configurable modifiers
* Some MenuInputAction variable name changes
* Add handle_input() to InputAction
* Update the shortcuts of buttons
* Fix shortcut selector menu position
* Change plugin name into Keychain
* Fix keyboard input dialog exiting when Enter or Space is being pressed
* Add two more groups
* Make groups folded by default
* Temporarily make tool modifier shortcuts not configurable
A temporary change, they will be made configurable again, with different actions that are currently mapped to the same events, local/independent from each other.
* Add license for Keychain
* Fix issue where a key event would be added in other input types
* Fix bug where the assigned state was not updated when the dialog appeared again
* Update Main.tscn
* Add a disabled line edit in keyboard shortcut selector to grab focus
* Load presets in the Keychain autoload
This way, the input actions get updated from the start, instead of only at the ShortcutEdit scene.
WARNING, this currently causes crashes if the menu items have no shortcut binded to them.
* Move custom settings away from Keychain.gd
To keep it the same as the upstream plugin
* Change menu enum names
* Made action_get_first_key() more general
* Use arrays for menu items instead of dictionaries, fixes crash
* Move moveable panels to Window menu
* Format
* Optimize hint tooltip updating
* Add support for translations in Keychain
* Translation changes
* Made tool modifiers configurable
Needs more testing.
* Made camera arrow key movement configurable & joypad axis support
This commit removes the ability to press Shift and Control+Shift to adjust the camera arrow key movement speed. Instead, the speed depends on the zoom level.
The right joypad analog stick is configured to move the camera by default.
* Rename presets into shortcut profiles, use Resources and let users create their own
* [skip ci] Update addons README
* Update Global.gd
2022-05-16 12:07:51 +00:00
|
|
|
if Input.is_action_pressed("draw_color_picker"):
|
2021-11-14 01:30:00 +00:00
|
|
|
_pick_color(position)
|
|
|
|
return
|
|
|
|
|
2021-04-23 20:37:07 +00:00
|
|
|
Global.canvas.selection.transform_content_confirm()
|
2021-11-25 12:48:30 +00:00
|
|
|
if (
|
|
|
|
!Global.current_project.layers[Global.current_project.current_layer].can_layer_get_drawn()
|
2022-08-02 15:57:06 +00:00
|
|
|
or !Rect2(Vector2.ZERO, Global.current_project.size).has_point(position)
|
2021-11-25 12:48:30 +00:00
|
|
|
):
|
2021-01-31 13:09:54 +00:00
|
|
|
return
|
2021-11-25 12:48:30 +00:00
|
|
|
if (
|
|
|
|
Global.current_project.has_selection
|
|
|
|
and not Global.current_project.can_pixel_get_drawn(position)
|
|
|
|
):
|
2020-07-09 12:22:17 +00:00
|
|
|
return
|
2022-09-18 15:43:50 +00:00
|
|
|
var undo_data := _get_undo_data()
|
|
|
|
match _fill_area:
|
|
|
|
FillArea.AREA:
|
|
|
|
fill_in_area(position)
|
|
|
|
FillArea.COLORS:
|
|
|
|
fill_in_color(position)
|
|
|
|
FillArea.SELECTION:
|
|
|
|
fill_in_selection()
|
2020-07-09 12:22:17 +00:00
|
|
|
commit_undo("Draw", undo_data)
|
|
|
|
|
|
|
|
|
2021-12-14 23:39:24 +00:00
|
|
|
func draw_move(position: Vector2) -> void:
|
|
|
|
.draw_move(position)
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
2021-12-14 23:39:24 +00:00
|
|
|
func draw_end(position: Vector2) -> void:
|
|
|
|
.draw_end(position)
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func fill_in_color(position: Vector2) -> void:
|
|
|
|
var project: Project = Global.current_project
|
2021-06-11 22:06:13 +00:00
|
|
|
var images := _get_selected_draw_images()
|
|
|
|
for image in images:
|
2022-09-18 16:22:06 +00:00
|
|
|
var color: Color = image.get_pixelv(position)
|
2022-09-18 12:56:36 +00:00
|
|
|
var pattern_image: Image
|
2022-09-18 15:43:50 +00:00
|
|
|
if _fill_with == FillWith.COLOR or _pattern == null:
|
2021-06-11 22:06:13 +00:00
|
|
|
if tool_slot.color.is_equal_approx(color):
|
2022-09-18 16:22:06 +00:00
|
|
|
continue
|
2022-09-18 12:56:36 +00:00
|
|
|
else:
|
|
|
|
# End early if we are filling with an empty pattern
|
|
|
|
pattern_image = _pattern.image
|
|
|
|
var pattern_size := pattern_image.get_size()
|
|
|
|
if pattern_size.x == 0 or pattern_size.y == 0:
|
|
|
|
return
|
2020-07-09 12:22:17 +00:00
|
|
|
|
2022-02-15 22:15:53 +00:00
|
|
|
var selection: Image
|
|
|
|
var selection_tex := ImageTexture.new()
|
|
|
|
if project.has_selection:
|
2022-08-08 00:03:17 +00:00
|
|
|
selection = project.selection_map
|
2022-02-15 22:15:53 +00:00
|
|
|
else:
|
|
|
|
selection = Image.new()
|
|
|
|
selection.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
|
|
|
|
selection.fill(Color(1, 1, 1, 1))
|
|
|
|
|
|
|
|
selection_tex.create_from_image(selection)
|
|
|
|
|
|
|
|
var pattern_tex := ImageTexture.new()
|
2022-09-18 12:56:36 +00:00
|
|
|
if _pattern and pattern_image:
|
|
|
|
pattern_tex.create_from_image(pattern_image)
|
2022-02-15 22:15:53 +00:00
|
|
|
|
|
|
|
var params := {
|
|
|
|
"size": project.size,
|
|
|
|
"old_color": color,
|
|
|
|
"new_color": tool_slot.color,
|
|
|
|
"similarity_percent": _similarity,
|
|
|
|
"selection": selection_tex,
|
|
|
|
"pattern": pattern_tex,
|
|
|
|
"pattern_size": pattern_tex.get_size(),
|
|
|
|
# pixel offset converted to pattern uv offset
|
|
|
|
"pattern_uv_offset":
|
|
|
|
Vector2.ONE / pattern_tex.get_size() * Vector2(_offset_x, _offset_y),
|
2022-09-18 15:43:50 +00:00
|
|
|
"has_pattern": true if _fill_with == FillWith.PATTERN else false
|
2022-02-15 22:15:53 +00:00
|
|
|
}
|
|
|
|
var gen := ShaderImageEffect.new()
|
2022-09-18 12:56:36 +00:00
|
|
|
gen.generate_image(image, COLOR_REPLACE_SHADER, params, project.size)
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func fill_in_area(position: Vector2) -> void:
|
|
|
|
var project: Project = Global.current_project
|
2020-07-20 19:45:22 +00:00
|
|
|
_flood_fill(position)
|
|
|
|
|
|
|
|
# Handle Mirroring
|
2020-07-16 01:25:59 +00:00
|
|
|
var mirror_x = project.x_symmetry_point - position.x
|
|
|
|
var mirror_y = project.y_symmetry_point - position.y
|
2021-11-25 12:48:30 +00:00
|
|
|
var mirror_x_inside: bool
|
|
|
|
var mirror_y_inside: bool
|
2020-07-20 19:15:34 +00:00
|
|
|
|
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
|
|
|
mirror_x_inside = project.can_pixel_get_drawn(Vector2(mirror_x, position.y))
|
|
|
|
mirror_y_inside = project.can_pixel_get_drawn(Vector2(position.x, mirror_y))
|
2020-07-09 12:22:17 +00:00
|
|
|
|
2022-02-17 14:07:03 +00:00
|
|
|
if Tools.horizontal_mirror and mirror_x_inside:
|
2020-07-09 12:22:17 +00:00
|
|
|
_flood_fill(Vector2(mirror_x, position.y))
|
2022-02-17 14:07:03 +00:00
|
|
|
if Tools.vertical_mirror and mirror_y_inside:
|
2020-07-09 12:22:17 +00:00
|
|
|
_flood_fill(Vector2(mirror_x, mirror_y))
|
2022-02-17 14:07:03 +00:00
|
|
|
if Tools.vertical_mirror and mirror_y_inside:
|
2020-07-09 12:22:17 +00:00
|
|
|
_flood_fill(Vector2(position.x, mirror_y))
|
|
|
|
|
|
|
|
|
2022-09-18 15:43:50 +00:00
|
|
|
func fill_in_selection() -> void:
|
|
|
|
var project: Project = Global.current_project
|
|
|
|
var images := _get_selected_draw_images()
|
2022-09-18 16:22:06 +00:00
|
|
|
if _fill_with == FillWith.COLOR or _pattern == null:
|
|
|
|
if project.has_selection:
|
|
|
|
var filler := Image.new()
|
|
|
|
filler.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
|
|
|
|
filler.fill(tool_slot.color)
|
|
|
|
var rect: Rect2 = Global.canvas.selection.big_bounding_rectangle
|
|
|
|
var selection_map_copy := SelectionMap.new()
|
|
|
|
selection_map_copy.copy_from(project.selection_map)
|
|
|
|
# In case the selection map is bigger than the canvas
|
|
|
|
selection_map_copy.crop(project.size.x, project.size.y)
|
|
|
|
for image in images:
|
|
|
|
image.blit_rect_mask(filler, selection_map_copy, rect, rect.position)
|
|
|
|
else:
|
|
|
|
for image in images:
|
|
|
|
image.fill(tool_slot.color)
|
2022-09-18 15:43:50 +00:00
|
|
|
else:
|
2022-09-18 16:22:06 +00:00
|
|
|
# End early if we are filling with an empty pattern
|
|
|
|
var pattern_image: Image = _pattern.image
|
|
|
|
var pattern_size := pattern_image.get_size()
|
|
|
|
if pattern_size.x == 0 or pattern_size.y == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
var selection: Image
|
|
|
|
var selection_tex := ImageTexture.new()
|
|
|
|
if project.has_selection:
|
|
|
|
selection = project.selection_map
|
|
|
|
else:
|
|
|
|
selection = Image.new()
|
|
|
|
selection.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
|
|
|
|
selection.fill(Color(1, 1, 1, 1))
|
|
|
|
|
|
|
|
selection_tex.create_from_image(selection)
|
|
|
|
|
|
|
|
var pattern_tex := ImageTexture.new()
|
|
|
|
if _pattern and pattern_image:
|
|
|
|
pattern_tex.create_from_image(pattern_image)
|
|
|
|
|
|
|
|
var params := {
|
|
|
|
"selection": selection_tex,
|
|
|
|
"size": project.size,
|
|
|
|
"pattern": pattern_tex,
|
|
|
|
"pattern_size": pattern_tex.get_size(),
|
|
|
|
# pixel offset converted to pattern uv offset
|
|
|
|
"pattern_uv_offset":
|
|
|
|
Vector2.ONE / pattern_tex.get_size() * Vector2(_offset_x, _offset_y),
|
|
|
|
}
|
2022-09-18 15:43:50 +00:00
|
|
|
for image in images:
|
2022-09-18 16:22:06 +00:00
|
|
|
var gen := ShaderImageEffect.new()
|
|
|
|
gen.generate_image(image, PATTERN_FILL_SHADER, params, project.size)
|
2022-09-18 15:43:50 +00:00
|
|
|
|
|
|
|
|
2022-04-16 09:22:51 +00:00
|
|
|
# Add a new segment to the array
|
|
|
|
func _add_new_segment(y: int = 0) -> void:
|
2022-09-18 16:22:06 +00:00
|
|
|
var segment := {}
|
2022-04-16 09:22:51 +00:00
|
|
|
segment.flooding = false
|
|
|
|
segment.todo_above = false
|
|
|
|
segment.todo_below = false
|
|
|
|
segment.left_position = -5 # anything less than -1 is ok
|
|
|
|
segment.right_position = -5
|
|
|
|
segment.y = y
|
|
|
|
segment.next = 0
|
|
|
|
_allegro_flood_segments.append(segment)
|
|
|
|
|
|
|
|
|
|
|
|
# fill an horizontal segment around the specifid position, and adds it to the
|
|
|
|
# list of segments filled. Returns the first x coordinate after the part of the
|
|
|
|
# line that has been filled.
|
2022-04-22 12:42:36 +00:00
|
|
|
func _flood_line_around_point(
|
|
|
|
position: Vector2, project: Project, image: Image, src_color: Color
|
|
|
|
) -> int:
|
2022-04-28 21:56:55 +00:00
|
|
|
# this method is called by `_flood_fill` after the required data structures
|
2022-04-16 09:22:51 +00:00
|
|
|
# have been initialized
|
|
|
|
if not image.get_pixelv(position).is_equal_approx(src_color):
|
|
|
|
return int(position.x) + 1
|
|
|
|
var west: Vector2 = position
|
|
|
|
var east: Vector2 = position
|
2022-04-21 01:57:39 +00:00
|
|
|
if project.has_selection:
|
|
|
|
while (
|
|
|
|
project.can_pixel_get_drawn(west)
|
|
|
|
&& image.get_pixelv(west).is_equal_approx(src_color)
|
|
|
|
):
|
|
|
|
west += Vector2.LEFT
|
|
|
|
while (
|
|
|
|
project.can_pixel_get_drawn(east)
|
|
|
|
&& image.get_pixelv(east).is_equal_approx(src_color)
|
|
|
|
):
|
|
|
|
east += Vector2.RIGHT
|
|
|
|
else:
|
|
|
|
while west.x >= 0 && image.get_pixelv(west).is_equal_approx(src_color):
|
|
|
|
west += Vector2.LEFT
|
|
|
|
while east.x < project.size.x && image.get_pixelv(east).is_equal_approx(src_color):
|
|
|
|
east += Vector2.RIGHT
|
2022-04-16 09:22:51 +00:00
|
|
|
# Make a note of the stuff we processed
|
|
|
|
var c = int(position.y)
|
|
|
|
var segment = _allegro_flood_segments[c]
|
|
|
|
# we may have already processed some segments on this y coordinate
|
|
|
|
if segment.flooding:
|
|
|
|
while segment.next > 0:
|
|
|
|
c = segment.next # index of next segment in this line of image
|
|
|
|
segment = _allegro_flood_segments[c]
|
|
|
|
# found last current segment on this line
|
|
|
|
c = _allegro_flood_segments.size()
|
|
|
|
segment.next = c
|
|
|
|
_add_new_segment(position.y)
|
|
|
|
segment = _allegro_flood_segments[c]
|
|
|
|
# set the values for the current segment
|
|
|
|
segment.flooding = true
|
|
|
|
segment.left_position = west.x + 1
|
|
|
|
segment.right_position = east.x - 1
|
|
|
|
segment.y = position.y
|
|
|
|
segment.next = 0
|
|
|
|
# Should we process segments above or below this one?
|
2022-04-21 01:57:39 +00:00
|
|
|
# when there is a selected area, the pixels above and below the one we started creating this
|
|
|
|
# segment from may be outside it. It's easier to assume we should be checking for segments
|
|
|
|
# above and below this one than to specifically check every single pixel in it, because that
|
|
|
|
# test will be performed later anyway.
|
|
|
|
# On the other hand, this test we described is the same `project.can_pixel_get_drawn` does if
|
|
|
|
# there is no selection, so we don't need branching here.
|
|
|
|
segment.todo_above = position.y > 0
|
|
|
|
segment.todo_below = position.y < project.size.y - 1
|
2022-04-16 09:22:51 +00:00
|
|
|
# this is an actual segment we should be coloring, so we add it to the results for the
|
|
|
|
# current image
|
2022-05-02 21:58:14 +00:00
|
|
|
if segment.right_position >= segment.left_position:
|
2022-04-28 21:56:55 +00:00
|
|
|
_allegro_image_segments.append(segment)
|
2022-04-16 09:22:51 +00:00
|
|
|
# we know the point just east of the segment is not part of a segment that should be
|
|
|
|
# processed, else it would be part of this segment
|
|
|
|
return int(east.x) + 1
|
|
|
|
|
|
|
|
|
|
|
|
func _check_flooded_segment(
|
|
|
|
y: int, left: int, right: int, project: Project, image: Image, src_color: Color
|
|
|
|
) -> bool:
|
|
|
|
var ret = false
|
|
|
|
var c: int = 0
|
|
|
|
while left <= right:
|
|
|
|
c = y
|
|
|
|
while true:
|
|
|
|
var segment = _allegro_flood_segments[c]
|
|
|
|
if left >= segment.left_position and left <= segment.right_position:
|
|
|
|
left = segment.right_position + 2
|
|
|
|
break
|
|
|
|
c = segment.next
|
|
|
|
if c == 0: # couldn't find a valid segment, so we draw a new one
|
|
|
|
left = _flood_line_around_point(Vector2(left, y), project, image, src_color)
|
|
|
|
ret = true
|
|
|
|
break
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func _flood_fill(position: Vector2) -> void:
|
2022-04-16 09:22:51 +00:00
|
|
|
# implements the floodfill routine by Shawn Hargreaves
|
|
|
|
# from https://www1.udel.edu/CIS/software/dist/allegro-4.2.1/src/flood.c
|
2021-11-25 12:48:30 +00:00
|
|
|
var project: Project = Global.current_project
|
2021-06-11 22:06:13 +00:00
|
|
|
var images := _get_selected_draw_images()
|
|
|
|
for image in images:
|
2021-11-25 12:48:30 +00:00
|
|
|
var color: Color = image.get_pixelv(position)
|
2022-09-18 15:43:50 +00:00
|
|
|
if _fill_with == FillWith.COLOR or _pattern == null:
|
2022-04-21 01:57:39 +00:00
|
|
|
# end early if we are filling with the same color
|
2021-06-11 22:06:13 +00:00
|
|
|
if tool_slot.color.is_equal_approx(color):
|
2022-09-18 16:22:06 +00:00
|
|
|
continue
|
2022-04-21 01:57:39 +00:00
|
|
|
else:
|
|
|
|
# end early if we are filling with an empty pattern
|
2022-09-18 12:56:36 +00:00
|
|
|
var pattern_size := _pattern.image.get_size()
|
2022-04-21 01:57:39 +00:00
|
|
|
if pattern_size.x == 0 or pattern_size.y == 0:
|
|
|
|
return
|
2022-04-16 09:22:51 +00:00
|
|
|
# init flood data structures
|
|
|
|
_allegro_flood_segments = []
|
|
|
|
_allegro_image_segments = []
|
2022-04-28 21:56:55 +00:00
|
|
|
_compute_segments_for_image(position, project, image, color)
|
2022-04-21 01:57:39 +00:00
|
|
|
# now actually color the image: since we have already checked a few things for the points
|
|
|
|
# we'll process here, we're going to skip a bunch of safety checks to speed things up.
|
2022-04-28 21:56:55 +00:00
|
|
|
_color_segments(image)
|
|
|
|
|
|
|
|
|
|
|
|
func _compute_segments_for_image(
|
|
|
|
position: Vector2, project: Project, image: Image, src_color: Color
|
|
|
|
) -> void:
|
|
|
|
# initially allocate at least 1 segment per line of image
|
|
|
|
for j in image.get_height():
|
|
|
|
_add_new_segment(j)
|
|
|
|
# start flood algorithm
|
|
|
|
_flood_line_around_point(position, project, image, src_color)
|
|
|
|
# test all segments while also discovering more
|
|
|
|
var done = false
|
|
|
|
while not done:
|
|
|
|
done = true
|
|
|
|
var max_index = _allegro_flood_segments.size()
|
|
|
|
for c in max_index:
|
|
|
|
var p = _allegro_flood_segments[c]
|
|
|
|
if p.todo_below: # check below the segment?
|
|
|
|
p.todo_below = false
|
|
|
|
if _check_flooded_segment(
|
|
|
|
p.y + 1, p.left_position, p.right_position, project, image, src_color
|
|
|
|
):
|
|
|
|
done = false
|
|
|
|
if p.todo_above: # check above the segment?
|
|
|
|
p.todo_above = false
|
|
|
|
if _check_flooded_segment(
|
|
|
|
p.y - 1, p.left_position, p.right_position, project, image, src_color
|
|
|
|
):
|
|
|
|
done = false
|
|
|
|
|
|
|
|
|
|
|
|
func _color_segments(image: Image) -> void:
|
2022-09-18 15:43:50 +00:00
|
|
|
if _fill_with == FillWith.COLOR or _pattern == null:
|
2022-07-29 12:31:06 +00:00
|
|
|
var color_str = tool_slot.color.to_html()
|
2022-04-28 21:56:55 +00:00
|
|
|
# short circuit for flat colors
|
|
|
|
for c in _allegro_image_segments.size():
|
|
|
|
var p = _allegro_image_segments[c]
|
|
|
|
for px in range(p.left_position, p.right_position + 1):
|
|
|
|
# We don't have to check again whether the point being processed is within the bounds
|
2022-07-29 12:31:06 +00:00
|
|
|
image.set_pixel(px, p.y, Color(color_str))
|
2022-04-28 21:56:55 +00:00
|
|
|
else:
|
|
|
|
# shortcircuit tests for patternfills
|
|
|
|
var pattern_size = _pattern.image.get_size()
|
|
|
|
# we know the pattern had a valid size when we began flooding, so we can skip testing that
|
|
|
|
# again for every point in the pattern.
|
|
|
|
for c in _allegro_image_segments.size():
|
|
|
|
var p = _allegro_image_segments[c]
|
|
|
|
for px in range(p.left_position, p.right_position + 1):
|
|
|
|
_set_pixel_pattern(image, px, p.y, pattern_size)
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
2022-04-21 01:57:39 +00:00
|
|
|
func _set_pixel_pattern(image: Image, x: int, y: int, pattern_size: Vector2) -> void:
|
|
|
|
_pattern.image.lock()
|
|
|
|
var px := int(x + _offset_x) % int(pattern_size.x)
|
|
|
|
var py := int(y + _offset_y) % int(pattern_size.y)
|
|
|
|
var pc := _pattern.image.get_pixel(px, py)
|
|
|
|
_pattern.image.unlock()
|
|
|
|
image.set_pixel(x, y, pc)
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func commit_undo(action: String, undo_data: Dictionary) -> void:
|
2021-06-11 22:06:13 +00:00
|
|
|
var redo_data := _get_undo_data()
|
2021-11-25 12:48:30 +00:00
|
|
|
var project: Project = Global.current_project
|
2021-06-11 22:06:13 +00:00
|
|
|
var frame := -1
|
|
|
|
var layer := -1
|
|
|
|
if Global.animation_timer.is_stopped() and project.selected_cels.size() == 1:
|
|
|
|
frame = project.current_frame
|
|
|
|
layer = project.current_layer
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
project.undos += 1
|
|
|
|
project.undo_redo.create_action(action)
|
2021-06-11 22:06:13 +00:00
|
|
|
for image in redo_data:
|
|
|
|
project.undo_redo.add_do_property(image, "data", redo_data[image])
|
2021-07-25 18:49:37 +00:00
|
|
|
image.unlock()
|
2021-06-11 22:06:13 +00:00
|
|
|
for image in undo_data:
|
|
|
|
project.undo_redo.add_undo_property(image, "data", undo_data[image])
|
2021-12-02 00:22:32 +00:00
|
|
|
project.undo_redo.add_do_method(Global, "undo_or_redo", false, frame, layer)
|
|
|
|
project.undo_redo.add_undo_method(Global, "undo_or_redo", true, frame, layer)
|
2020-07-09 12:22:17 +00:00
|
|
|
project.undo_redo.commit_action()
|
|
|
|
|
|
|
|
|
|
|
|
func _get_undo_data() -> Dictionary:
|
2021-06-11 22:06:13 +00:00
|
|
|
var data := {}
|
|
|
|
var images := _get_selected_draw_images()
|
|
|
|
for image in images:
|
|
|
|
image.unlock()
|
|
|
|
data[image] = image.data
|
|
|
|
image.lock()
|
2020-07-09 12:22:17 +00:00
|
|
|
return data
|
2021-11-14 01:30:00 +00:00
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func _pick_color(position: Vector2) -> void:
|
|
|
|
var project: Project = Global.current_project
|
2022-06-20 09:07:20 +00:00
|
|
|
position = project.tiles.get_canon_position(position)
|
2021-11-14 01:30:00 +00:00
|
|
|
|
|
|
|
if position.x < 0 or position.y < 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
var image := Image.new()
|
|
|
|
image.copy_from(_get_draw_image())
|
|
|
|
if position.x > image.get_width() - 1 or position.y > image.get_height() - 1:
|
|
|
|
return
|
|
|
|
|
|
|
|
image.lock()
|
|
|
|
var color := image.get_pixelv(position)
|
|
|
|
image.unlock()
|
|
|
|
var button := BUTTON_LEFT if Tools._slots[BUTTON_LEFT].tool_node == self else BUTTON_RIGHT
|
|
|
|
Tools.assign_color(color, button, false)
|