1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
//!
use crate::{NcAlign, NcBlitter, NcPlane, NcRgba, NcScale, NcVisualFlag, NcVisualOptions};
/// Builder object for [`NcVisualOptions`].
///
/// Can be constructed by calling [`NcVisualOptions::builder()`].
///
/// [`NcVisualOptions::builder()`]: NcVisualOptions#method.builder
#[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)
}
}
}
/// # Constructors
impl<'ncplane> NcVisualOptionsBuilder<'ncplane> {
/// New default `NcVisualOptionsBuilder`.
pub fn new() -> Self {
Self::default()
}
/// New builder from pre-existing options.
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
}
}
/// # Methods (chainable)
impl<'ncplane> NcVisualOptionsBuilder<'ncplane> {
/// Sets the `NcPlane` where the blitting will be done.
///
/// This `NcPlane` could also be considered the parent of a new plane where
/// the blitting will occur by utilizing the [`child`] method.
///
/// When no `NcPlane` is provided, one will be created using the exact size
/// necessary to render the source with perfect fidelity (this might be
/// smaller or larger than the rendering area).
///
/// Default: *`None`* (no plane).
///
/// See also: *[`parent`]*, *[`child`]*, *[`no_plane`]*.
///
/// [`child`]: NcVisualOptionsBuilder#method.child
/// [`parent`]: NcVisualOptionsBuilder#method.parent
/// [`no_plane`]: NcVisualOptionsBuilder#method.no_plane
pub fn plane(mut self, plane: &'ncplane mut NcPlane) -> Self {
self.plane = Some(plane);
self
}
/// If true, a [`plane`] must also be provided, which will be the parent
/// of a new child `NcPlane` into which the blitting will be done.
///
/// If false, the blitting will occur in the provided [`plane`], if any,
/// or in a newly created `NcPlane` otherwise.
///
/// Default: *false* (no child plaane).
///
/// Effect: Sets the [`CHILDPLANE`] flag.
///
/// See also: *[`plane`]*, *[`parent`]*.
///
/// [`CHILDPLANE`]: NcVisualOptions#associatedconstant.CHILDPLANE
/// [`plane`]: NcVisualOptionsBuilder#method.plane
/// [`parent`]: NcVisualOptionsBuilder#method.parent
pub fn child(mut self, child: bool) -> Self {
if child {
self.flags |= NcVisualFlag::ChildPlane;
} else {
self.flags &= !NcVisualFlag::ChildPlane;
}
self
}
/// Sets the `NcPlane` that will be the parent of a new `NcPlane` where
/// the blitting will be done.
///
/// This is the same as calling both [`plane`] and [`child`].
///
/// See also: *[`plane`]*, *[`child`]*.
///
/// [`plane`]: NcVisualOptionsBuilder#method.plane
/// [`child`]: NcVisualOptionsBuilder#method.child
pub fn parent(mut self, plane: &'ncplane mut NcPlane) -> Self {
self.plane = Some(plane);
self.flags |= NcVisualFlag::ChildPlane;
self
}
/// Unsets the `NcPlane`.
///
/// Effect: unsets the plane & the [`CHILDPLANE`] flag.
///
/// Default: *`None`* (no plane).
///
/// [`CHILDPLANE`]: NcVisualOptions#associatedconstant.CHILDPLANE
pub fn no_plane(mut self) -> Self {
self.plane = None;
self.flags &= !NcVisualFlag::ChildPlane;
self
}
/// Sets the `NcScale`.
///
/// Default: *[`NcScale::NOSCALE`][crate::NcScale#associatedconstant.NOSCALE]*.
pub fn scale(mut self, scale: impl Into<NcScale>) -> Self {
self.scale = scale.into();
self
}
/// Sets the vertical placement.
///
/// Default: *`0`*.
///
/// Effect: Sets the *y* coordinate, and unsets the [`VerAligned`] flag.
///
/// [`VerAligned`]: NcVisualOptions#associatedconstant.VerAligned
pub fn y(mut self, y: i32) -> Self {
self.y = y;
self.flags &= !NcVisualFlag::VerAligned;
self
}
/// Sets the horizontal placement.
///
/// Default: *`0`*.
///
/// Effect: Sets the *x* coordinate, and unsets the [`HorAligned`] flag.
///
/// [`HorAligned`]: NcVisualOptions#associatedconstant.HorAligned
pub fn x(mut self, x: i32) -> Self {
self.x = x;
self.flags &= !NcVisualFlag::HorAligned;
self
}
/// Sets the vertical & horizontal placement.
///
/// Default: *`(0, 0)`*.
///
/// Effect: Sets the *`y` & `x`* coordinates and unsets the [`VerAligned`]
/// & [`HorAligned`] flags.
///
/// [`VerAligned`]: NcVisualOptions#associatedconstant.VerAligned
/// [`HorAligned`]: NcVisualOptions#associatedconstant.HorAligned
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
}
/// Sets the vertical alignment.
///
/// Default: *[`NcAlign::Top`]*.
///
/// Effect: Sets the *y* alignment and the [`VerAligned`] flag.
///
/// [`VerAligned`]: NcVisualFlag#associatedconstant.VerAligned
pub fn valign(mut self, valign: impl Into<NcAlign>) -> Self {
self.y = valign.into().into();
self.flags |= NcVisualFlag::VerAligned;
self
}
/// Sets the horizontal alignment.
///
/// Default: *[`NcAlign::Left`]*.
///
/// Effect: Sets the *`x`* alignment and the [`HorAligned`] flag.
///
/// [`HorAligned`]: NcVisualFlag#associatedconstant.HorAligned
pub fn halign(mut self, halign: impl Into<NcAlign>) -> Self {
self.x = halign.into().into();
self.flags |= NcVisualFlag::HorAligned;
self
}
/// Sets the vertical & horizontal alignments.
///
/// Default: *`(`[`NcAlign::Top`]*`, `*[`NcAlign::Left`]`)`*.
///
/// Effect: Sets the *`y` & `x`* alignments and the [`VerAligned`] and
/// [`HorAligned`] flags.
///
/// [`VerAligned`]: NcVisualFlag#associatedconstant.VerAligned
/// [`HorAligned`]: NcVisualFlag#associatedconstant.HorAligned
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
}
/// Choose the `NcBlitter`.
///
/// Default: *[`NcBlitter::Default`]*.
pub fn blitter(mut self, blitter: impl Into<NcBlitter>) -> Self {
self.blitter = blitter.into();
self
}
/// Choose [`NcBlitter::Pixel`] for the blitter.
pub fn pixel(mut self) -> Self {
self.blitter = NcBlitter::Pixel;
self
}
/// Choose the color to be considered transparent, or `None`.
///
/// Default: *none*.
///
/// Efect: (Un)Sets the transparent color, and the [`AddAlpha`] flag.
///
/// [`AddAlpha`]: NcVisualFlag#associatedconstant.AddAlpha
pub fn transcolor(mut self, color: Option<impl Into<NcRgba>>) -> Self {
// if color.is_none() {
if let Some(color) = color {
self.transcolor = color.into();
self.flags |= NcVisualFlag::AddAlpha;
} else {
self.flags &= !NcVisualFlag::AddAlpha;
}
self
}
/// Choose whether to use [`NcAlpha::Blend`] with the [`NcVisual`], so that
/// the foreground or background colors can be a composite between
/// a color and the corresponding colors underneath it.
///
/// Default: *false* (blends not).
///
/// Effect: Sets the [`Blend`] flag.
///
/// [`Blend`]: NcVisualFlag#associatedconstant.Blend
/// [`NcAlpha::Blend`]: crate::NcAlpha#associatedconstant.Blend
/// [`NcVisual`]: crate::NcVisual
pub fn blend(mut self, blend: bool) -> Self {
if blend {
self.flags |= NcVisualFlag::Blend;
} else {
self.flags &= !NcVisualFlag::Blend;
}
self
}
/// Choose between gracefully degrading the blitter, or fail if the choosen
/// `NcBlitter` is not supported by the terminal.
///
/// Default: *true* (degrades).
///
/// Effect: Sets the [`NoDegrade`] flag.
///
/// See also: the [*rules of degradation*].
///
/// [`NoDegrade`]: NcVisualFlag#associatedconstant.NoDegrade
/// [*rules of degradation*]: NcBlitter#degradation
pub fn degrade(mut self, degrade: bool) -> Self {
if degrade {
self.flags &= !NcVisualFlag::NoDegrade;
} else {
self.flags |= NcVisualFlag::NoDegrade;
}
self
}
/// Sets the `NoInterpolate` flag.
///
/// Default: *true* (interpolates).
///
/// [`NoInterpolate`]: NcVisualFlag#associatedconstant.NoInterpolate
pub fn interpolate(mut self, interpolate: bool) -> Self {
if interpolate {
self.flags &= !NcVisualFlag::NoInterpolate;
} else {
self.flags |= NcVisualFlag::NoInterpolate;
}
self
}
/// Sets the region to be rendered.
///
/// (start_y, start_x, len_y, len_x)
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
}
/// Sets the pixel offset within the [`NcCell`][crate::NcCell].
///
pub fn cell_offset(mut self, y: u32, x: u32) -> Self {
self.cell_offset_yx = Some((y, x));
self
}
/// Finishes the building and returns [`NcVisualOptions`].
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,
)
}
}