From d659e5d24f7344d7cbab45f05f2ed2821c3fb270 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 30 Jul 2022 15:34:24 +0200 Subject: [PATCH] Add `Shape::hline` and `Shape::vline` --- egui/src/painter.rs | 12 +++--------- epaint/src/shape.rs | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/egui/src/painter.rs b/egui/src/painter.rs index af835be7c..e9a130541 100644 --- a/egui/src/painter.rs +++ b/egui/src/painter.rs @@ -2,7 +2,7 @@ use std::ops::RangeInclusive; use std::sync::Arc; use crate::{ - emath::{pos2, Align2, Pos2, Rect, Vec2}, + emath::{Align2, Pos2, Rect, Vec2}, layers::{LayerId, PaintList, ShapeIdx}, Color32, Context, FontId, }; @@ -257,18 +257,12 @@ impl Painter { /// Paints a horizontal line. pub fn hline(&self, x: RangeInclusive, y: f32, stroke: impl Into) { - self.add(Shape::LineSegment { - points: [pos2(*x.start(), y), pos2(*x.end(), y)], - stroke: stroke.into(), - }); + self.add(Shape::hline(x, y, stroke)); } /// Paints a vertical line. pub fn vline(&self, x: f32, y: RangeInclusive, stroke: impl Into) { - self.add(Shape::LineSegment { - points: [pos2(x, *y.start()), pos2(x, *y.end())], - stroke: stroke.into(), - }); + self.add(Shape::vline(x, y, stroke)); } pub fn circle( diff --git a/epaint/src/shape.rs b/epaint/src/shape.rs index ae79b3624..fae70cb88 100644 --- a/epaint/src/shape.rs +++ b/epaint/src/shape.rs @@ -1,5 +1,6 @@ //! The different shapes that can be painted. +use std::ops::RangeInclusive; use std::{any::Any, sync::Arc}; use crate::{ @@ -72,6 +73,22 @@ impl Shape { } } + /// A horizontal line. + pub fn hline(x: RangeInclusive, y: f32, stroke: impl Into) -> Self { + Shape::LineSegment { + points: [pos2(*x.start(), y), pos2(*x.end(), y)], + stroke: stroke.into(), + } + } + + /// A vertical line. + pub fn vline(x: f32, y: RangeInclusive, stroke: impl Into) -> Self { + Shape::LineSegment { + points: [pos2(x, *y.start()), pos2(x, *y.end())], + stroke: stroke.into(), + } + } + /// A line through many points. /// /// Use [`Self::line_segment`] instead if your line only connects two points.