| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  particles_2d_editor_plugin.cpp                                       */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							|  |  |  | /*                    http://www.godotengine.org                         */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2016-01-01 11:50:53 -02:00
										 |  |  | /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | #include "particles_2d_editor_plugin.h"
 | 
					
						
							|  |  |  | #include "canvas_item_editor_plugin.h"
 | 
					
						
							|  |  |  | #include "io/image_loader.h"
 | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | #include "scene/gui/separator.h"
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void Particles2DEditorPlugin::edit(Object *p_object) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (p_object) { | 
					
						
							|  |  |  | 		particles=p_object->cast_to<Particles2D>(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		particles=NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Particles2DEditorPlugin::handles(Object *p_object) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return p_object->is_type("Particles2D"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Particles2DEditorPlugin::make_visible(bool p_visible) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (p_visible) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | 		toolbar->show(); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | 		toolbar->hide(); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Particles2DEditorPlugin::_file_selected(const String& p_file) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	print_line("file: "+p_file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int epc=epoints->get_val(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Image img; | 
					
						
							|  |  |  | 	Error err = ImageLoader::load_image(p_file,&img); | 
					
						
							| 
									
										
										
										
											2016-05-19 00:08:12 +02:00
										 |  |  | 	ERR_EXPLAIN(TTR("Error loading image:")+" "+p_file); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	ERR_FAIL_COND(err!=OK); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	img.convert(Image::FORMAT_GRAYSCALE_ALPHA); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(img.get_format()!=Image::FORMAT_GRAYSCALE_ALPHA); | 
					
						
							|  |  |  | 	Size2i s = Size2(img.get_width(),img.get_height()); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(s.width==0 || s.height==0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	DVector<uint8_t> data = img.get_data(); | 
					
						
							|  |  |  | 	DVector<uint8_t>::Read r = data.read(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Vector<Point2i> valid_positions; | 
					
						
							|  |  |  | 	valid_positions.resize(s.width*s.height); | 
					
						
							|  |  |  | 	int vpc=0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(int i=0;i<s.width*s.height;i++) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		uint8_t a = r[i*2+1]; | 
					
						
							|  |  |  | 		if (a>128) { | 
					
						
							|  |  |  | 			valid_positions[vpc++]=Point2i(i%s.width,i/s.width); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	valid_positions.resize(vpc); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 	ERR_EXPLAIN(TTR("No pixels with transparency > 128 in image..")); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	ERR_FAIL_COND(valid_positions.size()==0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	DVector<Point2> epoints; | 
					
						
							|  |  |  | 	epoints.resize(epc); | 
					
						
							|  |  |  | 	DVector<Point2>::Write w = epoints.write(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Size2 extents = Size2(img.get_width()*0.5,img.get_height()*0.5); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(int i=0;i<epc;i++) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Point2 p = valid_positions[Math::rand()%vpc]; | 
					
						
							|  |  |  | 		p-=s/2; | 
					
						
							|  |  |  | 		w[i]=p/extents; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	w = DVector<Point2>::Write(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 	undo_redo->create_action(TTR("Set Emission Mask")); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	undo_redo->add_do_method(particles,"set_emission_points",epoints); | 
					
						
							|  |  |  | 	undo_redo->add_do_method(particles,"set_emission_half_extents",extents); | 
					
						
							|  |  |  | 	undo_redo->add_undo_method(particles,"set_emission_points",particles->get_emission_points()); | 
					
						
							|  |  |  | 	undo_redo->add_undo_method(particles,"set_emission_half_extents",particles->get_emission_half_extents()); | 
					
						
							|  |  |  | 	undo_redo->commit_action(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Particles2DEditorPlugin::_menu_callback(int p_idx) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch(p_idx) { | 
					
						
							|  |  |  | 		case MENU_LOAD_EMISSION_MASK: { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			file->popup_centered_ratio(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case MENU_CLEAR_EMISSION_MASK: { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 			undo_redo->create_action(TTR("Clear Emission Mask")); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 			undo_redo->add_do_method(particles,"set_emission_points",DVector<Vector2>()); | 
					
						
							|  |  |  | 			undo_redo->add_undo_method(particles,"set_emission_points",particles->get_emission_points()); | 
					
						
							|  |  |  | 			undo_redo->commit_action(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Particles2DEditorPlugin::_notification(int p_what) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (p_what==NOTIFICATION_ENTER_TREE) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		menu->get_popup()->connect("item_pressed",this,"_menu_callback"); | 
					
						
							| 
									
										
										
										
											2015-08-29 17:16:11 -03:00
										 |  |  | 		menu->set_icon(menu->get_popup()->get_icon("Particles2D","EditorIcons")); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 		file->connect("file_selected",this,"_file_selected"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Particles2DEditorPlugin::_bind_methods() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ObjectTypeDB::bind_method(_MD("_menu_callback"),&Particles2DEditorPlugin::_menu_callback); | 
					
						
							|  |  |  | 	ObjectTypeDB::bind_method(_MD("_file_selected"),&Particles2DEditorPlugin::_file_selected); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Particles2DEditorPlugin::Particles2DEditorPlugin(EditorNode *p_node) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	particles=NULL; | 
					
						
							|  |  |  | 	editor=p_node; | 
					
						
							|  |  |  | 	undo_redo=editor->get_undo_redo(); | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	toolbar = memnew( HBoxContainer ); | 
					
						
							|  |  |  | 	add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar); | 
					
						
							|  |  |  | 	toolbar->hide(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	toolbar->add_child( memnew( VSeparator ) ); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	menu = memnew( MenuButton ); | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 	menu->get_popup()->add_item(TTR("Load Emission Mask"),MENU_LOAD_EMISSION_MASK); | 
					
						
							|  |  |  | 	menu->get_popup()->add_item(TTR("Clear Emission Mask"),MENU_CLEAR_EMISSION_MASK); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	menu->set_text("Particles"); | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | 	toolbar->add_child(menu); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | 	file = memnew( EditorFileDialog ); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	List<String> ext; | 
					
						
							|  |  |  | 	ImageLoader::get_recognized_extensions(&ext); | 
					
						
							|  |  |  | 	for(List<String>::Element *E=ext.front();E;E=E->next()) { | 
					
						
							|  |  |  | 		file->add_filter("*."+E->get()+"; "+E->get().to_upper()); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-06-06 09:44:38 -03:00
										 |  |  | 	file->set_mode(EditorFileDialog::MODE_OPEN_FILE); | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | 	toolbar->add_child(file); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	epoints = memnew( SpinBox ); | 
					
						
							|  |  |  | 	epoints->set_min(1); | 
					
						
							|  |  |  | 	epoints->set_max(8192); | 
					
						
							|  |  |  | 	epoints->set_step(1); | 
					
						
							|  |  |  | 	epoints->set_val(512); | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 	file->get_vbox()->add_margin_child(TTR("Generated Point Count:"),epoints); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Particles2DEditorPlugin::~Particles2DEditorPlugin() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |