Add draw opacity

This commit is contained in:
mars 2022-07-09 18:23:21 -06:00
parent 5d1c12ffe2
commit a962886de2
1 changed files with 22 additions and 2 deletions

View File

@ -4,6 +4,7 @@ use crate::{Color, Vec2};
pub struct DrawContext {
panel: UiPanel,
offset: Vec2,
opacity: Option<f32>,
}
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
}
}
}