use crate::{NcAlign, NcBlitter, NcPlane, NcRgba, NcScale, NcVisualFlag, NcVisualOptions};
#[derive(Debug, Default)]
pub struct NcVisualOptionsBuilder<'ncplane> {
plane: Option<&'ncplane mut NcPlane>,
scale: NcScale,
y: i32,
x: i32,
region_yx_lenyx: Option<(u32, u32, u32, u32)>,
cell_offset_yx: Option<(u32, u32)>,
blitter: NcBlitter,
flags: NcVisualFlag,
transcolor: NcRgba,
}
mod core_impls {
use super::{NcVisualOptions, NcVisualOptionsBuilder};
impl<'a> From<NcVisualOptionsBuilder<'a>> for NcVisualOptions {
fn from(builder: NcVisualOptionsBuilder<'a>) -> NcVisualOptions {
builder.build()
}
}
impl<'a> From<NcVisualOptions> for NcVisualOptionsBuilder<'a> {
fn from(options: NcVisualOptions) -> NcVisualOptionsBuilder<'a> {
Self::from_options(&options)
}
}
}
impl<'ncplane> NcVisualOptionsBuilder<'ncplane> {
pub fn new() -> Self {
Self::default()
}
pub fn from_options(o: &NcVisualOptions) -> Self {
let mut builder = Self::default();
builder = builder.scale(o.scaling);
builder = builder.blitter(o.blitter);
builder = builder.cell_offset(o.pxoffy, o.pxoffx);
builder = builder.region(o.begy, o.begx, o.leny, o.lenx);
if o.does_plane() {
builder = builder.plane(unsafe { &mut *o.n });
if o.does_child_plane() {
builder = builder.child(true);
}
}
if o.does_alpha() {
builder = builder.transcolor(Some(o.transcolor))
}
if o.does_blend() {
builder = builder.blend(true);
}
if o.does_degrade() {
builder = builder.degrade(true);
}
if o.does_interpolate() {
builder = builder.interpolate(true);
}
if o.is_veraligned() {
builder = builder.valign(o.y);
} else {
builder = builder.y(o.y);
}
if o.is_horaligned() {
builder = builder.halign(o.x);
} else {
builder = builder.x(o.x);
}
builder
}
}
impl<'ncplane> NcVisualOptionsBuilder<'ncplane> {
pub fn plane(mut self, plane: &'ncplane mut NcPlane) -> Self {
self.plane = Some(plane);
self
}
pub fn child(mut self, child: bool) -> Self {
if child {
self.flags |= NcVisualFlag::ChildPlane;
} else {
self.flags &= !NcVisualFlag::ChildPlane;
}
self
}
pub fn parent(mut self, plane: &'ncplane mut NcPlane) -> Self {
self.plane = Some(plane);
self.flags |= NcVisualFlag::ChildPlane;
self
}
pub fn no_plane(mut self) -> Self {
self.plane = None;
self.flags &= !NcVisualFlag::ChildPlane;
self
}
pub fn scale(mut self, scale: impl Into<NcScale>) -> Self {
self.scale = scale.into();
self
}
pub fn y(mut self, y: i32) -> Self {
self.y = y;
self.flags &= !NcVisualFlag::VerAligned;
self
}
pub fn x(mut self, x: i32) -> Self {
self.x = x;
self.flags &= !NcVisualFlag::HorAligned;
self
}
pub fn yx(mut self, y: i32, x: i32) -> Self {
self.y = y;
self.x = x;
self.flags &= !NcVisualFlag::VerAligned;
self.flags &= !NcVisualFlag::HorAligned;
self
}
pub fn valign(mut self, valign: impl Into<NcAlign>) -> Self {
self.y = valign.into().into();
self.flags |= NcVisualFlag::VerAligned;
self
}
pub fn halign(mut self, halign: impl Into<NcAlign>) -> Self {
self.x = halign.into().into();
self.flags |= NcVisualFlag::HorAligned;
self
}
pub fn align(mut self, valign: impl Into<NcAlign>, halign: impl Into<NcAlign>) -> Self {
self.y = valign.into().into();
self.x = halign.into().into();
self.flags |= NcVisualFlag::VerAligned;
self.flags |= NcVisualFlag::HorAligned;
self
}
pub fn blitter(mut self, blitter: impl Into<NcBlitter>) -> Self {
self.blitter = blitter.into();
self
}
pub fn pixel(mut self) -> Self {
self.blitter = NcBlitter::Pixel;
self
}
pub fn transcolor(mut self, color: Option<impl Into<NcRgba>>) -> Self {
if let Some(color) = color {
self.transcolor = color.into();
self.flags |= NcVisualFlag::AddAlpha;
} else {
self.flags &= !NcVisualFlag::AddAlpha;
}
self
}
pub fn blend(mut self, blend: bool) -> Self {
if blend {
self.flags |= NcVisualFlag::Blend;
} else {
self.flags &= !NcVisualFlag::Blend;
}
self
}
pub fn degrade(mut self, degrade: bool) -> Self {
if degrade {
self.flags &= !NcVisualFlag::NoDegrade;
} else {
self.flags |= NcVisualFlag::NoDegrade;
}
self
}
pub fn interpolate(mut self, interpolate: bool) -> Self {
if interpolate {
self.flags &= !NcVisualFlag::NoInterpolate;
} else {
self.flags |= NcVisualFlag::NoInterpolate;
}
self
}
pub fn region(mut self, beg_y: u32, beg_x: u32, len_y: u32, len_x: u32) -> Self {
self.region_yx_lenyx = Some((beg_y, beg_x, len_y, len_x));
self
}
pub fn cell_offset(mut self, y: u32, x: u32) -> Self {
self.cell_offset_yx = Some((y, x));
self
}
pub fn build(self) -> NcVisualOptions {
NcVisualOptions::new(
self.plane,
self.scale,
self.y,
self.x,
self.region_yx_lenyx,
self.cell_offset_yx,
self.blitter,
self.flags,
self.transcolor,
)
}
}