1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-19 17:49:47 +00:00
Pixelorama/src/Autoload/Tools.gd

241 lines
7.9 KiB
GDScript3
Raw Normal View History

extends Node
class Slot:
var name : String
var kname : String
var tool_node : Node = null
var button : int
var color : Color
var pixel_perfect := false
var horizontal_mirror := false
var vertical_mirror := false
func _init(slot_name : String) -> void:
name = slot_name
kname = name.replace(" ", "_").to_lower()
load_config()
func save_config() -> void:
var config := {
"pixel_perfect" : pixel_perfect,
"horizontal_mirror" : horizontal_mirror,
"vertical_mirror" : vertical_mirror,
}
Global.config_cache.set_value(kname, "slot", config)
func load_config() -> void:
var config = Global.config_cache.get_value(kname, "slot", {})
pixel_perfect = config.get("pixel_perfect", pixel_perfect)
horizontal_mirror = config.get("horizontal_mirror", horizontal_mirror)
vertical_mirror = config.get("vertical_mirror", vertical_mirror)
signal color_changed(color, button)
var _tools = {
"RectSelect" : "res://src/Tools/SelectionTools/RectSelect.tscn",
"EllipseSelect" : "res://src/Tools/SelectionTools/EllipseSelect.tscn",
"PolygonSelect" : "res://src/Tools/SelectionTools/PolygonSelect.tscn",
"ColorSelect" : "res://src/Tools/SelectionTools/ColorSelect.tscn",
"MagicWand" : "res://src/Tools/SelectionTools/MagicWand.tscn",
"Lasso" : "res://src/Tools/SelectionTools/Lasso.tscn",
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
"Move" : "res://src/Tools/Move.tscn",
"Zoom" : "res://src/Tools/Zoom.tscn",
2020-12-23 18:41:42 +00:00
"Pan" : "res://src/Tools/Pan.tscn",
"ColorPicker" : "res://src/Tools/ColorPicker.tscn",
"Pencil" : "res://src/Tools/Pencil.tscn",
"Eraser" : "res://src/Tools/Eraser.tscn",
"Bucket" : "res://src/Tools/Bucket.tscn",
"Shading" : "res://src/Tools/Shading.tscn",
"LineTool" : "res://src/Tools/LineTool.tscn",
"RectangleTool" : "res://src/Tools/RectangleTool.tscn",
"EllipseTool" : "res://src/Tools/EllipseTool.tscn",
}
var _slots = {}
var _panels = {}
var _tool_buttons : Node
var _active_button := -1
var _last_position := Vector2.INF
var pen_pressure := 1.0
var control := false
var shift := false
var alt := false
func _ready() -> void:
_tool_buttons = Global.find_node_by_name(Global.control, "ToolButtons")
yield(get_tree(), "idle_frame")
_slots[BUTTON_LEFT] = Slot.new("Left tool")
_slots[BUTTON_RIGHT] = Slot.new("Right tool")
_panels[BUTTON_LEFT] = Global.find_node_by_name(Global.control, "LeftPanelContainer")
_panels[BUTTON_RIGHT] = Global.find_node_by_name(Global.control, "RightPanelContainer")
var value = Global.config_cache.get_value(_slots[BUTTON_LEFT].kname, "tool", "Pencil")
if not value in _tools:
value = "Pencil"
set_tool(value, BUTTON_LEFT)
value = Global.config_cache.get_value(_slots[BUTTON_RIGHT].kname, "tool", "Eraser")
if not value in _tools:
value = "Eraser"
set_tool(value, BUTTON_RIGHT)
value = Global.config_cache.get_value(_slots[BUTTON_LEFT].kname, "color", Color.black)
assign_color(value, BUTTON_LEFT, false)
value = Global.config_cache.get_value(_slots[BUTTON_RIGHT].kname, "color", Color.white)
assign_color(value, BUTTON_RIGHT, false)
update_tool_buttons()
update_tool_cursors()
func set_tool(name : String, button : int) -> void:
var slot = _slots[button]
var panel : Node = _panels[button]
var node : Node = load(_tools[name]).instance()
node.name = name
node.tool_slot = slot
slot.tool_node = node
slot.button = button
panel.add_child(slot.tool_node)
func assign_tool(name : String, button : int) -> void:
var slot = _slots[button]
var panel : Node = _panels[button]
if slot.tool_node != null:
if slot.tool_node.name == name:
return
panel.remove_child(slot.tool_node)
slot.tool_node.queue_free()
set_tool(name, button)
update_tool_buttons()
update_tool_cursors()
Global.config_cache.set_value(slot.kname, "tool", name)
func default_color() -> void:
assign_color(Color.black, BUTTON_LEFT)
assign_color(Color.white, BUTTON_RIGHT)
func swap_color() -> void:
var left = _slots[BUTTON_LEFT].color
var right = _slots[BUTTON_RIGHT].color
assign_color(right, BUTTON_LEFT, false)
assign_color(left, BUTTON_RIGHT, false)
func assign_color(color : Color, button : int, change_alpha := true) -> void:
var c : Color = _slots[button].color
# This was requested by Issue #54 on GitHub
if color.a == 0 and change_alpha:
if color.r != c.r or color.g != c.g or color.b != c.b:
color.a = 1
_slots[button].color = color
Global.config_cache.set_value(_slots[button].kname, "color", color)
emit_signal("color_changed", color, button)
func get_assigned_color(button : int) -> Color:
return _slots[button].color
func set_button_size(button_size : int) -> void:
if button_size == Global.ButtonSize.SMALL:
for t in _tool_buttons.get_children():
t.rect_min_size = Vector2(24, 24)
t.get_node("ToolIcon").rect_position = Vector2.ONE
t.get_node("BackgroundLeft").rect_size.x = 12
t.get_node("BackgroundRight").rect_size.x = 12
t.get_node("BackgroundRight").rect_position = Vector2(24, 24)
else:
for t in _tool_buttons.get_children():
t.rect_min_size = Vector2(32, 32)
t.get_node("ToolIcon").rect_position = Vector2.ONE * 5
t.get_node("BackgroundLeft").rect_size.x = 16
t.get_node("BackgroundRight").rect_size.x = 16
t.get_node("BackgroundRight").rect_position = Vector2(32, 32)
# It doesn't actually set the size to zero, it just resets it
_tool_buttons.get_parent().rect_size = Vector2.ZERO
func update_tool_buttons() -> void:
for child in _tool_buttons.get_children():
var left_background : NinePatchRect = child.get_node("BackgroundLeft")
var right_background : NinePatchRect = child.get_node("BackgroundRight")
left_background.visible = _slots[BUTTON_LEFT].tool_node.name == child.name
right_background.visible = _slots[BUTTON_RIGHT].tool_node.name == child.name
func update_tool_cursors() -> void:
var left_image = load("res://assets/graphics/tools/%s.png" % _slots[BUTTON_LEFT].tool_node.name.to_lower())
Global.left_cursor_tool_texture = left_image
var right_image = load("res://assets/graphics/tools/%s.png" % _slots[BUTTON_RIGHT].tool_node.name.to_lower())
Global.right_cursor_tool_texture = right_image
func draw_indicator() -> void:
if Global.left_square_indicator_visible:
_slots[BUTTON_LEFT].tool_node.draw_indicator()
if Global.right_square_indicator_visible:
_slots[BUTTON_RIGHT].tool_node.draw_indicator()
func draw_preview() -> void:
_slots[BUTTON_LEFT].tool_node.draw_preview()
_slots[BUTTON_RIGHT].tool_node.draw_preview()
func handle_draw(position : Vector2, event : InputEvent) -> void:
if not (Global.can_draw and Global.has_focus):
return
2021-03-29 20:00:09 +00:00
var draw_pos := position
if Global.mirror_view:
draw_pos.x = Global.current_project.size.x - position.x - 1
if event is InputEventWithModifiers:
control = event.control
shift = event.shift
alt = event.alt
if event is InputEventMouseButton:
if event.button_index in [BUTTON_LEFT, BUTTON_RIGHT]:
if event.pressed and _active_button == -1:
_active_button = event.button_index
2021-03-29 20:00:09 +00:00
_slots[_active_button].tool_node.draw_start(draw_pos)
elif not event.pressed and event.button_index == _active_button:
2021-03-29 20:00:09 +00:00
_slots[_active_button].tool_node.draw_end(draw_pos)
_active_button = -1
if event is InputEventMouseMotion:
pen_pressure = event.pressure
if Global.pressure_sensitivity_mode == Global.PressureSensitivity.NONE:
pen_pressure = 1.0
if not position.is_equal_approx(_last_position):
_last_position = position
_slots[BUTTON_LEFT].tool_node.cursor_move(position)
_slots[BUTTON_RIGHT].tool_node.cursor_move(position)
if _active_button != -1:
2021-03-29 20:00:09 +00:00
_slots[_active_button].tool_node.draw_move(draw_pos)
var project : Project = Global.current_project
var text := "[%s×%s]" % [project.size.x, project.size.y]
if Global.has_focus:
text += " %s, %s" % [position.x, position.y]
if not _slots[BUTTON_LEFT].tool_node.cursor_text.empty():
text += " %s" % _slots[BUTTON_LEFT].tool_node.cursor_text
if not _slots[BUTTON_RIGHT].tool_node.cursor_text.empty():
text += " %s" % _slots[BUTTON_RIGHT].tool_node.cursor_text
Global.cursor_position_label.text = text