Merge remote-tracking branch 'origin/main' into meshmaps/uv-tile-images

This commit is contained in:
bflatastic committed 2026-11-02 19:07:20 +07:00
commit a8b6042ddc
2 files changed
+121 -14

No files matched your search

+42 -14
View File
@@ -33,21 +33,49 @@ fn slider_inner(ui: &mut Ui, label: &str, value: &mut f32, range: std::ops::Rang
let (lo, hi) = (*range.start(), *range.end());
let w = ui.available_width();
let mut edit = None;
ui.horizontal(|ui| {
ui.label(RichText::new(label).color(tk.text));
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
let mut text = format!("{:.*}", decimals, value);
let r = ui.add(egui::TextEdit::singleline(&mut text).desired_width(44.0).align(egui::Align2::RIGHT_CENTER).frame(egui::Frame::NONE));
if r.lost_focus()
&& let Ok(v) = text.trim().parse::<f32>()
&& v.is_finite()
{
*value = v.clamp(lo, hi);
edit = Some(false);
}
});
});
let field = ui
.horizontal(|ui| {
ui.label(RichText::new(label).color(tk.text));
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
let id = ui.next_auto_id();
let shown = format!("{:.*}", decimals, value);
// What is typed lives here while the field has focus (`value` is re-read every frame).
let mut text = ui.data(|d| d.get_temp::<String>(id)).unwrap_or_else(|| shown.clone());
let r = ui.add(egui::TextEdit::singleline(&mut text).id(id).desired_width(44.0).align(egui::Align2::RIGHT_CENTER).frame(egui::Frame::NONE));
if r.has_focus() {
ui.data_mut(|d| d.insert_temp(id, text.clone()));
} else {
ui.data_mut(|d| d.remove_temp::<String>(id));
// Escape unfocuses the field before the app's shortcuts may consume the key, so
// look at the raw input.
let escape = ui.input(|i| i.raw.events.iter().any(|e| matches!(e, egui::Event::Key { key: egui::Key::Escape, pressed: true, .. })));
// Leaving the field unchanged (or with Escape) keeps the value as it is.
if r.lost_focus()
&& !escape
&& text != shown
&& let Ok(v) = text.trim().parse::<f32>()
&& v.is_finite()
&& v.clamp(lo, hi) != *value
{
*value = v.clamp(lo, hi);
edit = Some(false);
}
}
id
})
.inner
})
.inner;
let (rect, mut resp) = ui.allocate_exact_size(vec2(w, 14.0), Sense::click_and_drag());
if resp.is_pointer_button_down_on() {
// Using the track drops what was typed in the field (a drag does not take its focus).
ui.data_mut(|d| d.remove_temp::<String>(field));
ui.memory_mut(|m| m.surrender_focus(field));
}
if edit.is_some() {
// A typed value is a change for `slider`'s callers too.
resp.mark_changed();
}
if (resp.dragged() || resp.clicked())
&& let Some(p) = resp.interact_pointer_pos()
{
+79
View File
@@ -110,3 +110,82 @@ fn clicking_the_effect_opacity_applies_it() {
h.run_steps(3);
assert!(opacity(&h, effect) < 0.5, "a click on the track applies: {}", opacity(&h, effect));
}
/// The number field in the row of the slider labelled `label`.
fn number_field<'a>(h: &'a Harness<'_, TextureCraftApp>, label: &str) -> egui_kittest::Node<'a> {
let row = h.get_by_label(label).rect().center().y;
h.get_all(by().role(Role::TextInput)).find(|n| (n.rect().center().y - row).abs() < 4.0).unwrap()
}
/// Focus the opacity field and press `keys`, then type `text`, one key per frame like a person.
fn type_opacity(h: &mut Harness<'_, TextureCraftApp>, keys: &[egui::Key], text: &str) {
number_field(h, "Opacity").focus();
h.run_steps(2);
for k in keys {
h.key_press(*k);
h.run_steps(1);
}
for c in text.chars() {
number_field(h, "Opacity").type_text(&c.to_string());
h.run_steps(1);
}
}
#[test]
fn a_value_typed_in_the_number_field_applies() {
let (mut h, effect) = harness();
let steps = h.state().session.history_labels().len();
type_opacity(&mut h, &[egui::Key::End, egui::Key::Backspace, egui::Key::Backspace, egui::Key::Backspace, egui::Key::Backspace], "0.25");
h.key_press(egui::Key::Enter);
h.run_steps(3);
assert_eq!(opacity(&h, effect), 0.25);
assert_eq!(h.state().session.history_labels().len(), steps + 1);
}
/// Dragging the track while the number field has focus drops what was typed there: leaving the
/// field afterwards does not undo the drag.
#[test]
fn a_track_drag_drops_text_typed_in_the_number_field() {
let (mut h, effect) = harness();
type_opacity(&mut h, &[egui::Key::End, egui::Key::Backspace], "5");
let t = track(&h, "Opacity");
h.drag_at(egui::pos2(t.right(), t.center().y));
h.run_steps(2);
h.hover_at(egui::pos2(t.left() + 20.0, t.center().y));
h.run_steps(2);
h.drop_at(egui::pos2(t.left() + 20.0, t.center().y));
h.run_steps(2);
let dragged = opacity(&h, effect);
assert!(dragged < 0.5, "the drag applies: {dragged}");
// Leave whatever has focus: a click on empty space.
h.drag_at(egui::pos2(5.0, 1390.0));
h.drop_at(egui::pos2(5.0, 1390.0));
h.key_press(egui::Key::Enter);
h.run_steps(3);
assert_eq!(opacity(&h, effect), dragged, "the text typed before the drag stays dropped");
}
/// Leaving the number field without typing, or with Escape, keeps the value (not the rounded
/// text the field shows) and makes no undo step.
#[test]
fn leaving_the_number_field_unchanged_keeps_the_value() {
let (mut h, effect) = harness();
texturecraft_engine::execute(&mut h.state_mut().session, "effect.set", &json!({"effect": effect.0, "opacity": 0.333})).unwrap();
h.run_steps(2);
let steps = h.state().session.history_labels().len();
type_opacity(&mut h, &[], "");
h.key_press(egui::Key::Enter);
h.run_steps(3);
assert_eq!(opacity(&h, effect), 0.333, "left without typing");
type_opacity(&mut h, &[egui::Key::End, egui::Key::Backspace], "9");
assert_eq!(number_field(&h, "Opacity").value().as_deref(), Some("0.39"));
assert!(number_field(&h, "Opacity").is_focused());
h.key_press(egui::Key::Escape);
h.run_steps(3);
assert_eq!(opacity(&h, effect), 0.333);
// Typing the value it already has is no edit either.
type_opacity(&mut h, &[egui::Key::End, egui::Key::Backspace, egui::Key::Backspace, egui::Key::Backspace, egui::Key::Backspace], "0.333");
h.key_press(egui::Key::Enter);
h.run_steps(3);
assert_eq!(h.state().session.history_labels().len(), steps);
}