2020-07-09 12:22:17 +00:00
|
|
|
|
extends Node
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
|
signal color_changed(color, button)
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
|
var pen_pressure := 1.0
|
|
|
|
|
var control := false
|
|
|
|
|
var shift := false
|
|
|
|
|
var alt := false
|
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
|
"Move": "res://src/Tools/Move.tscn",
|
|
|
|
|
"Zoom": "res://src/Tools/Zoom.tscn",
|
|
|
|
|
"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
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
|
|
|
|
|
|
class Slot:
|
|
|
|
|
var name: String
|
|
|
|
|
var kname: String
|
|
|
|
|
var tool_node: Node = null
|
|
|
|
|
var button: int
|
|
|
|
|
var color: Color
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
var pixel_perfect := false
|
|
|
|
|
var horizontal_mirror := false
|
|
|
|
|
var vertical_mirror := false
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
|
func _init(slot_name: String) -> void:
|
2020-07-09 12:22:17 +00:00
|
|
|
|
name = slot_name
|
|
|
|
|
kname = name.replace(" ", "_").to_lower()
|
|
|
|
|
load_config()
|
|
|
|
|
|
|
|
|
|
func save_config() -> void:
|
|
|
|
|
var config := {
|
2021-11-25 12:48:30 +00:00
|
|
|
|
"pixel_perfect": pixel_perfect,
|
|
|
|
|
"horizontal_mirror": horizontal_mirror,
|
|
|
|
|
"vertical_mirror": vertical_mirror,
|
2020-07-09 12:22:17 +00:00
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
2020-10-23 17:18:39 +00:00
|
|
|
|
func _ready() -> void:
|
2021-06-26 11:01:07 +00:00
|
|
|
|
_tool_buttons = Global.control.find_node("ToolButtons")
|
2020-07-09 12:22:17 +00:00
|
|
|
|
_slots[BUTTON_LEFT] = Slot.new("Left tool")
|
|
|
|
|
_slots[BUTTON_RIGHT] = Slot.new("Right tool")
|
2021-08-16 23:56:19 +00:00
|
|
|
|
_panels[BUTTON_LEFT] = Global.control.find_node("LeftPanelContainer", true, false)
|
|
|
|
|
_panels[BUTTON_RIGHT] = Global.control.find_node("RightPanelContainer", true, false)
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
|
var tool_name: String = Global.config_cache.get_value(
|
|
|
|
|
_slots[BUTTON_LEFT].kname, "tool", "Pencil"
|
|
|
|
|
)
|
2021-11-17 17:59:14 +00:00
|
|
|
|
if not tool_name in _tools:
|
|
|
|
|
tool_name = "Pencil"
|
|
|
|
|
set_tool(tool_name, BUTTON_LEFT)
|
|
|
|
|
tool_name = Global.config_cache.get_value(_slots[BUTTON_RIGHT].kname, "tool", "Eraser")
|
|
|
|
|
if not tool_name in _tools:
|
|
|
|
|
tool_name = "Eraser"
|
|
|
|
|
set_tool(tool_name, BUTTON_RIGHT)
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
update_tool_buttons()
|
|
|
|
|
update_tool_cursors()
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
|
# Yield is necessary for the color picker nodes to update their color values
|
|
|
|
|
yield(get_tree(), "idle_frame")
|
|
|
|
|
var color_value: Color = Global.config_cache.get_value(
|
|
|
|
|
_slots[BUTTON_LEFT].kname, "color", Color.black
|
|
|
|
|
)
|
2021-11-17 17:59:14 +00:00
|
|
|
|
assign_color(color_value, BUTTON_LEFT, false)
|
|
|
|
|
color_value = Global.config_cache.get_value(_slots[BUTTON_RIGHT].kname, "color", Color.white)
|
|
|
|
|
assign_color(color_value, BUTTON_RIGHT, false)
|
|
|
|
|
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
|
func set_tool(name: String, button: int) -> void:
|
2020-07-09 12:22:17 +00:00
|
|
|
|
var slot = _slots[button]
|
2021-11-25 12:48:30 +00:00
|
|
|
|
var panel: Node = _panels[button]
|
|
|
|
|
var node: Node = load(_tools[name]).instance()
|
2020-07-09 12:22:17 +00:00
|
|
|
|
node.name = name
|
|
|
|
|
node.tool_slot = slot
|
|
|
|
|
slot.tool_node = node
|
|
|
|
|
slot.button = button
|
|
|
|
|
panel.add_child(slot.tool_node)
|
|
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
|
func assign_tool(name: String, button: int) -> void:
|
2020-07-09 12:22:17 +00:00
|
|
|
|
var slot = _slots[button]
|
2021-11-25 12:48:30 +00:00
|
|
|
|
var panel: Node = _panels[button]
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
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
|
2020-10-23 17:18:39 +00:00
|
|
|
|
assign_color(right, BUTTON_LEFT, false)
|
|
|
|
|
assign_color(left, BUTTON_RIGHT, false)
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
|
func assign_color(color: Color, button: int, change_alpha := true) -> void:
|
|
|
|
|
var c: Color = _slots[button].color
|
2020-10-23 17:18:39 +00:00
|
|
|
|
# This was requested by Issue #54 on GitHub
|
|
|
|
|
if color.a == 0 and change_alpha:
|
2020-07-09 12:22:17 +00:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
|
func get_assigned_color(button: int) -> Color:
|
2020-07-09 12:22:17 +00:00
|
|
|
|
return _slots[button].color
|
|
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
|
func set_button_size(button_size: int) -> void:
|
2021-06-13 23:14:31 +00:00
|
|
|
|
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)
|
2021-06-14 22:03:46 +00:00
|
|
|
|
_tool_buttons.get_parent().rect_position.x = 9
|
2021-06-13 23:14:31 +00:00
|
|
|
|
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)
|
2021-06-14 22:03:46 +00:00
|
|
|
|
_tool_buttons.get_parent().rect_position.x = 6
|
2021-06-13 23:14:31 +00:00
|
|
|
|
|
|
|
|
|
# It doesn't actually set the size to zero, it just resets it
|
|
|
|
|
_tool_buttons.get_parent().rect_size = Vector2.ZERO
|
2021-08-16 23:56:19 +00:00
|
|
|
|
_tool_buttons.columns = 1
|
|
|
|
|
_tool_buttons.get_parent().get_parent().get_parent().get_parent().split_offset = 0
|
2021-06-13 23:14:31 +00:00
|
|
|
|
|
|
|
|
|
|
2020-07-09 12:22:17 +00:00
|
|
|
|
func update_tool_buttons() -> void:
|
|
|
|
|
for child in _tool_buttons.get_children():
|
2021-11-25 12:48:30 +00:00
|
|
|
|
var left_background: NinePatchRect = child.get_node("BackgroundLeft")
|
|
|
|
|
var right_background: NinePatchRect = child.get_node("BackgroundRight")
|
2021-06-13 23:14:31 +00:00
|
|
|
|
left_background.visible = _slots[BUTTON_LEFT].tool_node.name == child.name
|
|
|
|
|
right_background.visible = _slots[BUTTON_RIGHT].tool_node.name == child.name
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func update_tool_cursors() -> void:
|
2021-11-25 12:48:30 +00:00
|
|
|
|
var left_image = load(
|
|
|
|
|
"res://assets/graphics/tools/cursors/%s.png" % _slots[BUTTON_LEFT].tool_node.name.to_lower()
|
|
|
|
|
)
|
2021-11-23 00:52:21 +00:00
|
|
|
|
Global.left_cursor.texture = left_image
|
2021-11-25 12:48:30 +00:00
|
|
|
|
var right_image = load(
|
|
|
|
|
(
|
|
|
|
|
"res://assets/graphics/tools/cursors/%s.png"
|
|
|
|
|
% _slots[BUTTON_RIGHT].tool_node.name.to_lower()
|
|
|
|
|
)
|
|
|
|
|
)
|
2021-11-23 00:52:21 +00:00
|
|
|
|
Global.right_cursor.texture = right_image
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
2021-03-30 17:07:13 +00:00
|
|
|
|
func draw_preview() -> void:
|
|
|
|
|
_slots[BUTTON_LEFT].tool_node.draw_preview()
|
|
|
|
|
_slots[BUTTON_RIGHT].tool_node.draw_preview()
|
|
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
|
func handle_draw(position: Vector2, event: InputEvent) -> void:
|
2020-07-09 12:22:17 +00:00
|
|
|
|
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
|
|
|
|
|
|
2020-07-09 12:22:17 +00:00
|
|
|
|
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)
|
2020-07-09 12:22:17 +00:00
|
|
|
|
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)
|
2020-07-09 12:22:17 +00:00
|
|
|
|
_active_button = -1
|
|
|
|
|
|
|
|
|
|
if event is InputEventMouseMotion:
|
2021-01-26 14:34:25 +00:00
|
|
|
|
pen_pressure = event.pressure
|
|
|
|
|
if Global.pressure_sensitivity_mode == Global.PressureSensitivity.NONE:
|
|
|
|
|
pen_pressure = 1.0
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
|
|
|
|
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)
|
2020-07-09 12:22:17 +00:00
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
|
var project: Project = Global.current_project
|
2020-07-09 12:22:17 +00:00
|
|
|
|
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
|