Skip to content

Commit

Permalink
CraterCrashGH-463 Restore style configuration via settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed Aug 15, 2024
1 parent e0a753b commit 7dc82df
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 30 deletions.
105 changes: 86 additions & 19 deletions src/editor/graph/nodes/graph_node_comment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
//
#include "graph_node_comment.h"

#include "common/macros.h"
#include "common/scene_utils.h"
#include "editor/graph/graph_edit.h"
#include "editor/plugins/orchestrator_editor_plugin.h"
#include "editor/theme/theme_cache.h"

#include <godot_cpp/classes/input_event_mouse_button.hpp>
#include <godot_cpp/classes/margin_container.hpp>
#include <godot_cpp/classes/style_box_flat.hpp>
#include <godot_cpp/classes/texture_rect.hpp>

OrchestratorGraphNodeComment::OrchestratorGraphNodeComment(OrchestratorGraphEdit* p_graph, const Ref<OScriptNodeComment>& p_node)
: OrchestratorGraphNode(p_graph, p_node)
Expand Down Expand Up @@ -168,41 +171,91 @@ void OrchestratorGraphFrameComment::_node_resized()

void OrchestratorGraphFrameComment::_script_node_changed()
{
set_tint_color(_node->get_background_color());
set_title(_node->get_node_title());

Label* title = Object::cast_to<Label>(get_titlebar_hbox()->get_child(0));
Label* title = Object::cast_to<Label>(get_titlebar_hbox()->get_child(1));
if (title)
{
Ref<Font> label_bold_font = SceneUtils::get_editor_font("main_bold_msdf");

title->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT);
title->add_theme_font_size_override("font_size", 16);
title->add_theme_font_override("font", label_bold_font);

if (_node->is_title_center_aligned())
title->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
}

const int font_size = _node->get_font_size();
_text->add_theme_font_size_override("font_size", font_size != 0 ? font_size : 14);
_text->set_text(_node->get("comments"));
_text->add_theme_color_override("font_color", _node->get_text_color());
set_title(_node->get_node_title());

if (_text)
{
_text->set_text(_node->get("comments"));

const int font_size = _node->get_font_size();
if (_text->get_theme_font_size("font_size") != font_size)
_text->add_theme_font_size_override("font_size", font_size != 0 ? font_size : 14);

if (_text->get_theme_color("font_color") != _node->get_text_color())
_text->add_theme_color_override("font_color", _node->get_text_color());
}

if (_icon)
_icon->set_texture(SceneUtils::get_editor_icon(_node->get_icon()));
}

void OrchestratorGraphFrameComment::_update_theme()
{
Ref<OrchestratorThemeCache> cache = OrchestratorPlugin::get_singleton()->get_theme_cache();
if (cache.is_valid())
{
// Cache these for draw calls
_theme_cache.titlebar = cache->get_theme_stylebox("titlebar", "GraphNode_comment");
_theme_cache.titlebar_selected = cache->get_theme_stylebox("titlebar_selected", "GraphNode_comment");

begin_bulk_theme_override();

Ref<StyleBoxFlat> panel = cache->get_theme_stylebox("panel", "GraphFrame");
if (panel.is_valid())
{
Ref<StyleBoxFlat> new_panel = panel->duplicate(true);
new_panel->set_bg_color(_node->get_background_color());
add_theme_stylebox_override("panel", new_panel);
}

Ref<StyleBoxFlat> panel_selected = cache->get_theme_stylebox("panel_selected", "GraphFrame");
if (panel_selected.is_valid())
{
Ref<StyleBoxFlat> new_panel_selected = panel_selected->duplicate(true);
new_panel_selected->set_bg_color(_node->get_background_color());
add_theme_stylebox_override("panel_selected", new_panel_selected);
}
end_bulk_theme_override();
queue_redraw();
}
}

void OrchestratorGraphFrameComment::_notification(int p_what)
{
if (p_what == NOTIFICATION_READY)
{
// Used to replicate size/position state to underlying node resource
connect("dragged", callable_mp(this, &OrchestratorGraphFrameComment::_node_moved));
connect("resized", callable_mp(this, &OrchestratorGraphFrameComment::_node_resized));
OCONNECT(this, "dragged", callable_mp(this, &OrchestratorGraphFrameComment::_node_moved));
OCONNECT(this, "resized", callable_mp(this, &OrchestratorGraphFrameComment::_node_resized));

_node->connect("changed", callable_mp(this, &OrchestratorGraphFrameComment::_script_node_changed));
// Notified when node attributes change
OCONNECT(_node, "changed", callable_mp(this, &OrchestratorGraphFrameComment::_script_node_changed));

Ref<OrchestratorThemeCache> cache = OrchestratorPlugin::get_singleton()->get_theme_cache();
if (cache.is_valid())
{
begin_bulk_theme_override();
add_theme_stylebox_override("panel", cache->get_theme_stylebox("panel", "GraphFrame"));
add_theme_stylebox_override("panel_selected", cache->get_theme_stylebox("panel_selected", "GraphFrame"));
end_bulk_theme_override();
}
OCONNECT(cache, "theme_changed", callable_mp(this, &OrchestratorGraphFrameComment::_update_theme));

_update_theme();
}
else if (p_what == NOTIFICATION_DRAW)
{
if (is_selected() && _theme_cache.titlebar_selected.is_valid())
draw_style_box(_theme_cache.titlebar_selected, Rect2(0, 0, get_size().x, TITLEBAR_HEIGHT));
else if (_theme_cache.titlebar.is_valid())
draw_style_box(_theme_cache.titlebar, Rect2(0, 0, get_size().x, TITLEBAR_HEIGHT));
}
}

Expand All @@ -215,14 +268,28 @@ OrchestratorGraphFrameComment::OrchestratorGraphFrameComment(OrchestratorGraphEd
, _node(p_node)
{
set_meta("__script_node", p_node);
set_tint_color_enabled(true);
set_tint_color_enabled(false);

MarginContainer* margin = memnew(MarginContainer);
margin->add_theme_constant_override("margin_left", 5);
get_titlebar_hbox()->add_child(margin);
get_titlebar_hbox()->move_child(margin, 0);

_icon = memnew(TextureRect);
_icon->set_custom_minimum_size(Vector2i(16, 16));
_icon->set_h_size_flags(SIZE_SHRINK_BEGIN);
_icon->set_v_size_flags(SIZE_SHRINK_CENTER);
_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
margin->add_child(_icon);

_text = memnew(Label);
_text->set_vertical_alignment(VERTICAL_ALIGNMENT_TOP);
_text->set_v_size_flags(SIZE_SHRINK_BEGIN);

add_child(_text);

get_titlebar_hbox()->set_custom_minimum_size(Vector2(0, TITLEBAR_HEIGHT));
get_titlebar_hbox()->set_v_size_flags(SIZE_SHRINK_CENTER);

_script_node_changed();
}
#endif
12 changes: 12 additions & 0 deletions src/editor/graph/nodes/graph_node_comment.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#if GODOT_VERSION >= 0x040300
#include <godot_cpp/classes/graph_frame.hpp>
#include <godot_cpp/classes/texture_rect.hpp>
#endif

/// A simple OrchestratorGraphNode implementation to render comment nodes.
Expand Down Expand Up @@ -79,10 +80,20 @@ class OrchestratorGraphFrameComment : public GraphFrame
GDCLASS(OrchestratorGraphFrameComment, GraphFrame);
static void _bind_methods();

struct ThemeCache
{
Ref<StyleBox> titlebar;
Ref<StyleBox> titlebar_selected;
};

const int TITLEBAR_HEIGHT{ 30 };

protected:
OrchestratorGraphEdit* _graph{ nullptr };
Ref<OScriptNodeComment> _node;
Label* _text{ nullptr };
TextureRect* _icon{ nullptr };
ThemeCache _theme_cache;

//~ Begin Object Interface
void _notification(int p_what);
Expand All @@ -91,6 +102,7 @@ class OrchestratorGraphFrameComment : public GraphFrame
void _node_moved(Vector2 p_old_pos, Vector2 p_new_pos);
void _node_resized();
void _script_node_changed();
void _update_theme();

/// Default constructor, intentionally protected
OrchestratorGraphFrameComment() = default;
Expand Down
61 changes: 57 additions & 4 deletions src/editor/theme/theme_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ void OrchestratorThemeCache::_settings_changed()
Ref<StyleBoxFlat> new_frame_panel = _get_editor_theme_stylebox("panel", "GraphFrame")->duplicate(true);
if (new_frame_panel.is_valid())
{
new_frame_panel->set_border_color(border);
new_frame_panel->set_border_width_all(bwidth);
new_frame_panel->set_corner_radius_all(4);

add_theme_stylebox("panel", "GraphFrame", new_frame_panel);

Ref<StyleBoxFlat> new_frame_panel_selected = new_frame_panel->duplicate(true);
Expand All @@ -59,11 +63,24 @@ void OrchestratorThemeCache::_settings_changed()
}
else
{
Ref<StyleBoxFlat> panel = get_theme_stylebox("panel", "GraphFrame");
if (panel.is_valid())
{
if (panel->get_border_color() != border)
panel->set_border_color(border);

if (!UtilityFunctions::is_equal_approx(panel->get_border_width(SIDE_LEFT), bwidth))
panel->set_border_width_all(bwidth);
}

Ref<StyleBoxFlat> panel_selected = get_theme_stylebox("panel_selected", "GraphFrame");
if (panel_selected.is_valid())
{
if (panel_selected->get_border_color() != select)
panel_selected->set_border_color(select);

if (!UtilityFunctions::is_equal_approx(panel_selected->get_border_width(SIDE_LEFT), bwidth))
panel_selected->set_border_width_all(bwidth);
}
}
#endif
Expand Down Expand Up @@ -98,10 +115,16 @@ void OrchestratorThemeCache::_settings_changed()
else
{
if (panel->get_border_color() != border)
{
panel->set_border_color(border);
_theme_changed = true;
}

if (panel->get_bg_color() != bkgrnd)
{
panel->set_bg_color(bkgrnd);
_theme_changed = true;
}

if (panel->get_corner_radius(CORNER_BOTTOM_LEFT) != radius)
{
Expand All @@ -114,16 +137,23 @@ void OrchestratorThemeCache::_settings_changed()
{
panel->set_border_width_all(bwidth);
panel->set_border_width(SIDE_TOP, 0);
_theme_changed = true;
}

Ref<StyleBoxFlat> panel_selected = get_theme_stylebox("panel_selected", "GraphNode");
if (panel_selected.is_valid())
{
if (panel_selected->get_bg_color() != bkgrnd)
{
panel_selected->set_bg_color(bkgrnd);
_theme_changed = true;
}

if (panel_selected->get_border_color() != select)
{
panel_selected->set_border_color(select);
_theme_changed = true;
}

if (panel_selected->get_corner_radius(CORNER_BOTTOM_LEFT) != radius)
{
Expand All @@ -136,6 +166,7 @@ void OrchestratorThemeCache::_settings_changed()
{
panel_selected->set_border_width_all(bwidth);
panel_selected->set_border_width(SIDE_TOP, 0);
_theme_changed = true;
}
}
}
Expand Down Expand Up @@ -184,6 +215,7 @@ void OrchestratorThemeCache::_settings_changed()
// Primed, but color changed
titlebar->set_bg_color(color);
titlebar->set_border_color(border);
_theme_changed = true;
}

if (titlebar->get_corner_radius(CORNER_TOP_LEFT) != radius)
Expand All @@ -197,15 +229,23 @@ void OrchestratorThemeCache::_settings_changed()
{
titlebar->set_border_width_all(bwidth);
titlebar->set_border_width(SIDE_BOTTOM, 0);
_theme_changed = true;
}

const Ref<StyleBoxFlat> titlebar_selected = get_theme_stylebox("titlebar_selected", type_name);
if (titlebar_selected.is_valid())
{
if (titlebar_selected->get_bg_color() != color)
{
titlebar_selected->set_bg_color(color);
_theme_changed = true;
}

if (titlebar_selected->get_border_color() != select)
{
titlebar_selected->set_border_color(select);
_theme_changed = true;
}

if (titlebar_selected->get_corner_radius(CORNER_TOP_LEFT) != radius)
{
Expand All @@ -218,10 +258,22 @@ void OrchestratorThemeCache::_settings_changed()
{
titlebar_selected->set_border_width_all(bwidth);
titlebar_selected->set_border_width(SIDE_BOTTOM, 0);
_theme_changed = true;
}
}
}
}

if (_theme_changed)
{
_theme_changed = false;
emit_signal("theme_changed");
}
}

Ref<StyleBox> OrchestratorThemeCache::_get_editor_theme_stylebox(const String& p_name, const String& p_type_name) const
{
return SceneUtils::get_editor_stylebox(p_name, p_type_name);
}

void OrchestratorThemeCache::add_theme_stylebox(const StringName& p_name, const String& p_type_name, const Ref<StyleBox>& p_stylebox)
Expand All @@ -230,6 +282,7 @@ void OrchestratorThemeCache::add_theme_stylebox(const StringName& p_name, const
_stylebox_cache[p_type_name] = HashMap<StringName, Ref<StyleBox>>();

_stylebox_cache[p_type_name][p_name] = p_stylebox;
_theme_changed = true;
}

Ref<StyleBox> OrchestratorThemeCache::get_theme_stylebox(const StringName& p_name, const String& p_type_name) const
Expand All @@ -243,13 +296,13 @@ Ref<StyleBox> OrchestratorThemeCache::get_theme_stylebox(const StringName& p_nam
return nullptr;
}

Ref<StyleBox> OrchestratorThemeCache::_get_editor_theme_stylebox(const String& p_name, const String& p_type_name) const
{
return SceneUtils::get_editor_stylebox(p_name, p_type_name);
}
void OrchestratorThemeCache::_notification(int p_what)
{
if (p_what == NOTIFICATION_POSTINITIALIZE)
callable_mp(this, &OrchestratorThemeCache::_settings_changed).call_deferred();
}

void OrchestratorThemeCache::_bind_methods()
{
ADD_SIGNAL(MethodInfo("theme_changed"));
}
15 changes: 8 additions & 7 deletions src/editor/theme/theme_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@ using namespace godot;
class OrchestratorThemeCache : public RefCounted
{
GDCLASS(OrchestratorThemeCache, RefCounted);
static void _bind_methods() {}
static void _bind_methods();

protected:
bool _theme_changed{ false };
HashMap<StringName, HashMap<StringName, Ref<StyleBox>>> _stylebox_cache;

//~ Begin Signal Handlers
void _settings_changed();
//~ End Signal Handlers

/// Gets the editor theme stylebox
/// @param p_name the item name
/// @param p_type_name the type name
/// @return the editor stylebox theme, should always be valid
Ref<StyleBox> _get_editor_theme_stylebox(const String& p_name, const String& p_type_name) const;

public:
//~ Begn Wrapped Interface
void _notification(int p_what);
Expand All @@ -53,12 +60,6 @@ class OrchestratorThemeCache : public RefCounted
/// @param p_type_name the type name
/// @returns the stylebox from the cache or an invalid stylebox reference if it doesn't exist
Ref<StyleBox> get_theme_stylebox(const StringName& p_name, const String& p_type_name) const;

/// Gets the editor theme stylebox
/// @param p_name the item name
/// @param p_type_name the type name
/// @return the editor stylebox theme, should always be valid
Ref<StyleBox>_get_editor_theme_stylebox(const String& p_name, const String& p_type_name) const;
};

#endif // ORCHESTRATOR_THEME_CACHE_H

0 comments on commit 7dc82df

Please sign in to comment.