parent
472a8bebd2
commit
1be00d543c
@ -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");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,96 @@
|
|||||||
|
use crate::error::Error;
|
||||||
|
use crate::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");
|
||||||
|
}
|
||||||
|
}
|
@ -1,88 +0,0 @@
|
|||||||
#[cfg(feature = "jpeg")]
|
|
||||||
use crate::ui::geometry::Offset;
|
|
||||||
use crate::ui::{
|
|
||||||
component::{image::Image, Component, Event, EventCtx, Never},
|
|
||||||
display,
|
|
||||||
geometry::{Alignment2D, Rect},
|
|
||||||
shape,
|
|
||||||
shape::Renderer,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Painter<F> {
|
|
||||||
area: Rect,
|
|
||||||
func: F,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F> Painter<F> {
|
|
||||||
pub fn new(func: F) -> Self {
|
|
||||||
Self {
|
|
||||||
func,
|
|
||||||
area: Rect::zero(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F> Component for Painter<F>
|
|
||||||
where
|
|
||||||
F: FnMut(Rect),
|
|
||||||
{
|
|
||||||
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) {
|
|
||||||
(self.func)(self.area);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render<'s>(&'s self, target: &mut impl Renderer<'s>) {
|
|
||||||
let area = self.area;
|
|
||||||
shape::Bar::new(area)
|
|
||||||
.with_thickness(1)
|
|
||||||
.with_fg(display::Color::white())
|
|
||||||
.render(target);
|
|
||||||
shape::Text::new(area.top_left(), "Paint").render(target); // !@# replace
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "ui_bounds")]
|
|
||||||
fn bounds(&self, sink: &mut dyn FnMut(Rect)) {
|
|
||||||
sink(self.area)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "ui_debug")]
|
|
||||||
impl<F> crate::trace::Trace for Painter<F> {
|
|
||||||
fn trace(&self, t: &mut dyn crate::trace::Tracer) {
|
|
||||||
t.component("Painter");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn image_painter(image: Image) -> Painter<impl FnMut(Rect)> {
|
|
||||||
let f = move |area: Rect| image.draw(area.center(), Alignment2D::CENTER);
|
|
||||||
Painter::new(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "jpeg")]
|
|
||||||
pub fn jpeg_painter<'a>(
|
|
||||||
image: impl Fn() -> &'a [u8],
|
|
||||||
size: Offset,
|
|
||||||
scale: u8,
|
|
||||||
) -> Painter<impl FnMut(Rect)> {
|
|
||||||
let off = Offset::new(size.x / (2 << scale), size.y / (2 << scale));
|
|
||||||
#[cfg(not(feature = "new_rendering"))]
|
|
||||||
let f = move |area: Rect| display::tjpgd::jpeg(image(), area.center() - off, scale);
|
|
||||||
#[cfg(feature = "new_rendering")]
|
|
||||||
let f = move |area: Rect| {};
|
|
||||||
Painter::new(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn rect_painter(fg: display::Color, bg: display::Color) -> Painter<impl FnMut(Rect)> {
|
|
||||||
let f = move |area: Rect| display::rect_fill_rounded(area, fg, bg, 2);
|
|
||||||
Painter::new(f)
|
|
||||||
}
|
|
Loading…
Reference in new issue