mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-20 14:30:43 +01:00
Offer only decoded images in the image menu
The image menu listed every imported resource. One that did not decode has no thumbnail, and effect.add.fill and effect.set refuse it, so clicking it ended in an error for an item the menu had offered. The menu now lists only the images that were decoded, and shows its search field only when there are some. A test puts a resource that is not an image next to a real one and checks that only the real one is offered.
This commit is contained in:
1 parent
6481e274c4
commit
b10ff75d43
2 files changed
+38
-12
No files matched your search
@@ -1068,15 +1068,16 @@ pub(crate) fn texture_set_settings(app: &mut TextureCraftApp, ui: &mut Ui) {
|
||||
});
|
||||
}
|
||||
|
||||
/// Menu listing the image resources with a name filter and thumbnails, then Import...: the image
|
||||
/// picked, if any.
|
||||
/// Menu listing the decoded image resources with a name filter and thumbnails, then Import...: the
|
||||
/// image picked, if any. A resource that did not decode is not offered, as the commands that take
|
||||
/// an image refuse it.
|
||||
fn image_menu(app: &mut TextureCraftApp, ui: &mut Ui) -> Option<String> {
|
||||
ui.label(caption(tr("Images")));
|
||||
let ctx = ui.ctx().clone();
|
||||
// A name filter atop the list, kept across frames while the menu stays open.
|
||||
let search_id = ui.make_persistent_id("image_search");
|
||||
let mut search: String = ui.data(|data| data.get_temp(search_id)).unwrap_or_default();
|
||||
if !app.session.doc.resources.is_empty() {
|
||||
if !app.session.images.is_empty() {
|
||||
ui.add(egui::TextEdit::singleline(&mut search).hint_text(tr("Search")).desired_width(f32::INFINITY));
|
||||
}
|
||||
let query = search.trim().to_lowercase();
|
||||
@@ -1088,7 +1089,7 @@ fn image_menu(app: &mut TextureCraftApp, ui: &mut Ui) -> Option<String> {
|
||||
.doc
|
||||
.resources
|
||||
.keys()
|
||||
.filter(|name| query.is_empty() || name.to_lowercase().contains(&query))
|
||||
.filter(|name| app.session.images.contains_key(name.as_str()) && (query.is_empty() || name.to_lowercase().contains(&query)))
|
||||
.cloned()
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter()
|
||||
|
||||
@@ -11,6 +11,17 @@ fn roughness<'a>(h: &'a Harness<'_, TextureCraftApp>) -> egui_kittest::Node<'a>
|
||||
h.get_all_by_label("Roughness").max_by(|a, b| a.rect().right().total_cmp(&b.rect().right())).unwrap()
|
||||
}
|
||||
|
||||
/// Opens the source menu of the Roughness channel, which sits in the row of the channel's name.
|
||||
fn open_roughness_source_menu(h: &mut Harness<'_, TextureCraftApp>) {
|
||||
// The row is scrolled into view first, and the scroll animates for about twenty frames.
|
||||
roughness(h).scroll_to_me();
|
||||
h.run_steps(30);
|
||||
let row = roughness(h).rect().center().y;
|
||||
let menu = h.get_all_by_label(egui_phosphor::regular::DOTS_THREE).find(|n| (n.rect().center().y - row).abs() < 6.0).unwrap();
|
||||
menu.click();
|
||||
h.run_steps(2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fill_channel_takes_an_image_from_its_source_menu() {
|
||||
let mut session = texturecraft_engine::sample_session(256, 64).unwrap();
|
||||
@@ -24,14 +35,7 @@ fn a_fill_channel_takes_an_image_from_its_source_menu() {
|
||||
h.state_mut().ui.prefs.language = "en".into();
|
||||
h.run_steps(4);
|
||||
|
||||
// The source menu button sits in the row of the channel's name, scrolled into view first.
|
||||
roughness(&h).scroll_to_me();
|
||||
// The scroll animates for about twenty frames.
|
||||
h.run_steps(30);
|
||||
let row = roughness(&h).rect().center().y;
|
||||
let menu = h.get_all_by_label(egui_phosphor::regular::DOTS_THREE).find(|n| (n.rect().center().y - row).abs() < 6.0).unwrap();
|
||||
menu.click();
|
||||
h.run_steps(2);
|
||||
open_roughness_source_menu(&mut h);
|
||||
h.get_by_label(&name).click();
|
||||
h.run_steps(3);
|
||||
|
||||
@@ -39,6 +43,27 @@ fn a_fill_channel_takes_an_image_from_its_source_menu() {
|
||||
assert_eq!(fill.source(Channel::Roughness), Source::image(name));
|
||||
}
|
||||
|
||||
/// A resource that did not decode is not offered, since the commands that take an image refuse it.
|
||||
#[test]
|
||||
fn the_source_menu_offers_only_images_that_were_decoded() {
|
||||
let mut session = texturecraft_engine::sample_session(256, 64).unwrap();
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_pixel(4, 4, image::Rgba([90, 90, 90, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
let name = session.import_resource_bytes("rough.png", png).unwrap();
|
||||
// Bytes that are not an image never reach the decoded images
|
||||
session.doc.resources.insert("broken.png".into(), std::sync::Arc::new(vec![1, 2, 3]));
|
||||
texturecraft_engine::execute(&mut session, "layer.add.fill", &json!({"roughness": 0.6})).unwrap();
|
||||
let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1400.0)).build_eframe(|cc| TextureCraftApp::new(cc, session));
|
||||
// The labels below are English; a first run would speak the system's language.
|
||||
h.state_mut().ui.prefs.language = "en".into();
|
||||
h.run_steps(4);
|
||||
|
||||
open_roughness_source_menu(&mut h);
|
||||
|
||||
assert!(h.query_by_label(&name).is_some(), "a decoded image is offered");
|
||||
assert!(h.query_by_label("broken.png").is_none(), "an image that did not decode is not");
|
||||
}
|
||||
|
||||
/// An image on a fill's Normal channel offers a DirectX toggle that flips its green.
|
||||
#[test]
|
||||
fn a_normal_image_can_be_marked_directx() {
|
||||
|
||||
Reference in new issue
Block a user