Commit ae9d8726 authored by Florian Blasius's avatar Florian Blasius 🤘
Browse files

[wip | extend] impl rect, image_element

parent f92516e4
Pipeline #2495 failed with stages
in 1 minute and 33 seconds
......@@ -7,7 +7,10 @@ use orbgl::Canvas;
use orbimage::Image;
use crate::{
core::{Brush, FillRule, GradientStop, ImageElement, RenderContext2D, Renderer, TextMetrics},
core::{
Brush, FillRule, GradientStop, ImageElement, Position, RenderContext2D, Renderer, Size,
TextMetrics,
},
properties::{Bounds, Point},
theme::{material_font_icons::MATERIAL_ICONS_REGULAR_FONT, ROBOTO_REGULAR_FONT},
};
......@@ -111,82 +114,109 @@ impl<'a> RenderContext2D for OrbRenderContext2D<'a> {
}
/// Draws an image on (x, y).
fn draw_image(&mut self, image_element: &ImageElement, x: f64, y: f64) {
fn draw_image(&mut self, image_element: &ImageElement) {
self.switch_context();
if !self.image_cache.contains_key(&image_element.path) {
if let Ok(image) = Image::from_path(&image_element.path) {
self.image_cache.insert(image_element.path.clone(), image);
let path = image_element.get_path();
if !self.image_cache.contains_key(path) {
if let Ok(image) = Image::from_path(path) {
self.image_cache.insert(path.to_string(), image);
}
}
if let Some(image) = self.image_cache.get(&image_element.path) {
self.orbclient_context.image_fast(
x as i32,
y as i32,
image.width(),
image.height(),
image.data(),
);
}
}
/// Draws an image on (x, y) with (width, height).
fn draw_image_d(
&mut self,
image_element: &ImageElement,
x: f64,
y: f64,
width: f64,
height: f64,
) {
self.switch_context();
if !self.image_cache.contains_key(&image_element.path) {
if let Ok(image) = Image::from_path(&image_element.path) {
self.image_cache.insert(image_element.path.clone(), image);
if let Some(image) = self.image_cache.get(path) {
let (width, height) = {
if image_element.get_width() == 0.0 && image_element.get_height() == 0.0 {
(image.width(), image.height())
} else {
(
image_element.get_width() as u32,
image_element.get_height() as u32,
)
}
};
if let Some(source_rect) = image_element.get_source_rect() {
let image_roi = image.roi(
source_rect.x as u32,
source_rect.y as u32,
source_rect.width as u32,
source_rect.height as u32,
);
image_roi.draw(
self.orbclient_context,
image_element.get_x() as i32,
image_element.get_y() as i32,
);
} else {
self.orbclient_context.image_fast(
image_element.get_x() as i32,
image_element.get_y() as i32,
width,
height,
image.data(),
);
}
}
if let Some(image) = self.image_cache.get(&image_element.path) {
self.orbclient_context.image_fast(
x as i32,
y as i32,
width as u32,
height as u32,
image.data(),
);
}
}
/// Draws a part of the image with the given (source_x, source_y, source_width, source_height) on (x, y) with (width, height).
fn draw_image_s(
&mut self,
image_element: &ImageElement,
source_x: f64,
source_y: f64,
source_width: f64,
source_height: f64,
x: f64,
y: f64,
_: f64,
_: f64,
) {
self.switch_context();
if !self.image_cache.contains_key(&image_element.path) {
if let Ok(image) = Image::from_path(&image_element.path) {
self.image_cache.insert(image_element.path.clone(), image);
}
}
if let Some(image) = self.image_cache.get(&image_element.path) {
let image_roi = image.roi(
source_x as u32,
source_y as u32,
source_width as u32,
source_height as u32,
);
image_roi.draw(self.orbclient_context, x as i32, y as i32);
}
}
// /// Draws an image on (x, y) with (width, height).
// fn draw_image_d(
// &mut self,
// image_element: &ImageElement,
// x: f64,
// y: f64,
// width: f64,
// height: f64,
// ) {
// self.switch_context();
// if !self.image_cache.contains_key(&image_element.path) {
// if let Ok(image) = Image::from_path(&image_element.path) {
// self.image_cache.insert(image_element.path.clone(), image);
// }
// }
// if let Some(image) = self.image_cache.get(&image_element.path) {
// self.orbclient_context.image_fast(
// x as i32,
// y as i32,
// width as u32,
// height as u32,
// image.data(),
// );
// }
// }
// /// Draws a part of the image with the given (source_x, source_y, source_width, source_height) on (x, y) with (width, height).
// fn draw_image_s(
// &mut self,
// image_element: &ImageElement,
// source_x: f64,
// source_y: f64,
// source_width: f64,
// source_height: f64,
// x: f64,
// y: f64,
// _: f64,
// _: f64,
// ) {
// self.switch_context();
// if !self.image_cache.contains_key(&image_element.path) {
// if let Ok(image) = Image::from_path(&image_element.path) {
// self.image_cache.insert(image_element.path.clone(), image);
// }
// }
// if let Some(image) = self.image_cache.get(&image_element.path) {
// let image_roi = image.roi(
// source_x as u32,
// source_y as u32,
// source_width as u32,
// source_height as u32,
// );
// image_roi.draw(self.orbclient_context, x as i32, y as i32);
// }
// }
/// Fills the current or given path with the current file style.
fn fill(&mut self, _: FillRule) {
......
......@@ -41,14 +41,8 @@ pub enum Instruction {
/// Erases the pixels in a rectangular area by setting them to transparent black.
ClearRect(f64, f64, f64, f64),
/// Draws an image on (x, y).
DrawImage(ImageElement, f64, f64),
/// Draws an image on (x, y) with (width, height).
DrawImageD(ImageElement, f64, f64, f64, f64),
/// Draws a part of the image with the given (source_x, source_y, source_width, source_height) on (x, y) with (width, height).
DrawImageS(ImageElement, f64, f64, f64, f64, f64, f64, f64, f64),
/// Draws an image.
DrawImage(ImageElement),
/// Fills the current or given path with the current file style.
Fill(FillRule),
......@@ -143,31 +137,7 @@ pub trait RenderContext2D {
fn close_path(&mut self);
/// Draws an image on (x, y).
fn draw_image(&mut self, image_element: &ImageElement, x: f64, y: f64);
/// Draws an image on (x, y) with (width, height).
fn draw_image_d(
&mut self,
image_element: &ImageElement,
x: f64,
y: f64,
width: f64,
height: f64,
);
/// Draws a part of the image with the given (source_x, source_y, source_width, source_height) on (x, y) with (width, height).
fn draw_image_s(
&mut self,
image_element: &ImageElement,
source_x: f64,
source_y: f64,
source_width: f64,
source_height: f64,
x: f64,
y: f64,
width: f64,
height: f64,
);
fn draw_image(&mut self, image_element: &ImageElement);
/// Fills the current or given path with the current file style.
fn fill(&mut self, fill_rule: FillRule);
......@@ -255,23 +225,7 @@ pub trait RenderContext2D {
Instruction::BezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) => self.bezier_curve_to(*cp1x, *cp1y, *cp2x, *cp2y, *x, *y),
Instruction::ClearRect(x, y, width, height) => self.clear_rect(*x, *y, *width, *height),
Instruction::ClosePath() => self.close_path(),
Instruction::DrawImage(image, x, y) => self.draw_image(image, *x, *y),
Instruction::DrawImageD(image, x, y, width, height) => {
self.draw_image_d(image, *x, *y, *width, *height)
}
Instruction::DrawImageS(
image,
s_x,
s_y,
s_width,
s_height,
x,
y,
width,
height,
) => self.draw_image_s(
image, *s_x, *s_y, *s_width, *s_height, *x, *y, *width, *height,
),
Instruction::DrawImage(image) => self.draw_image(image),
Instruction::Fill(file_rule) => self.fill(*file_rule),
Instruction::FillRect(x, y, width, height) => {
self.fill_rect(*x, *y, *width, *height)
......
pub struct Image {
}
\ No newline at end of file
use super::Instruction;
pub use self::image::Image;
pub use self::rectangle::Rectangle;
mod image;
mod rectangle;
pub trait Shape2D {
......
use crate::{core::Shape2D, Brush, FillRule, ImageElement, Instruction};
#[derive(Copy, Clone, Default, Debug, PartialEq)]
pub struct Thickness {
pub left: f64,
pub top: f64,
pub right: f64,
pub bottom: f64,
}
#[derive(Copy, Clone, Default, Debug, PartialEq)]
pub struct Rect {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
}
#[derive(Clone, Default, Debug, PartialEq)]
pub struct Border {
pub brush: Brush,
pub thickness: Thickness,
}
use crate::{
core::{Border, Rect, Shape2D},
Brush, FillRule, ImageElement, ImageElementBuilder, Instruction
};
#[derive(Default)]
pub struct RectangleBuilder {
pub background: Brush,
pub rect: Rect,
pub border: Border,
pub radius: f64,
}
impl RectangleBuilder {
......@@ -49,6 +31,11 @@ impl RectangleBuilder {
self
}
pub fn with_radius(mut self, radius: f64) -> Self {
self.radius = radius;
self
}
pub fn build(self) -> Rectangle {
Rectangle::new()
}
......@@ -64,23 +51,19 @@ impl Rectangle {
Rectangle {
instructions: vec![
Instruction::DrawImage(
ImageElement {
path: "res/orbtk-space.png".to_string(),
},
10.0,
10.0,
ImageElementBuilder::new("res/orbtk-space.png".to_string()).with_position( 10.0,
10.0).build()
),
Instruction::SetFillStyleBrush(Brush::from("#6195ED")),
Instruction::FillRect(10.0, 10.0, 100.0, 200.0),
Instruction::DrawImage(
ImageElement {
path: "res/orbtk-space.png".to_string(),
},
60.0,
30.0,
),
Instruction::SetFillStyleBrush(Brush::from("#80ED61")),
Instruction::FillRect(20.0, 20.0, 80.0, 180.0),
Instruction::DrawImage(
ImageElementBuilder::new("res/orbtk-space.png".to_string()).with_position( 60.0,
60.0).with_source_rect(50.0, 50.0, 150.0, 150.0).build()
),
],
}
}
......
use super::{Brush, Thickness};
#[derive(Clone, Default, Debug, PartialEq)]
pub struct Border {
pub brush: Brush,
pub thickness: Thickness,
}
\ No newline at end of file
#[derive(Clone, PartialEq, Debug)]
use crate::core::{Position, Rect, Size};
pub struct ImageElementBuilder {
pub path: String,
pub rect: Rect,
pub source_rect: Option<Rect>,
}
impl ImageElementBuilder {
pub fn new<S: Into<String>>(path: S) -> Self {
ImageElementBuilder {
path: path.into(),
rect: Rect::default(),
source_rect: None,
}
}
pub fn with_position(mut self, x: f64, y: f64) -> Self {
self.rect.x = x;
self.rect.y = y;
self
}
pub fn with_size(mut self, width: f64, height: f64) -> Self {
self.rect.width = width;
self.rect.height = height;
self
}
pub fn with_rect(mut self, x: f64, y: f64, width: f64, height: f64) -> Self {
self.with_position(x, y).with_size(width, height)
}
pub fn with_source_rect(
mut self,
source_x: f64,
source_y: f64,
source_width: f64,
source_height: f64,
) -> Self {
self.source_rect = Some(Rect {
x: source_x,
y: source_y,
width: source_width,
height: source_height,
});
self
}
pub fn build(self) -> ImageElement {
ImageElement {
path: self.path,
rect: self.rect,
source_rect: self.source_rect,
}
}
}
#[derive(Clone, PartialEq, Debug, Default)]
pub struct ImageElement {
pub path: String
}
\ No newline at end of file
path: String,
rect: Rect,
source_rect: Option<Rect>,
}
impl ImageElement {
pub fn set_path<S: Into<String>>(&mut self, path: S) {
self.path = path.into();
}
pub fn get_path(&self) -> &str {
&self.path
}
pub fn set_source_rect(&mut self, source_rect: Rect) {
self.source_rect = Some(source_rect);
}
pub fn get_source_rect(&self) -> &Option<Rect> {
&self.source_rect
}
}
impl Size for ImageElement {
fn set_with(&mut self, width: f64) {
self.rect.width = width;
}
fn get_width(&self) -> f64 {
self.rect.height
}
fn set_height(&mut self, height: f64) {
self.rect.height = height;
}
fn get_height(&self) -> f64 {
self.rect.height
}
fn set_size(&mut self, width: f64, height: f64) {
self.rect.width = width;
self.rect.height = height;
}
fn get_size(&self) -> (f64, f64) {
(self.rect.width, self.rect.height)
}
}
impl Position for ImageElement {
fn set_with(&mut self, x: f64) {
self.rect.x = x;
}
fn get_x(&self) -> f64 {
self.rect.y
}
fn set_y(&mut self, y: f64) {
self.rect.y = y;
}
fn get_y(&self) -> f64 {
self.rect.y
}
fn set_position(&mut self, x: f64, y: f64) {
self.rect.x = x;
self.rect.y = y;
}
fn get_position(&self) -> (f64, f64) {
(self.rect.x, self.rect.y)
}
}
pub use self::border::Border;
pub use self::brush::{Brush, GradientStop};
pub use self::font::TextMetrics;
pub use self::image_element::ImageElement;
pub use self::image_element::{ImageElement, ImageElementBuilder};
pub use self::rect::{Rect, Size, Position};
pub use self::thickness::Thickness;
mod border;
mod brush;
mod font;
mod image_element;
\ No newline at end of file
mod image_element;
mod rect;
mod thickness;
\ No newline at end of file
#[derive(Copy, Clone, Default, Debug, PartialEq)]
pub struct Rect {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
}
pub trait Size {
fn set_with(&mut self, width: f64);
fn get_width(&self) -> f64;
fn set_height(&mut self, height: f64);
fn get_height(&self) -> f64;
fn set_size(&mut self, width: f64, height: f64);
fn get_size(&self) -> (f64, f64);
}
pub trait Position {
fn set_with(&mut self, x: f64);
fn get_x(&self) -> f64;
fn set_y(&mut self, y: f64);
fn get_y(&self) -> f64;
fn set_position(&mut self, x: f64, y: f64);
fn get_position(&self) -> (f64, f64);
}
#[derive(Copy, Clone, Default, Debug, PartialEq)]
pub struct Thickness {
pub left: f64,
pub top: f64,
pub right: f64,
pub bottom: f64,
}
\ No newline at end of file
Supports Markdown
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