mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-30 18:14:00 +01:00
Merge pull request #399 from test-bit-sys/fix/pen-pressure-intermittent-spikes
fix(ui): retain pen pressure across frames without touch updates (#287)
This commit is contained in:
2 files changed
+239
-45
No files matched your search
@@ -1709,28 +1709,72 @@ pub(crate) fn brush_cursor(app: &TextureCraftApp, ui: &Ui, rect: Rect, position:
|
||||
/// windowing layer reports it for Windows Ink pens, and in the browser for pens that send touch
|
||||
/// events (tablets and phones); other pens come through as a mouse.
|
||||
/// shortcut: the windowing layer reports no pen tilt, so tilt can't drive the brush angle yet.
|
||||
fn pen_pressure(ui: &Ui) -> f32 {
|
||||
let id = egui::Id::unique("pen_pressure");
|
||||
let touch = ui.input(|i| {
|
||||
i.raw.events.iter().rev().find_map(|e| match e {
|
||||
egui::Event::Touch { phase, force, .. } => Some((*phase, force.filter(|f| f.is_finite()))),
|
||||
pub fn pen_pressure(ui: &Ui) -> f32 {
|
||||
let pressure_id = egui::Id::unique("pen_pressure");
|
||||
let touch = ui.input(|input| {
|
||||
let events = &input.raw.events;
|
||||
events.iter().enumerate().rev().find_map(|(index, event)| match event {
|
||||
egui::Event::Touch { device_id, id: touch_id, phase, force, .. } => {
|
||||
let mut contact_force = force.filter(|pressure| pressure.is_finite());
|
||||
let mut effective_phase = *phase;
|
||||
if contact_force.is_none() && matches!(*phase, egui::TouchPhase::Move | egui::TouchPhase::End) {
|
||||
// Recover pressure only from this contact, including an unforced press-frame move
|
||||
for previous_event in events[..index].iter().rev() {
|
||||
if let egui::Event::Touch { device_id: previous_device_id, id: previous_touch_id, phase: previous_phase, force: previous_force, .. } =
|
||||
previous_event
|
||||
&& previous_device_id == device_id
|
||||
&& previous_touch_id == touch_id
|
||||
{
|
||||
// A completed contact cannot supply pressure to a reused touch ID
|
||||
if matches!(previous_phase, egui::TouchPhase::End | egui::TouchPhase::Cancel) {
|
||||
effective_phase = egui::TouchPhase::Cancel;
|
||||
break;
|
||||
}
|
||||
|
||||
contact_force = previous_force.filter(|pressure| pressure.is_finite());
|
||||
if contact_force.is_none() && *previous_phase == egui::TouchPhase::Start {
|
||||
effective_phase = egui::TouchPhase::Start;
|
||||
break;
|
||||
}
|
||||
|
||||
if contact_force.is_some() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some((effective_phase, contact_force))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
});
|
||||
|
||||
match touch {
|
||||
Some((_, Some(f))) => {
|
||||
let f = f.clamp(0.0, 1.0);
|
||||
ui.data_mut(|d| d.insert_temp(id, f));
|
||||
f
|
||||
Some((_, Some(pressure))) => {
|
||||
let pressure = pressure.clamp(0.0, 1.0);
|
||||
ui.data_mut(|data| *data.get_temp_mut_or(pressure_id, pressure) = pressure);
|
||||
pressure
|
||||
}
|
||||
// A pen lifts with no pressure, so keep its contact pressure for a tap
|
||||
Some((egui::TouchPhase::End, None)) => ui.data(|d| d.get_temp(id)).unwrap_or(1.0),
|
||||
// A finger must not inherit an earlier pen's pressure
|
||||
Some((egui::TouchPhase::Start, None)) => {
|
||||
ui.data_mut(|d| d.remove_temp::<f32>(id));
|
||||
Some((egui::TouchPhase::End, None)) => ui.data(|data| data.get_temp(pressure_id)).unwrap_or(1.0),
|
||||
// A touch without pressure must not inherit an earlier pen's pressure
|
||||
Some((egui::TouchPhase::Start | egui::TouchPhase::Cancel, None)) => {
|
||||
ui.data_mut(|data| data.remove_temp::<f32>(pressure_id));
|
||||
1.0
|
||||
}
|
||||
_ => 1.0,
|
||||
_ => {
|
||||
let (primary_pressed, primary_active) =
|
||||
ui.input(|input| (input.pointer.primary_pressed(), input.pointer.primary_down() || input.pointer.primary_released()));
|
||||
|
||||
// A fresh mouse press must not inherit the previous pen's pressure
|
||||
if primary_active && !primary_pressed {
|
||||
ui.data(|data| data.get_temp::<f32>(pressure_id)).unwrap_or(1.0)
|
||||
} else {
|
||||
ui.data_mut(|data| data.remove_temp::<f32>(pressure_id));
|
||||
1.0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,67 +8,73 @@ use texturecraft_ui_egui::state::ViewLayout;
|
||||
|
||||
/// One frame of input: the windowing layer sends a pen sample as a touch (`pen`: its phase and
|
||||
/// force) plus the pointer events it drives, in the same frame. A mouse sends no touch.
|
||||
fn frame(h: &mut Harness<'_, TextureCraftApp>, pen: Option<(TouchPhase, Option<f32>)>, pos: Pos2, button: Option<bool>) {
|
||||
let events = &mut h.input_mut().events;
|
||||
fn frame(harness: &mut Harness<'_, TextureCraftApp>, pen: Option<(TouchPhase, Option<f32>)>, position: Pos2, button: Option<bool>) {
|
||||
let events = &mut harness.input_mut().events;
|
||||
if let Some((phase, force)) = pen {
|
||||
events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase, pos, force });
|
||||
events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase, pos: position, force });
|
||||
}
|
||||
events.push(Event::PointerMoved(pos));
|
||||
events.push(Event::PointerMoved(position));
|
||||
if let Some(pressed) = button {
|
||||
events.push(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE });
|
||||
events.push(Event::PointerButton { pos: position, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE });
|
||||
}
|
||||
h.step();
|
||||
|
||||
harness.step();
|
||||
}
|
||||
|
||||
/// Paint in the 2D view with `input` and return the summed base color alpha it added.
|
||||
fn paint_2d(input: impl FnOnce(&mut Harness<'_, TextureCraftApp>)) -> f64 {
|
||||
let session = texturecraft_engine::sample_session(256, 64).unwrap();
|
||||
let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1500.0)).build_eframe(|cc| TextureCraftApp::new(cc, session));
|
||||
h.state_mut().ui.layout = ViewLayout::Only2D;
|
||||
h.state_mut().session.tool.brush.min_size = 0.0;
|
||||
h.run_steps(4);
|
||||
let before = painted(&h);
|
||||
input(&mut h);
|
||||
h.run_steps(3);
|
||||
painted(&h) - before
|
||||
let mut harness = Harness::builder().with_size(egui::vec2(1600.0, 1500.0)).build_eframe(|creation_context| TextureCraftApp::new(creation_context, session));
|
||||
harness.state_mut().ui.layout = ViewLayout::Only2D;
|
||||
harness.state_mut().session.tool.brush.min_size = 0.0;
|
||||
harness.run_steps(4);
|
||||
let before = painted(&harness);
|
||||
|
||||
input(&mut harness);
|
||||
harness.run_steps(3);
|
||||
|
||||
painted(&harness) - before
|
||||
}
|
||||
|
||||
const FROM: Pos2 = pos2(600.0, 500.0);
|
||||
const STROKE_START: Pos2 = pos2(600.0, 500.0);
|
||||
|
||||
/// One stroke across the view, with pen `force` on every sample (None: a mouse).
|
||||
fn stroke(force: Option<f32>) -> f64 {
|
||||
let pen = |phase| force.map(|f| (phase, Some(f)));
|
||||
let to = pos2(800.0, 500.0);
|
||||
paint_2d(|h| {
|
||||
frame(h, pen(TouchPhase::Start), FROM, Some(true));
|
||||
for k in 1..=8 {
|
||||
frame(h, pen(TouchPhase::Move), FROM + (to - FROM) * (k as f32 / 8.0), None);
|
||||
let pen = |phase| force.map(|pressure| (phase, Some(pressure)));
|
||||
let stroke_end = pos2(800.0, 500.0);
|
||||
|
||||
paint_2d(|harness| {
|
||||
frame(harness, pen(TouchPhase::Start), STROKE_START, Some(true));
|
||||
for sample_index in 1..=8 {
|
||||
frame(harness, pen(TouchPhase::Move), STROKE_START + (stroke_end - STROKE_START) * (sample_index as f32 / 8.0), None);
|
||||
}
|
||||
frame(h, pen(TouchPhase::End), to, Some(false));
|
||||
frame(harness, pen(TouchPhase::End), stroke_end, Some(false));
|
||||
})
|
||||
}
|
||||
|
||||
/// A tap; a pen lifts reporting no pressure (as Windows Ink does), and the dab paints then.
|
||||
fn tap(force: Option<f32>) -> f64 {
|
||||
paint_2d(|h| {
|
||||
frame(h, force.map(|f| (TouchPhase::Start, Some(f))), FROM, Some(true));
|
||||
frame(h, force.map(|_| (TouchPhase::End, None)), FROM, Some(false));
|
||||
paint_2d(|harness| {
|
||||
frame(harness, force.map(|pressure| (TouchPhase::Start, Some(pressure))), STROKE_START, Some(true));
|
||||
frame(harness, force.map(|_| (TouchPhase::End, None)), STROKE_START, Some(false));
|
||||
})
|
||||
}
|
||||
|
||||
/// The selected layer's summed base color alpha.
|
||||
fn painted(h: &Harness<'_, TextureCraftApp>) -> f64 {
|
||||
let s = &h.state().session;
|
||||
let id = s.selected_layer().expect("a selected layer");
|
||||
let Some(img) = s.doc.pixels.layer(id, Channel::BaseColor) else { return 0.0 };
|
||||
let (w, hgt) = s.doc.project.texture_sets[s.ts()].size();
|
||||
(0..hgt).flat_map(|y| (0..w).map(move |x| (x, y))).map(|(x, y)| img.get(x, y)[3] as f64 / 65535.0).sum()
|
||||
fn painted(harness: &Harness<'_, TextureCraftApp>) -> f64 {
|
||||
let session = &harness.state().session;
|
||||
let layer_id = session.selected_layer().expect("a selected layer");
|
||||
let Some(image) = session.doc.pixels.layer(layer_id, Channel::BaseColor) else { return 0.0 };
|
||||
|
||||
let (width, height) = session.doc.project.texture_sets[session.ts()].size();
|
||||
(0..height).flat_map(|y| (0..width).map(move |x| (x, y))).map(|(x, y)| image.get(x, y)[3] as f64 / 65535.0).sum()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pen_pressure_thins_strokes_in_the_2d_view() {
|
||||
let mouse = stroke(None);
|
||||
let light = stroke(Some(0.2));
|
||||
|
||||
assert!(mouse > 10.0, "the mouse stroke painted: {mouse}");
|
||||
assert!(light > 0.0 && light < mouse * 0.5, "a light pen paints a thinner stroke: {light} vs {mouse}");
|
||||
}
|
||||
@@ -77,6 +83,150 @@ fn pen_pressure_thins_strokes_in_the_2d_view() {
|
||||
fn a_light_pen_tap_paints_a_small_dab() {
|
||||
let mouse = tap(None);
|
||||
let light = tap(Some(0.2));
|
||||
|
||||
assert!(mouse > 1.0, "the mouse click painted: {mouse}");
|
||||
assert!(light > 0.0 && light < mouse * 0.25, "the tap keeps its contact pressure: {light} vs {mouse}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frames_omitting_touch_events_during_stroke_do_not_spike_pressure() {
|
||||
let stroke_end = pos2(800.0, 500.0);
|
||||
let intermittent = paint_2d(|harness| {
|
||||
frame(harness, Some((TouchPhase::Start, Some(0.2))), STROKE_START, Some(true));
|
||||
for sample_index in 1..=8 {
|
||||
let touch = if sample_index % 2 == 0 { Some((TouchPhase::Move, Some(0.2))) } else { None };
|
||||
frame(harness, touch, STROKE_START + (stroke_end - STROKE_START) * (sample_index as f32 / 8.0), None);
|
||||
}
|
||||
frame(harness, Some((TouchPhase::End, Some(0.2))), stroke_end, Some(false));
|
||||
});
|
||||
let mouse = stroke(None);
|
||||
|
||||
assert!(intermittent > 0.0 && intermittent < mouse * 0.5, "intermittent frames without touch keep light pressure: {intermittent} vs {mouse}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mouse_stroke_immediately_after_pen_release_uses_full_pressure() {
|
||||
let stroke_end = pos2(800.0, 500.0);
|
||||
let baseline = stroke(None);
|
||||
let immediate_mouse = paint_2d(|harness| {
|
||||
let tap_position = pos2(600.0, 680.0);
|
||||
frame(harness, Some((TouchPhase::Start, Some(0.2))), tap_position, Some(true));
|
||||
frame(harness, Some((TouchPhase::End, None)), tap_position, Some(false));
|
||||
|
||||
frame(harness, None, STROKE_START, Some(true));
|
||||
for sample_index in 1..=8 {
|
||||
frame(harness, None, STROKE_START + (stroke_end - STROKE_START) * (sample_index as f32 / 8.0), None);
|
||||
}
|
||||
frame(harness, None, stroke_end, Some(false));
|
||||
});
|
||||
|
||||
assert!(immediate_mouse > baseline * 0.9, "mouse stroke after pen release keeps full pressure: {immediate_mouse} vs {baseline}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn secondary_pressed_during_active_primary_pen_stroke_retains_light_pressure() {
|
||||
let context = egui::Context::default();
|
||||
let mut start_input = egui::RawInput::default();
|
||||
start_input.events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase: TouchPhase::Start, pos: pos2(100.0, 100.0), force: Some(0.2) });
|
||||
start_input.events.push(Event::PointerButton { pos: pos2(100.0, 100.0), button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
|
||||
|
||||
context
|
||||
.run_ui(start_input, |ui| {
|
||||
let pressure = texturecraft_ui_egui::viewport::pen_pressure(ui);
|
||||
assert!((pressure - 0.2).abs() < 1e-4, "initial touch records 0.2: {pressure}");
|
||||
})
|
||||
.drop_without_applying_deltas();
|
||||
|
||||
let mut secondary_input = egui::RawInput::default();
|
||||
secondary_input.events.push(Event::PointerButton { pos: pos2(110.0, 100.0), button: PointerButton::Secondary, pressed: true, modifiers: Modifiers::NONE });
|
||||
|
||||
context
|
||||
.run_ui(secondary_input, |ui| {
|
||||
let pressure = texturecraft_ui_egui::viewport::pen_pressure(ui);
|
||||
assert!((pressure - 0.2).abs() < 1e-4, "secondary press preserves contact pressure: {pressure}");
|
||||
})
|
||||
.drop_without_applying_deltas();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn touch_start_and_unforced_end_in_one_frame_keep_contact_pressure() {
|
||||
let context = egui::Context::default();
|
||||
let position = pos2(100.0, 100.0);
|
||||
let mut input = egui::RawInput::default();
|
||||
input.events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase: TouchPhase::Start, pos: position, force: Some(0.2) });
|
||||
input.events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase: TouchPhase::End, pos: position, force: None });
|
||||
|
||||
context
|
||||
.run_ui(input, |ui| {
|
||||
let pressure = texturecraft_ui_egui::viewport::pen_pressure(ui);
|
||||
assert!((pressure - 0.2).abs() < 1e-4, "the tap keeps its contact pressure: {pressure}");
|
||||
})
|
||||
.drop_without_applying_deltas();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unforced_move_on_press_frame_keeps_same_contact_pressure() {
|
||||
let context = egui::Context::default();
|
||||
let position = pos2(100.0, 100.0);
|
||||
let mut press_input = egui::RawInput::default();
|
||||
press_input.events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase: TouchPhase::Start, pos: position, force: Some(0.2) });
|
||||
press_input.events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase: TouchPhase::Move, pos: position, force: None });
|
||||
press_input.events.push(Event::PointerButton { pos: position, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
|
||||
|
||||
context
|
||||
.run_ui(press_input, |ui| {
|
||||
let pressure = texturecraft_ui_egui::viewport::pen_pressure(ui);
|
||||
assert!((pressure - 0.2).abs() < 1e-4, "press frame keeps the same contact's pressure: {pressure}");
|
||||
})
|
||||
.drop_without_applying_deltas();
|
||||
|
||||
context
|
||||
.run_ui(egui::RawInput::default(), |ui| {
|
||||
let pressure = texturecraft_ui_egui::viewport::pen_pressure(ui);
|
||||
assert!((pressure - 0.2).abs() < 1e-4, "touch-free frame keeps the recovered pressure: {pressure}");
|
||||
})
|
||||
.drop_without_applying_deltas();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unforced_move_does_not_recover_a_completed_contact() {
|
||||
let context = egui::Context::default();
|
||||
let position = pos2(100.0, 100.0);
|
||||
let mut input = egui::RawInput::default();
|
||||
input.events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase: TouchPhase::End, pos: position, force: Some(0.2) });
|
||||
input.events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase: TouchPhase::Move, pos: position, force: None });
|
||||
input.events.push(Event::PointerButton { pos: position, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
|
||||
|
||||
context
|
||||
.run_ui(input, |ui| {
|
||||
let pressure = texturecraft_ui_egui::viewport::pen_pressure(ui);
|
||||
assert!((pressure - 1.0).abs() < 1e-4, "a completed contact cannot supply pressure to the later move: {pressure}");
|
||||
})
|
||||
.drop_without_applying_deltas();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unforced_touch_start_and_end_in_one_frame_clear_old_pen_pressure() {
|
||||
let context = egui::Context::default();
|
||||
let position = pos2(100.0, 100.0);
|
||||
let mut pen_input = egui::RawInput::default();
|
||||
pen_input.events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase: TouchPhase::Start, pos: position, force: Some(0.2) });
|
||||
pen_input.events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase: TouchPhase::End, pos: position, force: None });
|
||||
|
||||
context
|
||||
.run_ui(pen_input, |ui| {
|
||||
assert!((texturecraft_ui_egui::viewport::pen_pressure(ui) - 0.2).abs() < 1e-4);
|
||||
})
|
||||
.drop_without_applying_deltas();
|
||||
|
||||
let mut finger_input = egui::RawInput::default();
|
||||
finger_input.events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase: TouchPhase::Start, pos: position, force: None });
|
||||
finger_input.events.push(Event::Touch { device_id: TouchDeviceId(1), id: TouchId(1), phase: TouchPhase::End, pos: position, force: None });
|
||||
|
||||
context
|
||||
.run_ui(finger_input, |ui| {
|
||||
let pressure = texturecraft_ui_egui::viewport::pen_pressure(ui);
|
||||
assert!((pressure - 1.0).abs() < 1e-4, "a pressure-free tap clears the old pen pressure: {pressure}");
|
||||
})
|
||||
.drop_without_applying_deltas();
|
||||
}
|
||||
Reference in new issue
Block a user