Commit 385fc18e authored by Florian Blasius's avatar Florian Blasius 🤘
Browse files

[documentation] core

parent 00575be9
// This submodule contians render related stuff.
use orbgl::Canvas;
use crate::theme::Theme;
......@@ -10,6 +12,7 @@ mod shapes;
mod structs;
mod renderer;
/// Provides data for rendering like renderer and theme.
pub struct RenderContext<'a> {
pub renderer: &'a mut Canvas,
pub theme: &'a Theme,
......
......@@ -2,14 +2,18 @@ use orbgl::{Canvas, Color};
use crate::core::{Brush, PathSegment, Shape};
/// Used to extends a render context and to make it easier to render a shape.
pub trait Renderer {
/// Renders the given shape.
fn render_shape(&mut self, shape: &mut Shape) {
self.render_path(shape.path());
}
/// Renders the given path.
fn render_path(&mut self, path: &mut [PathSegment]);
}
// Default Render implementation of OrbGL Canvas.
impl Renderer for Canvas {
fn render_path(&mut self, path: &mut [PathSegment]) {
for instruction in path {
......@@ -130,6 +134,7 @@ impl Renderer for Canvas {
}
}
// Helper to convert a color.
fn get_color(hex: &str) -> Color {
let clean_hex = hex.trim_start_matches("#");
match clean_hex.len() {
......
......@@ -20,7 +20,7 @@ impl ImageElementBuilder {
}
}
/// Inserts a new position:
/// Inserts a new position.
pub fn with_position(mut self, x: f64, y: f64) -> Self {
self.rect.x = x;
self.rect.y = y;
......
// This private sub module contains a list of shapes.
pub use self::image_element::{ImageElement, ImageElementBuilder};
pub use self::rectangle::{Rectangle, RectangleBuilder};
......
......@@ -11,26 +11,30 @@ pub struct RectangleBuilder {
}
impl RectangleBuilder {
/// Creates a new rectangle bilder with.
/// Creates a new `RectangleBuilder` with default values.
pub fn new() -> Self {
RectangleBuilder::default()
}
/// Inserts a new background brush.
pub fn with_background(mut self, background: Brush) -> Self {
self.background = background;
self
}
/// Inserts a new bounds rect.
pub fn with_rect(mut self, rect: Rect) -> Self {
self.rect = rect;
self
}
/// Inserts a new border.
pub fn with_border(mut self, border: Border) -> Self {
self.border = border;
self
}
/// Builds the rectangle.
pub fn build(self) -> Rectangle {
let mut rect = Rectangle {
path: vec![],
......@@ -43,6 +47,7 @@ impl RectangleBuilder {
}
}
/// The ÌmageElement` is used to display a rectangle on the screen.
#[derive(Default)]
pub struct Rectangle {
path: Vec<PathSegment>,
......@@ -52,14 +57,17 @@ pub struct Rectangle {
}
impl Rectangle {
/// Creates a new `RectangleBuilder` object with default values.
pub fn create() -> RectangleBuilder {
RectangleBuilder::default()
RectangleBuilder::new()
}
/// Gets the background brush.
pub fn background(&self) -> &Brush {
&self.background
}
/// Sets the background brush.
pub fn set_background(&mut self, background: Brush) {
self.background = background;
}
......@@ -84,15 +92,15 @@ impl Rectangle {
self.rect.y,
self.rect.width,
self.rect.height,
self.border.brush.clone(),
self.border.brush().clone(),
);
// content
self.build_rect_path(
self.rect.x + self.border.thickness.left,
self.rect.y + self.border.thickness.top,
self.rect.width - self.border.thickness.left - self.border.thickness.right,
self.rect.height - self.border.thickness.top - self.border.thickness.right,
self.rect.x + self.border.thickness().left,
self.rect.y + self.border.thickness().top,
self.rect.width - self.border.thickness().left - self.border.thickness().right,
self.rect.height - self.border.thickness().top - self.border.thickness().right,
self.background.clone(),
);
}
......@@ -154,17 +162,17 @@ impl Rectangle {
self.rect.y,
self.rect.width,
self.rect.height,
self.border.radius,
self.border.brush.clone(),
self.border.radius(),
self.border.brush().clone(),
);
// content
self.build_rounded_rect_path(
self.rect.x + self.border.thickness.left,
self.rect.y + self.border.thickness.top,
self.rect.width - self.border.thickness.left - self.border.thickness.right,
self.rect.height - self.border.thickness.top - self.border.thickness.right,
self.border.radius,
self.rect.x + self.border.thickness().left,
self.rect.y + self.border.thickness().top,
self.rect.width - self.border.thickness().left - self.border.thickness().right,
self.rect.height - self.border.thickness().top - self.border.thickness().right,
self.border.radius(),
self.background.clone(),
);
}
......@@ -177,12 +185,12 @@ impl Shape for Rectangle {
fn build_path(&mut self) {
self.path.clear();
let has_thickness = self.border.thickness.left > 0.0
|| self.border.thickness.top > 0.0
|| self.border.thickness.right > 0.0
|| self.border.thickness.bottom > 0.0;
let has_thickness = self.border.thickness().left > 0.0
|| self.border.thickness().top > 0.0
|| self.border.thickness().right > 0.0
|| self.border.thickness().bottom > 0.0;
if self.border.radius > 0.0 {
if self.border.radius() > 0.0 {
if has_thickness {
self.build_rounded_bordered_rect_path();
} else {
......@@ -191,7 +199,7 @@ impl Shape for Rectangle {
self.rect.y,
self.rect.width,
self.rect.height,
self.border.radius,
self.border.radius(),
self.background.clone(),
);
}
......@@ -267,27 +275,27 @@ impl Position for Rectangle {
impl Bordered for Rectangle {
fn border_thickness(&self) -> &Thickness {
&self.border.thickness
&self.border.thickness()
}
fn set_border_thickness(&mut self, thickness: Thickness) {
self.border.thickness = thickness;
self.border.set_thickness(thickness);
}
fn border_brush(&self) -> &Brush {
&self.border.brush
&self.border.brush()
}
fn set_border_brush(&mut self, brush: Brush) {
self.border.brush = brush;
self.border.set_brush(brush);
}
fn border_radius(&self) -> f64 {
self.border.radius
self.border.radius()
}
fn set_border_radius(&mut self, radius: f64) {
self.border.radius = radius;
self.border.set_radius(radius);
}
fn border(&self) -> &Border {
......
use super::{Brush, Thickness};
pub trait Bordered {
fn border_thickness(&self) -> &Thickness;
fn set_border_thickness(&mut self, thickness: Thickness);
fn border_brush(&self) -> &Brush;
fn set_border_brush(&mut self, brush: Brush);
fn border_radius(&self) -> f64;
fn set_border_radius(&mut self, radius: f64);
fn border(&self) -> &Border;
fn set_border(&mut self, border: Border);
}
/// Used to build a border, specifying additional details.
#[derive(Default)]
pub struct BorderBuilder {
brush: Brush,
......@@ -20,25 +9,30 @@ pub struct BorderBuilder {
}
impl BorderBuilder {
/// Creates a new border bilder with default values.
pub fn new() -> BorderBuilder {
BorderBuilder::default()
}
/// Inserts a border brsuh.
pub fn with_brush(mut self, brush: Brush) -> Self {
self.brush = brush;
self
}
/// Inserts a border thickness.
pub fn with_thickness(mut self, thickness: Thickness) -> Self {
self.thickness = thickness;
self
}
/// Inserts a border radius.
pub fn with_radius(mut self, radius: f64) -> Self {
self.radius = radius;
self
}
/// Builds the border.
pub fn build(self) -> Border {
Border {
brush: self.brush,
......@@ -48,20 +42,74 @@ impl BorderBuilder {
}
}
/// Describes a border of a shape with border `brush`, `thickness` and `radius`.
#[derive(Clone, Default, Debug, PartialEq)]
pub struct Border {
pub brush: Brush,
pub thickness: Thickness,
pub radius: f64
brush: Brush,
thickness: Thickness,
radius: f64
}
impl Border {
pub fn new(brush: Brush, thickness: Thickness, radius: f64) -> Self {
Border {
brush,
thickness,
radius,
}
/// Creates a new `BorderBuilder` with default values.
pub fn create() -> BorderBuilder {
BorderBuilder::new()
}
/// Gets the border brush.
pub fn brush(&self) -> &Brush {
&self.brush
}
/// Sets the border brush.
pub fn set_brush(&mut self, brush: Brush) {
self.brush = brush;
}
/// Gets the thickness.
pub fn thickness(&self) -> &Thickness {
&self.thickness
}
/// Sets the thickness.
pub fn set_thickness(&mut self, thickness: Thickness) {
self.thickness = thickness;
}
/// Gets the radius.
pub fn radius(&self) -> f64 {
self.radius
}
/// Sets the radius.
pub fn set_radius(&mut self, radius: f64) {
self.radius = radius
}
}
/// Contains a set of getters and setters to read and write to a border.
pub trait Bordered {
/// Gets the thickness.
fn border_thickness(&self) -> &Thickness;
/// Sets the border thickness.
fn set_border_thickness(&mut self, thickness: Thickness);
/// Gets the border brush.
fn border_brush(&self) -> &Brush;
/// Sets the border brush.
fn set_border_brush(&mut self, brush: Brush);
/// Gets the border radius.
fn border_radius(&self) -> f64;
/// Sets the border radius.
fn set_border_radius(&mut self, radius: f64);
/// Gets the complete border.
fn border(&self) -> &Border;
/// Sets the complete border.
fn set_border(&mut self, border: Border);
}
\ No newline at end of file
/// Describes a position on a coloerfull gradient.
#[derive(Default, Clone, PartialEq, Debug)]
pub struct GradientStop {
pub position: f64,
pub color: String,
}
// todo: Color struct
// impl Default for Gradient {
// fn default() -> Self {
// Gradient {
// color_stops: BTreeMap::new()
// }
// }
// }
// impl Gradient {
// pub fn new() -> Self {
// Gradient::default()
// }
// pub fn add_color_stop(&mut self)
// }
/// A `Brush`describes how a shape is filled or stroked.
#[derive(Clone, PartialEq, Debug)]
pub enum Brush {
/// Paints an area with a solid color.
SolidColor(String),
/// Paints an area with a linear gradient.
Gradient(Vec<GradientStop>),
}
......
// This private submodule contains a set of basic structs used for rendering and shapes.
pub use self::border::{Border, BorderBuilder, Bordered};
pub use self::brush::{Brush, GradientStop};
pub use self::font::TextMetrics;
......
/// Describes a new visual rectangle.
#[derive(Copy, Clone, Default, Debug, PartialEq)]
pub struct Rect {
//// X position of the rectangle.
pub x: f64,
/// Y position of the rectangle.
pub y: f64,
/// Width of the rectangle.
pub width: f64,
/// Height of the rectangle.
pub height: f64,
}
impl Rect {
/// Create a new rectangle with the given parameters.
pub fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
Rect {
x,
y,
y,
width,
height,
}
}
}
/// Contains a set of getters and setters to read and write x and y.
pub trait Position {
/// Gets x.
fn x(&self) -> f64;
/// Sets x.
fn set_x(&mut self, x: f64);
/// Gets y.
fn y(&self) -> f64;
/// Sets y.
fn set_y(&mut self, y: f64);
/// Gets position with x and y.
fn position(&self) -> (f64, f64);
/// Sets position with x and y.
fn set_position(&mut self, x: f64, y: f64);
}
/// Contains a set of getters and setters to read and write with and height.
pub trait Size {
fn set_with(&mut self, width: f64);
/// Gets the width.
fn width(&self) -> f64;
fn set_height(&mut self, height: f64);
/// Sets the width.
fn set_with(&mut self, width: f64);
/// Gets the height.
fn height(&self) -> f64;
fn set_size(&mut self, width: f64, height: f64);
/// Sets the height.
fn set_height(&mut self, height: f64);
/// Gets the size with width and height.
fn size(&self) -> (f64, f64);
}
pub trait Position {
fn set_x(&mut self, x: f64);
fn x(&self) -> f64;
fn set_y(&mut self, y: f64);
fn y(&self) -> f64;
fn set_position(&mut self, x: f64, y: f64);
fn position(&self) -> (f64, f64);
/// Sets the size with width and height.
fn set_size(&mut self, width: f64, height: f64);
}
/// Used to describes a thickness e.g a border thickness.
#[derive(Copy, Clone, Default, Debug, PartialEq)]
pub struct Thickness {
/// Left of thickness.
pub left: f64,
/// Top of thickness.
pub top: f64,
/// Right of thickness.
pub right: f64,
/// Bottom of thickness.
pub bottom: f64,
}
impl Thickness {
/// Create a new thickness with the given parameters.
pub fn new(left: f64, top: f64, right: f64, bottom: f64) -> Self {
Thickness {
left,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment