#![cfg_attr(target_arch = "xtensa", no_std, no_main)]
#![cfg_attr(
target_arch = "xtensa",
feature(impl_trait_in_assoc_type, type_alias_impl_trait)
)]
extern crate alloc;
use oxivgl::{
draw::{Area, DrawLabelDscOwned},
draw_buf::{ColorFormat, DrawBuf},
fonts,
style::{GradDir, Selector, color_black, color_make, color_white},
view::{NavAction, View},
widgets::{Canvas, Obj, TextAlign, WidgetError},
};
const MASK_WIDTH: u32 = 150;
const MASK_HEIGHT: u32 = 60;
#[derive(Default)]
struct WidgetLabel4 {
_mask: Option<DrawBuf>,
_grad: Option<Obj<'static>>,
}
impl View for WidgetLabel4 {
fn create(&mut self, container: &Obj<'static>) -> Result<(), WidgetError> {
let scratch_buf = DrawBuf::create(MASK_WIDTH, MASK_HEIGHT, ColorFormat::ARGB8888)
.ok_or(WidgetError::LvglNullPointer)?;
let scratch = Canvas::new(container, scratch_buf)?;
scratch.fill_bg(color_black(), 255);
{
let mut layer = scratch.init_layer();
let mut ldc = DrawLabelDscOwned::default_font();
ldc.set_color(color_white())
.set_align(TextAlign::Center)
.set_font(fonts::MONTSERRAT_24);
layer.draw_label(
&ldc,
Area { x1: 0, y1: 0, x2: MASK_WIDTH as i32 - 1, y2: MASK_HEIGHT as i32 - 1 },
"Text with gradient",
);
}
let mask = DrawBuf::create(MASK_WIDTH, MASK_HEIGHT, ColorFormat::L8)
.ok_or(WidgetError::LvglNullPointer)?;
Canvas::draw_to_buf(&mask, |mask_canvas| {
mask_canvas.fill_bg(color_black(), 255);
for y in 0..MASK_HEIGHT as i32 {
for x in 0..MASK_WIDTH as i32 {
let px = scratch.get_px(x, y);
let luma = {
let r = px.red;
let g = px.green;
let b = px.blue;
r.max(g).max(b)
};
if luma > 0 {
mask_canvas.set_px(x, y, color_make(luma, luma, luma), 255);
}
}
}
});
drop(scratch);
let grad = Obj::new(container)?;
grad.size(MASK_WIDTH as i32, MASK_HEIGHT as i32);
grad.center();
grad.border_width(0);
grad.style_bg_color(color_make(0xff, 0, 0), Selector::DEFAULT)
.style_bg_grad_color(color_make(0, 0, 0xff), Selector::DEFAULT)
.style_bg_grad_dir(GradDir::Hor, Selector::DEFAULT);
unsafe { grad.style_bitmap_mask_src(&mask, Selector::DEFAULT) };
self._mask = Some(mask);
self._grad = Some(grad);
Ok(())
}
fn update(&mut self) -> Result<NavAction, WidgetError> {
Ok(NavAction::None)
}
}
oxivgl_examples_common::example_main!(WidgetLabel4::default());