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(ctx: &mut Context) -> Self {
const FONT: &[u8] = include_bytes!("../assets/DejaVuSans.ttf");
let font_id = spottedcat::register_font(ctx, 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 = spottedcat::image::create(
ctx,
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 =
spottedcat::image::create(ctx, Pt::from(width), Pt::from(height), &rgba).unwrap();
Self {
font_id,
outer_image,
inner_image,
}
}
fn draw(&mut self, ctx: &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)]);
spottedcat::image::with_clip_scope(ctx, self.outer_image, outer_opts, |ctx1| {
let inner_opts = DrawOption::default().with_position([Pt::from(10.0), Pt::from(10.0)]);
spottedcat::image::with_clip_scope(ctx1, self.inner_image, 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, _) =
spottedcat::text::measure_with_y_offset(ctx2, &text);
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)]);
spottedcat::text::draw(ctx2, &text, text_opts);
});
});
}
fn update(&mut self, _ctx: &mut Context, _dt: std::time::Duration) {}
fn remove(&mut self, _ctx: &mut Context) {}
}
fn main() {
run::<CenteredTextTestSpot>(WindowConfig::default());
}