use crate::Pt;
use crate::ShaderOpts;
use crate::Text;
#[cfg(feature = "model-3d")]
pub(crate) use crate::drawable_3d::DrawCommand3D;
use crate::image_shader::ImageShaderBindings;
pub trait Drawable {
type Options;
fn draw_to(self, ctx: &mut crate::Context, target: crate::Image, options: Self::Options);
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ImageCommand {
pub id: u32,
pub target_texture_id: u32,
pub opts: DrawOption,
pub shader_id: u32,
pub shader_opts: ShaderOpts,
pub shader_bindings: ImageShaderBindings,
pub size: [Pt; 2],
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct TextCommand {
pub target_texture_id: u32,
pub text: Box<Text>,
pub opts: DrawOption,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum DrawCommand {
Image(Box<ImageCommand>),
Text(Box<TextCommand>),
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DrawOption {
position: [Pt; 2],
rotation: f32,
scale: [f32; 2],
opacity: f32,
}
impl Default for DrawOption {
fn default() -> Self {
Self {
position: [Pt(0.0), Pt(0.0)],
scale: [1.0, 1.0],
rotation: 0.0,
opacity: 1.0,
}
}
}
impl DrawOption {
pub fn new(position: [Pt; 2], rotation: f32, scale: [f32; 2]) -> Self {
Self {
position,
rotation,
scale,
opacity: 1.0,
}
}
pub fn position(&self) -> [Pt; 2] {
self.position
}
pub fn with_position(mut self, position: [Pt; 2]) -> Self {
self.position = position;
self
}
pub fn set_position(&mut self, x: Pt, y: Pt) {
self.position = [x, y];
}
pub fn rotation(&self) -> f32 {
self.rotation
}
pub fn with_rotation(mut self, rotation: f32) -> Self {
self.rotation = rotation;
self
}
pub fn scale(&self) -> [f32; 2] {
self.scale
}
pub fn with_scale(mut self, scale: [f32; 2]) -> Self {
self.scale = scale;
self
}
pub fn opacity(&self) -> f32 {
self.opacity
}
pub fn with_opacity(mut self, opacity: f32) -> Self {
self.opacity = opacity.clamp(0.0, 1.0);
self
}
}