Skip to content
0.2.0-dev Download ZIP
144 lines ·5.6 KB Raw
1/**************************************************************************/
2/* mesh_library_editor_plugin.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#pragma once
32
33#include "editor/docks/editor_dock.h"
34#include "editor/plugins/editor_plugin.h"
35
36class ConfirmationDialog;
37class EditorFileDialog;
38class EditorInspector;
39class EditorZoomWidget;
40class FilterLineEdit;
41class HSplitContainer;
42class ItemList;
43class MenuButton;
44class MeshInstance3D;
45class MeshLibrary;
46class Timer;
47
48class MeshLibraryEditor : public EditorDock {
49 GDCLASS(MeshLibraryEditor, EditorDock);
50
51 class MeshLibraryItem : public RefCounted {
52 GDCLASS(MeshLibraryItem, RefCounted);
53
54 friend MeshLibraryEditor;
55
56 Ref<MeshLibrary> mesh_library;
57 int mesh_id = -1;
58
59 protected:
60 bool _set(const StringName &p_name, const Variant &p_value);
61 bool _get(const StringName &p_name, Variant &r_ret) const;
62 void _get_property_list(List<PropertyInfo> *p_list) const;
63 };
64
65 enum {
66 MENU_OPTION_ADD_ITEM,
67 MENU_OPTION_REMOVE_ITEM,
68 MENU_OPTION_UPDATE_FROM_SCENE,
69 MENU_OPTION_IMPORT_FROM_SCENE,
70 MENU_OPTION_IMPORT_FROM_SCENE_APPLY_XFORMS
71 };
72
73 const float UPDATE_ITEMS_DELAY_TIMEOUT = 0.1;
74
75 Ref<MeshLibrary> mesh_library;
76 Ref<MeshLibraryItem> mesh_library_item; // Keep a reference to avoid crashes.
77 int selected_item = -1;
78
79 Button *add_item = nullptr;
80 Button *remove_item = nullptr;
81 MenuButton *import_scene = nullptr;
82 FilterLineEdit *search_box = nullptr;
83 EditorZoomWidget *zoom_widget = nullptr;
84 HSplitContainer *item_split = nullptr;
85 ItemList *mesh_items = nullptr;
86 EditorInspector *inspector = nullptr;
87 Label *empty_lib = nullptr;
88 Timer *update_items_delay = nullptr;
89
90 EditorFileDialog *file = nullptr;
91 ConfirmationDialog *cd_update = nullptr;
92
93 bool apply_xforms = false;
94 bool import_update = false;
95
96 void _update_mesh_items(bool p_reselect = true, Ref<MeshLibrary> p_lib_check = Ref<MeshLibrary>());
97 void _update_resource_preview(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, int p_idx);
98
99 void _select_item(int p_id, Ref<MeshLibrary> p_lib_check = Ref<MeshLibrary>());
100 void _select_item_and_button(int p_id, Ref<MeshLibrary> p_lib_check = Ref<MeshLibrary>());
101 void _select_prev_item_and_button(int p_id, Ref<MeshLibrary> p_lib_check = Ref<MeshLibrary>());
102
103 void _mesh_items_cbk(int p_idx);
104 void _mesh_items_input(const Ref<InputEvent> &p_event);
105
106 void _menu_cbk(int p_option);
107 void _menu_update_confirm(bool p_apply_xforms);
108
109 void _import_scene_cbk(const String &p_str);
110 static void _import_scene(Node *p_scene, Ref<MeshLibrary> p_library, bool p_merge, bool p_apply_xforms);
111 static void _import_scene_parse_node(Ref<MeshLibrary> p_library, HashMap<int, MeshInstance3D *> &p_mesh_instances, Node *p_node, bool p_merge, bool p_apply_xforms);
112
113 void _icon_size_changed(float p_value);
114
115private:
116 void _notification(int p_what);
117 static void _bind_methods();
118
119public:
120 void edit(const Ref<MeshLibrary> &p_mesh_library);
121 static Error update_library_file(Node *p_base_scene, Ref<MeshLibrary> ml, bool p_merge = true, bool p_apply_xforms = false);
122
123 MeshLibraryEditor();
124};
125
126class MeshLibraryEditorPlugin : public EditorPlugin {
127 GDCLASS(MeshLibraryEditorPlugin, EditorPlugin);
128
129 static inline MeshLibraryEditorPlugin *singleton = nullptr;
130
131 MeshLibraryEditor *mesh_library_editor = nullptr;
132
133public:
134 _FORCE_INLINE_ static MeshLibraryEditorPlugin *get_singleton() { return singleton; }
135
136 virtual void edit(Object *p_node) override;
137 virtual bool handles(Object *p_node) const override;
138 virtual void make_visible(bool p_visible) override;
139
140 void open_editor();
141
142 MeshLibraryEditorPlugin();
143 ~MeshLibraryEditorPlugin() { singleton = nullptr; }
144};