Very simple renderer for lines

This commit is contained in:
Ruben van de Ven 2025-04-25 21:49:49 +02:00
parent 03f91f29bf
commit 1c040ef55c

View file

@ -3,7 +3,8 @@
//! UI.
use bevy::math::Mat3;
use nannou::geom::Rect;
use nannou::lyon::geom::euclid::Transform2D;
use nannou::{geom::Rect, math::map_range as nannou_map_range};
use nannou::prelude::*;
use nannou_egui::{self, egui, Egui};
use nannou_laser::{self as laser, util::map_range};
@ -73,6 +74,7 @@ fn setup_zmq() -> Socket{
let url = "tcp://100.109.175.82:99174";
let context = zmq::Context::new();
let subscriber = context.socket(zmq::SUB).unwrap();
subscriber.set_conflate(true).unwrap(); // only keep latest entry
assert!(subscriber.connect(url).is_ok());
// let filter = "10001";
@ -83,6 +85,8 @@ fn setup_zmq() -> Socket{
}
// fn zmq_receive(subscriber: &Socket, laser_streams: &Vec<laser::FrameStream<LaserModel>>) {
/// Receive items if available on the queue and update Model with the new data
fn zmq_receive(model: &mut Model) {
let subscriber = &model.zmq;
let mut items = [
@ -138,12 +142,20 @@ fn zmq_receive(model: &mut Model) {
fn model(app: &App) -> Model {
// Create a window to receive keyboard events.
let w_id = app
let w_id_lasersettings = app
.new_window()
.size(312, 530)
// .key_pressed(key_pressed)
.raw_event(raw_window_event)
.view(view)
.view(view_laser_settings)
.build()
.unwrap();
let w_id_linecanvas = app
.new_window()
.size(1024, 768)
// .key_pressed(key_pressed)
.view(view_line_canvas)
.build()
.unwrap();
@ -200,7 +212,7 @@ fn model(app: &App) -> Model {
let laser_streams = vec![];
// A user-interface to tweak the settings.
let window = app.window(w_id).unwrap();
let window = app.window(w_id_lasersettings).unwrap();
let egui = Egui::from_window(&window);
// egui.ctx().set_fonts(fonts());
egui.ctx().set_style(style());
@ -479,10 +491,69 @@ fn update(_app: &App, model: &mut Model, update: Update) {
// }
// }
fn view(_app: &App, model: &Model, frame: Frame) {
fn view_laser_settings(_app: &App, model: &Model, frame: Frame) {
model.egui.draw_to_frame(&frame).unwrap();
}
fn view_line_canvas(app: &App, model: &Model, frame: Frame) {
// get canvas to draw on
let draw = app.draw();
// set background to blue
draw.background().color(BLACK);
let win = app.window_rect();
let scale = 25.;
draw_grid(&draw, &win, scale, 1.);
// let t = app.time;
// let n_points = 10;
let thickness = 2.0;
// let hz = ((app.mouse.x + win.right()) / win.w()).powi(4) * 1000.0;
let vertices = model.current_points.iter().map(|p| {
let color = srgba(p.color[0], p.color[1], p.color[2], 1.0);
let pos = [p.position[0] * scale, p.position[1] * -scale];
(pos, color)
});
// Draw the polyline as a stroked path.
draw.polyline()
.weight(thickness)
.join_round()
.points_colored(vertices);
// model.current_points
// put everything on the frame
draw.to_frame(app, &frame).unwrap();
}
fn draw_grid(draw: &Draw, win: &Rect, step: f32, weight: f32) {
let step_by = || (0..).map(|i| i as f32 * step);
let r_iter = step_by().take_while(|&f| f < win.right());
let l_iter = step_by().map(|f| -f).take_while(|&f| f > win.left());
let x_iter = r_iter.chain(l_iter);
for x in x_iter {
draw.line()
.weight(weight)
.points(pt2(x, win.bottom()), pt2(x, win.top()))
.color(GRAY);
}
let t_iter = step_by().take_while(|&f| f < win.top());
let b_iter = step_by().map(|f| -f).take_while(|&f| f > win.bottom());
let y_iter = t_iter.chain(b_iter);
for y in y_iter {
draw.line()
.weight(weight)
.points(pt2(win.left(), y), pt2(win.right(), y))
.color(GRAY);
}
}
// The following functions are some custom styling preferences in an attempt to improve on the
// default egui theming.