mirror of
				https://github.com/godotengine/godot.git
				synced 2025-10-30 21:21:10 +00:00 
			
		
		
		
	Show "transparent background" texture only behind actual texture in TexturePreview class + add borders for readability
				
					
				
			This commit is contained in:
		
							parent
							
								
									bdf625bd54
								
							
						
					
					
						commit
						db4ae2a91c
					
				
					 3 changed files with 57 additions and 7 deletions
				
			
		|  | @ -91,6 +91,7 @@ Camera3DPreview::Camera3DPreview(Camera3D *p_camera) : | |||
| 	TextureRect *display = get_texture_display(); | ||||
| 	display->set_texture(sub_viewport->get_texture()); | ||||
| 	sub_viewport->connect("size_changed", callable_mp((CanvasItem *)display, &CanvasItem::queue_redraw)); | ||||
| 	sub_viewport->get_texture()->connect_changed(callable_mp((TexturePreview *)this, &Camera3DPreview::_update_texture_display_ratio)); | ||||
| 
 | ||||
| 	ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Camera3DPreview::_update_sub_viewport_size)); | ||||
| 	_update_sub_viewport_size(); | ||||
|  |  | |||
|  | @ -32,6 +32,8 @@ | |||
| 
 | ||||
| #include "editor/editor_string_names.h" | ||||
| #include "editor/themes/editor_scale.h" | ||||
| #include "scene/gui/aspect_ratio_container.h" | ||||
| #include "scene/gui/color_rect.h" | ||||
| #include "scene/gui/label.h" | ||||
| #include "scene/gui/texture_rect.h" | ||||
| #include "scene/resources/animated_texture.h" | ||||
|  | @ -61,11 +63,25 @@ void TexturePreview::_notification(int p_what) { | |||
| 				metadata_label->add_theme_font_override(SceneStringName(font), metadata_label_font); | ||||
| 			} | ||||
| 
 | ||||
| 			bg_rect->set_color(get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor))); | ||||
| 			checkerboard->set_texture(get_editor_theme_icon(SNAME("Checkerboard"))); | ||||
| 			cached_outline_color = get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor)); | ||||
| 		} break; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void TexturePreview::_draw_outline() { | ||||
| 	const float outline_width = Math::round(EDSCALE); | ||||
| 	const Rect2 outline_rect = Rect2(Vector2(), texture_display->get_size()).grow(outline_width * 0.5); | ||||
| 	texture_display->draw_rect(outline_rect, cached_outline_color, false, outline_width); | ||||
| } | ||||
| 
 | ||||
| void TexturePreview::_update_texture_display_ratio() { | ||||
| 	if (texture_display->get_texture().is_valid()) { | ||||
| 		centering_container->set_ratio(texture_display->get_texture()->get_size().aspect()); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void TexturePreview::_update_metadata_label_text() { | ||||
| 	const Ref<Texture2D> texture = texture_display->get_texture(); | ||||
| 
 | ||||
|  | @ -124,25 +140,49 @@ void TexturePreview::_update_metadata_label_text() { | |||
| } | ||||
| 
 | ||||
| TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) { | ||||
| 	set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE); | ||||
| 
 | ||||
| 	bg_rect = memnew(ColorRect); | ||||
| 
 | ||||
| 	add_child(bg_rect); | ||||
| 
 | ||||
| 	margin_container = memnew(MarginContainer); | ||||
| 	const float outline_width = Math::round(EDSCALE); | ||||
| 	margin_container->add_theme_constant_override("margin_right", outline_width); | ||||
| 	margin_container->add_theme_constant_override("margin_top", outline_width); | ||||
| 	margin_container->add_theme_constant_override("margin_left", outline_width); | ||||
| 	margin_container->add_theme_constant_override("margin_bottom", outline_width); | ||||
| 	add_child(margin_container); | ||||
| 
 | ||||
| 	centering_container = memnew(AspectRatioContainer); | ||||
| 	margin_container->add_child(centering_container); | ||||
| 
 | ||||
| 	checkerboard = memnew(TextureRect); | ||||
| 	checkerboard->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); | ||||
| 	checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE); | ||||
| 	checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED); | ||||
| 	checkerboard->set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE); | ||||
| 	add_child(checkerboard); | ||||
| 	centering_container->add_child(checkerboard); | ||||
| 
 | ||||
| 	texture_display = memnew(TextureRect); | ||||
| 	texture_display->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS); | ||||
| 	texture_display->set_texture(p_texture); | ||||
| 	texture_display->set_anchors_preset(TextureRect::PRESET_FULL_RECT); | ||||
| 	texture_display->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED); | ||||
| 	texture_display->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); | ||||
| 	add_child(texture_display); | ||||
| 	centering_container->add_child(texture_display); | ||||
| 
 | ||||
| 	texture_display->connect(SceneStringName(draw), callable_mp(this, &TexturePreview::_draw_outline)); | ||||
| 
 | ||||
| 	if (p_texture.is_valid()) { | ||||
| 		_update_texture_display_ratio(); | ||||
| 		p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_texture_display_ratio)); | ||||
| 	} | ||||
| 
 | ||||
| 	if (p_show_metadata) { | ||||
| 		metadata_label = memnew(Label); | ||||
| 
 | ||||
| 		if (p_texture.is_valid()) { | ||||
| 			_update_metadata_label_text(); | ||||
| 			p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_metadata_label_text)); | ||||
| 		} | ||||
| 
 | ||||
| 		// It's okay that these colors are static since the grid color is static too.
 | ||||
| 		metadata_label->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1)); | ||||
|  |  | |||
|  | @ -36,6 +36,8 @@ | |||
| #include "scene/gui/margin_container.h" | ||||
| #include "scene/resources/texture.h" | ||||
| 
 | ||||
| class AspectRatioContainer; | ||||
| class ColorRect; | ||||
| class TextureRect; | ||||
| 
 | ||||
| class TexturePreview : public MarginContainer { | ||||
|  | @ -44,13 +46,20 @@ class TexturePreview : public MarginContainer { | |||
| private: | ||||
| 	TextureRect *texture_display = nullptr; | ||||
| 
 | ||||
| 	MarginContainer *margin_container = nullptr; | ||||
| 	AspectRatioContainer *centering_container = nullptr; | ||||
| 	ColorRect *bg_rect = nullptr; | ||||
| 	TextureRect *checkerboard = nullptr; | ||||
| 	Label *metadata_label = nullptr; | ||||
| 
 | ||||
| 	Color cached_outline_color; | ||||
| 
 | ||||
| 	void _draw_outline(); | ||||
| 	void _update_metadata_label_text(); | ||||
| 
 | ||||
| protected: | ||||
| 	void _notification(int p_what); | ||||
| 	void _update_texture_display_ratio(); | ||||
| 
 | ||||
| public: | ||||
| 	TextureRect *get_texture_display(); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 arkology
						arkology