summaryrefslogtreecommitdiff
path: root/tools/editor/plugins/mesh_editor_plugin.cpp
blob: ecf17fec1981efef994bebaf81d5f13cbaa252c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "mesh_editor_plugin.h"

void MeshEditor::_input_event(InputEvent p_event) {


	if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask&BUTTON_MASK_LEFT) {

		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();
	}
}

void MeshEditor::_notification(int p_what) {

	if (p_what==NOTIFICATION_FIXED_PROCESS) {

	}


	if (p_what==NOTIFICATION_READY) {

		//get_scene()->connect("node_removed",this,"_node_removed");

		if (first_enter) {
			//it's in propertyeditor so.. could be moved around

			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;
		}

	}

	if (p_what==NOTIFICATION_DRAW) {


		Ref<Texture> checkerboard = get_icon("Checkerboard","EditorIcons");
		Size2 size = get_size();

		draw_texture_rect(checkerboard,Rect2(Point2(),size),true);

	}
}

void MeshEditor::_update_rotation() {

	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);

}

void MeshEditor::edit(Ref<Mesh> p_mesh) {

	mesh=p_mesh;
	mesh_instance->set_mesh(mesh);

	if (mesh.is_null()) {

		hide();
	} else {
		rot_x=0;
		rot_y=0;
		_update_rotation();

		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);
		}

	}

}


void MeshEditor::_button_pressed(Node* p_button) {

	if (p_button==light_1_switch) {
		light1->set_enabled(!light_1_switch->is_pressed());
	}

	if (p_button==light_2_switch) {
		light2->set_enabled(!light_2_switch->is_pressed());
	}


}

void MeshEditor::_bind_methods() {

	ObjectTypeDB::bind_method(_MD("_input_event"),&MeshEditor::_input_event);
	ObjectTypeDB::bind_method(_MD("_button_pressed"),&MeshEditor::_button_pressed);

}

MeshEditor::MeshEditor() {

	viewport = memnew( Viewport );
	Ref<World> world;
	world.instance();
	viewport->set_world(world); //use own world
	add_child(viewport);
	viewport->set_process_input(false);

	camera = memnew( Camera );
	camera->set_transform(Transform(Matrix3(),Vector3(0,0,3)));
	camera->set_perspective(45,0.1,10);
	viewport->add_child(camera);

	light1 = memnew( DirectionalLight );
	light1->set_transform(Transform().looking_at(Vector3(-1,-1,-1),Vector3(0,1,0)));
	viewport->add_child(light1);

	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);

	mesh_instance = memnew( MeshInstance );
	viewport->add_child(mesh_instance);



	set_custom_minimum_size(Size2(1,150));

	HBoxContainer *hb = memnew( HBoxContainer );
	add_child(hb);
	hb->set_area_as_parent_rect(2);

	hb->add_spacer();

	VBoxContainer *vb_light = memnew( VBoxContainer );
	hb->add_child(vb_light);

	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));

	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));

	first_enter=true;

	rot_x=0;
	rot_y=0;


}


void MeshEditorPlugin::edit(Object *p_object) {

	Mesh * s = p_object->cast_to<Mesh>();
	if (!s)
		return;

	mesh_editor->edit(Ref<Mesh>(s));
}

bool MeshEditorPlugin::handles(Object *p_object) const {

	return p_object->is_type("Mesh");
}

void MeshEditorPlugin::make_visible(bool p_visible) {

	if (p_visible) {
		mesh_editor->show();
//		mesh_editor->set_process(true);
	} else {

		mesh_editor->hide();
//		mesh_editor->set_process(false);
	}

}

MeshEditorPlugin::MeshEditorPlugin(EditorNode *p_node) {

	editor=p_node;
	mesh_editor = memnew( MeshEditor );
	add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM,mesh_editor);
	mesh_editor->hide();



}


MeshEditorPlugin::~MeshEditorPlugin()
{
}