Skip to content

Commit

Permalink
Refactor editor icon retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
willnationsdev committed Sep 14, 2018
1 parent 06c8b5a commit 5436abe
Show file tree
Hide file tree
Showing 29 changed files with 192 additions and 252 deletions.
9 changes: 3 additions & 6 deletions editor/animation_bezier_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include "animation_bezier_editor.h"

#include "editor/editor_node.h"

float AnimationBezierTrackEdit::_bezier_h_to_pixel(float p_h) {
float h = p_h;
h = (h - v_scroll) / v_zoom;
Expand Down Expand Up @@ -288,12 +290,7 @@ void AnimationBezierTrackEdit::_notification(int p_what) {
int h = font->get_height();

if (node) {
Ref<Texture> icon;
if (has_icon(node->get_class(), "EditorIcons")) {
icon = get_icon(node->get_class(), "EditorIcons");
} else {
icon = get_icon("Node", "EditorIcons");
}
Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(node, "Node");

h = MAX(h, icon->get_height());

Expand Down
46 changes: 7 additions & 39 deletions editor/create_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void CreateDialog::popup_create(bool p_dont_clear, bool p_replace_mode) {

TreeItem *ti = recent->create_item(root);
ti->set_text(0, l);
ti->set_icon(0, _get_editor_icon(l));
ti->set_icon(0, EditorNode::get_singleton()->get_class_icon(l, base_type));
}
}

Expand Down Expand Up @@ -151,41 +151,6 @@ void CreateDialog::_sbox_input(const Ref<InputEvent> &p_ie) {
}
}

Ref<Texture> CreateDialog::_get_editor_icon(const String &p_type) const {

if (has_icon(p_type, "EditorIcons")) {
return get_icon(p_type, "EditorIcons");
}

if (ScriptServer::is_global_class(p_type)) {
String icon_path = EditorNode::get_editor_data().script_class_get_icon_path(p_type);
RES icon;
if (FileAccess::exists(icon_path)) {
icon = ResourceLoader::load(icon_path);
}
if (!icon.is_valid()) {
icon = get_icon(ScriptServer::get_global_class_base(p_type), "EditorIcons");
}
return icon;
}

const Map<String, Vector<EditorData::CustomType> > &p_map = EditorNode::get_editor_data().get_custom_types();
for (const Map<String, Vector<EditorData::CustomType> >::Element *E = p_map.front(); E; E = E->next()) {
const Vector<EditorData::CustomType> &ct = E->value();
for (int i = 0; i < ct.size(); ++i) {
if (ct[i].name == p_type) {
if (ct[i].icon.is_valid()) {
return ct[i].icon;
} else {
return get_icon("Object", "EditorIcons");
}
}
}
}

return get_icon("Object", "EditorIcons");
}

void CreateDialog::add_type(const String &p_type, HashMap<String, TreeItem *> &p_types, TreeItem *p_root, TreeItem **to_select) {

if (p_types.has(p_type))
Expand Down Expand Up @@ -246,7 +211,10 @@ void CreateDialog::add_type(const String &p_type, HashMap<String, TreeItem *> &p
to_select_type = to_select_type.split(" ")[0];
bool current_item_is_preferred;
if (cpp_type) {
current_item_is_preferred = ClassDB::is_parent_class(p_type, preferred_search_result_type) && !ClassDB::is_parent_class(to_select_type, preferred_search_result_type) && search_box->get_text() != to_select_type;
String cpp_to_select_type = to_select_type;
if (ScriptServer::is_global_class(to_select_type))
cpp_to_select_type = ScriptServer::get_global_class_base(to_select_type);
current_item_is_preferred = ClassDB::is_parent_class(p_type, preferred_search_result_type) && !ClassDB::is_parent_class(cpp_to_select_type, preferred_search_result_type);
} else {
current_item_is_preferred = ed.script_class_is_parent(p_type, preferred_search_result_type) && !ed.script_class_is_parent(to_select_type, preferred_search_result_type) && search_box->get_text() != to_select_type;
}
Expand Down Expand Up @@ -274,7 +242,7 @@ void CreateDialog::add_type(const String &p_type, HashMap<String, TreeItem *> &p
const String &description = EditorHelp::get_doc_data()->class_list[p_type].brief_description;
item->set_tooltip(0, description);

item->set_icon(0, _get_editor_icon(p_type));
item->set_icon(0, EditorNode::get_singleton()->get_class_icon(p_type, base_type));

p_types[p_type] = item;
}
Expand Down Expand Up @@ -578,7 +546,7 @@ void CreateDialog::_update_favorite_list() {
continue;
TreeItem *ti = favorites->create_item(root);
ti->set_text(0, l);
ti->set_icon(0, _get_editor_icon(l));
ti->set_icon(0, EditorNode::get_singleton()->get_class_icon(l, base_type));
}
emit_signal("favorites_updated");
}
Expand Down
31 changes: 5 additions & 26 deletions editor/dependency_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,7 @@ void DependencyEditor::_update_list() {
}
String name = path.get_file();

Ref<Texture> icon;
if (has_icon(type, "EditorIcons")) {
icon = get_icon(type, "EditorIcons");
} else {
icon = get_icon("Object", "EditorIcons");
}
Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(type);
item->set_text(0, name);
item->set_icon(0, icon);
item->set_metadata(0, type);
Expand Down Expand Up @@ -346,13 +341,7 @@ void DependencyEditorOwners::_fill_owners(EditorFileSystemDirectory *efsd) {
if (!found)
continue;

Ref<Texture> icon;
String type = efsd->get_file_type(i);
if (!has_icon(type, "EditorIcons")) {
icon = get_icon("Object", "EditorIcons");
} else {
icon = get_icon(type, "EditorIcons");
}
Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(efsd->get_file_type(i));

owners->add_item(efsd->get_file_path(i), icon);
}
Expand Down Expand Up @@ -460,7 +449,7 @@ void DependencyRemoveDialog::_build_removed_dependency_tree(const Vector<Removed
}

//List this file under this dependency
Ref<Texture> icon = has_icon(rd.file_type, "EditorIcons") ? get_icon(rd.file_type, "EditorIcons") : get_icon("Object", "EditorIcons");
Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(rd.file_type);
TreeItem *file_item = owners->create_item(tree_items[rd.dependency]);
file_item->set_text(0, rd.file);
file_item->set_icon(0, icon);
Expand Down Expand Up @@ -579,12 +568,7 @@ void DependencyErrorDialog::show(const String &p_for_file, const Vector<String>
if (report[i].get_slice_count("::") > 0)
type = report[i].get_slice("::", 1);

Ref<Texture> icon;
if (!has_icon(type, "EditorIcons")) {
icon = get_icon("Object", "EditorIcons");
} else {
icon = get_icon(type, "EditorIcons");
}
Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(type);

TreeItem *ti = files->create_item(root);
ti->set_text(0, dep);
Expand Down Expand Up @@ -687,12 +671,7 @@ bool OrphanResourcesDialog::_fill_owners(EditorFileSystemDirectory *efsd, HashMa

String type = efsd->get_file_type(i);

Ref<Texture> icon;
if (has_icon(type, "EditorIcons")) {
icon = get_icon(type, "EditorIcons");
} else {
icon = get_icon("Object", "EditorIcons");
}
Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(type);
ti->set_icon(0, icon);
int ds = efsd->get_file_deps(i).size();
ti->set_text(1, itos(ds));
Expand Down
5 changes: 1 addition & 4 deletions editor/editor_audio_buses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,10 +765,7 @@ EditorAudioBus::EditorAudioBus(EditorAudioBuses *p_buses, bool p_is_master) {
if (!ClassDB::can_instance(E->get()))
continue;

Ref<Texture> icon;
if (has_icon(E->get(), "EditorIcons")) {
icon = get_icon(E->get(), "EditorIcons");
}
Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(E->get());
String name = E->get().operator String().replace("AudioEffect", "");
effect_options->add_item(name);
effect_options->set_item_metadata(effect_options->get_item_count() - 1, E->get());
Expand Down
45 changes: 36 additions & 9 deletions editor/editor_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ bool EditorData::script_class_is_parent(const String &p_class, const String &p_i
return true;
}

StringName EditorData::script_class_get_base(const String &p_class) {
StringName EditorData::script_class_get_base(const String &p_class) const {

if (!ScriptServer::is_global_class(p_class))
return StringName();
Expand All @@ -895,24 +895,48 @@ Object *EditorData::script_class_instance(const String &p_class) {
RES script = ResourceLoader::load(ScriptServer::get_global_class_path(p_class));
if (script.is_valid())
obj->set_script(script.get_ref_ptr());

RES icon = ResourceLoader::load(script_class_get_icon_path(p_class));
if (icon.is_valid())
obj->set_meta("_editor_icon", icon);

return obj;
}
}
return NULL;
}

void EditorData::script_class_set_icon_path(const String &p_class, const String &p_icon_path) {
_script_class_icon_paths[p_class] = p_icon_path;
}

String EditorData::script_class_get_icon_path(const String &p_class) const {
if (!ScriptServer::is_global_class(p_class))
return String();

String current = p_class;
String ret = _script_class_icon_paths[current];
while (ret.empty()) {
current = script_class_get_base(current);
if (!ScriptServer::is_global_class(current))
return String();
ret = _script_class_icon_paths.has(current) ? _script_class_icon_paths[current] : String();
}

return ret;
}

StringName EditorData::script_class_get_name(const String &p_path) const {
return _script_class_file_to_path.has(p_path) ? _script_class_file_to_path[p_path] : StringName();
}

void EditorData::script_class_set_name(const String &p_path, const StringName &p_class) {
_script_class_file_to_path[p_path] = p_class;
}

void EditorData::script_class_save_icon_paths() {
List<StringName> keys;
_script_class_icon_paths.get_key_list(&keys);

Dictionary d;
for (List<StringName>::Element *E = keys.front(); E; E = E->next()) {
d[E->get()] = _script_class_icon_paths[E->get()];
if (ScriptServer::is_global_class(E->get()))
d[E->get()] = _script_class_icon_paths[E->get()];
}

ProjectSettings::get_singleton()->set("_global_script_class_icons", d);
Expand All @@ -927,8 +951,11 @@ void EditorData::script_class_load_icon_paths() {
d.get_key_list(&keys);

for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
String key = E->get().operator String();
_script_class_icon_paths[key] = d[key];
String name = E->get().operator String();
_script_class_icon_paths[name] = d[name];

String path = ScriptServer::get_global_class_path(name);
script_class_set_name(path, name);
}
}

Expand Down
11 changes: 8 additions & 3 deletions editor/editor_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class EditorData {
bool _find_updated_instances(Node *p_root, Node *p_node, Set<String> &checked_paths);

HashMap<StringName, String> _script_class_icon_paths;
HashMap<String, StringName> _script_class_file_to_path;

public:
EditorPlugin *get_editor(Object *p_object);
Expand Down Expand Up @@ -214,10 +215,14 @@ class EditorData {
void notify_resource_saved(const Ref<Resource> &p_resource);

bool script_class_is_parent(const String &p_class, const String &p_inherits);
StringName script_class_get_base(const String &p_class);
StringName script_class_get_base(const String &p_class) const;
Object *script_class_instance(const String &p_class);
String script_class_get_icon_path(const String &p_class) const { return _script_class_icon_paths.has(p_class) ? _script_class_icon_paths[p_class] : String(); }
void script_class_set_icon_path(const String &p_class, const String &p_icon_path) { _script_class_icon_paths[p_class] = p_icon_path; }

StringName script_class_get_name(const String &p_path) const;
void script_class_set_name(const String &p_path, const StringName &p_class);

String script_class_get_icon_path(const String &p_class) const;
void script_class_set_icon_path(const String &p_class, const String &p_icon_path);
void script_class_clear_icon_paths() { _script_class_icon_paths.clear(); }
void script_class_save_icon_paths();
void script_class_load_icon_paths();
Expand Down
1 change: 1 addition & 0 deletions editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,7 @@ void EditorFileSystem::_scan_script_classes(EditorFileSystemDirectory *p_dir) {
}
ScriptServer::add_global_class(files[i]->script_class_name, files[i]->script_class_extends, lang, p_dir->get_file_path(i));
EditorNode::get_editor_data().script_class_set_icon_path(files[i]->script_class_name, files[i]->script_class_icon_path);
EditorNode::get_editor_data().script_class_set_name(files[i]->file, files[i]->script_class_name);
}
for (int i = 0; i < p_dir->get_subdir_count(); i++) {
_scan_script_classes(p_dir->get_subdir(i));
Expand Down
18 changes: 5 additions & 13 deletions editor/editor_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,16 @@ void EditorHelpSearch::IncrementalSearch::phase1(Map<String, DocData::ClassDoc>:
TreeItem *item = search_options->create_item(root);
item->set_metadata(0, "class_name:" + E->key());
item->set_text(0, E->key() + " (Class)");
if (search->has_icon(E->key(), "EditorIcons"))
item->set_icon(0, search->get_icon(E->key(), "EditorIcons"));
else
item->set_icon(0, def_icon);
Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(E->key(), "Node");
item->set_icon(0, icon);
}
}

void EditorHelpSearch::IncrementalSearch::phase2(Map<String, DocData::ClassDoc>::Element *E) {

DocData::ClassDoc &c = E->get();

Ref<Texture> cicon;
if (search->has_icon(E->key(), "EditorIcons"))
cicon = search->get_icon(E->key(), "EditorIcons");
else
cicon = def_icon;
Ref<Texture> cicon = EditorNode::get_singleton()->get_class_icon(E->key(), "Node");

for (int i = 0; i < c.methods.size(); i++) {
if ((term.begins_with(".") && c.methods[i].name.begins_with(term.right(1))) || (term.ends_with("(") && c.methods[i].name.ends_with(term.left(term.length() - 1).strip_edges())) || (term.begins_with(".") && term.ends_with("(") && c.methods[i].name == term.substr(1, term.length() - 2).strip_edges()) || c.methods[i].name.findn(term) != -1) {
Expand Down Expand Up @@ -343,10 +337,8 @@ void EditorHelpIndex::add_type(const String &p_type, HashMap<String, TreeItem *>
item->set_tooltip(0, EditorHelp::get_doc_data()->class_list[p_type].brief_description);
item->set_text(0, p_type);

if (has_icon(p_type, "EditorIcons")) {

item->set_icon(0, get_icon(p_type, "EditorIcons"));
}
Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(p_type);
item->set_icon(0, icon);

p_types[p_type] = item;
}
Expand Down
5 changes: 1 addition & 4 deletions editor/editor_inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,10 +1423,7 @@ void EditorInspector::update_tree() {
category_vbox = NULL; //reset

String type = p.name;
if (has_icon(type, "EditorIcons"))
category->icon = get_icon(type, "EditorIcons");
else
category->icon = get_icon("Object", "EditorIcons");
category->icon = EditorNode::get_singleton()->get_class_icon(type, "Object");
category->label = type;

category->bg_color = get_color("prop_category", "Editor");
Expand Down
Loading

0 comments on commit 5436abe

Please sign in to comment.