diff --git a/src/draw.rs b/src/draw.rs index 5c5ec75..5a82fc7 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -4,6 +4,7 @@ use crate::{Color, Vec2}; pub struct DrawContext { panel: UiPanel, offset: Vec2, + opacity: Option, } impl DrawContext { @@ -11,13 +12,19 @@ impl DrawContext { Self { panel, offset: Vec2::ZERO, + opacity: None, } } - pub fn draw_triangle(&self, v1: Vec2, v2: Vec2, v3: Vec2, color: Color) { + pub fn draw_triangle(&self, v1: Vec2, v2: Vec2, v3: Vec2, mut color: Color) { let v1 = v1 + self.offset; let v2 = v2 + self.offset; let v3 = v3 + self.offset; + + if let Some(opacity) = self.opacity.as_ref() { + color.a *= opacity; + } + self.panel.draw_triangle(v1, v2, v3, color); } @@ -73,8 +80,21 @@ impl DrawContext { pub fn with_offset(&self, offset: Vec2) -> Self { Self { - panel: self.panel, offset: self.offset + offset, + ..*self + } + } + + pub fn with_opacity(&self, opacity: f32) -> Self { + let opacity = if let Some(old) = self.opacity.as_ref() { + opacity * old + } else { + opacity + }; + + Self { + opacity: Some(opacity), + ..*self } } }