1 /*************************************************************************/
2 /*  baked_lightmap_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 "baked_lightmap_editor_plugin.h"
32 
_bake()33 void BakedLightmapEditorPlugin::_bake() {
34 
35 	if (lightmap) {
36 		BakedLightmap::BakeError err;
37 		if (get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root() == lightmap) {
38 			err = lightmap->bake(lightmap);
39 		} else {
40 			err = lightmap->bake(lightmap->get_parent());
41 		}
42 
43 		switch (err) {
44 			case BakedLightmap::BAKE_ERROR_NO_SAVE_PATH:
45 				EditorNode::get_singleton()->show_warning(TTR("Can't determine a save path for lightmap images.\nSave your scene (for images to be saved in the same dir), or pick a save path from the BakedLightmap properties."));
46 				break;
47 			case BakedLightmap::BAKE_ERROR_NO_MESHES:
48 				EditorNode::get_singleton()->show_warning(TTR("No meshes to bake. Make sure they contain an UV2 channel and that the 'Bake Light' flag is on."));
49 				break;
50 			case BakedLightmap::BAKE_ERROR_CANT_CREATE_IMAGE:
51 				EditorNode::get_singleton()->show_warning(TTR("Failed creating lightmap images, make sure path is writable."));
52 				break;
53 			default: {
54 			}
55 		}
56 	}
57 }
58 
edit(Object * p_object)59 void BakedLightmapEditorPlugin::edit(Object *p_object) {
60 
61 	BakedLightmap *s = Object::cast_to<BakedLightmap>(p_object);
62 	if (!s)
63 		return;
64 
65 	lightmap = s;
66 }
67 
handles(Object * p_object) const68 bool BakedLightmapEditorPlugin::handles(Object *p_object) const {
69 
70 	return p_object->is_class("BakedLightmap");
71 }
72 
make_visible(bool p_visible)73 void BakedLightmapEditorPlugin::make_visible(bool p_visible) {
74 
75 	if (p_visible) {
76 		bake->show();
77 	} else {
78 
79 		bake->hide();
80 	}
81 }
82 
83 EditorProgress *BakedLightmapEditorPlugin::tmp_progress = NULL;
84 
bake_func_begin(int p_steps)85 void BakedLightmapEditorPlugin::bake_func_begin(int p_steps) {
86 
87 	ERR_FAIL_COND(tmp_progress != NULL);
88 
89 	tmp_progress = memnew(EditorProgress("bake_lightmaps", TTR("Bake Lightmaps"), p_steps, true));
90 }
91 
bake_func_step(int p_step,const String & p_description)92 bool BakedLightmapEditorPlugin::bake_func_step(int p_step, const String &p_description) {
93 
94 	ERR_FAIL_COND_V(tmp_progress == NULL, false);
95 	return tmp_progress->step(p_description, p_step, false);
96 }
97 
bake_func_end()98 void BakedLightmapEditorPlugin::bake_func_end() {
99 	ERR_FAIL_COND(tmp_progress == NULL);
100 	memdelete(tmp_progress);
101 	tmp_progress = NULL;
102 }
103 
_bind_methods()104 void BakedLightmapEditorPlugin::_bind_methods() {
105 
106 	ClassDB::bind_method("_bake", &BakedLightmapEditorPlugin::_bake);
107 }
108 
BakedLightmapEditorPlugin(EditorNode * p_node)109 BakedLightmapEditorPlugin::BakedLightmapEditorPlugin(EditorNode *p_node) {
110 
111 	editor = p_node;
112 	bake = memnew(ToolButton);
113 	bake->set_icon(editor->get_gui_base()->get_icon("Bake", "EditorIcons"));
114 	bake->set_text(TTR("Bake Lightmaps"));
115 	bake->hide();
116 	bake->connect("pressed", this, "_bake");
117 	add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake);
118 	lightmap = NULL;
119 
120 	BakedLightmap::bake_begin_function = bake_func_begin;
121 	BakedLightmap::bake_step_function = bake_func_step;
122 	BakedLightmap::bake_end_function = bake_func_end;
123 }
124 
~BakedLightmapEditorPlugin()125 BakedLightmapEditorPlugin::~BakedLightmapEditorPlugin() {
126 }
127