zed/crates/gpui3/src/scene.rs

127 lines
3 KiB
Rust
Raw Normal View History

use std::cmp;
2023-09-19 19:19:22 +00:00
use super::{Bounds, Hsla, Pixels, Point};
2023-09-22 22:31:26 +00:00
use crate::{Corners, Edges, FontId, GlyphId};
2023-09-19 19:19:22 +00:00
use bytemuck::{Pod, Zeroable};
use plane_split::BspSplitter;
// Exported to metal
pub type PointF = Point<f32>;
2023-09-19 19:19:22 +00:00
pub struct Scene {
opaque_primitives: PrimitiveBatch,
transparent_primitives: slotmap::SlotMap<slotmap::DefaultKey, Primitive>,
splitter: BspSplitter<slotmap::DefaultKey>,
max_order: u32,
2023-09-23 20:20:07 +00:00
scale_factor: f32,
2023-09-19 19:19:22 +00:00
}
impl Scene {
2023-09-23 20:20:07 +00:00
pub fn new(scale_factor: f32) -> Scene {
2023-09-19 19:19:22 +00:00
Scene {
opaque_primitives: PrimitiveBatch::default(),
transparent_primitives: slotmap::SlotMap::new(),
splitter: BspSplitter::new(),
max_order: 0,
2023-09-23 20:20:07 +00:00
scale_factor,
2023-09-19 19:19:22 +00:00
}
}
pub fn insert(&mut self, primitive: impl Into<Primitive>) {
2023-09-23 20:20:07 +00:00
let mut primitive = primitive.into();
primitive.scale(self.scale_factor);
self.max_order = cmp::max(self.max_order, primitive.order());
match primitive {
Primitive::Quad(quad) => self.opaque_primitives.quads.push(quad),
2023-09-19 19:19:22 +00:00
}
}
pub fn opaque_primitives(&self) -> &PrimitiveBatch {
&self.opaque_primitives
}
pub fn max_order(&self) -> u32 {
self.max_order
}
2023-09-19 19:19:22 +00:00
}
#[derive(Clone, Debug)]
pub enum Primitive {
Quad(Quad),
}
impl Primitive {
pub fn order(&self) -> u32 {
match self {
Primitive::Quad(quad) => quad.order,
}
}
2023-09-19 19:19:22 +00:00
pub fn is_transparent(&self) -> bool {
match self {
Primitive::Quad(quad) => {
quad.background.is_transparent() && quad.border_color.is_transparent()
}
2023-09-23 20:20:07 +00:00
}
}
pub fn scale(&mut self, factor: f32) {
match self {
Primitive::Quad(quad) => {
quad.scale(factor);
}
2023-09-19 19:19:22 +00:00
}
}
}
#[derive(Default)]
pub struct PrimitiveBatch {
pub quads: Vec<Quad>,
}
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
2023-09-19 19:19:22 +00:00
#[repr(C)]
pub struct Quad {
pub order: u32,
2023-09-19 19:19:22 +00:00
pub bounds: Bounds<Pixels>,
2023-09-22 22:31:26 +00:00
pub clip_bounds: Bounds<Pixels>,
pub clip_corner_radii: Corners<Pixels>,
2023-09-19 19:19:22 +00:00
pub background: Hsla,
pub border_color: Hsla,
2023-09-22 22:31:26 +00:00
pub corner_radii: Corners<Pixels>,
pub border_widths: Edges<Pixels>,
2023-09-19 19:19:22 +00:00
}
impl Quad {
pub fn vertices(&self) -> impl Iterator<Item = Point<Pixels>> {
let x1 = self.bounds.origin.x;
let y1 = self.bounds.origin.y;
let x2 = x1 + self.bounds.size.width;
let y2 = y1 + self.bounds.size.height;
[
Point::new(x1, y1),
Point::new(x2, y1),
Point::new(x2, y2),
Point::new(x1, y2),
]
.into_iter()
}
}
2023-09-23 20:20:07 +00:00
impl Quad {
pub fn scale(&mut self, factor: f32) {
self.bounds.origin *= factor;
self.bounds.size *= factor;
self.clip_bounds.origin *= factor;
self.clip_corner_radii *= factor;
self.corner_radii *= factor;
self.border_widths *= factor;
2023-09-19 19:19:22 +00:00
}
}
2023-09-23 20:20:07 +00:00
impl From<Quad> for Primitive {
fn from(quad: Quad) -> Self {
Primitive::Quad(quad)
2023-09-19 19:19:22 +00:00
}
}