use crate::buffer::BufferHandle;
use crate::color::{BlendMode, Color};
use crate::geometry::Rect;
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RenderOp {
Nop = 0,
Clear = 1,
FillRect = 2,
StrokeRect = 3,
DrawLine = 4,
Blit = 5,
BlitScaled = 6,
SetClip = 7,
ClearClip = 8,
Save = 9,
Restore = 10,
}
impl RenderOp {
#[inline]
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Nop),
1 => Some(Self::Clear),
2 => Some(Self::FillRect),
3 => Some(Self::StrokeRect),
4 => Some(Self::DrawLine),
5 => Some(Self::Blit),
6 => Some(Self::BlitScaled),
7 => Some(Self::SetClip),
8 => Some(Self::ClearClip),
9 => Some(Self::Save),
10 => Some(Self::Restore),
_ => None,
}
}
#[inline]
pub const fn name(&self) -> &'static str {
match self {
Self::Nop => "Nop",
Self::Clear => "Clear",
Self::FillRect => "FillRect",
Self::StrokeRect => "StrokeRect",
Self::DrawLine => "DrawLine",
Self::Blit => "Blit",
Self::BlitScaled => "BlitScaled",
Self::SetClip => "SetClip",
Self::ClearClip => "ClearClip",
Self::Save => "Save",
Self::Restore => "Restore",
}
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct FillParams {
pub rect: Rect,
pub color: Color,
pub blend: BlendMode,
}
impl FillParams {
#[inline]
pub const fn new(rect: Rect, color: Color) -> Self {
Self {
rect,
color,
blend: BlendMode::Normal,
}
}
#[inline]
pub const fn with_blend(mut self, blend: BlendMode) -> Self {
self.blend = blend;
self
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct BlitParams {
pub src: BufferHandle,
pub src_rect: Rect,
pub dst_x: i32,
pub dst_y: i32,
pub blend: BlendMode,
pub alpha: u8,
}
impl BlitParams {
#[inline]
pub const fn new(src: BufferHandle, src_rect: Rect, dst_x: i32, dst_y: i32) -> Self {
Self {
src,
src_rect,
dst_x,
dst_y,
blend: BlendMode::SourceOver,
alpha: 255,
}
}
#[inline]
pub const fn with_blend(mut self, blend: BlendMode) -> Self {
self.blend = blend;
self
}
#[inline]
pub const fn with_alpha(mut self, alpha: u8) -> Self {
self.alpha = alpha;
self
}
#[inline]
pub const fn dst_rect(&self) -> Rect {
Rect::new(
self.dst_x,
self.dst_y,
self.src_rect.width,
self.src_rect.height,
)
}
}