/*************************************************************************/
/*  register_scene_types.cpp                                             */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "register_scene_types.h"

#include "core/config/project_settings.h"
#include "core/input/shortcut.h"
#include "core/object/class_db.h"
#include "core/os/os.h"
#include "scene/2d/animated_sprite.h"
#include "scene/2d/area_2d.h"
#include "scene/2d/audio_stream_player_2d.h"
#include "scene/2d/back_buffer_copy.h"
#include "scene/2d/camera_2d.h"
#include "scene/main/canvas_item.h"
#include "scene/2d/canvas_modulate.h"
#include "scene/2d/collision_polygon_2d.h"
#include "scene/2d/collision_shape_2d.h"
#include "scene/2d/cpu_particles_2d.h"
#include "scene/2d/joints_2d.h"
#include "scene/2d/line_2d.h"
#include "scene/2d/listener_2d.h"
#include "scene/2d/mesh_instance_2d.h"
#include "scene/2d/multimesh_instance_2d.h"
#include "scene/2d/parallax_background.h"
#include "scene/2d/parallax_layer.h"
#include "scene/2d/path_2d.h"
#include "scene/2d/physics_body_2d.h"
#include "scene/2d/polygon_2d.h"
#include "scene/2d/position_2d.h"
#include "scene/2d/ray_cast_2d.h"
#include "scene/2d/remote_transform_2d.h"
#include "scene/2d/shape_cast_2d.h"
#include "scene/2d/sprite.h"
#include "scene/2d/touch_screen_button.h"
#include "scene/2d/visibility_notifier_2d.h"
#include "scene/2d/y_sort.h"
#include "scene/animation/animation_player.h"
#include "scene/animation/scene_tree_tween.h"
#include "scene/animation/tween.h"
#include "scene/animation/animation.h"
#include "scene/audio/audio_stream_player.h"
#include "scene/gui/aspect_ratio_container.h"
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
#include "scene/gui/center_container.h"
#include "scene/gui/check_box.h"
#include "scene/gui/check_button.h"
#include "scene/gui/color_picker.h"
#include "scene/gui/color_rect.h"
#include "scene/main/control.h"
#include "scene/gui/dialogs.h"
#include "scene/gui/file_dialog.h"
#include "scene/gui/flow_container.h"
#include "scene/gui/graph_edit.h"
#include "scene/gui/graph_node.h"
#include "scene/gui/grid_container.h"
#include "scene/gui/item_list.h"
#include "scene/gui/label.h"
#include "scene/gui/line_edit.h"
#include "scene/gui/link_button.h"
#include "scene/gui/margin_container.h"
#include "scene/gui/menu_button.h"
#include "scene/gui/nine_patch_rect.h"
#include "scene/gui/option_button.h"
#include "scene/gui/panel.h"
#include "scene/gui/panel_container.h"
#include "scene/gui/popup_menu.h"
#include "scene/gui/progress_bar.h"
#include "scene/gui/reference_rect.h"
#include "scene/gui/rich_text_effect.h"
#include "scene/gui/rich_text_label.h"
#include "scene/gui/scroll_bar.h"
#include "scene/gui/scroll_container.h"
#include "scene/gui/separator.h"
#include "scene/gui/slider.h"
#include "scene/gui/spin_box.h"
#include "scene/gui/split_container.h"
#include "scene/gui/tab_container.h"
#include "scene/gui/tabs.h"
#include "scene/gui/text_edit.h"
#include "scene/gui/texture_button.h"
#include "scene/gui/texture_progress.h"
#include "scene/gui/texture_rect.h"
#include "scene/gui/tool_button.h"
#include "scene/gui/tree.h"
#include "scene/gui/video_player.h"
#include "scene/gui/viewport_container.h"
#include "scene/main/canvas_layer.h"
#include "scene/main/http_request.h"
#include "scene/main/instance_placeholder.h"
#include "scene/main/process_group.h"
#include "scene/main/resource_preloader.h"
#include "scene/main/scene_tree.h"
#include "scene/main/timer.h"
#include "scene/main/viewport.h"
#include "scene/main/world.h"
#include "scene/audio/audio_stream_sample.h"
#include "scene/resources/bit_map.h"
#include "scene/resources/shapes_2d/capsule_shape_2d.h"
#include "scene/resources/shapes_2d/circle_shape_2d.h"
#include "scene/resources/shapes_2d/concave_polygon_shape_2d.h"
#include "scene/resources/shapes_2d/convex_polygon_shape_2d.h"
#include "scene/resources/default_theme/default_theme.h"
#include "scene/resources/gradient.h"
#include "scene/resources/mesh/immediate_mesh.h"
#include "scene/resources/shapes_2d/line_shape_2d.h"
#include "scene/resources/material/material.h"
#include "scene/resources/material/shader_material.h"
#include "scene/resources/mesh/mesh.h"
#include "scene/resources/mesh/mesh_data_tool.h"
#include "scene/resources/mesh/multimesh.h"
#include "scene/resources/packed_scene.h"
#include "scene/resources/mesh/polygon_path_finder.h"
#include "scene/resources/mesh/primitive_meshes.h"
#include "scene/resources/shapes_2d/rectangle_shape_2d.h"
#include "scene/resources/resource_format_text.h"
#include "scene/resources/shapes_2d/segment_shape_2d.h"
#include "scene/resources/physics_material.h"
#include "scene/resources/mesh/surface_tool.h"
#include "scene/gui/resources/syntax_highlighter.h"
#include "scene/resources/text_file.h"
#include "scene/resources/texture.h"
#include "scene/resources/video_stream.h"
#include "scene/resources/world_2d.h"
#include "scene/main/scene_string_names.h"

#include "modules/modules_enabled.gen.h" // For freetype.

static Ref<ResourceFormatSaverText> resource_saver_text;
static Ref<ResourceFormatLoaderText> resource_loader_text;

static Ref<ResourceFormatLoaderStreamTexture> resource_loader_stream_texture;
static Ref<ResourceFormatLoaderTextureLayered> resource_loader_texture_layered;

static Ref<ResourceFormatLoaderBMFont> resource_loader_bmfont;

static Ref<ResourceFormatSaverShader> resource_saver_shader;
static Ref<ResourceFormatLoaderShader> resource_loader_shader;

void register_scene_types() {
	SceneStringNames::create();

	OS::get_singleton()->yield(); //may take time to init

	Node::init_node_hrcr();

	resource_loader_stream_texture.instance();
	ResourceLoader::add_resource_format_loader(resource_loader_stream_texture);

	resource_loader_texture_layered.instance();
	ResourceLoader::add_resource_format_loader(resource_loader_texture_layered);

	resource_saver_text.instance();
	ResourceSaver::add_resource_format_saver(resource_saver_text, true);

	resource_loader_text.instance();
	ResourceLoader::add_resource_format_loader(resource_loader_text, true);

	resource_saver_shader.instance();
	ResourceSaver::add_resource_format_saver(resource_saver_shader, true);

	resource_loader_shader.instance();
	ResourceLoader::add_resource_format_loader(resource_loader_shader, true);

	resource_loader_bmfont.instance();
	ResourceLoader::add_resource_format_loader(resource_loader_bmfont, true);

	OS::get_singleton()->yield(); //may take time to init

	ClassDB::register_class<Object>();

	ClassDB::register_class<Node>();
	ClassDB::register_virtual_class<InstancePlaceholder>();

	ClassDB::register_class<World>();
	ClassDB::register_class<Viewport>();
	ClassDB::register_class<ViewportTexture>();
	ClassDB::register_class<HTTPRequest>();
	ClassDB::register_class<Timer>();
	ClassDB::register_class<CanvasLayer>();
	ClassDB::register_class<CanvasModulate>();
	ClassDB::register_class<ResourcePreloader>();
	ClassDB::register_class<ProcessGroup>();

	/* REGISTER GUI */
	ClassDB::register_class<ButtonGroup>();
	ClassDB::register_virtual_class<BaseButton>();

	OS::get_singleton()->yield(); //may take time to init

	ClassDB::register_class<ShortCut>();
	ClassDB::register_class<Control>();
	ClassDB::register_class<Button>();
	ClassDB::register_class<Label>();
	ClassDB::register_virtual_class<ScrollBar>();
	ClassDB::register_class<HScrollBar>();
	ClassDB::register_class<VScrollBar>();
	ClassDB::register_class<ProgressBar>();
	ClassDB::register_virtual_class<Slider>();
	ClassDB::register_class<HSlider>();
	ClassDB::register_class<VSlider>();
	ClassDB::register_class<Popup>();
	ClassDB::register_class<PopupPanel>();
	ClassDB::register_class<MenuButton>();
	ClassDB::register_class<CheckBox>();
	ClassDB::register_class<CheckButton>();
	ClassDB::register_class<ToolButton>();
	ClassDB::register_class<LinkButton>();
	ClassDB::register_class<Panel>();
	ClassDB::register_virtual_class<Range>();

	OS::get_singleton()->yield(); //may take time to init

	ClassDB::register_class<TextureRect>();
	ClassDB::register_class<ColorRect>();
	ClassDB::register_class<NinePatchRect>();
	ClassDB::register_class<ReferenceRect>();
	ClassDB::register_class<AspectRatioContainer>();
	ClassDB::register_class<TabContainer>();
	ClassDB::register_class<Tabs>();
	ClassDB::register_virtual_class<Separator>();
	ClassDB::register_class<HSeparator>();
	ClassDB::register_class<VSeparator>();
	ClassDB::register_class<TextureButton>();
	ClassDB::register_class<Container>();
	ClassDB::register_virtual_class<BoxContainer>();
	ClassDB::register_class<HBoxContainer>();
	ClassDB::register_class<VBoxContainer>();
	ClassDB::register_class<CBoxContainer>();
	ClassDB::register_class<GridContainer>();
	ClassDB::register_class<CenterContainer>();
	ClassDB::register_class<ScrollContainer>();
	ClassDB::register_class<PanelContainer>();
	ClassDB::register_virtual_class<FlowContainer>();
	ClassDB::register_class<HFlowContainer>();
	ClassDB::register_class<VFlowContainer>();

	OS::get_singleton()->yield(); //may take time to init

	ClassDB::register_class<TextureProgress>();
	ClassDB::register_class<ItemList>();

	ClassDB::register_class<LineEdit>();
	ClassDB::register_class<VideoPlayer>();

#ifndef ADVANCED_GUI_DISABLED

	ClassDB::register_class<FileDialog>();

	ClassDB::register_class<PopupMenu>();
	ClassDB::register_class<Tree>();

	ClassDB::register_class<TextEdit>();
	ClassDB::register_class<SyntaxHighlighter>();
	ClassDB::register_class<CodeHighlighter>();

	ClassDB::register_virtual_class<TreeItem>();
	ClassDB::register_class<OptionButton>();
	ClassDB::register_class<SpinBox>();
	ClassDB::register_class<ColorPicker>();
	ClassDB::register_class<ColorPickerButton>();
	ClassDB::register_class<ColorSelectorButton>();
	ClassDB::register_class<RichTextLabel>();
	ClassDB::register_class<RichTextEffect>();
	ClassDB::register_class<CharFXTransform>();
	ClassDB::register_class<PopupDialog>();
	ClassDB::register_class<WindowDialog>();
	ClassDB::register_class<AcceptDialog>();
	ClassDB::register_class<ConfirmationDialog>();
	ClassDB::register_class<MarginContainer>();
	ClassDB::register_class<ViewportContainer>();
	ClassDB::register_virtual_class<SplitContainer>();
	ClassDB::register_class<HSplitContainer>();
	ClassDB::register_class<VSplitContainer>();
	ClassDB::register_class<CSplitContainer>();
	ClassDB::register_class<GraphNode>();
	ClassDB::register_class<GraphEdit>();

	OS::get_singleton()->yield(); //may take time to init

#endif

	/* REGISTER 3D */

	ClassDB::register_class<AnimationPlayer>();
	ClassDB::register_class<Tween>();
	ClassDB::register_class<SceneTreeTween>();
	ClassDB::register_virtual_class<Tweener>();
	ClassDB::register_class<PropertyTweener>();
	ClassDB::register_class<IntervalTweener>();
	ClassDB::register_class<CallbackTweener>();
	ClassDB::register_class<MethodTweener>();

	OS::get_singleton()->yield(); //may take time to init

#ifndef _3D_DISABLED
	ClassDB::register_class<Curve3D>();

	OS::get_singleton()->yield(); //may take time to init
#endif

	AcceptDialog::set_swap_ok_cancel(GLOBAL_DEF_NOVAL("gui/common/swap_ok_cancel", bool(OS::get_singleton()->get_swap_ok_cancel())));

	ClassDB::register_class<Shader>();

	ClassDB::register_class<ShaderMaterial>();
	ClassDB::register_virtual_class<CanvasItem>();
	ClassDB::register_class<CanvasItemMaterial>();
	SceneTree::add_idle_callback(CanvasItemMaterial::flush_changes);
	CanvasItemMaterial::init_shaders();
	ClassDB::register_class<Node2D>();
	ClassDB::register_class<CPUParticles2D>();
	//ClassDB::register_class<ParticleAttractor2D>();
	ClassDB::register_class<Sprite>();
	//ClassDB::register_type<ViewportSprite>();
	ClassDB::register_class<SpriteFrames>();
	ClassDB::register_class<AnimatedSprite>();
	ClassDB::register_class<Position2D>();
	ClassDB::register_class<Line2D>();
	ClassDB::register_class<MeshInstance2D>();
	ClassDB::register_class<MultiMeshInstance2D>();
	ClassDB::register_virtual_class<CollisionObject2D>();
	ClassDB::register_virtual_class<PhysicsBody2D>();
	ClassDB::register_class<StaticBody2D>();
	ClassDB::register_class<RigidBody2D>();
	ClassDB::register_class<KinematicBody2D>();
	ClassDB::register_class<KinematicCollision2D>();
	ClassDB::register_class<Area2D>();
	ClassDB::register_class<CollisionShape2D>();
	ClassDB::register_class<CollisionPolygon2D>();
	ClassDB::register_class<RayCast2D>();
	ClassDB::register_class<ShapeCast2D>();
	ClassDB::register_class<VisibilityNotifier2D>();
	ClassDB::register_class<VisibilityEnabler2D>();
	ClassDB::register_class<Polygon2D>();
	ClassDB::register_class<YSort>();
	ClassDB::register_class<BackBufferCopy>();

	OS::get_singleton()->yield(); //may take time to init

	ClassDB::register_class<Camera2D>();
	ClassDB::register_class<Listener2D>();
	ClassDB::register_virtual_class<Joint2D>();
	ClassDB::register_class<PinJoint2D>();
	ClassDB::register_class<GrooveJoint2D>();
	ClassDB::register_class<DampedSpringJoint2D>();
	ClassDB::register_class<ParallaxBackground>();
	ClassDB::register_class<ParallaxLayer>();
	ClassDB::register_class<TouchScreenButton>();
	ClassDB::register_class<RemoteTransform2D>();

	OS::get_singleton()->yield(); //may take time to init

	/* REGISTER RESOURCES */

	ClassDB::register_virtual_class<Shader>();

	ClassDB::register_virtual_class<Mesh>();
	ClassDB::register_class<ArrayMesh>();
	ClassDB::register_class<MultiMesh>();
	ClassDB::register_class<ImmediateMesh>();
	ClassDB::register_class<SurfaceTool>();
	ClassDB::register_class<MeshDataTool>();

#ifndef _3D_DISABLED
	ClassDB::register_virtual_class<PrimitiveMesh>();
	ClassDB::register_class<CapsuleMesh>();
	ClassDB::register_class<CubeMesh>();
	ClassDB::register_class<CylinderMesh>();
	ClassDB::register_class<PlaneMesh>();
	ClassDB::register_class<PrismMesh>();
	ClassDB::register_class<QuadMesh>();
	ClassDB::register_class<SphereMesh>();
	ClassDB::register_class<TextMesh>();
	ClassDB::register_class<PointMesh>();
	ClassDB::register_virtual_class<Material>();

	OS::get_singleton()->yield(); //may take time to init

#endif
	ClassDB::register_class<PhysicsMaterial>();
	ClassDB::register_class<World2D>();
	ClassDB::register_virtual_class<Texture>();
	ClassDB::register_class<StreamTexture>();
	ClassDB::register_class<ImageTexture>();
	ClassDB::register_class<AtlasTexture>();
	ClassDB::register_class<MeshTexture>();
	ClassDB::register_class<LargeTexture>();
	ClassDB::register_class<CurveTexture>();
	ClassDB::register_class<GradientTexture>();
	ClassDB::register_class<GradientTexture2D>();
	ClassDB::register_class<ProxyTexture>();
	ClassDB::register_class<AnimatedTexture>();
	ClassDB::register_class<ExternalTexture>();
	ClassDB::register_class<CubeMap>();
	ClassDB::register_virtual_class<TextureLayered>();
	ClassDB::register_class<Texture3D>();
	ClassDB::register_class<TextureArray>();
	ClassDB::register_class<Animation>();
	ClassDB::register_virtual_class<Font>();
	ClassDB::register_class<BitmapFont>();
	ClassDB::register_class<Curve>();

	ClassDB::register_class<TextFile>();

	ClassDB::register_virtual_class<StyleBox>();
	ClassDB::register_class<StyleBoxEmpty>();
	ClassDB::register_class<StyleBoxTexture>();
	ClassDB::register_class<StyleBoxFlat>();
	ClassDB::register_class<StyleBoxLine>();
	ClassDB::register_class<Theme>();

	ClassDB::register_class<PolygonPathFinder>();
	ClassDB::register_class<BitMap>();
	ClassDB::register_class<Gradient>();

	OS::get_singleton()->yield(); //may take time to init

	ClassDB::register_class<AudioStreamPlayer>();
	ClassDB::register_class<AudioStreamPlayer2D>();
	ClassDB::register_virtual_class<VideoStream>();
	ClassDB::register_class<AudioStreamSample>();

	OS::get_singleton()->yield(); //may take time to init

	ClassDB::register_virtual_class<Shape2D>();
	ClassDB::register_class<LineShape2D>();
	ClassDB::register_class<SegmentShape2D>();
	ClassDB::register_class<RayShape2D>();
	ClassDB::register_class<CircleShape2D>();
	ClassDB::register_class<RectangleShape2D>();
	ClassDB::register_class<CapsuleShape2D>();
	ClassDB::register_class<ConvexPolygonShape2D>();
	ClassDB::register_class<ConcavePolygonShape2D>();
	ClassDB::register_class<Curve2D>();
	ClassDB::register_class<Path2D>();
	ClassDB::register_class<PathFollow2D>();

	OS::get_singleton()->yield(); //may take time to init

	ClassDB::register_virtual_class<SceneState>();
	ClassDB::register_class<PackedScene>();

	ClassDB::register_class<SceneTree>();
	ClassDB::register_virtual_class<SceneTreeTimer>(); //sorry, you can't create it

	OS::get_singleton()->yield(); //may take time to init

	for (int i = 0; i < 20; i++) {
		GLOBAL_DEF("layer_names/2d_render/layer_" + itos(i + 1), "");
	}

	for (int i = 0; i < 32; i++) {
		GLOBAL_DEF("layer_names/2d_physics/layer_" + itos(i + 1), "");
	}
}

void initialize_theme() {
	bool default_theme_hidpi = GLOBAL_DEF("gui/theme/use_hidpi", false);
	ProjectSettings::get_singleton()->set_custom_property_info("gui/theme/use_hidpi", PropertyInfo(Variant::BOOL, "gui/theme/use_hidpi", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED));
	String theme_path = GLOBAL_DEF_RST("gui/theme/custom", "");
	ProjectSettings::get_singleton()->set_custom_property_info("gui/theme/custom", PropertyInfo(Variant::STRING, "gui/theme/custom", PROPERTY_HINT_FILE, "*.tres,*.res,*.theme", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED));
	String font_path = GLOBAL_DEF_RST("gui/theme/custom_font", "");
	ProjectSettings::get_singleton()->set_custom_property_info("gui/theme/custom_font", PropertyInfo(Variant::STRING, "gui/theme/custom_font", PROPERTY_HINT_FILE, "*.tres,*.res,*.font", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED));

	Ref<Font> font;
	if (font_path != String()) {
		font = ResourceLoader::load(font_path);
		if (!font.is_valid()) {
			ERR_PRINT("Error loading custom font '" + font_path + "'");
		}
	}

	// Always make the default theme to avoid invalid default font/icon/style in the given theme
	make_default_theme(default_theme_hidpi, font);

	if (theme_path != String()) {
		Ref<Theme> theme = ResourceLoader::load(theme_path);
		if (theme.is_valid()) {
			Theme::set_project_default(theme);
			if (font.is_valid()) {
				Theme::set_default_font(font);
			}
		} else {
			ERR_PRINT("Error loading custom theme '" + theme_path + "'");
		}
	}
}

void unregister_scene_types() {
	clear_default_theme();

	ResourceLoader::remove_resource_format_loader(resource_loader_texture_layered);
	resource_loader_texture_layered.unref();

	ResourceLoader::remove_resource_format_loader(resource_loader_stream_texture);
	resource_loader_stream_texture.unref();

	ResourceSaver::remove_resource_format_saver(resource_saver_text);
	resource_saver_text.unref();

	ResourceLoader::remove_resource_format_loader(resource_loader_text);
	resource_loader_text.unref();

	ResourceSaver::remove_resource_format_saver(resource_saver_shader);
	resource_saver_shader.unref();

	ResourceLoader::remove_resource_format_loader(resource_loader_shader);
	resource_loader_shader.unref();

	ResourceLoader::remove_resource_format_loader(resource_loader_bmfont);
	resource_loader_bmfont.unref();

	CanvasItemMaterial::finish_shaders();
	SceneStringNames::free();
}
