Browse Source

Proper inheritance checking when requesting theem resources

Juan Linietsky 8 years ago
parent
commit
f698e2be4f
3 changed files with 127 additions and 25 deletions
  1. 13 1
      core/object_type_db.cpp
  2. 1 0
      core/object_type_db.h
  3. 113 24
      scene/gui/control.cpp

+ 13 - 1
core/object_type_db.cpp

@@ -258,12 +258,23 @@ void ClassDB::get_inheriters_from_class( const StringName& p_class,List<StringNa
 
 }
 
+StringName ClassDB::get_parent_class_nocheck(const StringName& p_class) {
+
+	OBJTYPE_RLOCK;
+
+	ClassInfo *ti = classes.getptr(p_class);
+	if (!ti)
+		return StringName();
+	return ti->inherits;
+
+}
+
 StringName ClassDB::get_parent_class(const StringName& p_class) {
 
 	OBJTYPE_RLOCK;
 
 	ClassInfo *ti = classes.getptr(p_class);
-	ERR_FAIL_COND_V(!ti,"");
+	ERR_FAIL_COND_V(!ti,StringName());
 	return ti->inherits;
 }
 
@@ -272,6 +283,7 @@ ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) {
 	OBJTYPE_RLOCK;
 
 	ClassInfo *ti = classes.getptr(p_class);
+
 	ERR_FAIL_COND_V(!ti,API_NONE);
 	return ti->api;
 }

+ 1 - 0
core/object_type_db.h

@@ -239,6 +239,7 @@ public:
 
 	static void get_class_list( List<StringName> *p_classes);
 	static void get_inheriters_from_class( const StringName& p_class,List<StringName> *p_classes);
+	static StringName get_parent_class_nocheck(const StringName& p_class);
 	static StringName get_parent_class(const StringName& p_class);
 	static bool class_exists(const StringName &p_class);
 	static bool is_parent_class(const StringName &p_class,const StringName& p_inherits);

+ 113 - 24
scene/gui/control.cpp

@@ -754,8 +754,16 @@ Ref<Texture> Control::get_icon(const StringName& p_name,const StringName& p_type
 
 	while(theme_owner) {
 
-		if (theme_owner->data.theme->has_icon(p_name, type ) )
-			return theme_owner->data.theme->get_icon(p_name, type );
+		StringName class_name = type;
+
+		while(class_name!=StringName()) {
+			if (theme_owner->data.theme->has_icon(p_name, class_name ) ) {
+				return theme_owner->data.theme->get_icon(p_name, class_name );
+			}
+
+			class_name = ClassDB::get_parent_class_nocheck(class_name);
+		}
+
 		Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
 
 		if (parent)
@@ -784,8 +792,16 @@ Ref<Shader> Control::get_shader(const StringName& p_name,const StringName& p_typ
 
 	while(theme_owner) {
 
-		if (theme_owner->data.theme->has_shader(p_name, type))
-			return theme_owner->data.theme->get_shader(p_name, type );
+		StringName class_name = type;
+
+		while(class_name!=StringName()) {
+			if (theme_owner->data.theme->has_shader(p_name, class_name ) ) {
+				return theme_owner->data.theme->get_shader(p_name, class_name );
+			}
+
+			class_name = ClassDB::get_parent_class_nocheck(class_name);
+		}
+
 		Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
 
 		if (parent)
@@ -813,9 +829,16 @@ Ref<StyleBox> Control::get_stylebox(const StringName& p_name,const StringName& p
 
 	while(theme_owner) {
 
-		if (theme_owner->data.theme->has_stylebox(p_name, type ) ) {
-			return theme_owner->data.theme->get_stylebox(p_name, type );
+		StringName class_name = type;
+
+		while(class_name!=StringName()) {
+			if (theme_owner->data.theme->has_stylebox(p_name, class_name ) ) {
+				return theme_owner->data.theme->get_stylebox(p_name, class_name );
+			}
+
+			class_name = ClassDB::get_parent_class_nocheck(class_name);
 		}
+
 		Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
 
 		if (parent)
@@ -842,8 +865,16 @@ Ref<Font> Control::get_font(const StringName& p_name,const StringName& p_type) c
 
 	while(theme_owner) {
 
-		if (theme_owner->data.theme->has_font(p_name, type ) )
-			return theme_owner->data.theme->get_font(p_name, type );
+		StringName class_name = type;
+
+		while(class_name!=StringName()) {
+			if (theme_owner->data.theme->has_font(p_name, class_name ) ) {
+				return theme_owner->data.theme->get_font(p_name, class_name );
+			}
+
+			class_name = ClassDB::get_parent_class_nocheck(class_name);
+		}
+
 		if (theme_owner->data.theme->get_default_theme_font().is_valid())
 			return theme_owner->data.theme->get_default_theme_font();
 		Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
@@ -872,8 +903,16 @@ Color Control::get_color(const StringName& p_name,const StringName& p_type) cons
 
 	while(theme_owner) {
 
-		if (theme_owner->data.theme->has_color(p_name, type ) )
-			return theme_owner->data.theme->get_color(p_name, type );
+		StringName class_name = type;
+
+		while(class_name!=StringName()) {
+			if (theme_owner->data.theme->has_color(p_name, class_name ) ) {
+				return theme_owner->data.theme->get_color(p_name, class_name );
+			}
+
+			class_name = ClassDB::get_parent_class_nocheck(class_name);
+		}
+
 		Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
 
 		if (parent)
@@ -901,8 +940,16 @@ int Control::get_constant(const StringName& p_name,const StringName& p_type) con
 
 	while(theme_owner) {
 
-		if (theme_owner->data.theme->has_constant(p_name, type ) )
-			return theme_owner->data.theme->get_constant(p_name, type );
+		StringName class_name = type;
+
+		while(class_name!=StringName()) {
+			if (theme_owner->data.theme->has_constant(p_name, class_name ) ) {
+				return theme_owner->data.theme->get_constant(p_name, class_name );
+			}
+
+			class_name = ClassDB::get_parent_class_nocheck(class_name);
+		}
+
 		Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
 
 		if (parent)
@@ -985,8 +1032,15 @@ bool Control::has_icon(const StringName& p_name,const StringName& p_type) const
 
 	while(theme_owner) {
 
-		if (theme_owner->data.theme->has_icon(p_name, type ) )
-			return true;
+		StringName class_name = type;
+
+		while(class_name!=StringName()) {
+			if (theme_owner->data.theme->has_icon(p_name, class_name ) ) {
+				return true;
+			}
+			class_name = ClassDB::get_parent_class_nocheck(class_name);
+		}
+
 		Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
 
 		if (parent)
@@ -1014,8 +1068,15 @@ bool Control::has_shader(const StringName &p_name, const StringName &p_type) con
 
 	while(theme_owner) {
 
-		if (theme_owner->data.theme->has_shader(p_name, type))
-			return true;
+		StringName class_name = type;
+
+		while(class_name!=StringName()) {
+			if (theme_owner->data.theme->has_shader(p_name, class_name ) ) {
+				return true;
+			}
+			class_name = ClassDB::get_parent_class_nocheck(class_name);
+		}
+
 		Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
 
 		if (parent)
@@ -1042,8 +1103,15 @@ bool Control::has_stylebox(const StringName& p_name,const StringName& p_type) co
 
 	while(theme_owner) {
 
-		if (theme_owner->data.theme->has_stylebox(p_name, type ) )
-			return true;
+		StringName class_name = type;
+
+		while(class_name!=StringName()) {
+			if (theme_owner->data.theme->has_stylebox(p_name, class_name ) ) {
+				return true;
+			}
+			class_name = ClassDB::get_parent_class_nocheck(class_name);
+		}
+
 		Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
 
 		if (parent)
@@ -1071,8 +1139,15 @@ bool Control::has_font(const StringName& p_name,const StringName& p_type) const
 
 	while(theme_owner) {
 
-		if (theme_owner->data.theme->has_font(p_name, type ) )
-			return true;
+		StringName class_name = type;
+
+		while(class_name!=StringName()) {
+			if (theme_owner->data.theme->has_font(p_name, class_name ) ) {
+				return true;
+			}
+			class_name = ClassDB::get_parent_class_nocheck(class_name);
+		}
+
 		Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
 
 		if (parent)
@@ -1100,8 +1175,15 @@ bool Control::has_color(const StringName& p_name, const StringName& p_type) cons
 
 	while(theme_owner) {
 
-		if (theme_owner->data.theme->has_color(p_name, type ) )
-			return true;
+		StringName class_name = type;
+
+		while(class_name!=StringName()) {
+			if (theme_owner->data.theme->has_color(p_name, class_name ) ) {
+				return true;
+			}
+			class_name = ClassDB::get_parent_class_nocheck(class_name);
+		}
+
 		Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
 
 		if (parent)
@@ -1130,8 +1212,15 @@ bool Control::has_constant(const StringName& p_name,const StringName& p_type) co
 
 	while(theme_owner) {
 
-		if (theme_owner->data.theme->has_constant(p_name, type ) )
-			return true;
+		StringName class_name = type;
+
+		while(class_name!=StringName()) {
+			if (theme_owner->data.theme->has_constant(p_name, class_name ) ) {
+				return true;
+			}
+			class_name = ClassDB::get_parent_class_nocheck(class_name);
+		}
+
 		Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL;
 
 		if (parent)