use spottedcat::{Context, DrawOption, Image, Pt, Spot, WindowConfig, run};
struct CenteredTextTestSpot {
font_id: u32,
outer_image: Image,
inner_image: Image,
}
impl Spot for CenteredTextTestSpot {
fn initialize(_context: &mut Context) -> Self {
const FONT: &[u8] = include_bytes!("../assets/DejaVuSans.ttf");
let font_id = spottedcat::register_font(FONT.to_vec());
let outer_width = 400.0;
let outer_height = 150.0;
let mut outer_rgba = vec![240u8; (outer_width as usize) * (outer_height as usize) * 4];
for i in 0..(outer_width as usize) * (outer_height as usize) {
outer_rgba[i * 4] = 240; outer_rgba[i * 4 + 1] = 248; outer_rgba[i * 4 + 2] = 255; outer_rgba[i * 4 + 3] = 255; }
let outer_image =
Image::new_from_rgba8(Pt::from(outer_width), Pt::from(outer_height), &outer_rgba)
.unwrap();
let width = 300.0;
let height = 50.0;
let mut rgba = vec![200u8; (width as usize) * (height as usize) * 4];
for i in 0..(width as usize) * (height as usize) {
rgba[i * 4] = 200; rgba[i * 4 + 1] = 200; rgba[i * 4 + 2] = 200; rgba[i * 4 + 3] = 255; }
let inner_image = Image::new_from_rgba8(Pt::from(width), Pt::from(height), &rgba).unwrap();
Self {
font_id,
outer_image,
inner_image,
}
}
fn draw(&mut self, context: &mut Context) {
let width = 300.0;
let height = 50.0;
let outer_opts = DrawOption::default().with_position([Pt::from(50.0), Pt::from(50.0)]);
self.outer_image
.with_clip_scope(context, outer_opts, |ctx1| {
let inner_opts =
DrawOption::default().with_position([Pt::from(10.0), Pt::from(10.0)]);
self.inner_image.with_clip_scope(ctx1, inner_opts, |ctx2| {
let text_content = "Centered Text";
let font_size = Pt::from(20.0);
let text = spottedcat::Text::new(text_content, self.font_id)
.with_font_size(font_size)
.with_color([0.0, 0.0, 0.0, 1.0]);
let (text_width, text_height, _) = text.measure_with_y_offset();
let centered_x = (width - text_width) / 2.0;
let centered_y = (height - text_height) / 2.0;
let text_opts = DrawOption::default()
.with_position([Pt::from(centered_x), Pt::from(centered_y)]);
text.draw(ctx2, text_opts);
});
});
}
fn update(&mut self, _context: &mut Context, _dt: std::time::Duration) {}
fn remove(&self) {}
}
fn main() {
run::<CenteredTextTestSpot>(WindowConfig::default());
}