161 lines
4.8 KiB
Rust
161 lines
4.8 KiB
Rust
use std::sync::Arc;
|
|
|
|
use app::App;
|
|
use gui::*;
|
|
use render::Renderer;
|
|
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: UiIds,
|
|
}
|
|
|
|
pub struct UiIds {
|
|
test: WidgetId<Span>,
|
|
}
|
|
|
|
impl Client {
|
|
pub fn create_ui() -> (Ui<Self>, UiIds) {
|
|
let mut ui = Ui::new();
|
|
let test = ui.id();
|
|
let rect = Rect {
|
|
color: UiColor::WHITE,
|
|
radius: 20.0,
|
|
thickness: 0.0,
|
|
inner_radius: 0.0,
|
|
};
|
|
let pad_test = ui.add(
|
|
(
|
|
rect.color(UiColor::BLUE),
|
|
(
|
|
rect.color(UiColor::RED).center((100.0, 100.0)),
|
|
(
|
|
rect.color(UiColor::ORANGE),
|
|
rect.color(UiColor::LIME).pad(10.0),
|
|
)
|
|
.span(Dir::RIGHT, [1, 1]),
|
|
rect.color(UiColor::YELLOW),
|
|
)
|
|
.span(Dir::RIGHT, [2, 2, 1])
|
|
.pad(10),
|
|
)
|
|
.span(Dir::RIGHT, [1, 3]),
|
|
);
|
|
let span_test = ui.add(
|
|
(
|
|
rect.color(UiColor::GREEN),
|
|
rect.color(UiColor::ORANGE),
|
|
rect.color(UiColor::CYAN),
|
|
rect.color(UiColor::BLUE),
|
|
rect.color(UiColor::MAGENTA),
|
|
rect.color(UiColor::RED),
|
|
)
|
|
.span(
|
|
Dir::LEFT,
|
|
[
|
|
fixed(100),
|
|
ratio(1),
|
|
ratio(1),
|
|
rel(0.5),
|
|
fixed(100),
|
|
fixed(100),
|
|
],
|
|
),
|
|
);
|
|
let span_add_test = ui.add(Span::empty(Dir::RIGHT).id(&test));
|
|
let main: WidgetId<Regioned> = ui.id();
|
|
|
|
fn switch_button<To>(
|
|
color: UiColor,
|
|
main: &WidgetId<Regioned>,
|
|
to: &WidgetId<To>,
|
|
) -> impl WidgetLike<Client, IdTag> {
|
|
let main = main.clone();
|
|
let to = to.clone().erase_type();
|
|
Rect::new(color)
|
|
.sense(Sense::Press, move |ui, _| {
|
|
ui[&main].inner = to.clone();
|
|
})
|
|
.sense_and_edit(Sense::Hover, move |r, _| {
|
|
r.color = color.add_rgb(0.1);
|
|
})
|
|
.sense_and_edit(Sense::NoHover, move |r, _| {
|
|
r.color = color;
|
|
})
|
|
.sense_and_edit(Sense::Held, move |r, _| {
|
|
r.color = color.add_rgb(-0.1);
|
|
})
|
|
}
|
|
|
|
let buttons = ui.add(
|
|
(
|
|
switch_button(UiColor::RED, &main, &pad_test),
|
|
switch_button(UiColor::GREEN, &main, &span_test),
|
|
switch_button(UiColor::BLUE, &main, &span_add_test),
|
|
)
|
|
.span(Dir::RIGHT, [1, 1, 1]),
|
|
);
|
|
ui.set_base(
|
|
(
|
|
buttons,
|
|
(
|
|
pad_test.pad(10).id(&main),
|
|
Rect::new(Color::PURPLE).radius(30).region(
|
|
UiRegion::bottom_right()
|
|
.size((150, 150))
|
|
.shifted((-75, -75)),
|
|
),
|
|
)
|
|
.stack(),
|
|
)
|
|
.span(Dir::DOWN, [fixed(40), ratio(1)]),
|
|
);
|
|
(ui, UiIds { test })
|
|
}
|
|
|
|
pub fn new(window: Arc<Window>, ui: UiIds) -> Self {
|
|
let renderer = Renderer::new(window);
|
|
Self {
|
|
renderer,
|
|
ui,
|
|
input: Input::default(),
|
|
}
|
|
}
|
|
|
|
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop, ui: &mut Ui<Self>) {
|
|
self.input.event(&event);
|
|
ui.run_sensors(self);
|
|
match event {
|
|
WindowEvent::CloseRequested => event_loop.exit(),
|
|
WindowEvent::RedrawRequested => {
|
|
let primitives = ui.update(self);
|
|
self.renderer.update(primitives);
|
|
self.renderer.draw()
|
|
}
|
|
WindowEvent::Resized(size) => self.renderer.resize(&size),
|
|
WindowEvent::KeyboardInput { event, .. } => {
|
|
if event.state.is_pressed() {
|
|
let child = ui.add(Rect::new(Color::YELLOW)).erase_type();
|
|
ui[&self.ui.test].children.push((child, fixed(20.0)));
|
|
self.renderer.window().request_redraw();
|
|
}
|
|
}
|
|
_ => (),
|
|
}
|
|
if ui.needs_redraw() {
|
|
self.renderer.window().request_redraw();
|
|
}
|
|
}
|
|
}
|