Skip to content

Commit

Permalink
Use Vec2b in parameters (#43)
Browse files Browse the repository at this point in the history
* [x] I have followed the instructions in the PR template
  • Loading branch information
bircni authored Dec 4, 2024
1 parent e96f4e1 commit dfde979
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 14 additions & 18 deletions demo/src/plot_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,19 +591,15 @@ impl CustomAxesDemo {

#[derive(PartialEq, serde::Deserialize, serde::Serialize)]
struct LinkedAxesDemo {
link_x: bool,
link_y: bool,
link_cursor_x: bool,
link_cursor_y: bool,
link_axis: Vec2b,
link_cursor: Vec2b,
}

impl Default for LinkedAxesDemo {
fn default() -> Self {
Self {
link_x: true,
link_y: true,
link_cursor_x: true,
link_cursor_y: true,
link_axis: Vec2b::new(true, true),
link_cursor: Vec2b::new(true, true),
}
}
}
Expand Down Expand Up @@ -644,13 +640,13 @@ impl LinkedAxesDemo {
fn ui(&mut self, ui: &mut egui::Ui) -> Response {
ui.horizontal(|ui| {
ui.label("Linked axes:");
ui.checkbox(&mut self.link_x, "X");
ui.checkbox(&mut self.link_y, "Y");
ui.checkbox(&mut self.link_axis.x, "X");
ui.checkbox(&mut self.link_axis.y, "Y");
});
ui.horizontal(|ui| {
ui.label("Linked cursors:");
ui.checkbox(&mut self.link_cursor_x, "X");
ui.checkbox(&mut self.link_cursor_y, "Y");
ui.checkbox(&mut self.link_cursor.x, "X");
ui.checkbox(&mut self.link_cursor.y, "Y");
});

ScrollArea::horizontal()
Expand All @@ -666,26 +662,26 @@ impl LinkedAxesDemo {
.data_aspect(1.0)
.width(250.0)
.height(250.0)
.link_axis(link_group_id, self.link_x, self.link_y)
.link_cursor(link_group_id, self.link_cursor_x, self.link_cursor_y)
.link_axis(link_group_id, self.link_axis)
.link_cursor(link_group_id, self.link_cursor)
.show(ui, Self::configure_plot);
Plot::new("right-top")
.data_aspect(2.0)
.width(150.0)
.height(250.0)
.y_axis_label("y")
.y_axis_position(egui_plot::HPlacement::Right)
.link_axis(link_group_id, self.link_x, self.link_y)
.link_cursor(link_group_id, self.link_cursor_x, self.link_cursor_y)
.link_axis(link_group_id, self.link_axis)
.link_cursor(link_group_id, self.link_cursor)
.show(ui, Self::configure_plot);
});
Plot::new("left-bottom")
.data_aspect(0.5)
.width(250.0)
.height(150.0)
.x_axis_label("x")
.link_axis(link_group_id, self.link_x, self.link_y)
.link_cursor(link_group_id, self.link_cursor_x, self.link_cursor_y)
.link_axis(link_group_id, self.link_axis)
.link_cursor(link_group_id, self.link_cursor)
.show(ui, Self::configure_plot)
.response
}
Expand Down
26 changes: 6 additions & 20 deletions egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,31 +578,17 @@ impl<'a> Plot<'a> {

/// Add this plot to an axis link group so that this plot will share the bounds with other plots in the
/// same group. A plot cannot belong to more than one axis group.
#[allow(clippy::fn_params_excessive_bools)] // TODO(emilk): use a `Vec2` as argument instead
#[inline]
pub fn link_axis(mut self, group_id: impl Into<Id>, link_x: bool, link_y: bool) -> Self {
self.linked_axes = Some((
group_id.into(),
Vec2b {
x: link_x,
y: link_y,
},
));
pub fn link_axis(mut self, group_id: impl Into<Id>, link: impl Into<Vec2b>) -> Self {
self.linked_axes = Some((group_id.into(), link.into()));
self
}

/// Add this plot to a cursor link group so that this plot will share the cursor position with other plots
/// in the same group. A plot cannot belong to more than one cursor group.
#[allow(clippy::fn_params_excessive_bools)] // TODO(emilk): use a `Vec2` as argument instead
#[inline]
pub fn link_cursor(mut self, group_id: impl Into<Id>, link_x: bool, link_y: bool) -> Self {
self.linked_cursors = Some((
group_id.into(),
Vec2b {
x: link_x,
y: link_y,
},
));
pub fn link_cursor(mut self, group_id: impl Into<Id>, link: Vec2b) -> Self {
self.linked_cursors = Some((group_id.into(), link));
self
}

Expand Down Expand Up @@ -853,7 +839,7 @@ impl<'a> Plot<'a> {
auto_bounds: default_auto_bounds,
hovered_legend_item: None,
hidden_items: Default::default(),
transform: PlotTransform::new(plot_rect, min_auto_bounds, center_axis.x, center_axis.y),
transform: PlotTransform::new(plot_rect, min_auto_bounds, center_axis),
last_click_pos_for_zoom: None,
x_axis_thickness: Default::default(),
y_axis_thickness: Default::default(),
Expand Down Expand Up @@ -1019,7 +1005,7 @@ impl<'a> Plot<'a> {
}
}

mem.transform = PlotTransform::new(plot_rect, bounds, center_axis.x, center_axis.y);
mem.transform = PlotTransform::new(plot_rect, bounds, center_axis);

// Enforce aspect ratio
if let Some(data_aspect) = data_aspect {
Expand Down
23 changes: 9 additions & 14 deletions egui_plot/src/transform.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::RangeInclusive;

use egui::{pos2, remap, Pos2, Rect, Vec2};
use egui::{pos2, remap, Pos2, Rect, Vec2, Vec2b};

use crate::Axis;

Expand Down Expand Up @@ -274,16 +274,12 @@ pub struct PlotTransform {
/// The plot bounds.
bounds: PlotBounds,

/// Whether to always center the x-range of the bounds.
x_centered: bool,

/// Whether to always center the y-range of the bounds.
y_centered: bool,
/// Whether to always center the x-range or y-range of the bounds.
centered: Vec2b,
}

impl PlotTransform {
#[allow(clippy::fn_params_excessive_bools)] // TODO(emilk): use a `Vec2` as argument instead
pub fn new(frame: Rect, bounds: PlotBounds, x_centered: bool, y_centered: bool) -> Self {
pub fn new(frame: Rect, bounds: PlotBounds, center_axis: Vec2b) -> Self {
debug_assert!(
0.0 <= frame.width() && 0.0 <= frame.height(),
"Bad plot frame: {frame:?}"
Expand Down Expand Up @@ -325,10 +321,10 @@ impl PlotTransform {
};

// Scale axes so that the origin is in the center.
if x_centered {
if center_axis.x {
new_bounds.make_x_symmetrical();
};
if y_centered {
if center_axis.y {
new_bounds.make_y_symmetrical();
};

Expand All @@ -340,8 +336,7 @@ impl PlotTransform {
Self {
frame,
bounds: new_bounds,
x_centered,
y_centered,
centered: center_axis,
}
}

Expand All @@ -363,10 +358,10 @@ impl PlotTransform {
}

pub fn translate_bounds(&mut self, mut delta_pos: (f64, f64)) {
if self.x_centered {
if self.centered.x {
delta_pos.0 = 0.;
}
if self.y_centered {
if self.centered.y {
delta_pos.1 = 0.;
}
delta_pos.0 *= self.dvalue_dpos()[0];
Expand Down
2 changes: 1 addition & 1 deletion scripts/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set -x
# Checks all tests, lints etc.
# Basically does what the CI does.

cargo +1.75.0 install --quiet typos-cli
cargo +1.76.0 install --quiet typos-cli

export RUSTFLAGS="-D warnings"
export RUSTDOCFLAGS="-D warnings" # https://github.com/emilk/egui/pull/1454
Expand Down

0 comments on commit dfde979

Please sign in to comment.