Fix multiple issues with region editor

Make the filter mode of the texture preview match the node/resource
being edited where applicable, and nearest neighbor with mipmaps as a
fallback.

Make the Edit Region button for Sprite3D only appear when region is
enabled, to match behavior of Sprite2D.

Fix the editor not correctly clearing reference to previously edited
resources, resulting in a visual bug displaying the incorrect texture.
This commit is contained in:
MrBlockers 2022-10-23 11:35:29 -04:00
parent 5b84583b95
commit 92ebbf2c0a
3 changed files with 82 additions and 8 deletions

View File

@ -86,8 +86,8 @@ void TextureRegionEditor::_region_draw() {
mtx.scale_basis(Vector2(draw_zoom, draw_zoom));
RS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(), mtx);
edit_draw->draw_rect(Rect2(Point2(), base_tex->get_size()), Color(0.5, 0.5, 0.5, 0.5), false);
edit_draw->draw_texture(base_tex, Point2());
edit_draw->draw_rect(Rect2(Point2(), preview_tex->get_size()), Color(0.5, 0.5, 0.5, 0.5), false);
edit_draw->draw_texture(preview_tex, Point2());
RS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(), Transform2D());
const Color color = get_theme_color(SNAME("mono_color"), SNAME("Editor"));
@ -905,6 +905,13 @@ void TextureRegionEditor::edit(Object *p_obj) {
if (atlas_tex.is_valid()) {
atlas_tex->disconnect("changed", callable_mp(this, &TextureRegionEditor::_texture_changed));
}
node_sprite_2d = nullptr;
node_sprite_3d = nullptr;
node_ninepatch = nullptr;
obj_styleBox = Ref<StyleBoxTexture>(nullptr);
atlas_tex = Ref<AtlasTexture>(nullptr);
if (p_obj) {
node_sprite_2d = Object::cast_to<Sprite2D>(p_obj);
node_sprite_3d = Object::cast_to<Sprite3D>(p_obj);
@ -926,13 +933,8 @@ void TextureRegionEditor::edit(Object *p_obj) {
p_obj->connect("texture_changed", callable_mp(this, &TextureRegionEditor::_texture_changed));
}
_edit_region();
} else {
node_sprite_2d = nullptr;
node_sprite_3d = nullptr;
node_ninepatch = nullptr;
obj_styleBox = Ref<StyleBoxTexture>(nullptr);
atlas_tex = Ref<AtlasTexture>(nullptr);
}
edit_draw->queue_redraw();
popup_centered_ratio(0.5);
request_center = true;
@ -946,20 +948,80 @@ void TextureRegionEditor::_texture_changed() {
}
void TextureRegionEditor::_edit_region() {
CanvasItem::TextureFilter filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
Ref<Texture2D> texture = nullptr;
if (atlas_tex.is_valid()) {
texture = atlas_tex->get_atlas();
} else if (node_sprite_2d) {
texture = node_sprite_2d->get_texture();
filter = node_sprite_2d->get_texture_filter_in_tree();
} else if (node_sprite_3d) {
texture = node_sprite_3d->get_texture();
StandardMaterial3D::TextureFilter filter_3d = node_sprite_3d->get_texture_filter();
switch (filter_3d) {
case StandardMaterial3D::TEXTURE_FILTER_NEAREST:
filter = CanvasItem::TEXTURE_FILTER_NEAREST;
break;
case StandardMaterial3D::TEXTURE_FILTER_LINEAR:
filter = CanvasItem::TEXTURE_FILTER_LINEAR;
break;
case StandardMaterial3D::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
break;
case StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS:
filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
break;
case StandardMaterial3D::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC:
filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC;
break;
case StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC:
filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC;
break;
default:
// fallback to project default
filter = CanvasItem::TEXTURE_FILTER_PARENT_NODE;
break;
}
} else if (node_ninepatch) {
texture = node_ninepatch->get_texture();
filter = node_ninepatch->get_texture_filter_in_tree();
} else if (obj_styleBox.is_valid()) {
texture = obj_styleBox->get_texture();
}
// occurs when get_texture_filter_in_tree reaches the scene root
if (filter == CanvasItem::TEXTURE_FILTER_PARENT_NODE) {
SubViewport *root = EditorNode::get_singleton()->get_scene_root();
if (root != nullptr) {
Viewport::DefaultCanvasItemTextureFilter filter_default = root->get_default_canvas_item_texture_filter();
// depending on default filter, set filter to match, otherwise fall back on nearest w/ mipmaps
switch (filter_default) {
case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST:
filter = CanvasItem::TEXTURE_FILTER_NEAREST;
break;
case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR:
filter = CanvasItem::TEXTURE_FILTER_LINEAR;
break;
case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS:
filter = CanvasItem::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
break;
case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
default:
filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
break;
}
} else {
filter = CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
}
}
if (texture.is_null()) {
preview_tex->set_diffuse_texture(nullptr);
_zoom_reset();
hscroll->hide();
vscroll->hide();
@ -967,6 +1029,9 @@ void TextureRegionEditor::_edit_region() {
return;
}
preview_tex->set_texture_filter(filter);
preview_tex->set_diffuse_texture(texture);
if (cache_map.has(texture->get_rid())) {
autoslice_cache = cache_map[texture->get_rid()];
autoslice_is_dirty = false;
@ -1002,6 +1067,8 @@ TextureRegionEditor::TextureRegionEditor() {
atlas_tex = Ref<AtlasTexture>(nullptr);
undo_redo = EditorNode::get_singleton()->get_undo_redo();
preview_tex = Ref<CanvasTexture>(memnew(CanvasTexture));
snap_step = Vector2(10, 10);
snap_separation = Vector2(0, 0);
snap_mode = SNAP_NONE;

View File

@ -86,6 +86,8 @@ class TextureRegionEditor : public AcceptDialog {
Ref<StyleBoxTexture> obj_styleBox;
Ref<AtlasTexture> atlas_tex;
Ref<CanvasTexture> preview_tex;
Rect2 rect;
Rect2 rect_prev;
float prev_margin = 0.0f;

View File

@ -680,6 +680,7 @@ void Sprite3D::set_region_enabled(bool p_region) {
region = p_region;
_queue_redraw();
notify_property_list_changed();
}
bool Sprite3D::is_region_enabled() const {
@ -781,6 +782,10 @@ void Sprite3D::_validate_property(PropertyInfo &p_property) const {
if (p_property.name == "frame_coords") {
p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
}
if (!region && (p_property.name == "region_rect")) {
p_property.usage = PROPERTY_USAGE_NO_EDITOR;
}
}
void Sprite3D::_bind_methods() {