Skip to content

Commit

Permalink
refactor to use Vec2b
Browse files Browse the repository at this point in the history
  • Loading branch information
bircni committed Aug 28, 2024
1 parent e724127 commit ffd8434
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
4 changes: 2 additions & 2 deletions egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,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 @@ -1005,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

0 comments on commit ffd8434

Please sign in to comment.