Editor: Add ability to rename theme types

This commit is contained in:
Danil Alexeev 2025-05-02 18:54:40 +03:00
parent b89c47bb85
commit e6c9a832c1
No known key found for this signature in database
GPG key ID: 5A52F75A8679EC57
5 changed files with 326 additions and 28 deletions

View file

@ -361,6 +361,17 @@ void Theme::remove_icon_type(const StringName &p_theme_type) {
_unfreeze_and_propagate_changes();
}
void Theme::rename_icon_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
if (!icon_map.has(p_old_theme_type) || icon_map.has(p_theme_type)) {
return;
}
icon_map[p_theme_type] = icon_map[p_old_theme_type];
icon_map.erase(p_old_theme_type);
}
void Theme::get_icon_type_list(List<StringName> *p_list) const {
ERR_FAIL_NULL(p_list);
@ -471,6 +482,17 @@ void Theme::remove_stylebox_type(const StringName &p_theme_type) {
_unfreeze_and_propagate_changes();
}
void Theme::rename_stylebox_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
if (!style_map.has(p_old_theme_type) || style_map.has(p_theme_type)) {
return;
}
style_map[p_theme_type] = style_map[p_old_theme_type];
style_map.erase(p_old_theme_type);
}
void Theme::get_stylebox_type_list(List<StringName> *p_list) const {
ERR_FAIL_NULL(p_list);
@ -587,6 +609,17 @@ void Theme::remove_font_type(const StringName &p_theme_type) {
_unfreeze_and_propagate_changes();
}
void Theme::rename_font_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
if (!font_map.has(p_old_theme_type) || font_map.has(p_theme_type)) {
return;
}
font_map[p_theme_type] = font_map[p_old_theme_type];
font_map.erase(p_old_theme_type);
}
void Theme::get_font_type_list(List<StringName> *p_list) const {
ERR_FAIL_NULL(p_list);
@ -679,6 +712,17 @@ void Theme::remove_font_size_type(const StringName &p_theme_type) {
font_size_map.erase(p_theme_type);
}
void Theme::rename_font_size_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
if (!font_size_map.has(p_old_theme_type) || font_size_map.has(p_theme_type)) {
return;
}
font_size_map[p_theme_type] = font_size_map[p_old_theme_type];
font_size_map.erase(p_old_theme_type);
}
void Theme::get_font_size_type_list(List<StringName> *p_list) const {
ERR_FAIL_NULL(p_list);
@ -765,6 +809,17 @@ void Theme::remove_color_type(const StringName &p_theme_type) {
color_map.erase(p_theme_type);
}
void Theme::rename_color_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
if (!color_map.has(p_old_theme_type) || color_map.has(p_theme_type)) {
return;
}
color_map[p_theme_type] = color_map[p_old_theme_type];
color_map.erase(p_old_theme_type);
}
void Theme::get_color_type_list(List<StringName> *p_list) const {
ERR_FAIL_NULL(p_list);
@ -851,6 +906,17 @@ void Theme::remove_constant_type(const StringName &p_theme_type) {
constant_map.erase(p_theme_type);
}
void Theme::rename_constant_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
if (!constant_map.has(p_old_theme_type) || constant_map.has(p_theme_type)) {
return;
}
constant_map[p_theme_type] = constant_map[p_old_theme_type];
constant_map.erase(p_old_theme_type);
}
void Theme::get_constant_type_list(List<StringName> *p_list) const {
ERR_FAIL_NULL(p_list);
@ -1099,6 +1165,31 @@ void Theme::remove_theme_item_type(DataType p_data_type, const StringName &p_the
}
}
void Theme::rename_theme_item_type(DataType p_data_type, const StringName &p_old_theme_type, const StringName &p_theme_type) {
switch (p_data_type) {
case DATA_TYPE_COLOR:
rename_color_type(p_old_theme_type, p_theme_type);
break;
case DATA_TYPE_CONSTANT:
rename_constant_type(p_old_theme_type, p_theme_type);
break;
case DATA_TYPE_FONT:
rename_font_type(p_old_theme_type, p_theme_type);
break;
case DATA_TYPE_FONT_SIZE:
rename_font_size_type(p_old_theme_type, p_theme_type);
break;
case DATA_TYPE_ICON:
rename_icon_type(p_old_theme_type, p_theme_type);
break;
case DATA_TYPE_STYLEBOX:
rename_stylebox_type(p_old_theme_type, p_theme_type);
break;
case DATA_TYPE_MAX:
break; // Can't happen, but silences warning.
}
}
void Theme::get_theme_item_type_list(DataType p_data_type, List<StringName> *p_list) const {
switch (p_data_type) {
case DATA_TYPE_COLOR:
@ -1217,6 +1308,35 @@ void Theme::remove_type(const StringName &p_theme_type) {
_emit_theme_changed(true);
}
void Theme::rename_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
// Gracefully rename the record in every data type map.
for (int i = 0; i < Theme::DATA_TYPE_MAX; i++) {
Theme::DataType dt = (Theme::DataType)i;
rename_theme_item_type(dt, p_old_theme_type, p_theme_type);
}
// If type is a variation, replace that connection.
const StringName base_type = get_type_variation_base(p_old_theme_type);
if (base_type != StringName()) {
clear_type_variation(p_old_theme_type);
if (p_theme_type != StringName() && !ClassDB::class_exists(p_theme_type)) {
set_type_variation(p_theme_type, base_type);
}
}
// If type is a variation base, replace all those connections.
List<StringName> names;
get_type_variation_list(p_old_theme_type, &names);
for (const StringName &E : names) {
clear_type_variation(E);
if (p_theme_type != StringName()) {
set_type_variation(E, p_theme_type);
}
}
_emit_theme_changed(true);
}
void Theme::get_type_list(List<StringName> *p_list) const {
ERR_FAIL_NULL(p_list);
@ -1769,6 +1889,7 @@ void Theme::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_type", "theme_type"), &Theme::add_type);
ClassDB::bind_method(D_METHOD("remove_type", "theme_type"), &Theme::remove_type);
ClassDB::bind_method(D_METHOD("rename_type", "old_theme_type", "theme_type"), &Theme::rename_type);
ClassDB::bind_method(D_METHOD("get_type_list"), &Theme::_get_type_list);
ClassDB::bind_method(D_METHOD("merge_with", "other"), &Theme::merge_with);