diff options
author | Juan Linietsky <reduzio@gmail.com> | 2016-06-17 16:00:27 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2016-06-17 16:01:30 -0300 |
commit | 55b83157e70a34a933a2a73f14a0052a832d0287 (patch) | |
tree | f81cff6a24c42a3fae5fbfaa20e5e60efaeecb39 /scene/resources | |
parent | ebbd705b63d44d6f25949f6fda9bacf8d974c8bd (diff) |
Keep track of when fonts change, so theme or controls that use fonts as overrides get properly updated.
closes #4622 , probably closes many other issues too
Diffstat (limited to 'scene/resources')
-rw-r--r-- | scene/resources/theme.cpp | 45 | ||||
-rw-r--r-- | scene/resources/theme.h | 9 |
2 files changed, 54 insertions, 0 deletions
diff --git a/scene/resources/theme.cpp b/scene/resources/theme.cpp index 6725f73ad9..92a6f0c0b9 100644 --- a/scene/resources/theme.cpp +++ b/scene/resources/theme.cpp @@ -34,6 +34,32 @@ Ref<Theme> Theme::default_theme; +void Theme::_emit_theme_changed() { + + emit_changed(); +} + +void Theme::_ref_font( Ref<Font> p_sc) { + + if (!font_refcount.has(p_sc)) { + font_refcount[p_sc]=1; + p_sc->connect("changed",this,"_emit_theme_changed"); + } else { + font_refcount[p_sc]+=1; + } +} + +void Theme::_unref_font(Ref<Font> p_sc) { + + ERR_FAIL_COND(!font_refcount.has(p_sc)); + font_refcount[p_sc]--; + if (font_refcount[p_sc]==0) { + p_sc->disconnect("changed",this,"_emit_theme_changed"); + font_refcount.erase(p_sc); + } +} + + bool Theme::_set(const StringName& p_name, const Variant& p_value) { String sname=p_name; @@ -392,8 +418,18 @@ void Theme::set_font(const StringName& p_name,const StringName& p_type,const Ref // ERR_FAIL_COND(p_font.is_null()); bool new_value=!font_map.has(p_type) || !font_map[p_type].has(p_name); + + if (!new_value) { + if (font_map[p_type][p_name].is_valid()) { + _unref_font(font_map[p_type][p_name]); + } + } font_map[p_type][p_name]=p_font; + if (p_font.is_valid()) { + _ref_font(p_font); + } + if (new_value) { _change_notify(); emit_changed();; @@ -420,6 +456,10 @@ void Theme::clear_font(const StringName& p_name,const StringName& p_type) { ERR_FAIL_COND(!font_map.has(p_type)); ERR_FAIL_COND(!font_map[p_type].has(p_name)); + if (font_map.has(p_type) && font_map[p_type].has(p_name) && font_map[p_type][p_name].is_valid()) { + _unref_font(font_map[p_type][p_name]); + } + font_map[p_type].erase(p_name); _change_notify(); emit_changed();; @@ -645,6 +685,11 @@ void Theme::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_type_list","type"),&Theme::_get_type_list); + ObjectTypeDB::bind_method(_MD("_emit_theme_changed"),&Theme::_emit_theme_changed); + + + + ObjectTypeDB::bind_method("copy_default_theme",&Theme::copy_default_theme); ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"default_font",PROPERTY_HINT_RESOURCE_TYPE,"Font"),_SCS("set_default_font"),_SCS("get_default_font")); diff --git a/scene/resources/theme.h b/scene/resources/theme.h index 9b84d0e8ad..1856bd4979 100644 --- a/scene/resources/theme.h +++ b/scene/resources/theme.h @@ -46,6 +46,15 @@ class Theme : public Resource { static Ref<Theme> default_theme; + + //keep a reference count to font, so each time the font changes, we emit theme changed too + Map< Ref<Font>, int> font_refcount; + + void _ref_font(Ref<Font> p_sc); + void _unref_font( Ref<Font> p_sc); + void _emit_theme_changed(); + + HashMap<StringName,HashMap<StringName,Ref<Texture>,StringNameHasher >, StringNameHasher > icon_map; HashMap<StringName,HashMap<StringName,Ref<StyleBox>,StringNameHasher >,StringNameHasher > style_map; HashMap<StringName,HashMap<StringName,Ref<Font>,StringNameHasher >,StringNameHasher > font_map; |