Cache theme items in TaskTree

This commit is contained in:
Serhii Snitsaruk 2023-08-29 11:07:18 +02:00
parent 4ff1b874f3
commit e794436c69
2 changed files with 29 additions and 6 deletions

View File

@ -38,13 +38,13 @@ void TaskTree::_update_item(TreeItem *p_item) {
ERR_FAIL_COND_MSG(!task.is_valid(), "Invalid task reference in metadata."); ERR_FAIL_COND_MSG(!task.is_valid(), "Invalid task reference in metadata.");
p_item->set_text(0, task->get_task_name()); p_item->set_text(0, task->get_task_name());
if (task->is_class_ptr(BTComment::get_class_ptr_static())) { if (task->is_class_ptr(BTComment::get_class_ptr_static())) {
p_item->set_custom_font(0, (get_theme_font(SNAME("doc_italic"), SNAME("EditorFonts")))); p_item->set_custom_font(0, theme_cache.comment_font);
p_item->set_custom_color(0, get_theme_color(SNAME("disabled_font_color"), SNAME("Editor"))); p_item->set_custom_color(0, theme_cache.comment_color);
} else if (task->get_custom_name().is_empty()) { } else if (task->get_custom_name().is_empty()) {
p_item->set_custom_font(0, nullptr); p_item->set_custom_font(0, theme_cache.normal_name_font);
p_item->clear_custom_color(0); p_item->clear_custom_color(0);
} else { } else {
p_item->set_custom_font(0, (get_theme_font(SNAME("bold"), SNAME("EditorFonts")))); p_item->set_custom_font(0, theme_cache.custom_name_font);
// p_item->set_custom_color(0, get_theme_color(SNAME("warning_color"), SNAME("Editor"))); // p_item->set_custom_color(0, get_theme_color(SNAME("warning_color"), SNAME("Editor")));
} }
String type_arg; String type_arg;
@ -70,7 +70,7 @@ void TaskTree::_update_item(TreeItem *p_item) {
warning_text += warnings[j]; warning_text += warnings[j];
} }
if (!warning_text.is_empty()) { if (!warning_text.is_empty()) {
p_item->add_button(0, get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons")), 0, false, warning_text); p_item->add_button(0, theme_cache.task_warning_icon, 0, false, warning_text);
} }
// TODO: Update probabilities. // TODO: Update probabilities.
@ -241,6 +241,18 @@ void TaskTree::_drop_data_fw(const Point2 &p_point, const Variant &p_data) {
} }
} }
void TaskTree::_update_theme_item_cache() {
Control::_update_theme_item_cache();
theme_cache.comment_font = get_theme_font(SNAME("doc_italic"), SNAME("EditorFonts"));
theme_cache.custom_name_font = get_theme_font(SNAME("bold"), SNAME("EditorFonts"));
// theme_cache.normal_name_font = Ref<Font>(nullptr);
theme_cache.task_warning_icon = get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons"));
theme_cache.comment_color = get_theme_color(SNAME("disabled_font_color"), SNAME("Editor"));
}
void TaskTree::_notification(int p_what) { void TaskTree::_notification(int p_what) {
switch (p_what) { switch (p_what) {
case NOTIFICATION_THEME_CHANGED: { case NOTIFICATION_THEME_CHANGED: {

View File

@ -23,6 +23,16 @@ private:
Ref<BTTask> last_selected; Ref<BTTask> last_selected;
bool editable; bool editable;
struct ThemeCache {
Ref<Font> comment_font;
Ref<Font> custom_name_font;
Ref<Font> normal_name_font;
Ref<Texture2D> task_warning_icon;
Color comment_color;
} theme_cache;
TreeItem *_create_tree(const Ref<BTTask> &p_task, TreeItem *p_parent, int p_idx = -1); TreeItem *_create_tree(const Ref<BTTask> &p_task, TreeItem *p_parent, int p_idx = -1);
void _update_item(TreeItem *p_item); void _update_item(TreeItem *p_item);
void _update_tree(); void _update_tree();
@ -38,9 +48,10 @@ private:
void _drop_data_fw(const Point2 &p_point, const Variant &p_data); void _drop_data_fw(const Point2 &p_point, const Variant &p_data);
protected: protected:
static void _bind_methods(); virtual void _update_theme_item_cache() override;
void _notification(int p_what); void _notification(int p_what);
static void _bind_methods();
public: public:
void load_bt(const Ref<BehaviorTree> &p_behavior_tree); void load_bt(const Ref<BehaviorTree> &p_behavior_tree);