From 29ac41440c9bc35dc2029d8e8e3de2ec6df3e934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?joseLu=C3=ADs?= Date: Tue, 6 Apr 2021 19:28:58 +0200 Subject: [PATCH] rust: add NcPlane methods resize_maximize & rgba; fix #1508 --- rust/src/plane/methods.rs | 84 ++++++++++++++++++++++++++++++++++++--- rust/src/plane/mod.rs | 6 +-- 2 files changed, 82 insertions(+), 8 deletions(-) diff --git a/rust/src/plane/methods.rs b/rust/src/plane/methods.rs index 62831e3fb..a7ec62071 100644 --- a/rust/src/plane/methods.rs +++ b/rust/src/plane/methods.rs @@ -1,12 +1,15 @@ //! `NcPlane*` methods and associated functions. -use core::ptr::{null, null_mut}; +use core::{ + ptr::{null, null_mut}, + slice::from_raw_parts_mut, +}; use crate::{ - cstring, error, error_ref, error_ref_mut, rstring, NcAlign, NcAlphaBits, NcBoxMask, NcCell, - NcChannel, NcChannelPair, NcColor, NcDim, NcEgc, NcError, NcFadeCb, NcOffset, NcPaletteIndex, - NcPlane, NcPlaneOptions, NcResizeCb, NcResult, NcRgb, NcStyleMask, NcTime, Notcurses, - NCRESULT_ERR, + cstring, error, error_ref, error_ref_mut, rstring, NcAlign, NcAlphaBits, NcBlitter, NcBoxMask, + NcCell, NcChannel, NcChannelPair, NcColor, NcDim, NcEgc, NcError, NcFadeCb, NcOffset, + NcPaletteIndex, NcPlane, NcPlaneOptions, NcResizeCb, NcResult, NcRgb, NcStyleMask, NcTime, + Notcurses, NCRESULT_ERR, }; /// # NcPlaneOptions Constructors @@ -1342,6 +1345,77 @@ impl NcPlane { ] } + /// Suitable for use as a 'resizecb', this will resize the plane + /// to the visual region's size. It is used for the standard plane. + /// + /// *C style function: [ncplane_resize_maximize()][crate::ncplane_resize_maximize].* + pub fn resize_maximize(&mut self) -> NcResult<()> { + error![ + unsafe { crate::ncplane_resize_maximize(self) }, + "NcPlane.resize_maximize()" + ] + } + + /// Creates an RGBA flat array from the selected region of the ncplane. + /// + /// Starts at the plane's `beg_y`x`beg_x` coordinate (which must lie on the + /// plane), continuing for `len_y`x`len_x` cells. + /// + /// Use `None` for either or both of `len_y` and `len_x` in order to + /// go through the boundary of the plane in that axis. + /// + /// Only glyphs from the specified blitset may be present. + /// + /// *C style function: [ncplane_rgba()][crate::ncplane_rgba].* + pub fn rgba( + &mut self, + blitter: NcBlitter, + beg_y: NcDim, + beg_x: NcDim, + len_y: Option, + len_x: Option, + ) -> NcResult<&mut [u32]> { + // converts length arguments to expected format + let len_y2: i32; + let len_x2: i32; + if let Some(y) = len_y { + len_y2 = y as i32; + } else { + len_y2 = -1; + } + if let Some(x) = len_x { + len_x2 = x as i32; + } else { + len_x2 = -1; + } + + let res_array = unsafe { + crate::ncplane_rgba(self, blitter, beg_y as i32, beg_x as i32, len_y2, len_x2) + }; + + // calculates array length + let array_len_y; + let array_len_x; + if len_y2 == -1 { + array_len_y = self.dim_y() - beg_y; + } else { + array_len_y = len_y2 as u32; + } + if len_x2 == -1 { + array_len_x = self.dim_x() - beg_x; + } else { + array_len_x = len_x2 as u32; + } + let array_len = (array_len_y * array_len_x) as usize; + + // returns the result + if res_array != null_mut() { + return Ok(unsafe { from_raw_parts_mut(res_array, array_len) }); + } else { + Err(NcError::with_msg(NCRESULT_ERR, "NcPlane.rgba()")) + } + } + /// Realigns this NcPlane against its parent, using the alignment specified /// at creation time. /// diff --git a/rust/src/plane/mod.rs b/rust/src/plane/mod.rs index f70bc19fb..3d81bb746 100644 --- a/rust/src/plane/mod.rs +++ b/rust/src/plane/mod.rs @@ -5,7 +5,7 @@ // (X) wont: 6 // (D) depr: 4 // (#) test: 13 -// (W) wrap: 79 of 98 +// (W) wrap: 81 of 98 // ------------------------------------------- //W ncpile_bottom //W# ncpile_create @@ -74,9 +74,9 @@ //W ncplane_reparent_family //W# ncplane_resize //W ncplane_resizecb -// ncplane_resize_maximize +//W ncplane_resize_maximize //W ncplane_resize_realign -// ncplane_rgba +//W ncplane_rgba //W ncplane_rotate_ccw //W ncplane_rotate_cw //W ncplane_set_base