Skip to content

Commit

Permalink
Convert Painter
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 1, 2021
1 parent 52df54e commit 682f86d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
2 changes: 1 addition & 1 deletion egui/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ impl Layout {
painter: &crate::Painter,
region: &Region,
stroke: epaint::Stroke,
text: impl ToString,
text: impl Into<crate::WidgetText>,
) {
let cursor = region.cursor;
let next_pos = self.next_widget_position(region);
Expand Down
33 changes: 13 additions & 20 deletions egui/src/painter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
emath::{Align2, Pos2, Rect, Vec2},
layers::{LayerId, PaintList, ShapeIdx},
Color32, CtxRef,
Color32, CtxRef, WidgetText,
};
use epaint::{
mutex::Mutex,
Expand Down Expand Up @@ -195,41 +195,34 @@ impl Painter {

/// ## Debug painting
impl Painter {
#[allow(clippy::needless_pass_by_value)]
pub fn debug_rect(&mut self, rect: Rect, color: Color32, text: impl ToString) {
pub fn debug_rect(&mut self, rect: Rect, color: Color32, text: impl Into<WidgetText>) {
self.rect_stroke(rect, 0.0, (1.0, color));
let text_style = TextStyle::Monospace;
self.text(
rect.min,
Align2::LEFT_TOP,
text.to_string(),
text_style,
color,
);
self.text(rect.min, Align2::LEFT_TOP, text, text_style, color);
}

pub fn error(&self, pos: Pos2, text: impl std::fmt::Display) -> Rect {
self.debug_text(pos, Align2::LEFT_TOP, Color32::RED, format!("🔥 {}", text))
}

/// text with a background
#[allow(clippy::needless_pass_by_value)]
pub fn debug_text(
&self,
pos: Pos2,
anchor: Align2,
color: Color32,
text: impl ToString,
text: impl Into<WidgetText>,
) -> Rect {
let galley = self.layout_no_wrap(text.to_string(), TextStyle::Monospace, color);
let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size()));
let text = text.into();
let text_galley = text.into_galley_raw(self.ctx(), f32::INFINITY, TextStyle::Monospace);
let rect = anchor.anchor_rect(Rect::from_min_size(pos, text_galley.size()));
let frame_rect = rect.expand(2.0);
self.add(Shape::rect_filled(
frame_rect,
0.0,
Color32::from_black_alpha(240),
));
self.galley(rect.min, galley);
text_galley.paint_with_fallback_color(self, rect.min, color);
frame_rect
}
}
Expand Down Expand Up @@ -332,18 +325,18 @@ impl Painter {
/// [`Self::layout`] or [`Self::layout_no_wrap`].
///
/// Returns where the text ended up.
#[allow(clippy::needless_pass_by_value)]
pub fn text(
&self,
pos: Pos2,
anchor: Align2,
text: impl ToString,
text: impl Into<WidgetText>,
text_style: TextStyle,
text_color: Color32,
) -> Rect {
let galley = self.layout_no_wrap(text.to_string(), text_style, text_color);
let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size()));
self.galley(rect.min, galley);
let text = text.into();
let text_galley = text.into_galley_raw(self.ctx(), f32::INFINITY, text_style);
let rect = anchor.anchor_rect(Rect::from_min_size(pos, text_galley.size()));
text_galley.paint_with_fallback_color(self, rect.min, text_color);
rect
}

Expand Down
6 changes: 5 additions & 1 deletion egui/src/placer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ impl Placer {
}

impl Placer {
pub(crate) fn debug_paint_cursor(&self, painter: &crate::Painter, text: impl ToString) {
pub(crate) fn debug_paint_cursor(
&self,
painter: &crate::Painter,
text: impl Into<crate::WidgetText>,
) {
let stroke = Stroke::new(1.0, Color32::DEBUG_COLOR);

if let Some(grid) = &self.grid {
Expand Down
2 changes: 1 addition & 1 deletion egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ impl Ui {

/// Shows the given text where the next widget is to be placed
/// if when [`Context::set_debug_on_hover`] has been turned on and the mouse is hovering the Ui.
pub fn trace_location(&self, text: impl ToString) {
pub fn trace_location(&self, text: impl Into<crate::WidgetText>) {
let rect = self.max_rect();
if self.style().debug.debug_on_hover && self.rect_contains_pointer(rect) {
self.placer
Expand Down
30 changes: 30 additions & 0 deletions egui/src/widget_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,36 @@ impl WidgetText {
},
}
}

pub fn into_galley_raw(
self,
ctx: &crate::Context,
wrap_width: f32,
default_text_style: TextStyle,
) -> WidgetTextGalley {
match self {
Self::RichText(text) => {
let valign = Align::Center;
let mut text_job = text.into_text_job(&ctx.style(), default_text_style, valign);
text_job.job.wrap_width = wrap_width;
WidgetTextGalley {
galley: ctx.fonts().layout_job(text_job.job),
galley_has_color: text_job.job_has_color,
}
}
Self::LayoutJob(mut job) => {
job.wrap_width = wrap_width;
WidgetTextGalley {
galley: ctx.fonts().layout_job(job),
galley_has_color: true,
}
}
Self::Galley(galley) => WidgetTextGalley {
galley,
galley_has_color: true,
},
}
}
}

impl From<&str> for WidgetText {
Expand Down

0 comments on commit 682f86d

Please sign in to comment.