From f699ab40fe604062a5cd210004488d32bd7b3e3d Mon Sep 17 00:00:00 2001 From: spuds <71292624+bananaturtlesandwich@users.noreply.github.com> Date: Fri, 7 Jul 2023 23:38:28 +0100 Subject: [PATCH 1/4] add anchor support --- src/backend.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/backend.rs b/src/backend.rs index 9d5fcb2..82070ad 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -5,10 +5,11 @@ use std::fmt::{Display, Formatter, Result as FmtResult}; use std::ops::{Add, AddAssign, MulAssign, Sub, SubAssign}; use egui::{ - epaint::PathShape, Align2, Color32, FontFamily as EguiFontFamily, FontId, Pos2, Rect, Stroke, - Ui, + epaint::PathShape, Align, Align2, Color32, FontFamily as EguiFontFamily, FontId, Pos2, Rect, + Stroke, Ui, }; use plotters_backend::{ + text_anchor::{HPos, Pos, VPos}, BackendColor, BackendCoord, BackendStyle, BackendTextStyle, DrawingBackend, DrawingErrorKind, FontFamily as PlottersFontFamily, }; @@ -16,9 +17,7 @@ use plotters_backend::{ #[derive(Debug, Clone, Copy)] /// Error to be returned by the backend. Since egui doesn't return any errors /// on any painter operations, this is a stub type. -pub enum EguiBackendError { - None, -} +pub struct EguiBackendError; impl Display for EguiBackendError { #[inline] @@ -322,7 +321,21 @@ impl<'a> DrawingBackend for EguiBackend<'a> { let color: Color32 = EguiBackendColor::from(style.color()).into(); - painter.text(pos.into(), Align2::LEFT_TOP, text, font, color); + let Pos { h_pos, v_pos } = style.anchor(); + let anchor = Align2([ + match h_pos { + HPos::Left => Align::LEFT, + HPos::Right => Align::RIGHT, + HPos::Center => Align::Center, + }, + match v_pos { + VPos::Top => Align::TOP, + VPos::Center => Align::Center, + VPos::Bottom => Align::BOTTOM, + }, + ]); + + painter.text(pos.into(), anchor, text, font, color); Ok(()) } From b556d36f4043f0810419b8d5b77012a5ea9df00d Mon Sep 17 00:00:00 2001 From: spuds <71292624+bananaturtlesandwich@users.noreply.github.com> Date: Sat, 8 Jul 2023 06:29:56 +0100 Subject: [PATCH 2/4] add rotation support --- src/backend.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/backend.rs b/src/backend.rs index 82070ad..897143d 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -1,12 +1,13 @@ //! Plotter backend for egui use std::error::Error as ErrorTrait; +use std::f32::consts::FRAC_PI_2; use std::fmt::{Display, Formatter, Result as FmtResult}; use std::ops::{Add, AddAssign, MulAssign, Sub, SubAssign}; use egui::{ - epaint::PathShape, Align, Align2, Color32, FontFamily as EguiFontFamily, FontId, Pos2, Rect, - Stroke, Ui, + epaint::{PathShape, TextShape}, + Align, Align2, Color32, FontFamily as EguiFontFamily, FontId, Pos2, Rect, Stroke, Ui, }; use plotters_backend::{ text_anchor::{HPos, Pos, VPos}, @@ -321,8 +322,11 @@ impl<'a> DrawingBackend for EguiBackend<'a> { let color: Color32 = EguiBackendColor::from(style.color()).into(); + let rotations = style.transform() as usize; + let angle = rotations as f32 * FRAC_PI_2; + let Pos { h_pos, v_pos } = style.anchor(); - let anchor = Align2([ + let mut anchor = Align2([ match h_pos { HPos::Left => Align::LEFT, HPos::Right => Align::RIGHT, @@ -334,8 +338,30 @@ impl<'a> DrawingBackend for EguiBackend<'a> { VPos::Bottom => Align::BOTTOM, }, ]); - - painter.text(pos.into(), anchor, text, font, color); + fn rotate(anchor: &mut Align2) { + *anchor = match anchor { + &mut Align2::LEFT_TOP => Align2::RIGHT_TOP, + &mut Align2::RIGHT_TOP => Align2::RIGHT_BOTTOM, + &mut Align2::RIGHT_BOTTOM => Align2::LEFT_BOTTOM, + &mut Align2::LEFT_BOTTOM => Align2::LEFT_TOP, + &mut Align2::LEFT_CENTER => Align2::CENTER_TOP, + &mut Align2::CENTER_TOP => Align2::RIGHT_CENTER, + &mut Align2::RIGHT_CENTER => Align2::CENTER_BOTTOM, + &mut Align2::CENTER_BOTTOM => Align2::LEFT_CENTER, + &mut Align2::CENTER_CENTER => Align2::CENTER_CENTER, + } + } + for _ in 0..rotations { + rotate(&mut anchor) + } + let galley = painter.layout_no_wrap(text.to_string(), font, color); + let rect = anchor.anchor_rect(Rect::from_min_size(pos.into(), galley.size())); + if !galley.is_empty() { + painter.add(TextShape { + angle, + ..TextShape::new(rect.min, galley) + }); + } Ok(()) } From e9d76352fb0cb680990f9fa90a016ee42b4a06f0 Mon Sep 17 00:00:00 2001 From: Charles Thompson Date: Sun, 23 Jul 2023 13:26:59 -0400 Subject: [PATCH 3/4] Removed workaround code in the xytime chart --- src/charts/xytime.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/charts/xytime.rs b/src/charts/xytime.rs index 360d279..33220b7 100644 --- a/src/charts/xytime.rs +++ b/src/charts/xytime.rs @@ -132,8 +132,6 @@ impl XyTimeData { ranges.push((range_x, range_y)); } - let y_unit: String = y_unit.split("").map(|c| format!("{}\n", c)).collect(); - // Turn all the vecs and strings into arcs since they are more or less read-only at // this point From 748781e1cbb6fba8f77b28b26ea3e6e091f6f051 Mon Sep 17 00:00:00 2001 From: Charles Thompson Date: Sun, 23 Jul 2023 13:33:59 -0400 Subject: [PATCH 4/4] Added todo notice to text rotation code --- src/backend.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/backend.rs b/src/backend.rs index 897143d..50ec0ac 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -326,6 +326,8 @@ impl<'a> DrawingBackend for EguiBackend<'a> { let angle = rotations as f32 * FRAC_PI_2; let Pos { h_pos, v_pos } = style.anchor(); + + // !TODO! Find a slightly more eligant rotation function. let mut anchor = Align2([ match h_pos { HPos::Left => Align::LEFT,