Rectangles

Rectangles are the most basic shape you can draw on a surface. They are defined by a position and a size. The position is the top-left corner of the rectangle, and the size is the width and height of the rectangle.

In AppCUI a rectangle is defined based on the following structure:

#![allow(unused)]
fn main() {
#[derive(Copy, Clone, Debug)]
pub struct Rect {
    left: i32,
    top: i32,
    right: i32,
    bottom: i32,
}
}

A rectangle can be created using the following methods:

  1. Rect::new(left, top, right, bottom) - creates a new rectangle based on the provided coordinates.
  2. Rect::with_size(left, top, width, height) - creates a new rectangle based on the provided position and size.
  3. Rect::with_alignament(x, y, width, height, align) - creates a new rectangle based on the provided position, size and alignment.
  4. Rect::with_point_and_size(point, size) - creates a new rectangle based on the provided point and size.

The alignament in the third method is defined as follows:

#![allow(unused)]
fn main() {
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Alignament {
    TopLeft = 0,
    Top,
    TopRight,
    Right,
    BottomRight,
    Bottom,
    BottomLeft,
    Left,
    Center,
}
}
AlignamentDecriptionPreview
TopLeft(X,Y) represents the top-left corner of the rectangleTopLeft
Top(X,Y) represents the top-center of the rectangleTop
TopRight(X,Y) represents the top-right corner of the rectangleTopRight
Right(X,Y) represents the right-center of the rectangleRight
BottomRight(X,Y) represents the bottom-right corner of the rectangleBottomRight
Bottom(X,Y) represents the bottom-center of the rectangleBottom
BottomLeft(X,Y) represents the bottom-left corner of the rectangleBottomLeft
Left(X,Y) represents the left-center of the rectangleLeft
Center(X,Y) represents the center of the rectangleCenter

To draw a rectangle on a surface, you can use the following methods:

MethodDescription
draw_rect(...)Draws a rectangle on the surface by providing a Rect object, a line type and a character attribute.
fill_rect(...)Fills a rectangle on the surface by providing a Rect object and a character attribute.

Example:

#![allow(unused)]
fn main() {
use appcui::graphics::*;

let mut surface = Surface::new(100, 50);
let r = Rect::new(10, 10, 20, 20);
// fill the rectangel with spaces (dark blue background)
surface.fill_rect(r, Character::new(' ', Color::White, Color::DarkBlue, CharFlags::None));
// draw a border around the rectangle (white on black)
surface.draw_rect(r, LineType::Single, CharAttribute::with_color(Color::White, Color::Black));
}