Files
iris/src/testing/mod.rs
2025-08-28 22:21:43 -04:00

201 lines
6.2 KiB
Rust

use std::sync::Arc;
use app::App;
use cosmic_text::Family;
use render::Renderer;
use senses::*;
use ui::prelude::*;
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window};
use crate::testing::input::Input;
mod app;
mod input;
mod render;
pub fn main() {
App::run();
}
pub struct Client {
renderer: Renderer,
input: Input,
ui: Ui,
info: WidgetId<Text>,
}
impl Client {
pub fn new(window: Arc<Window>) -> Self {
let renderer = Renderer::new(window);
let mut ui = Ui::new();
let rect = Rect {
color: Color::WHITE,
radius: 20.0,
thickness: 0.0,
inner_radius: 0.0,
};
let pad_test = ui.add_static(
(
rect.color(Color::BLUE),
(
rect.color(Color::RED).size(100).center(),
(rect.color(Color::ORANGE), rect.color(Color::LIME).pad(10.0))
.span(Dir::RIGHT, [1, 1]),
rect.color(Color::YELLOW),
)
.span(Dir::RIGHT, [2, 2, 1])
.pad(10),
)
.span(Dir::RIGHT, [1, 3]),
);
let span_test = ui.add_static(
(
rect.color(Color::GREEN),
rect.color(Color::ORANGE),
rect.color(Color::CYAN),
rect.color(Color::BLUE),
rect.color(Color::MAGENTA),
rect.color(Color::RED),
)
.span(
Dir::LEFT,
[
fixed(100),
ratio(1),
ratio(1),
relative(0.5),
fixed(100),
fixed(100),
],
),
);
let span_add = ui.add_static(Span::empty(Dir::RIGHT));
let main = ui.add_static(pad_test.pad(10));
let switch_button = |color, to, label| {
let rect = Rect::new(color)
.id_on(PRESS_START, move |id, ui| {
ui[main].inner.set_static(to);
ui[id].color = color.add_rgb(-0.2);
})
.edit_on(HOVER_START | PRESS_END, move |r| {
r.color = color.add_rgb(0.4);
})
.edit_on(HOVER_END, move |r| {
r.color = color;
});
(rect, text(label).font_size(30)).stack()
};
let text_test = ui.add_static(
(
text("this is a").font_size(30).align(Align::Left),
text("teeeeeeeest").font_size(30).align(Align::Left),
text("okkk\nokkkkkk!").font_size(30).align(Align::Left),
text("hmm").font_size(30),
text("a").font_size(30),
(
text("'")
.font_size(30)
.family(Family::Monospace)
.align(Align::Top),
text("'").font_size(30).family(Family::Monospace),
text(":gamer mode").font_size(30).family(Family::Monospace),
Rect::new(Color::BLUE).size(100),
)
.span(Dir::RIGHT, sized())
.center(),
text("pretty cool right?").font_size(30),
)
.span(Dir::DOWN, sized()),
);
let tabs = (
switch_button(Color::RED, pad_test, "pad test"),
switch_button(Color::GREEN, span_test, "span test"),
switch_button(Color::BLUE, span_add, "span add test"),
switch_button(Color::MAGENTA, text_test, "text test"),
)
.span(Dir::RIGHT, ratio(1));
let add_button = Rect::new(Color::LIME)
.radius(30)
.on(PRESS_START, move |ui| {
let child = ui
.add(image(include_bytes!("assets/sungals.png")).center())
.erase_type();
ui[span_add].children.push((child, sized()));
})
.size(150)
.align(Align::BotRight);
let del_button = Rect::new(Color::RED)
.radius(30)
.on(PRESS_START, move |ui| {
ui[span_add].children.pop();
})
.size(150)
.align(Align::BotLeft);
let info = ui.add(text(""));
let info_sect = info
.clone()
.region(Align::TopRight.pos().expand((150, 150)).shifted((-75, 0)));
ui.set_base(
(
tabs.label("tabs"),
(
main,
add_button.label("add button"),
del_button.label("del button"),
info_sect.label("info sect"),
)
.stack()
.label("main stack"),
)
.span(Dir::DOWN, [fixed(40), ratio(1)])
.label("root"),
);
Self {
renderer,
input: Input::default(),
ui,
info,
}
}
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
self.input.event(&event);
let cursor_state = self.cursor_state();
let window_size = self.window_size();
self.ui.run_sensors(&cursor_state, window_size);
match event {
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => {
self.ui.update();
self.renderer.update(&mut self.ui);
self.renderer.draw()
}
WindowEvent::Resized(size) => {
self.ui.resize((size.width, size.height));
self.renderer.resize(&size)
}
_ => (),
}
let new = format!(
"widgets: {}\nviews: {}",
self.ui.num_widgets(),
self.renderer.ui.view_count()
);
if new != self.ui[&self.info].content {
self.ui[&self.info].content = new;
}
if self.ui.needs_redraw() {
self.renderer.window().request_redraw();
}
}
}