Add drawing functionality to new ui

This commit is contained in:
2024-04-28 17:57:40 -05:00
parent f7490d00c3
commit b9f1424b54
2 changed files with 311 additions and 81 deletions

View File

@@ -1,4 +1,3 @@
use crate::wayland::pixel::{Pixel, Box, BoxMut, PixelEncoding};
use raqote::{DrawTarget, PathBuilder, Source, StrokeStyle, DrawOptions, SolidSource, Color};
struct UIColor {
@@ -72,11 +71,77 @@ impl<'a, 'b> Window<'a, 'b> {
}
}
#[derive(Clone, Copy, Debug)]
pub(super) struct Position {
pub(super) x: f32,
pub(super) y: f32,
}
pub(super) struct Path(Vec<Position>);
impl IntoIterator for Path {
type Item = Position;
type IntoIter = std::vec::IntoIter<Position>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a> IntoIterator for &'a Path {
type Item = &'a Position;
type IntoIter = std::slice::Iter<'a, Position>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
pub(super) struct DrawPath {
pub(super) paths: Vec<Path>,
pub(super) newline: bool,
}
impl DrawPath {
pub(super) fn new() -> Self {
Self {
paths: Vec::new(),
newline: false,
}
}
pub(super) fn add_point(&mut self, point: Position, newline: bool) {
if newline {
self.paths.push(Path(vec![point]))
} else if self.paths.len() > 0 {
self.paths
.last_mut()
.unwrap()
.0
.push(point);
}
}
}
impl IntoIterator for DrawPath {
type Item = Path;
type IntoIter = std::vec::IntoIter<Path>;
fn into_iter(self) -> Self::IntoIter {
self.paths.into_iter()
}
}
impl<'a> IntoIterator for &'a DrawPath {
type Item = &'a Path;
type IntoIter = std::slice::Iter<'a, Path>;
fn into_iter(self) -> Self::IntoIter {
self.paths.iter()
}
}
impl Position {
pub(super) fn new(x: f32, y: f32) -> Self {
Position { x, y }
@@ -84,40 +149,60 @@ impl Position {
}
pub(super) enum Widget<'a> {
Button(Button<'a>),
Button(Button),
DrawArea(DrawArea<'a>),
TextArea(TextArea<'a>)
TextArea(TextArea)
}
pub(super) trait Draw {
fn render(&self, area: &mut DrawTarget<&mut [u32]>);
}
pub(super) struct Button<'a> {
pub(super) struct ButtonColors {
background: Color,
border: Color,
glyph: Color,
}
impl Default for ButtonColors {
fn default() -> Self {
Self {
background: Color::new(0xff, 0x25, 0x2c, 0x33),
border: Color::new(0xff, 0x77, 0x7f, 0x97),
glyph: Color::new(0xff, 0xea, 0xea, 0xe6),
}
}
}
pub(super) struct Button {
pub(super) width: f32,
pub(super) height: f32,
pub(super) position: Position,
pub(super) source: Source<'a>,
pub(super) colors: ButtonColors,
pub(super) options: DrawOptions,
}
pub(super) struct ButtonBuilder<'a> {
impl Button {
fn background_source(&self) -> Source {
Source::Solid(self.colors.background.into())
}
}
pub(super) struct ButtonBuilder {
pub(super) width: Option<f32>,
pub(super) height: Option<f32>,
pub(super) position: Option<Position>,
pub(super) source: Option<Source<'a>>,
pub(super) color: Option<Color>,
pub(super) colors: Option<ButtonColors>,
pub(super) options: Option<DrawOptions>,
}
impl<'a> ButtonBuilder<'a> {
impl<'a> ButtonBuilder {
pub(super) fn new() -> Self {
Self {
width: None,
height: None,
position: None,
source: None,
color: None,
colors: None,
options: None,
}
}
@@ -134,12 +219,8 @@ impl<'a> ButtonBuilder<'a> {
self.position = Some(Position::new(x, y))
}
pub(super) fn source(&mut self, source: Source<'a>) {
self.source = Some(source);
}
pub(super) fn color(&mut self, r: u8, g: u8, b: u8) {
self.color = Some(Color::new(0xff, r, g, b))
pub(super) fn colors(&mut self, colors: ButtonColors) {
self.colors = Some(colors)
}
pub(super) fn finish(self) -> Widget<'a> {
@@ -148,18 +229,14 @@ impl<'a> ButtonBuilder<'a> {
width: self.width.expect("Button must have width"),
height: self.height.expect("Button must have height"),
position: self.position.expect("Button must have a position"),
source: self.source.unwrap_or(
Source::Solid(
self.color.unwrap_or(Color::new(0xff, 0x00, 0x00, 0x00)).into()
)
),
colors: self.colors.unwrap_or_default(),
options: self.options.unwrap_or(DrawOptions::new()),
}
)
}
}
impl<'a> Draw for Button<'a> {
impl Draw for Button {
fn render(&self, area: &mut DrawTarget<&mut [u32]>) {
(*area).fill_rect(
@@ -167,26 +244,58 @@ impl<'a> Draw for Button<'a> {
self.position.y,
self.width,
self.height,
&self.source,
&self.background_source(),
&self.options,
)
}
}
pub(super) struct DrawAreaColors {
background_color: Color,
drawing_color: Color,
border_color: Color,
}
impl Default for DrawAreaColors {
fn default() -> Self {
Self {
background_color: Color::new(0xff, 0x4a, 0x4e, 0x64),
drawing_color: Color::new(0xff, 0xea, 0xea, 0xe6),
border_color: Color::new(0xff, 0x77, 0x7f, 0x97),
}
}
}
pub(super) struct DrawArea<'a> {
pub(super) width: f32,
pub(super) height: f32,
pub(super) position: Position,
pub(super) source: Source<'a>,
pub(super) path: &'a DrawPath,
pub(super) colors: DrawAreaColors,
pub(super) options: DrawOptions,
}
impl DrawArea<'_> {
fn background_source(&self) -> Source {
Source::Solid(self.colors.background_color.into())
}
fn drawing_source(&self) -> Source {
Source::Solid(self.colors.drawing_color.into())
}
fn border_source(&self) -> Source {
Source::Solid(self.colors.border_color.into())
}
}
pub(super) struct DrawAreaBuilder<'a> {
pub(super) width: Option<f32>,
pub(super) height: Option<f32>,
pub(super) position: Option<Position>,
pub(super) source: Option<Source<'a>>,
pub(super) color: Option<Color>,
pub(super) path: Option<&'a DrawPath>,
pub(super) colors: Option<DrawAreaColors>,
pub(super) options: Option<DrawOptions>,
}
@@ -196,8 +305,8 @@ impl<'a> DrawAreaBuilder<'a> {
width: None,
height: None,
position: None,
source: None,
color: None,
path: None,
colors: None,
options: None,
}
}
@@ -214,12 +323,12 @@ impl<'a> DrawAreaBuilder<'a> {
self.position = Some(Position::new(x, y))
}
pub(super) fn source(&mut self, source: Source<'a>) {
self.source = Some(source);
pub(super) fn colors(&mut self, colors: DrawAreaColors) {
self.colors = Some(colors)
}
pub(super) fn color(&mut self, r: u8, g: u8, b: u8) {
self.color = Some(Color::new(0xff, r, g, b))
pub(super) fn path(&mut self, path: &'a DrawPath) {
self.path = Some(path);
}
pub(super) fn finish(self) -> Widget<'a> {
@@ -228,56 +337,104 @@ impl<'a> DrawAreaBuilder<'a> {
width: self.width.expect("Draw area must have width"),
height: self.height.expect("Draw area must have height"),
position: self.position.expect("Draw area must have a position"),
source: self.source.unwrap_or(
Source::Solid(
self.color.unwrap_or(Color::new(0xff, 0x00, 0x00, 0x00)).into()
)
),
path: self.path.expect("Draw must have a path"),
colors: self.colors.unwrap_or_default(),
options: self.options.unwrap_or(DrawOptions::new()),
}
)
}
}
impl<'a> Draw for DrawArea<'a> {
impl Draw for DrawArea<'_> {
fn render(&self, area: &mut DrawTarget<&mut [u32]>) {
let mut draw_path = PathBuilder::new();
for path in self.path.into_iter() {
if path.0.len() > 1 {
let mut path = path.into_iter();
let first_point = path
.by_ref()
.next();
draw_path.move_to(first_point.unwrap().x, first_point.unwrap().y);
for point in path {
draw_path.line_to(point.x, point.y);
}
}
}
let path = draw_path.finish();
area.fill_rect(
self.position.x,
self.position.y,
self.width,
self.height,
&self.source,
&self.background_source(),
&self.options,
)
);
area.stroke(
&path,
&self.drawing_source(),
&StrokeStyle {width: 3.0, ..StrokeStyle::default()},
&self.options,
);
}
}
pub(super) struct TextArea<'a> {
pub(super) struct TextAreaColors {
pub(super) background: Color,
pub(super) text: Color,
}
impl Default for TextAreaColors {
fn default() -> Self {
Self {
background: Color::new(0xff, 0x25, 0x2c, 0x33),
text: Color::new(0xff, 0xfc, 0xf8, 0xef),
}
}
}
pub(super) struct TextArea {
pub(super) width: f32,
pub(super) height: f32,
pub(super) position: Position,
pub(super) source: Source<'a>,
pub(super) colors: TextAreaColors,
pub(super) options: DrawOptions,
}
pub(super) struct TextAreaBuilder<'a> {
impl TextArea {
pub(super) fn text_source(&self) -> Source {
Source::Solid(self.colors.text.into())
}
pub(super) fn background_source(&self) -> Source {
Source::Solid(self.colors.background.into())
}
}
pub(super) struct TextAreaBuilder {
pub(super) width: Option<f32>,
pub(super) height: Option<f32>,
pub(super) position: Option<Position>,
pub(super) source: Option<Source<'a>>,
pub(super) color: Option<Color>,
pub(super) colors: Option<TextAreaColors>,
pub(super) options: Option<DrawOptions>,
}
impl<'a> TextAreaBuilder<'a> {
impl<'a> TextAreaBuilder {
pub(super) fn new() -> Self {
Self {
width: None,
height: None,
position: None,
source: None,
color: None,
colors: None,
options: None,
}
}
@@ -294,12 +451,9 @@ impl<'a> TextAreaBuilder<'a> {
self.position = Some(Position::new(x, y))
}
pub(super) fn source(&mut self, source: Source<'a>) {
self.source = Some(source);
}
pub(super) fn color(&mut self, r: u8, g: u8, b: u8) {
self.color = Some(Color::new(0xff, r, g, b))
pub(super) fn colors(&mut self, colors: TextAreaColors) {
self.colors = Some(colors);
}
pub(super) fn finish(self) -> Widget<'a> {
@@ -308,28 +462,27 @@ impl<'a> TextAreaBuilder<'a> {
width: self.width.expect("Button must have width"),
height: self.height.expect("Button must have height"),
position: self.position.expect("Button must have a position"),
source: self.source.unwrap_or(
Source::Solid(
self.color.unwrap_or(Color::new(0xff, 0x00, 0x00, 0x00)).into()
)
),
colors: self.colors.unwrap_or_default(),
options: self.options.unwrap_or(DrawOptions::new()),
}
)
}
}
impl<'a> Draw for TextArea<'a> {
impl Draw for TextArea {
fn render(&self, area: &mut DrawTarget<&mut [u32]>) {
(*area).fill_rect(
area.fill_rect(
self.position.x,
self.position.y,
self.width,
self.height,
&self.source,
&self.background_source(),
&self.options,
)
);
}
}