mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 13:50:04 -04:00
Refactor: put each shape into its own file (#5564)
Much easier to navigate the code
This commit is contained in:
97
crates/epaint/src/shapes/text_shape.rs
Normal file
97
crates/epaint/src/shapes/text_shape.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::*;
|
||||
|
||||
/// How to paint some text on screen.
|
||||
///
|
||||
/// This needs to be recreated if `pixels_per_point` (dpi scale) changes.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
||||
pub struct TextShape {
|
||||
/// Top left corner of the first character.
|
||||
pub pos: Pos2,
|
||||
|
||||
/// The laid out text, from [`Fonts::layout_job`].
|
||||
pub galley: Arc<Galley>,
|
||||
|
||||
/// Add this underline to the whole text.
|
||||
/// You can also set an underline when creating the galley.
|
||||
pub underline: Stroke,
|
||||
|
||||
/// Any [`Color32::PLACEHOLDER`] in the galley will be replaced by the given color.
|
||||
/// Affects everything: backgrounds, glyphs, strikethrough, underline, etc.
|
||||
pub fallback_color: Color32,
|
||||
|
||||
/// If set, the text color in the galley will be ignored and replaced
|
||||
/// with the given color.
|
||||
///
|
||||
/// This only affects the glyphs and will NOT replace background color nor strikethrough/underline color.
|
||||
pub override_text_color: Option<Color32>,
|
||||
|
||||
/// If set, the text will be rendered with the given opacity in gamma space
|
||||
/// Affects everything: backgrounds, glyphs, strikethrough, underline, etc.
|
||||
pub opacity_factor: f32,
|
||||
|
||||
/// Rotate text by this many radians clockwise.
|
||||
/// The pivot is `pos` (the upper left corner of the text).
|
||||
pub angle: f32,
|
||||
}
|
||||
|
||||
impl TextShape {
|
||||
/// The given fallback color will be used for any uncolored part of the galley (using [`Color32::PLACEHOLDER`]).
|
||||
///
|
||||
/// Any non-placeholder color in the galley takes precedence over this fallback color.
|
||||
#[inline]
|
||||
pub fn new(pos: Pos2, galley: Arc<Galley>, fallback_color: Color32) -> Self {
|
||||
Self {
|
||||
pos,
|
||||
galley,
|
||||
underline: Stroke::NONE,
|
||||
fallback_color,
|
||||
override_text_color: None,
|
||||
opacity_factor: 1.0,
|
||||
angle: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// The visual bounding rectangle
|
||||
#[inline]
|
||||
pub fn visual_bounding_rect(&self) -> Rect {
|
||||
self.galley.mesh_bounds.translate(self.pos.to_vec2())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn with_underline(mut self, underline: Stroke) -> Self {
|
||||
self.underline = underline;
|
||||
self
|
||||
}
|
||||
|
||||
/// Use the given color for the text, regardless of what color is already in the galley.
|
||||
#[inline]
|
||||
pub fn with_override_text_color(mut self, override_text_color: Color32) -> Self {
|
||||
self.override_text_color = Some(override_text_color);
|
||||
self
|
||||
}
|
||||
|
||||
/// Rotate text by this many radians clockwise.
|
||||
/// The pivot is `pos` (the upper left corner of the text).
|
||||
#[inline]
|
||||
pub fn with_angle(mut self, angle: f32) -> Self {
|
||||
self.angle = angle;
|
||||
self
|
||||
}
|
||||
|
||||
/// Render text with this opacity in gamma space
|
||||
#[inline]
|
||||
pub fn with_opacity_factor(mut self, opacity_factor: f32) -> Self {
|
||||
self.opacity_factor = opacity_factor;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TextShape> for Shape {
|
||||
#[inline(always)]
|
||||
fn from(shape: TextShape) -> Self {
|
||||
Self::Text(shape)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user