parent
924cdb34d8
commit
03457b5d45
@ -1,5 +1,6 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "rust_ui_bootloader.h"
|
||||
#include "rust_ui_common.h"
|
||||
|
@ -0,0 +1,87 @@
|
||||
use crate::ui::{
|
||||
display::Color,
|
||||
geometry::{Offset, Point, Rect},
|
||||
shape::{Canvas, DrawingCache, Renderer, Shape, ShapeClone},
|
||||
};
|
||||
|
||||
use without_alloc::alloc::LocalAllocLeakExt;
|
||||
|
||||
// Shape of horizontal solid/dotted line
|
||||
pub struct HorizontalLine {
|
||||
/// Position of the left-top point
|
||||
pos: Point,
|
||||
// Length of the line
|
||||
length: i16,
|
||||
/// Line thickness (default 1)
|
||||
thickness: u8,
|
||||
/// Steps of dots (default 0 - full line)
|
||||
step: u8,
|
||||
/// Color
|
||||
color: Color,
|
||||
}
|
||||
|
||||
impl HorizontalLine {
|
||||
pub fn new(pos: Point, length: i16) -> Self {
|
||||
Self {
|
||||
pos,
|
||||
length,
|
||||
thickness: 1,
|
||||
step: 0,
|
||||
color: Color::white(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_color(self, color: Color) -> Self {
|
||||
Self { color, ..self }
|
||||
}
|
||||
|
||||
pub fn with_thickness(self, thickness: u8) -> Self {
|
||||
Self { thickness, ..self }
|
||||
}
|
||||
|
||||
pub fn with_step(self, step: u8) -> Self {
|
||||
Self { step, ..self }
|
||||
}
|
||||
|
||||
pub fn render<'s>(self, renderer: &mut impl Renderer<'s>) {
|
||||
renderer.render_shape(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> Shape<'s> for HorizontalLine {
|
||||
fn bounds(&self, _cache: &DrawingCache<'s>) -> Rect {
|
||||
let size = Offset::new(self.length, self.thickness as i16);
|
||||
Rect::from_top_left_and_size(self.pos, size)
|
||||
}
|
||||
|
||||
fn cleanup(&mut self, _cache: &DrawingCache) {}
|
||||
|
||||
fn draw(&mut self, canvas: &mut dyn Canvas, _cache: &DrawingCache) {
|
||||
if self.step <= self.thickness {
|
||||
// Solid line
|
||||
let size = Offset::new(self.length, self.thickness as i16);
|
||||
let r = Rect::from_top_left_and_size(self.pos, size);
|
||||
canvas.fill_rect(r, self.color, 255);
|
||||
} else {
|
||||
// Dotted line
|
||||
let thickness = self.thickness as i16;
|
||||
for x in (0..self.length - thickness).step_by(self.step as usize) {
|
||||
let r = Rect::from_top_left_and_size(
|
||||
self.pos + Offset::x(x),
|
||||
Offset::uniform(thickness),
|
||||
);
|
||||
canvas.fill_rect(r, self.color, 255);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> ShapeClone<'s> for HorizontalLine {
|
||||
fn clone_at_bump<'alloc, T>(self, bump: &'alloc T) -> Option<&'alloc mut dyn Shape<'s>>
|
||||
where
|
||||
T: LocalAllocLeakExt<'alloc>,
|
||||
{
|
||||
let clone = bump.alloc_t::<HorizontalLine>()?;
|
||||
Some(clone.uninit.init(HorizontalLine { ..self }))
|
||||
}
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
use crate::ui::{
|
||||
display::Color,
|
||||
geometry::{Offset, Point, Rect},
|
||||
shape::{Canvas, DrawingCache, Renderer, Shape, ShapeClone},
|
||||
};
|
||||
|
||||
use without_alloc::alloc::LocalAllocLeakExt;
|
||||
|
||||
static CELLS: [Offset; 24] = [
|
||||
Offset::new(1, -4),
|
||||
Offset::new(2, -4),
|
||||
Offset::new(3, -3),
|
||||
Offset::new(4, -2),
|
||||
Offset::new(4, -1),
|
||||
Offset::new(4, 0),
|
||||
Offset::new(4, 1),
|
||||
Offset::new(4, 2),
|
||||
Offset::new(3, 3),
|
||||
Offset::new(2, 4),
|
||||
Offset::new(1, 4),
|
||||
Offset::new(0, 4),
|
||||
Offset::new(-1, 4),
|
||||
Offset::new(-2, 4),
|
||||
Offset::new(-3, 3),
|
||||
Offset::new(-4, 2),
|
||||
Offset::new(-4, 1),
|
||||
Offset::new(-4, 0),
|
||||
Offset::new(-4, -1),
|
||||
Offset::new(-4, -2),
|
||||
Offset::new(-3, -3),
|
||||
Offset::new(-2, -4),
|
||||
Offset::new(-1, -4),
|
||||
Offset::new(0, -4),
|
||||
];
|
||||
|
||||
pub struct LoaderCircular {
|
||||
/// Position of point (0,0)
|
||||
pos: Point,
|
||||
/// Value 0..1000
|
||||
value: u16,
|
||||
/// Color
|
||||
color: Color,
|
||||
/// Scale (length of square size)
|
||||
scale: i16,
|
||||
}
|
||||
|
||||
impl LoaderCircular {
|
||||
pub fn new(pos: Point, value: u16) -> Self {
|
||||
Self {
|
||||
pos,
|
||||
value,
|
||||
color: Color::white(),
|
||||
scale: 2,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_color(self, color: Color) -> Self {
|
||||
Self { color, ..self }
|
||||
}
|
||||
|
||||
pub fn with_scale(self, scale: i16) -> Self {
|
||||
Self { scale, ..self }
|
||||
}
|
||||
|
||||
fn cells(&self) -> &[Offset] {
|
||||
let value = self.value.clamp(0, 1000);
|
||||
let last = (CELLS.len() * value as usize) / 1000;
|
||||
&CELLS[..last]
|
||||
}
|
||||
|
||||
fn cell_rect(&self, offset: Offset) -> Rect {
|
||||
let pt = Point::new(
|
||||
self.pos.x + (offset.x * self.scale) - self.scale / 2,
|
||||
self.pos.y + (offset.y * self.scale) - self.scale / 2,
|
||||
);
|
||||
Rect::from_top_left_and_size(pt, Offset::uniform(self.scale))
|
||||
}
|
||||
|
||||
pub fn render<'s>(self, renderer: &mut impl Renderer<'s>) {
|
||||
renderer.render_shape(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> Shape<'s> for LoaderCircular {
|
||||
fn bounds(&self, _cache: &DrawingCache<'s>) -> Rect {
|
||||
let cells = self.cells();
|
||||
|
||||
if cells.is_empty() {
|
||||
Rect::zero()
|
||||
} else {
|
||||
let mut b = self.cell_rect(cells[0]);
|
||||
cells[1..]
|
||||
.iter()
|
||||
.for_each(|c| b = b.union(self.cell_rect(*c)));
|
||||
b
|
||||
}
|
||||
}
|
||||
|
||||
fn cleanup(&mut self, _cache: &DrawingCache) {}
|
||||
|
||||
fn draw(&mut self, canvas: &mut dyn Canvas, _cache: &DrawingCache) {
|
||||
for c in self.cells().iter() {
|
||||
canvas.fill_rect(self.cell_rect(*c), self.color, 255);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ShapeClone<'_> for LoaderCircular {
|
||||
fn clone_at_bump<'alloc, T>(self, bump: &'alloc T) -> Option<&'alloc mut dyn Shape>
|
||||
where
|
||||
T: LocalAllocLeakExt<'alloc>,
|
||||
{
|
||||
let clone = bump.alloc_t::<LoaderCircular>()?;
|
||||
Some(clone.uninit.init(LoaderCircular { ..self }))
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
use crate::ui::{
|
||||
display::Color,
|
||||
geometry::{Offset, Point, Rect},
|
||||
shape::{Canvas, DrawingCache, Renderer, Shape, ShapeClone},
|
||||
};
|
||||
|
||||
use without_alloc::alloc::LocalAllocLeakExt;
|
||||
|
||||
use core::f32::consts::SQRT_2;
|
||||
|
||||
const STAR_COUNT: usize = 8;
|
||||
const RADIUS: i16 = 3;
|
||||
const DIAGONAL: i16 = ((RADIUS as f32 * SQRT_2) / 2_f32) as i16;
|
||||
|
||||
// Offset of the normal point and then the extra offset for the main point
|
||||
static STARS: [(Offset, Offset); STAR_COUNT] = [
|
||||
(Offset::y(-RADIUS), Offset::y(-1)),
|
||||
(Offset::new(DIAGONAL, -DIAGONAL), Offset::new(1, -1)),
|
||||
(Offset::x(RADIUS), Offset::x(1)),
|
||||
(Offset::new(DIAGONAL, DIAGONAL), Offset::new(1, 1)),
|
||||
(Offset::y(RADIUS), Offset::y(1)),
|
||||
(Offset::new(-DIAGONAL, DIAGONAL), Offset::new(-1, 1)),
|
||||
(Offset::x(-RADIUS), Offset::x(-1)),
|
||||
(Offset::new(-DIAGONAL, -DIAGONAL), Offset::new(-1, -1)),
|
||||
];
|
||||
|
||||
/// A shape of a TS3 small loader
|
||||
pub struct LoaderSmall {
|
||||
/// Position of point (0,0)
|
||||
pos: Point,
|
||||
/// Value 0..1000
|
||||
value: u16,
|
||||
/// Color
|
||||
color: Color,
|
||||
}
|
||||
|
||||
impl LoaderSmall {
|
||||
pub fn new(pos: Point, value: u16) -> Self {
|
||||
Self {
|
||||
pos,
|
||||
value,
|
||||
color: Color::white(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_color(self, color: Color) -> Self {
|
||||
Self { color, ..self }
|
||||
}
|
||||
|
||||
pub fn render<'s>(self, renderer: &mut impl Renderer<'s>) {
|
||||
renderer.render_shape(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl Shape<'_> for LoaderSmall {
|
||||
fn bounds(&self, _cache: &DrawingCache) -> Rect {
|
||||
Rect::from_top_left_and_size(self.pos, Offset::uniform(1)).expand(RADIUS + 1)
|
||||
}
|
||||
|
||||
fn cleanup(&mut self, _cache: &DrawingCache) {}
|
||||
|
||||
fn draw(&mut self, canvas: &mut dyn Canvas, _cache: &DrawingCache) {
|
||||
// Calculate index of the highlighted star
|
||||
let sel_idx = (STAR_COUNT * self.value as usize / 1000) % STAR_COUNT;
|
||||
|
||||
for (i, (star_offset, hili_offset)) in STARS.iter().enumerate() {
|
||||
if (sel_idx + 1) % STAR_COUNT != i {
|
||||
// Draw a star if it's not behind the highlighted one (clockwise)
|
||||
let star_pos = self.pos + *star_offset;
|
||||
canvas.draw_pixel(star_pos, self.color);
|
||||
if sel_idx == i {
|
||||
// Higlight the main star
|
||||
canvas.draw_pixel(star_pos + *hili_offset, self.color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ShapeClone<'_> for LoaderSmall {
|
||||
fn clone_at_bump<'alloc, T>(self, bump: &'alloc T) -> Option<&'alloc mut dyn Shape>
|
||||
where
|
||||
T: LocalAllocLeakExt<'alloc>,
|
||||
{
|
||||
let clone = bump.alloc_t::<LoaderSmall>()?;
|
||||
Some(clone.uninit.init(LoaderSmall { ..self }))
|
||||
}
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
use crate::ui::{
|
||||
display::Color,
|
||||
geometry::{Offset, Point, Rect},
|
||||
shape::{Canvas, DrawingCache, Renderer, Shape, ShapeClone},
|
||||
};
|
||||
|
||||
use without_alloc::alloc::LocalAllocLeakExt;
|
||||
|
||||
use core::f32::consts::SQRT_2;
|
||||
|
||||
const STAR_COUNT: usize = 8;
|
||||
const STAR_SMALL: i16 = 2;
|
||||
const STAR_MEDIUM: i16 = 4;
|
||||
const STAR_LARGE: i16 = 6;
|
||||
|
||||
const RADIUS: i16 = 13;
|
||||
const DIAGONAL: i16 = ((RADIUS as f32 * SQRT_2) / 2_f32) as i16;
|
||||
|
||||
// Offset of the normal point and then the extra offset for the main point
|
||||
static STARS: [Offset; STAR_COUNT] = [
|
||||
Offset::y(-RADIUS),
|
||||
Offset::new(DIAGONAL, -DIAGONAL),
|
||||
Offset::x(RADIUS),
|
||||
Offset::new(DIAGONAL, DIAGONAL),
|
||||
Offset::y(RADIUS),
|
||||
Offset::new(-DIAGONAL, DIAGONAL),
|
||||
Offset::x(-RADIUS),
|
||||
Offset::new(-DIAGONAL, -DIAGONAL),
|
||||
];
|
||||
|
||||
/// A shape of a TS3 starry loader
|
||||
pub struct LoaderStarry {
|
||||
/// Position of point (0,0)
|
||||
pos: Point,
|
||||
/// Value 0..1000
|
||||
value: u16,
|
||||
/// Color
|
||||
color: Color,
|
||||
}
|
||||
|
||||
impl LoaderStarry {
|
||||
pub fn new(pos: Point, value: u16) -> Self {
|
||||
Self {
|
||||
pos,
|
||||
value,
|
||||
color: Color::white(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_color(self, color: Color) -> Self {
|
||||
Self { color, ..self }
|
||||
}
|
||||
|
||||
pub fn render<'s>(self, renderer: &mut impl Renderer<'s>) {
|
||||
renderer.render_shape(self);
|
||||
}
|
||||
|
||||
fn draw_large_star(&self, canvas: &mut dyn Canvas, offset: Offset) {
|
||||
let r = Rect::from_center_and_size(self.pos + offset, Offset::uniform(STAR_LARGE));
|
||||
canvas.fill_round_rect(r, 2, self.color, 255);
|
||||
}
|
||||
|
||||
fn draw_medium_star(&self, canvas: &mut dyn Canvas, offset: Offset) {
|
||||
let r = Rect::from_center_and_size(self.pos + offset, Offset::uniform(STAR_MEDIUM));
|
||||
canvas.fill_round_rect(r, 1, self.color, 255);
|
||||
}
|
||||
|
||||
fn draw_small_star(&self, canvas: &mut dyn Canvas, offset: Offset) {
|
||||
let r = Rect::from_center_and_size(self.pos + offset, Offset::uniform(STAR_SMALL));
|
||||
canvas.fill_rect(r, self.color, 255);
|
||||
}
|
||||
}
|
||||
|
||||
impl Shape<'_> for LoaderStarry {
|
||||
fn bounds(&self, _cache: &DrawingCache) -> Rect {
|
||||
Rect::from_top_left_and_size(self.pos, Offset::uniform(1)).expand(RADIUS + STAR_LARGE)
|
||||
}
|
||||
|
||||
fn cleanup(&mut self, _cache: &DrawingCache) {}
|
||||
|
||||
fn draw(&mut self, canvas: &mut dyn Canvas, _cache: &DrawingCache) {
|
||||
// Calculate the index of the big star
|
||||
let sel_idx = (STAR_COUNT * self.value as usize / 1000) % STAR_COUNT;
|
||||
|
||||
for (i, c) in STARS.iter().enumerate() {
|
||||
if i == sel_idx {
|
||||
self.draw_large_star(canvas, *c);
|
||||
} else if (sel_idx + 1) % 8 == i || (sel_idx - 1) % 8 == i {
|
||||
self.draw_medium_star(canvas, *c);
|
||||
} else {
|
||||
self.draw_small_star(canvas, *c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ShapeClone<'_> for LoaderStarry {
|
||||
fn clone_at_bump<'alloc, T>(self, bump: &'alloc T) -> Option<&'alloc mut dyn Shape>
|
||||
where
|
||||
T: LocalAllocLeakExt<'alloc>,
|
||||
{
|
||||
let clone = bump.alloc_t::<LoaderStarry>()?;
|
||||
Some(clone.uninit.init(LoaderStarry { ..self }))
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
pub mod dotted_line;
|
||||
pub mod loader_circular;
|
||||
pub mod loader_small;
|
||||
pub mod loader_starry;
|
||||
|
||||
pub use dotted_line::HorizontalLine;
|
||||
pub use loader_circular::LoaderCircular;
|
||||
pub use loader_small::LoaderSmall;
|
||||
pub use loader_starry::LoaderStarry;
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue