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-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 #include "mesh_editor_plugin.h"
31 
_input_event(InputEvent p_event)32 void MeshEditor::_input_event(InputEvent p_event) {
33 
34 	if (p_event.type == InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask & BUTTON_MASK_LEFT) {
35 
36 		rot_x -= p_event.mouse_motion.relative_y * 0.01;
37 		rot_y -= p_event.mouse_motion.relative_x * 0.01;
38 		if (rot_x < -Math_PI / 2)
39 			rot_x = -Math_PI / 2;
40 		else if (rot_x > Math_PI / 2) {
41 			rot_x = Math_PI / 2;
42 		}
43 		_update_rotation();
44 	}
45 }
46 
_notification(int p_what)47 void MeshEditor::_notification(int p_what) {
48 
49 	if (p_what == NOTIFICATION_FIXED_PROCESS) {
50 	}
51 
52 	if (p_what == NOTIFICATION_READY) {
53 
54 		//get_scene()->connect("node_removed",this,"_node_removed");
55 
56 		if (first_enter) {
57 			//it's in propertyeditor so.. could be moved around
58 
59 			light_1_switch->set_normal_texture(get_icon("MaterialPreviewLight1", "EditorIcons"));
60 			light_1_switch->set_pressed_texture(get_icon("MaterialPreviewLight1Off", "EditorIcons"));
61 			light_2_switch->set_normal_texture(get_icon("MaterialPreviewLight2", "EditorIcons"));
62 			light_2_switch->set_pressed_texture(get_icon("MaterialPreviewLight2Off", "EditorIcons"));
63 			first_enter = false;
64 		}
65 	}
66 
67 	if (p_what == NOTIFICATION_DRAW) {
68 
69 		Ref<Texture> checkerboard = get_icon("Checkerboard", "EditorIcons");
70 		Size2 size = get_size();
71 
72 		draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
73 	}
74 }
75 
_update_rotation()76 void MeshEditor::_update_rotation() {
77 
78 	Transform t;
79 	t.basis.rotate(Vector3(0, 1, 0), rot_y);
80 	t.basis.rotate(Vector3(1, 0, 0), rot_x);
81 	mesh_instance->set_transform(t);
82 }
83 
edit(Ref<Mesh> p_mesh)84 void MeshEditor::edit(Ref<Mesh> p_mesh) {
85 
86 	mesh = p_mesh;
87 	mesh_instance->set_mesh(mesh);
88 
89 	if (mesh.is_null()) {
90 
91 		hide();
92 	} else {
93 		rot_x = 0;
94 		rot_y = 0;
95 		_update_rotation();
96 
97 		AABB aabb = mesh->get_aabb();
98 		Vector3 ofs = aabb.pos + aabb.size * 0.5;
99 		aabb.pos -= ofs;
100 		float m = MAX(aabb.size.x, aabb.size.y) * 0.5;
101 		if (m != 0) {
102 			m = 1.0 / m;
103 			m *= 0.5;
104 			//print_line("scale: "+rtos(m));
105 			Transform xform;
106 			xform.basis.scale(Vector3(m, m, m));
107 			xform.origin = -xform.basis.xform(ofs); //-ofs*m;
108 			xform.origin.z -= aabb.size.z * 2;
109 			mesh_instance->set_transform(xform);
110 		}
111 	}
112 }
113 
_button_pressed(Node * p_button)114 void MeshEditor::_button_pressed(Node *p_button) {
115 
116 	if (p_button == light_1_switch) {
117 		light1->set_enabled(!light_1_switch->is_pressed());
118 	}
119 
120 	if (p_button == light_2_switch) {
121 		light2->set_enabled(!light_2_switch->is_pressed());
122 	}
123 }
124 
_bind_methods()125 void MeshEditor::_bind_methods() {
126 
127 	ObjectTypeDB::bind_method(_MD("_input_event"), &MeshEditor::_input_event);
128 	ObjectTypeDB::bind_method(_MD("_button_pressed"), &MeshEditor::_button_pressed);
129 }
130 
MeshEditor()131 MeshEditor::MeshEditor() {
132 
133 	viewport = memnew(Viewport);
134 	Ref<World> world;
135 	world.instance();
136 	viewport->set_world(world); //use own world
137 	add_child(viewport);
138 	viewport->set_disable_input(true);
139 
140 	camera = memnew(Camera);
141 	camera->set_transform(Transform(Matrix3(), Vector3(0, 0, 3)));
142 	camera->set_perspective(45, 0.1, 10);
143 	viewport->add_child(camera);
144 
145 	light1 = memnew(DirectionalLight);
146 	light1->set_transform(Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
147 	viewport->add_child(light1);
148 
149 	light2 = memnew(DirectionalLight);
150 	light2->set_transform(Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
151 	light2->set_color(Light::COLOR_DIFFUSE, Color(0.7, 0.7, 0.7));
152 	light2->set_color(Light::COLOR_SPECULAR, Color(0.7, 0.7, 0.7));
153 	viewport->add_child(light2);
154 
155 	mesh_instance = memnew(MeshInstance);
156 	viewport->add_child(mesh_instance);
157 
158 	set_custom_minimum_size(Size2(1, 150) * EDSCALE);
159 
160 	HBoxContainer *hb = memnew(HBoxContainer);
161 	add_child(hb);
162 	hb->set_area_as_parent_rect(2);
163 
164 	hb->add_spacer();
165 
166 	VBoxContainer *vb_light = memnew(VBoxContainer);
167 	hb->add_child(vb_light);
168 
169 	light_1_switch = memnew(TextureButton);
170 	light_1_switch->set_toggle_mode(true);
171 	vb_light->add_child(light_1_switch);
172 	light_1_switch->connect("pressed", this, "_button_pressed", varray(light_1_switch));
173 
174 	light_2_switch = memnew(TextureButton);
175 	light_2_switch->set_toggle_mode(true);
176 	vb_light->add_child(light_2_switch);
177 	light_2_switch->connect("pressed", this, "_button_pressed", varray(light_2_switch));
178 
179 	first_enter = true;
180 
181 	rot_x = 0;
182 	rot_y = 0;
183 }
184 
edit(Object * p_object)185 void MeshEditorPlugin::edit(Object *p_object) {
186 
187 	Mesh *s = p_object->cast_to<Mesh>();
188 	if (!s)
189 		return;
190 
191 	mesh_editor->edit(Ref<Mesh>(s));
192 }
193 
handles(Object * p_object) const194 bool MeshEditorPlugin::handles(Object *p_object) const {
195 
196 	return p_object->is_type("Mesh");
197 }
198 
make_visible(bool p_visible)199 void MeshEditorPlugin::make_visible(bool p_visible) {
200 
201 	if (p_visible) {
202 		mesh_editor->show();
203 		//		mesh_editor->set_process(true);
204 	} else {
205 
206 		mesh_editor->hide();
207 		//		mesh_editor->set_process(false);
208 	}
209 }
210 
MeshEditorPlugin(EditorNode * p_node)211 MeshEditorPlugin::MeshEditorPlugin(EditorNode *p_node) {
212 
213 	editor = p_node;
214 	mesh_editor = memnew(MeshEditor);
215 	add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, mesh_editor);
216 	mesh_editor->hide();
217 }
218 
~MeshEditorPlugin()219 MeshEditorPlugin::~MeshEditorPlugin() {
220 }
221