Add Painter::hline and Painter::vline

This commit is contained in:
Emil Ernerfeldt 2022-04-12 10:54:38 +02:00
parent 701ae3cb46
commit 038b3cf2e2
5 changed files with 30 additions and 27 deletions

View file

@ -13,6 +13,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
* Added `Plot::allow_scroll`, `Plot::allow_zoom` no longer affects scrolling ([#1382](https://github.com/emilk/egui/pull/1382)).
* Added `Ui::push_id` to resolve id clashes ([#1374](https://github.com/emilk/egui/pull/1374)).
* Added `Frame::outer_margin`.
* Added `Painter::hline` and `Painter::vline`.
### Changed 🔧
* `ClippedMesh` has been replaced with `ClippedPrimitive` ([#1351](https://github.com/emilk/egui/pull/1351)).

View file

@ -273,11 +273,9 @@ impl SidePanel {
// draw on top of ALL panels so that the resize line won't be covered by subsequent panels
let resize_layer = LayerId::new(Order::Foreground, Id::new("panel_resize"));
let resize_x = side.opposite().side_x(rect);
let top = pos2(resize_x, rect.top());
let bottom = pos2(resize_x, rect.bottom());
ui.ctx()
.layer_painter(resize_layer)
.line_segment([top, bottom], stroke);
.vline(resize_x, rect.y_range(), stroke);
}
inner_response
@ -560,11 +558,9 @@ impl TopBottomPanel {
// draw on top of ALL panels so that the resize line won't be covered by subsequent panels
let resize_layer = LayerId::new(Order::Foreground, Id::new("panel_resize"));
let resize_y = side.opposite().side_y(rect);
let left = pos2(rect.left(), resize_y);
let right = pos2(rect.right(), resize_y);
ui.ctx()
.layer_painter(resize_layer)
.line_segment([left, right], stroke);
.hline(rect.x_range(), resize_y, stroke);
}
inner_response

View file

@ -882,14 +882,10 @@ impl TitleBar {
if let Some(content_response) = &content_response {
// paint separator between title and content:
let left = outer_rect.left();
let right = outer_rect.right();
let y = content_response.rect.top() + ui.spacing().item_spacing.y * 0.5;
// let y = lerp(self.rect.bottom()..=content_response.rect.top(), 0.5);
ui.painter().line_segment(
[pos2(left, y), pos2(right, y)],
ui.visuals().widgets.noninteractive.bg_stroke,
);
let stroke = ui.visuals().widgets.noninteractive.bg_stroke;
ui.painter().hline(outer_rect.x_range(), y, stroke);
}
if ui

View file

@ -1,5 +1,7 @@
use std::ops::RangeInclusive;
use crate::{
emath::{Align2, Pos2, Rect, Vec2},
emath::{pos2, Align2, Pos2, Rect, Vec2},
layers::{LayerId, PaintList, ShapeIdx},
Color32, Context, FontId,
};
@ -233,8 +235,7 @@ impl Painter {
/// # Paint different primitives
impl Painter {
/// Paints the line from the first point to the second using the `stroke`
/// for outlining shape.
/// Paints a line from the first point to the second.
pub fn line_segment(&self, points: [Pos2; 2], stroke: impl Into<Stroke>) {
self.add(Shape::LineSegment {
points,
@ -242,6 +243,22 @@ impl Painter {
});
}
/// Paints a horizontal line.
pub fn hline(&self, x: RangeInclusive<f32>, y: f32, stroke: impl Into<Stroke>) {
self.add(Shape::LineSegment {
points: [pos2(*x.start(), y), pos2(*x.end(), y)],
stroke: stroke.into(),
});
}
/// Paints a vertical line.
pub fn vline(&self, x: f32, y: RangeInclusive<f32>, stroke: impl Into<Stroke>) {
self.add(Shape::LineSegment {
points: [pos2(x, *y.start()), pos2(x, *y.end())],
stroke: stroke.into(),
});
}
pub fn circle(
&self,
center: Pos2,

View file

@ -71,19 +71,12 @@ impl Widget for Separator {
let (rect, response) = ui.allocate_at_least(size, Sense::hover());
if ui.is_rect_visible(response.rect) {
let points = if is_horizontal_line {
[
pos2(rect.left(), rect.center().y),
pos2(rect.right(), rect.center().y),
]
} else {
[
pos2(rect.center().x, rect.top()),
pos2(rect.center().x, rect.bottom()),
]
};
let stroke = ui.visuals().widgets.noninteractive.bg_stroke;
ui.painter().line_segment(points, stroke);
if is_horizontal_line {
ui.painter().hline(rect.x_range(), rect.center().y, stroke);
} else {
ui.painter().vline(rect.center().x, rect.y_range(), stroke);
}
}
response