tool
extends EditorPlugin

signal document_changed(new_doc)
signal sd_scene_changed(new_scene)

const MainScreen = preload("res://addons/summer_day/main_screen.tscn")
const Timeline = preload("res://addons/summer_day/user_interface/timeline/timeline.tscn")
const ScenePicker = preload("res://addons/summer_day/user_interface/resource_picker/scene_picker.gd")

var Canvas: SummerDayCanvas

var main_screen_instance: SummerDayMainScreen
var timeline_instance: SummerDayTimeline
var scene_picker_instance: SummerDayScenePicker

var tool_context := SummerDayToolContext.new()



func _enter_tree():
	main_screen_instance = MainScreen.instance()
	Canvas = main_screen_instance.get_node("Canvas")
	Canvas.tool_context = tool_context
	self.connect("document_changed",
		Canvas, "_on_SummerDay_document_changed")
	self.connect("sd_scene_changed",
		Canvas, "_on_SummerDay_scene_changed"
	)
	self.connect("document_changed",
		tool_context, "_on_SummerDay_document_changed"
	)
	self.connect("sd_scene_changed",
		tool_context, "_on_SummerDay_scene_changed"
	)
	# Add the main panel to the editor's main viewport.
	get_editor_interface().get_editor_viewport().add_child(main_screen_instance)
	# Hide the main panel. Very much required.
	make_visible(false)
	
	
	timeline_instance = Timeline.instance()
	add_control_to_bottom_panel(timeline_instance, "SummerDay Timeline")
	
	
	scene_picker_instance = ScenePicker.new()
	add_inspector_plugin(scene_picker_instance)
	
	
	add_custom_type("SummerDayDocument", "Resource",
	load("res://addons/summer_day/data/document/document.gd"),
	get_editor_interface().get_base_control().get_icon("Animation", "EditorIcons"))
	
	add_custom_type("SummerDayDisplay", "Control",
	load("res://addons/summer_day/display/display.gd"),
	get_editor_interface().get_base_control().get_icon("TextureRect", "EditorIcons"))


func _exit_tree():
	if main_screen_instance:
		main_screen_instance.queue_free()
	remove_custom_type("SummerDayDocument")
	remove_custom_type("SummerDayDisplay")
	remove_control_from_bottom_panel(timeline_instance)
	remove_inspector_plugin(scene_picker_instance)


func has_main_screen():
	return true


func make_visible(visible):
	if main_screen_instance:
		main_screen_instance.visible = visible
		
#	if visible:
#		make_bottom_panel_item_visible(timeline_instance)
#	else:
#		hide_bottom_panel()


func handles(object):
	if object is SummerDayDocument:
		return true


func edit(object):
	emit_signal("document_changed", object)
	emit_signal("sd_scene_changed", object.scene)


func get_plugin_name():
	return "SummerDay"


func get_plugin_icon():
	return get_editor_interface().get_base_control().get_icon("AnimationPlayer", "EditorIcons")
