GraphicsEditor/addons/graphics_editor/Editor.gd

187 lines
7.1 KiB
GDScript3
Raw Normal View History

2019-08-04 09:57:42 +02:00
tool
extends Control
onready var paint_canvas_container_node = get_node("PaintCanvasContainer")
2019-11-23 12:33:43 +01:00
onready var paint_canvas_node = paint_canvas_container_node.get_node("ViewportContainer/Viewport/PaintCanvas")
onready var paint_canvas_image_node = paint_canvas_node.get_node("CanvasImage")
onready var camera = paint_canvas_container_node.get_node("ViewportContainer/Viewport/Camera2D")
2019-08-04 09:57:42 +02:00
onready var grids_node = paint_canvas_node.get_node("Grids")
2019-11-23 12:33:43 +01:00
onready var tool_manager = get_node("ToolManager")
2019-08-04 09:57:42 +02:00
onready var textinfo = get_node("BottomPanel/TextInfo")
2019-11-23 12:33:43 +01:00
onready var layers = get_node("ToolMenu/Layers")
2019-08-04 09:57:42 +02:00
onready var grid_size = paint_canvas_node.grid_size
2019-11-23 12:33:43 +01:00
onready var chunk_size = paint_canvas_node.chunk_size
var selected_color = Color(1, 1, 1, 1)
var util = preload("res://addons/graphics_editor/Util.gd")
2019-08-04 09:57:42 +02:00
var allow_drawing = true
2019-11-23 12:33:43 +01:00
#TODO:
#Work on the brush system!
#Make a GUI Notification script!
2019-08-04 09:57:42 +02:00
func _ready():
2019-11-23 12:33:43 +01:00
#-----------------
#Setup active tool
#-----------------
tool_manager.set_active_tool("Pencil")
2019-08-04 09:57:42 +02:00
#---------------------------
#Setup the info bottom panel
#---------------------------
add_text_info_variables()
#------------------
#Setup visual grids
#------------------
2019-11-23 12:33:43 +01:00
paint_canvas_node.connect("grid_resized", self, "grid_resized")
grid_resized(paint_canvas_node.grid_size)
2019-08-04 09:57:42 +02:00
2019-11-23 12:33:43 +01:00
#-----------------------------------------------------------------------
#Set the selected color to what the color picker has selected as default
#-----------------------------------------------------------------------
2019-08-04 09:57:42 +02:00
selected_color = get_node("ToolMenu/Buttons/ColorPicker").color
2019-11-23 12:33:43 +01:00
#TODO: Make the paint canvas chunk size not a vector2?
func grid_resized(size):
grids_node.get_node("VisualGrid").rect_size = paint_canvas_node.canvas_size * size
grids_node.get_node("VisualGrid").size = size
grids_node.get_node("VisualGrid2").rect_size = paint_canvas_node.canvas_size * size
grids_node.get_node("VisualGrid2").size = size * paint_canvas_node.chunk_size.x
2019-08-04 09:57:42 +02:00
var mouse_position = Vector2()
var canvas_position = Vector2()
var canvas_mouse_position = Vector2()
var cell_mouse_position = Vector2()
var cell_region_position = Vector2()
var cell_position_in_region = Vector2()
var cell_color = Color()
2019-11-23 12:33:43 +01:00
func process_common_used_variables():
grid_size = paint_canvas_node.grid_size
chunk_size = paint_canvas_node.chunk_size
mouse_position = get_local_mouse_position()
canvas_mouse_position = paint_canvas_node.get_local_mouse_position()
cell_mouse_position = Vector2(floor(canvas_mouse_position.x / grid_size), floor(canvas_mouse_position.y / grid_size))
cell_region_position = Vector2(floor(cell_mouse_position.x / chunk_size.x), floor(cell_mouse_position.y / chunk_size.y))
cell_position_in_region = paint_canvas_node.pixel_in_canvas_region(cell_mouse_position)
cell_color = paint_canvas_node.get_pixel(cell_mouse_position)
2019-08-04 09:57:42 +02:00
var last_mouse_position = Vector2()
var last_canvas_position = Vector2()
var last_canvas_mouse_position = Vector2()
var last_cell_mouse_position = Vector2()
var last_cell_color = Color()
2019-11-23 12:33:43 +01:00
func process_last_common_used_variables():
last_mouse_position = mouse_position
last_canvas_position = canvas_position
last_canvas_mouse_position = canvas_mouse_position
last_cell_mouse_position = cell_mouse_position
last_cell_color = cell_color
var active_tool
func process_active_tool():
active_tool = get_node("ToolManager").get_active_tool()
active_tool.cell_mouse_position = cell_mouse_position
active_tool.last_cell_mouse_position = last_cell_mouse_position
active_tool.selected_color = selected_color
active_tool.cell_color = cell_color
2019-08-04 09:57:42 +02:00
# warning-ignore:unused_argument
func _process(delta):
#It's a lot more easier to just keep updating the variables in here than just have a bunch of local variables
#in every update function and make it very messy
#Update commonly used variables
2019-11-23 12:33:43 +01:00
process_common_used_variables()
#Process the active tool
process_active_tool()
2019-08-04 09:57:42 +02:00
#Process the brush drawing stuff
if paint_canvas_container_node.mouse_in_region and paint_canvas_container_node.mouse_on_top:
brush_process()
#Render the highlighting stuff
update()
#Canvas Shift Moving
2019-11-23 12:33:43 +01:00
if mouse_position != last_mouse_position:
2019-08-04 09:57:42 +02:00
if paint_canvas_container_node.has_focus():
2019-11-23 12:33:43 +01:00
if Input.is_key_pressed(KEY_SHIFT) or Input.is_mouse_button_pressed(BUTTON_MIDDLE):
if Input.is_mouse_button_pressed(BUTTON_LEFT) or Input.is_mouse_button_pressed(BUTTON_MIDDLE):
2019-08-04 09:57:42 +02:00
var relative = mouse_position - last_mouse_position
2019-11-23 12:33:43 +01:00
camera.position -= relative * camera.zoom
2019-08-04 09:57:42 +02:00
allow_drawing = false
else:
allow_drawing = true
#Update text info
update_text_info()
#Update last variables with the current variables
2019-11-23 12:33:43 +01:00
process_last_common_used_variables()
2019-08-04 09:57:42 +02:00
func brush_process():
if Input.is_mouse_button_pressed(BUTTON_LEFT):
if allow_drawing:
2019-11-23 12:33:43 +01:00
active_tool.on_left_mouse_click()
2019-08-04 09:57:42 +02:00
elif Input.is_mouse_button_pressed(BUTTON_RIGHT):
if allow_drawing:
2019-11-23 12:33:43 +01:00
active_tool.on_right_mouse_click()
var zoom_amount = 0.5
func _input(event):
if event is InputEventMouseButton:
if event.is_pressed():
if event.button_index == BUTTON_WHEEL_UP:
if camera.zoom - Vector2(zoom_amount, zoom_amount) > Vector2(0, 0):
camera.zoom -= Vector2(zoom_amount, zoom_amount)
elif event.button_index == BUTTON_WHEEL_DOWN:
camera.zoom += Vector2(zoom_amount, zoom_amount)
2019-08-04 09:57:42 +02:00
func add_text_info_variables():
textinfo.add_text_info("FPS")
textinfo.add_text_info("Mouse Position")
textinfo.add_text_info("Canvas Mouse Position")
textinfo.add_text_info("Canvas Position")
textinfo.add_text_info("Cell Position")
var cell_color_texture_rect = ColorRect.new()
cell_color_texture_rect.name = "Cell Color"
cell_color_texture_rect.rect_size = Vector2(14, 14)
cell_color_texture_rect.rect_position.x = 120
textinfo.add_text_info("Cell Color", cell_color_texture_rect)
textinfo.add_text_info("Cell Region")
textinfo.add_text_info("Cell Position in Region")
func update_text_info():
textinfo.update_text_info("FPS", Engine.get_frames_per_second())
textinfo.update_text_info("Mouse Position", mouse_position)
textinfo.update_text_info("Canvas Mouse Position", canvas_mouse_position)
textinfo.update_text_info("Canvas Position", canvas_position)
textinfo.update_text_info("Cell Position", cell_mouse_position)
var cell_color_text = cell_color
2019-11-23 12:33:43 +01:00
if paint_canvas_container_node.mouse_in_region and paint_canvas_container_node.mouse_on_top:
2019-08-04 09:57:42 +02:00
if Input.is_mouse_button_pressed(BUTTON_LEFT) or Input.is_mouse_button_pressed(BUTTON_RIGHT):
2019-11-23 12:33:43 +01:00
if paint_canvas_node.last_pixel_drawn.size() > 0:
cell_color_text = paint_canvas_node.last_pixel_drawn[1]
2019-08-04 09:57:42 +02:00
if cell_color_text == null:
cell_color_text = Color(0, 0, 0, 0)
textinfo.update_text_info("Cell Color", cell_color_text, "Cell Color", "color", cell_color_text)
textinfo.update_text_info("Cell Region", cell_region_position)
textinfo.update_text_info("Cell Position in Region", cell_position_in_region)
func _on_PaintTool_pressed():
2019-11-23 12:33:43 +01:00
tool_manager.set_active_tool("Pencil")
2019-08-04 09:57:42 +02:00
func _on_BucketTool_pressed():
2019-11-23 12:33:43 +01:00
tool_manager.set_active_tool("Bucket")
2019-08-04 09:57:42 +02:00
func _on_ColorPicker_color_changed(color):
selected_color = color
func _on_Save_pressed():
get_node("SaveFileDialog").show()
func _on_RainbowTool_pressed():
2019-11-23 12:33:43 +01:00
tool_manager.set_active_tool("Rainbow")