| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | #include "mesh_editor_plugin.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | void MeshEditor::_input_event(InputEvent p_event) { | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask&BUTTON_MASK_LEFT) { | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		rot_x-=p_event.mouse_motion.relative_y*0.01; | 
					
						
							|  |  |  | 		rot_y-=p_event.mouse_motion.relative_x*0.01; | 
					
						
							|  |  |  | 		if (rot_x<-Math_PI/2) | 
					
						
							|  |  |  | 			rot_x=-Math_PI/2; | 
					
						
							|  |  |  | 		else if (rot_x>Math_PI/2) { | 
					
						
							|  |  |  | 			rot_x=Math_PI/2; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		_update_rotation(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | void MeshEditor::_notification(int p_what) { | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	if (p_what==NOTIFICATION_FIXED_PROCESS) { | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	if (p_what==NOTIFICATION_READY) { | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		//get_scene()->connect("node_removed",this,"_node_removed");
 | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		if (first_enter) { | 
					
						
							|  |  |  | 			//it's in propertyeditor so.. could be moved around
 | 
					
						
							| 
									
										
										
										
											2015-11-22 20:08:50 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 			light_1_switch->set_normal_texture(get_icon("MaterialPreviewLight1","EditorIcons")); | 
					
						
							|  |  |  | 			light_1_switch->set_pressed_texture(get_icon("MaterialPreviewLight1Off","EditorIcons")); | 
					
						
							|  |  |  | 			light_2_switch->set_normal_texture(get_icon("MaterialPreviewLight2","EditorIcons")); | 
					
						
							|  |  |  | 			light_2_switch->set_pressed_texture(get_icon("MaterialPreviewLight2Off","EditorIcons")); | 
					
						
							|  |  |  | 			first_enter=false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-11-22 20:08:50 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-11-22 20:08:50 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	if (p_what==NOTIFICATION_DRAW) { | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		Ref<Texture> checkerboard = get_icon("Checkerboard","EditorIcons"); | 
					
						
							|  |  |  | 		Size2 size = get_size(); | 
					
						
							| 
									
										
										
										
											2015-11-22 20:08:50 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		draw_texture_rect(checkerboard,Rect2(Point2(),size),true); | 
					
						
							| 
									
										
										
										
											2015-11-22 20:08:50 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | void MeshEditor::_update_rotation() { | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	Transform t; | 
					
						
							|  |  |  | 	t.basis.rotate(Vector3(0, 1, 0), rot_y); | 
					
						
							|  |  |  | 	t.basis.rotate(Vector3(1, 0, 0), rot_x); | 
					
						
							|  |  |  | 	mesh_instance->set_transform(t); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | void MeshEditor::edit(Ref<Mesh> p_mesh) { | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	mesh=p_mesh; | 
					
						
							|  |  |  | 	mesh_instance->set_mesh(mesh); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	if (mesh.is_null()) { | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		hide(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		rot_x=0; | 
					
						
							|  |  |  | 		rot_y=0; | 
					
						
							|  |  |  | 		_update_rotation(); | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		AABB aabb= mesh->get_aabb(); | 
					
						
							|  |  |  | 		Vector3 ofs = aabb.pos + aabb.size*0.5; | 
					
						
							|  |  |  | 		aabb.pos-=ofs; | 
					
						
							|  |  |  | 		float m = MAX(aabb.size.x,aabb.size.y)*0.5; | 
					
						
							|  |  |  | 		if (m!=0) { | 
					
						
							|  |  |  | 			m=1.0/m; | 
					
						
							|  |  |  | 			m*=0.5; | 
					
						
							|  |  |  | 			//print_line("scale: "+rtos(m));
 | 
					
						
							|  |  |  | 			Transform xform; | 
					
						
							|  |  |  | 			xform.basis.scale(Vector3(m,m,m)); | 
					
						
							|  |  |  | 			xform.origin=-xform.basis.xform(ofs); //-ofs*m;
 | 
					
						
							|  |  |  | 			xform.origin.z-=aabb.size.z*2; | 
					
						
							|  |  |  | 			mesh_instance->set_transform(xform); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 20:08:50 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | void MeshEditor::_button_pressed(Node* p_button) { | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	if (p_button==light_1_switch) { | 
					
						
							|  |  |  | 		light1->set_enabled(!light_1_switch->is_pressed()); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	if (p_button==light_2_switch) { | 
					
						
							|  |  |  | 		light2->set_enabled(!light_2_switch->is_pressed()); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | void MeshEditor::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	ObjectTypeDB::bind_method(_MD("_input_event"),&MeshEditor::_input_event); | 
					
						
							|  |  |  | 	ObjectTypeDB::bind_method(_MD("_button_pressed"),&MeshEditor::_button_pressed); | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | MeshEditor::MeshEditor() { | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	viewport = memnew( Viewport ); | 
					
						
							|  |  |  | 	Ref<World> world; | 
					
						
							|  |  |  | 	world.instance(); | 
					
						
							|  |  |  | 	viewport->set_world(world); //use own world
 | 
					
						
							|  |  |  | 	add_child(viewport); | 
					
						
							|  |  |  | 	viewport->set_process_input(false); | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	camera = memnew( Camera ); | 
					
						
							|  |  |  | 	camera->set_transform(Transform(Matrix3(),Vector3(0,0,3))); | 
					
						
							|  |  |  | 	camera->set_perspective(45,0.1,10); | 
					
						
							|  |  |  | 	viewport->add_child(camera); | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	light1 = memnew( DirectionalLight ); | 
					
						
							|  |  |  | 	light1->set_transform(Transform().looking_at(Vector3(-1,-1,-1),Vector3(0,1,0))); | 
					
						
							|  |  |  | 	viewport->add_child(light1); | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	light2 = memnew( DirectionalLight ); | 
					
						
							|  |  |  | 	light2->set_transform(Transform().looking_at(Vector3(0,1,0),Vector3(0,0,1))); | 
					
						
							|  |  |  | 	light2->set_color(Light::COLOR_DIFFUSE,Color(0.7,0.7,0.7)); | 
					
						
							|  |  |  | 	light2->set_color(Light::COLOR_SPECULAR,Color(0.7,0.7,0.7)); | 
					
						
							|  |  |  | 	viewport->add_child(light2); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	mesh_instance = memnew( MeshInstance ); | 
					
						
							|  |  |  | 	viewport->add_child(mesh_instance); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-30 00:28:29 -03:00
										 |  |  | 	set_custom_minimum_size(Size2(1,150)*EDSCALE); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	HBoxContainer *hb = memnew( HBoxContainer ); | 
					
						
							|  |  |  | 	add_child(hb); | 
					
						
							|  |  |  | 	hb->set_area_as_parent_rect(2); | 
					
						
							| 
									
										
										
										
											2015-08-29 17:16:11 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	hb->add_spacer(); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	VBoxContainer *vb_light = memnew( VBoxContainer ); | 
					
						
							|  |  |  | 	hb->add_child(vb_light); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	light_1_switch = memnew( TextureButton ); | 
					
						
							|  |  |  | 	light_1_switch->set_toggle_mode(true); | 
					
						
							|  |  |  | 	vb_light->add_child(light_1_switch); | 
					
						
							|  |  |  | 	light_1_switch->connect("pressed",this,"_button_pressed",varray(light_1_switch)); | 
					
						
							| 
									
										
										
										
											2015-10-18 13:31:44 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	light_2_switch = memnew( TextureButton ); | 
					
						
							|  |  |  | 	light_2_switch->set_toggle_mode(true); | 
					
						
							|  |  |  | 	vb_light->add_child(light_2_switch); | 
					
						
							|  |  |  | 	light_2_switch->connect("pressed",this,"_button_pressed",varray(light_2_switch)); | 
					
						
							| 
									
										
										
										
											2015-10-18 13:31:44 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	first_enter=true; | 
					
						
							| 
									
										
										
										
											2015-10-18 13:31:44 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	rot_x=0; | 
					
						
							|  |  |  | 	rot_y=0; | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-03 10:28:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | void MeshEditorPlugin::edit(Object *p_object) { | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	Mesh * s = p_object->cast_to<Mesh>(); | 
					
						
							|  |  |  | 	if (!s) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mesh_editor->edit(Ref<Mesh>(s)); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | bool MeshEditorPlugin::handles(Object *p_object) const { | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	return p_object->is_type("Mesh"); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | void MeshEditorPlugin::make_visible(bool p_visible) { | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (p_visible) { | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		mesh_editor->show(); | 
					
						
							|  |  |  | //		mesh_editor->set_process(true);
 | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		mesh_editor->hide(); | 
					
						
							|  |  |  | //		mesh_editor->set_process(false);
 | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | MeshEditorPlugin::MeshEditorPlugin(EditorNode *p_node) { | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	editor=p_node; | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	mesh_editor = memnew( MeshEditor ); | 
					
						
							|  |  |  | 	add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM,mesh_editor); | 
					
						
							|  |  |  | 	mesh_editor->hide(); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | MeshEditorPlugin::~MeshEditorPlugin() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } |