-
-
Notifications
You must be signed in to change notification settings - Fork 655
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - replace painter by jpeg and bar components
- Loading branch information
Showing
7 changed files
with
184 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::ui::{ | ||
component::{Component, Event, EventCtx, Never}, | ||
display, | ||
display::Color, | ||
geometry::Rect, | ||
shape, | ||
shape::Renderer, | ||
}; | ||
|
||
pub struct Bar { | ||
area: Rect, | ||
color: Color, | ||
bg_color: Color, | ||
radius: i16, | ||
} | ||
|
||
impl Bar { | ||
pub fn new(color: Color, bg_color: Color, radius: i16) -> Self { | ||
Self { | ||
area: Rect::zero(), | ||
color, | ||
bg_color, | ||
radius, | ||
} | ||
} | ||
} | ||
|
||
impl Component for Bar { | ||
type Msg = Never; | ||
|
||
fn place(&mut self, bounds: Rect) -> Rect { | ||
self.area = bounds; | ||
self.area | ||
} | ||
|
||
fn event(&mut self, _ctx: &mut EventCtx, _event: Event) -> Option<Self::Msg> { | ||
None | ||
} | ||
|
||
fn paint(&mut self) { | ||
display::rect_fill_rounded(self.area, self.color, self.bg_color, self.radius as u8); | ||
} | ||
|
||
fn render<'s>(&'s self, target: &mut impl Renderer<'s>) { | ||
shape::Bar::new(self.area) | ||
.with_bg(self.color) | ||
.with_radius(self.radius) | ||
.render(target); | ||
} | ||
|
||
#[cfg(feature = "ui_bounds")] | ||
fn bounds(&self, sink: &mut dyn FnMut(Rect)) { | ||
sink(self.area) | ||
} | ||
} | ||
|
||
#[cfg(feature = "ui_debug")] | ||
impl crate::trace::Trace for Bar { | ||
fn trace(&self, t: &mut dyn crate::trace::Tracer) { | ||
t.component("Bar"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use crate::{ | ||
error::Error, | ||
micropython::{buffer::get_buffer, obj::Obj}, | ||
}; | ||
|
||
use crate::ui::{ | ||
component::{Component, Event, EventCtx, Never}, | ||
display, | ||
geometry::{Alignment2D, Offset, Rect}, | ||
shape, | ||
shape::Renderer, | ||
}; | ||
|
||
pub enum ImageBuffer { | ||
Object { obj: Obj }, | ||
Slice { data: &'static [u8] }, | ||
} | ||
|
||
impl ImageBuffer { | ||
pub fn from_object(obj: Obj) -> Result<Self, Error> { | ||
if !obj.is_bytes() { | ||
return Err(Error::TypeError); | ||
} | ||
Ok(ImageBuffer::Object { obj }) | ||
} | ||
|
||
pub fn from_slice(data: &'static [u8]) -> Self { | ||
ImageBuffer::Slice { data } | ||
} | ||
|
||
pub fn is_empty(&self) -> bool { | ||
self.data().is_empty() | ||
} | ||
|
||
pub fn data(&self) -> &[u8] { | ||
match self { | ||
// SAFETY: We expect no existing mutable reference. Resulting reference is | ||
// discarded before returning to micropython. | ||
ImageBuffer::Object { obj } => unsafe { unwrap!(get_buffer(*obj)) }, | ||
ImageBuffer::Slice { data } => data, | ||
} | ||
} | ||
} | ||
|
||
pub struct Jpeg { | ||
area: Rect, | ||
data: ImageBuffer, | ||
scale: u8, | ||
} | ||
|
||
impl Jpeg { | ||
pub fn new(data: ImageBuffer, scale: u8) -> Self { | ||
Self { | ||
area: Rect::zero(), | ||
data, | ||
scale, | ||
} | ||
} | ||
} | ||
|
||
impl Component for Jpeg { | ||
type Msg = Never; | ||
|
||
fn place(&mut self, bounds: Rect) -> Rect { | ||
self.area = bounds; | ||
self.area | ||
} | ||
|
||
fn event(&mut self, _ctx: &mut EventCtx, _event: Event) -> Option<Self::Msg> { | ||
None | ||
} | ||
|
||
fn paint(&mut self) { | ||
if let Some((size, _)) = display::tjpgd::jpeg_info(self.data.data()) { | ||
let off = Offset::new(size.x / (2 << self.scale), size.y / (2 << self.scale)); | ||
display::tjpgd::jpeg(self.data.data(), self.area.center() - off, self.scale); | ||
} | ||
} | ||
|
||
fn render<'s>(&'s self, target: &mut impl Renderer<'s>) { | ||
shape::JpegImage::new(self.area.center(), self.data.data()) | ||
.with_align(Alignment2D::CENTER) | ||
.with_scale(self.scale) | ||
.render(target); | ||
} | ||
|
||
#[cfg(feature = "ui_bounds")] | ||
fn bounds(&self, sink: &mut dyn FnMut(Rect)) { | ||
sink(self.area) | ||
} | ||
} | ||
|
||
#[cfg(feature = "ui_debug")] | ||
impl crate::trace::Trace for Jpeg { | ||
fn trace(&self, t: &mut dyn crate::trace::Tracer) { | ||
t.component("Jpeg"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters