mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-30 10:04:02 +01:00
Address review for pull request 134
This commit is contained in:
1 parent
446cf794cf
commit
19e6bb4b65
2 files changed
+46
-8
No files matched your search
@@ -68,8 +68,10 @@ pub fn slider(ui: &mut Ui, label: &str, value: &mut f32, range: std::ops::RangeI
|
||||
let t = ((x - rect.left() - 5.0) / (rect.width() - 10.0).max(1.0)).clamp(0.0, 1.0);
|
||||
f64::from(lo) + (f64::from(hi) - f64::from(lo)) * f64::from(t)
|
||||
};
|
||||
// Use the same round-value aiming as egui's standard sliders, within one physical pixel.
|
||||
let radius = ui.input(|i| i.aim_radius());
|
||||
// Snap to a round value only within half a physical pixel (in points), so
|
||||
// round values are reachable by aiming at them while fractional values
|
||||
// between round steps remain reachable.
|
||||
let radius = 0.5 / ui.ctx().pixels_per_point();
|
||||
let v = egui::emath::smart_aim::best_in_range_f64(value_at(p.x - radius), value_at(p.x + radius)) as f32;
|
||||
if (v - *value).abs() > f32::EPSILON {
|
||||
*value = v;
|
||||
@@ -95,8 +97,10 @@ pub fn bar_slider(ui: &mut Ui, label: &str, value: &mut f32, range: std::ops::Ra
|
||||
let t = ((x - rect.left() - 4.0) / (rect.width() - 8.0).max(1.0)).clamp(0.0, 1.0);
|
||||
f64::from(lo) + (f64::from(hi) - f64::from(lo)) * f64::from(t)
|
||||
};
|
||||
// Use the same round-value aiming as egui's standard sliders, within one physical pixel.
|
||||
let radius = ui.input(|i| i.aim_radius());
|
||||
// Snap to a round value only within half a physical pixel (in points), so
|
||||
// round values are reachable by aiming at them while fractional values
|
||||
// between round steps remain reachable.
|
||||
let radius = 0.5 / ui.ctx().pixels_per_point();
|
||||
let v = egui::emath::smart_aim::best_in_range_f64(value_at(p.x - radius), value_at(p.x + radius)) as f32;
|
||||
if (v - *value).abs() > f32::EPSILON {
|
||||
*value = v;
|
||||
|
||||
@@ -8,6 +8,12 @@ use materialcraft_engine::core::{NodeId, Value};
|
||||
use materialcraft_ui_egui::{MaterialCraftApp, UiState, widgets};
|
||||
use serde_json::json;
|
||||
|
||||
/// Offset from the exact round value, in physical pixels. Must be positive (so the
|
||||
/// unsnapped value differs from the round value by more than `f32::EPSILON`) and
|
||||
/// smaller than the half-physical-pixel snap range in `widgets`, so the round
|
||||
/// value is the one snapped to.
|
||||
const AIM_OFFSET_PHYSICAL_PIXELS: f32 = 0.4;
|
||||
|
||||
fn harness(ppp: f32, render: bool, definition: &str) -> (Harness<'static, MaterialCraftApp>, NodeId) {
|
||||
let mut app = MaterialCraftApp::headless(materialcraft_engine::new_session(), UiState::default());
|
||||
let id = NodeId(app.run("graph.node.add", json!({"def":definition})).unwrap()["id"].as_u64().unwrap());
|
||||
@@ -28,8 +34,8 @@ fn harness(ppp: f32, render: bool, definition: &str) -> (Harness<'static, Materi
|
||||
fn drag_near(h: &mut Harness<'static, MaterialCraftApp>, label: &str, index: usize, value: f32, lo: f32, hi: f32) {
|
||||
let rect = h.get_all(By::new().role(egui::accesskit::Role::Slider).label(label)).nth(index).unwrap().rect();
|
||||
let start = pos2(rect.left() + rect.width() * 0.2, rect.center().y);
|
||||
// Aim slightly off the exact value, within one physical pixel.
|
||||
let target = pos2(rect.left() + 5.0 + (rect.width() - 10.0) * (value - lo) / (hi - lo) + 0.4 / h.ctx.pixels_per_point(), rect.center().y);
|
||||
// Aim slightly off the exact value, within the half-physical-pixel snap range.
|
||||
let target = pos2(rect.left() + 5.0 + (rect.width() - 10.0) * (value - lo) / (hi - lo) + AIM_OFFSET_PHYSICAL_PIXELS / h.ctx.pixels_per_point(), rect.center().y);
|
||||
h.event(Event::PointerMoved(start));
|
||||
h.step();
|
||||
h.event(Event::PointerButton { pos: start, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
|
||||
@@ -87,8 +93,8 @@ fn bar_slider_drag_reaches_exact_round_values() {
|
||||
h.run_steps(3);
|
||||
let rect = h.get(By::new().role(egui::accesskit::Role::Slider).label("Brightness")).rect();
|
||||
let start = pos2(rect.left() + rect.width() * 0.2, rect.center().y);
|
||||
// Aim 0.4 of a physical pixel to the right of 0.5, as the other drags do.
|
||||
let target = pos2(rect.left() + 4.0 + (rect.width() - 8.0) * 0.5 + 0.4 / h.ctx.pixels_per_point(), rect.center().y);
|
||||
// Aim AIM_OFFSET_PHYSICAL_PIXELS to the right of 0.5, as the other drags do.
|
||||
let target = pos2(rect.left() + 4.0 + (rect.width() - 8.0) * 0.5 + AIM_OFFSET_PHYSICAL_PIXELS / h.ctx.pixels_per_point(), rect.center().y);
|
||||
h.event(Event::PointerMoved(start));
|
||||
h.step();
|
||||
h.event(Event::PointerButton { pos: start, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
|
||||
@@ -103,6 +109,34 @@ fn bar_slider_drag_reaches_exact_round_values() {
|
||||
assert_eq!(*h.state(), 0.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bar_slider_drag_preserves_fractional_values() {
|
||||
let mut h = Harness::builder().with_size(vec2(800.0, 400.0)).with_pixels_per_point(1.0).build_ui_state(
|
||||
|ui, value: &mut f32| {
|
||||
widgets::bar_slider(ui, "Brightness", value, 0.0..=1.0, 128.0);
|
||||
},
|
||||
0.0f32,
|
||||
);
|
||||
h.run_steps(3);
|
||||
let rect = h.get(By::new().role(egui::accesskit::Role::Slider).label("Brightness")).rect();
|
||||
let start = pos2(rect.left() + rect.width() * 0.2, rect.center().y);
|
||||
// Aim exactly at 33/120 = 0.275, a fractional value between hundredths
|
||||
// (0.27 and 0.28), so round-value aiming must not move it.
|
||||
let target = pos2(rect.left() + 4.0 + 33.0, rect.center().y);
|
||||
h.event(Event::PointerMoved(start));
|
||||
h.step();
|
||||
h.event(Event::PointerButton { pos: start, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
|
||||
h.step();
|
||||
h.event(Event::PointerMoved(start + vec2(10.0, 0.0)));
|
||||
h.step();
|
||||
h.event(Event::PointerMoved(target));
|
||||
h.step();
|
||||
h.event(Event::PointerButton { pos: target, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE });
|
||||
h.step();
|
||||
h.run_steps(3);
|
||||
assert_eq!(*h.state(), 33.0 / 120.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "requires an offscreen GPU"]
|
||||
fn exact_angle_screenshot() {
|
||||
|
||||
Reference in new issue
Block a user