summaryrefslogtreecommitdiff
path: root/editor/editor_path.cpp
blob: 23d28261d1bce76afa0b820608b212231645d9e6 (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
/*************************************************************************/
/*  editor_path.cpp                                                      */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
/*                                                                       */
/* 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.                */
/*************************************************************************/

#include "editor_path.h"

#include "editor_node.h"
#include "editor_scale.h"

void EditorPath::_add_children_to_popup(Object *p_obj, int p_depth) {

	if (p_depth > 8)
		return;

	List<PropertyInfo> pinfo;
	p_obj->get_property_list(&pinfo);
	for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {

		if (!(E->get().usage & PROPERTY_USAGE_EDITOR))
			continue;
		if (E->get().hint != PROPERTY_HINT_RESOURCE_TYPE)
			continue;

		Variant value = p_obj->get(E->get().name);
		if (value.get_type() != Variant::OBJECT)
			continue;
		Object *obj = value;
		if (!obj)
			continue;

		Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(obj);

		int index = get_popup()->get_item_count();
		get_popup()->add_icon_item(icon, E->get().name.capitalize(), objects.size());
		get_popup()->set_item_h_offset(index, p_depth * 10 * EDSCALE);
		objects.push_back(obj->get_instance_id());

		_add_children_to_popup(obj, p_depth + 1);
	}
}

void EditorPath::_about_to_show() {

	Object *obj = ObjectDB::get_instance(history->get_path_object(history->get_path_size() - 1));
	if (!obj)
		return;

	objects.clear();
	get_popup()->clear();
	get_popup()->set_size(Size2(get_size().width, 1));
	_add_children_to_popup(obj);
}

void EditorPath::update_path() {
	set_text("");
	set_tooltip("");
	set_icon(NULL);

	for (int i = 0; i < history->get_path_size(); i++) {

		Object *obj = ObjectDB::get_instance(history->get_path_object(i));
		if (!obj)
			continue;

		Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(obj);
		if (icon.is_valid())
			set_icon(icon);

		if (i == history->get_path_size() - 1) {
			String name;
			if (Object::cast_to<Resource>(obj)) {

				Resource *r = Object::cast_to<Resource>(obj);
				if (r->get_path().is_resource_file())
					name = r->get_path().get_file();
				else
					name = r->get_name();

				if (name == "")
					name = r->get_class();
			} else if (obj->is_class("ScriptEditorDebuggerInspectedObject"))
				name = obj->call("get_title");
			else if (Object::cast_to<Node>(obj))
				name = Object::cast_to<Node>(obj)->get_name();
			else if (Object::cast_to<Resource>(obj) && Object::cast_to<Resource>(obj)->get_name() != "")
				name = Object::cast_to<Resource>(obj)->get_name();
			else
				name = obj->get_class();

			set_text(" " + name); // An extra space so the text is not too close of the icon.
			set_tooltip(obj->get_class());
		}
	}
}

void EditorPath::_id_pressed(int p_idx) {

	ERR_FAIL_INDEX(p_idx, objects.size());

	Object *obj = ObjectDB::get_instance(objects[p_idx]);
	if (!obj)
		return;

	EditorNode::get_singleton()->push_item(obj);
}

void EditorPath::_bind_methods() {

	ClassDB::bind_method("_about_to_show", &EditorPath::_about_to_show);
	ClassDB::bind_method("_id_pressed", &EditorPath::_id_pressed);
}

EditorPath::EditorPath(EditorHistory *p_history) {

	history = p_history;
	set_text_align(ALIGN_LEFT);
	get_popup()->connect("about_to_show", this, "_about_to_show");
	get_popup()->connect("id_pressed", this, "_id_pressed");
}