1 /*************************************************************************/
2 /*  mesh_editor_plugin.cpp                                               */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 #include "mesh_editor_plugin.h"
32 
33 #include "editor/editor_scale.h"
34 
_gui_input(Ref<InputEvent> p_event)35 void MeshEditor::_gui_input(Ref<InputEvent> p_event) {
36 
37 	Ref<InputEventMouseMotion> mm = p_event;
38 	if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_LEFT) {
39 
40 		rot_x -= mm->get_relative().y * 0.01;
41 		rot_y -= mm->get_relative().x * 0.01;
42 		if (rot_x < -Math_PI / 2)
43 			rot_x = -Math_PI / 2;
44 		else if (rot_x > Math_PI / 2) {
45 			rot_x = Math_PI / 2;
46 		}
47 		_update_rotation();
48 	}
49 }
50 
_notification(int p_what)51 void MeshEditor::_notification(int p_what) {
52 
53 	if (p_what == NOTIFICATION_READY) {
54 
55 		//get_scene()->connect("node_removed",this,"_node_removed");
56 
57 		if (first_enter) {
58 			//it's in propertyeditor so. could be moved around
59 
60 			light_1_switch->set_normal_texture(get_icon("MaterialPreviewLight1", "EditorIcons"));
61 			light_1_switch->set_pressed_texture(get_icon("MaterialPreviewLight1Off", "EditorIcons"));
62 			light_2_switch->set_normal_texture(get_icon("MaterialPreviewLight2", "EditorIcons"));
63 			light_2_switch->set_pressed_texture(get_icon("MaterialPreviewLight2Off", "EditorIcons"));
64 			first_enter = false;
65 		}
66 	}
67 }
68 
_update_rotation()69 void MeshEditor::_update_rotation() {
70 
71 	Transform t;
72 	t.basis.rotate(Vector3(0, 1, 0), -rot_y);
73 	t.basis.rotate(Vector3(1, 0, 0), -rot_x);
74 	rotation->set_transform(t);
75 }
76 
edit(Ref<Mesh> p_mesh)77 void MeshEditor::edit(Ref<Mesh> p_mesh) {
78 
79 	mesh = p_mesh;
80 	mesh_instance->set_mesh(mesh);
81 
82 	rot_x = Math::deg2rad(-15.0);
83 	rot_y = Math::deg2rad(30.0);
84 	_update_rotation();
85 
86 	AABB aabb = mesh->get_aabb();
87 	Vector3 ofs = aabb.position + aabb.size * 0.5;
88 	float m = aabb.get_longest_axis_size();
89 	if (m != 0) {
90 		m = 1.0 / m;
91 		m *= 0.5;
92 		Transform xform;
93 		xform.basis.scale(Vector3(m, m, m));
94 		xform.origin = -xform.basis.xform(ofs); //-ofs*m;
95 		//xform.origin.z -= aabb.get_longest_axis_size() * 2;
96 		mesh_instance->set_transform(xform);
97 	}
98 }
99 
_button_pressed(Node * p_button)100 void MeshEditor::_button_pressed(Node *p_button) {
101 
102 	if (p_button == light_1_switch) {
103 		light1->set_visible(!light_1_switch->is_pressed());
104 	}
105 
106 	if (p_button == light_2_switch) {
107 		light2->set_visible(!light_2_switch->is_pressed());
108 	}
109 }
110 
_bind_methods()111 void MeshEditor::_bind_methods() {
112 
113 	ClassDB::bind_method(D_METHOD("_gui_input"), &MeshEditor::_gui_input);
114 	ClassDB::bind_method(D_METHOD("_button_pressed"), &MeshEditor::_button_pressed);
115 }
116 
MeshEditor()117 MeshEditor::MeshEditor() {
118 
119 	viewport = memnew(Viewport);
120 	Ref<World> world;
121 	world.instance();
122 	viewport->set_world(world); //use own world
123 	add_child(viewport);
124 	viewport->set_disable_input(true);
125 	viewport->set_msaa(Viewport::MSAA_2X);
126 	set_stretch(true);
127 	camera = memnew(Camera);
128 	camera->set_transform(Transform(Basis(), Vector3(0, 0, 1.1)));
129 	camera->set_perspective(45, 0.1, 10);
130 	viewport->add_child(camera);
131 
132 	light1 = memnew(DirectionalLight);
133 	light1->set_transform(Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
134 	viewport->add_child(light1);
135 
136 	light2 = memnew(DirectionalLight);
137 	light2->set_transform(Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
138 	light2->set_color(Color(0.7, 0.7, 0.7));
139 	viewport->add_child(light2);
140 
141 	rotation = memnew(Spatial);
142 	viewport->add_child(rotation);
143 	mesh_instance = memnew(MeshInstance);
144 	rotation->add_child(mesh_instance);
145 
146 	set_custom_minimum_size(Size2(1, 150) * EDSCALE);
147 
148 	HBoxContainer *hb = memnew(HBoxContainer);
149 	add_child(hb);
150 	hb->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 2);
151 
152 	hb->add_spacer();
153 
154 	VBoxContainer *vb_light = memnew(VBoxContainer);
155 	hb->add_child(vb_light);
156 
157 	light_1_switch = memnew(TextureButton);
158 	light_1_switch->set_toggle_mode(true);
159 	vb_light->add_child(light_1_switch);
160 	light_1_switch->connect("pressed", this, "_button_pressed", varray(light_1_switch));
161 
162 	light_2_switch = memnew(TextureButton);
163 	light_2_switch->set_toggle_mode(true);
164 	vb_light->add_child(light_2_switch);
165 	light_2_switch->connect("pressed", this, "_button_pressed", varray(light_2_switch));
166 
167 	first_enter = true;
168 
169 	rot_x = 0;
170 	rot_y = 0;
171 }
172 
173 ///////////////////////
174 
can_handle(Object * p_object)175 bool EditorInspectorPluginMesh::can_handle(Object *p_object) {
176 
177 	return Object::cast_to<Mesh>(p_object) != NULL;
178 }
179 
parse_begin(Object * p_object)180 void EditorInspectorPluginMesh::parse_begin(Object *p_object) {
181 
182 	Mesh *mesh = Object::cast_to<Mesh>(p_object);
183 	if (!mesh) {
184 		return;
185 	}
186 	Ref<Mesh> m(mesh);
187 
188 	MeshEditor *editor = memnew(MeshEditor);
189 	editor->edit(m);
190 	add_custom_control(editor);
191 }
192 
MeshEditorPlugin(EditorNode * p_node)193 MeshEditorPlugin::MeshEditorPlugin(EditorNode *p_node) {
194 
195 	Ref<EditorInspectorPluginMesh> plugin;
196 	plugin.instance();
197 	add_inspector_plugin(plugin);
198 }
199