[lab3] Extracted lengths editor to lib
This commit is contained in:
parent
da01fc5bcb
commit
c8b821042b
@ -4,12 +4,11 @@ mod algo;
|
||||
|
||||
use crate::algo::{AntsSimulationConfig, EdgeExtraData, EdgesVec, VerticesVec};
|
||||
use eframe::egui;
|
||||
use eframe::egui::{Frame, ScrollArea, Ui};
|
||||
use eframe::egui::{Frame, Ui};
|
||||
use eframe::emath::Numeric;
|
||||
use egui_extras::{Column, TableBuilder};
|
||||
use std::collections::HashSet;
|
||||
use std::ops::RangeInclusive;
|
||||
use std::ptr::NonNull;
|
||||
use tsp_utility::gui::lengths_table::{draw_lengths_table, UpdatePending};
|
||||
|
||||
fn main() -> eframe::Result {
|
||||
let options = eframe::NativeOptions {
|
||||
@ -23,12 +22,6 @@ fn main() -> eframe::Result {
|
||||
)
|
||||
}
|
||||
|
||||
enum UpdatePending {
|
||||
NoChange,
|
||||
Add,
|
||||
Remove(usize),
|
||||
}
|
||||
|
||||
enum ViewState {
|
||||
Stop,
|
||||
Running { lastUpdateTimestamp: u64 },
|
||||
@ -177,72 +170,12 @@ fn edit_panel(data: &mut MyApp, ui: &mut Ui) {
|
||||
let run = ui.button("Run").clicked();
|
||||
});
|
||||
|
||||
ScrollArea::both()
|
||||
.auto_shrink([false, false])
|
||||
.show_viewport(ui, |ui, _area| {
|
||||
TableBuilder::new(ui)
|
||||
.striped(true) // Alternating row colors
|
||||
.resizable(true)
|
||||
.vscroll(false)
|
||||
.column(Column::remainder())
|
||||
.columns(Column::remainder(), data.vertices.len())
|
||||
.header(20.0, |mut header| {
|
||||
header.col(|ui| {
|
||||
ui.label("#");
|
||||
});
|
||||
for (i, _) in data.vertices.iter().enumerate() {
|
||||
header.col(|ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(i.to_string());
|
||||
if ui.button("-").clicked() {
|
||||
data.vertex_update = UpdatePending::Remove(i);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
.body(|mut body| {
|
||||
for (ri, (vi, v)) in data.vertices.iter_indexed().enumerate() {
|
||||
body.row(20.0, |mut row| {
|
||||
row.col(|ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(ri.to_string());
|
||||
if ui.button("-").clicked() {
|
||||
data.vertex_update = UpdatePending::Remove(vi);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
let mut edges = Vec::<(usize, NonNull<f64>)>::with_capacity(v.len());
|
||||
for ei in v.iter() {
|
||||
let e = &mut data.edges[*ei];
|
||||
let p = (e.another(vi), &mut e.length);
|
||||
edges.push((p.0, NonNull::from_mut(p.1)));
|
||||
}
|
||||
edges.sort_by_key(|(k, _)| *k);
|
||||
|
||||
let mut ci = 0usize;
|
||||
for &(_, mut l) in edges.iter() {
|
||||
if (ci == ri) {
|
||||
row.col(|ui| {});
|
||||
ci += 1
|
||||
}
|
||||
row.col(|ui| {
|
||||
ui.add(
|
||||
egui::Slider::new(unsafe { l.as_mut() }, 0.0..=10.0)
|
||||
.step_by(0.1),
|
||||
);
|
||||
});
|
||||
ci += 1;
|
||||
}
|
||||
|
||||
if ri == data.vertices.len() - 1 {
|
||||
row.col(|ui| {});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
draw_lengths_table(
|
||||
ui,
|
||||
&mut data.vertices,
|
||||
&mut data.edges,
|
||||
&mut data.vertex_update,
|
||||
)
|
||||
}
|
||||
|
||||
fn visualization_panel(data: &mut MyApp, ui: &mut Ui) {
|
||||
|
||||
85
tsp-utility/src/gui/lengths_table.rs
Normal file
85
tsp-utility/src/gui/lengths_table.rs
Normal file
@ -0,0 +1,85 @@
|
||||
use crate::graph::{EdgesVec, VerticesVec};
|
||||
use eframe::egui;
|
||||
use eframe::egui::{ScrollArea, Ui};
|
||||
use egui_extras::{Column, TableBuilder};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
pub enum UpdatePending {
|
||||
NoChange,
|
||||
Add,
|
||||
Remove(usize),
|
||||
}
|
||||
|
||||
pub fn draw_lengths_table<D>(
|
||||
ui: &mut Ui,
|
||||
vertices: &mut VerticesVec,
|
||||
edges: &mut EdgesVec<D>,
|
||||
update: &mut UpdatePending,
|
||||
) {
|
||||
ScrollArea::both()
|
||||
.auto_shrink([false, false])
|
||||
.show_viewport(ui, |ui, _area| {
|
||||
TableBuilder::new(ui)
|
||||
.striped(true) // Alternating row colors
|
||||
.resizable(true)
|
||||
.vscroll(false)
|
||||
.column(Column::remainder())
|
||||
.columns(Column::remainder(), vertices.len())
|
||||
.header(20.0, |mut header| {
|
||||
header.col(|ui| {
|
||||
ui.label("#");
|
||||
});
|
||||
for (i, _) in vertices.iter().enumerate() {
|
||||
header.col(|ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(i.to_string());
|
||||
if ui.button("-").clicked() {
|
||||
*update = UpdatePending::Remove(i);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
.body(|mut body| {
|
||||
for (ri, (vi, v)) in vertices.iter_indexed().enumerate() {
|
||||
body.row(20.0, |mut row| {
|
||||
row.col(|ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(ri.to_string());
|
||||
if ui.button("-").clicked() {
|
||||
*update = UpdatePending::Remove(vi);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
let mut local_edges = Vec::<(usize, NonNull<f64>)>::with_capacity(v.len());
|
||||
for ei in v.iter() {
|
||||
let e = &mut edges[*ei];
|
||||
let p = (e.another(vi), &mut e.length);
|
||||
local_edges.push((p.0, NonNull::from_mut(p.1)));
|
||||
}
|
||||
local_edges.sort_by_key(|(k, _)| *k);
|
||||
|
||||
let mut ci = 0usize;
|
||||
for &(_, mut l) in local_edges.iter() {
|
||||
if (ci == ri) {
|
||||
row.col(|ui| {});
|
||||
ci += 1
|
||||
}
|
||||
row.col(|ui| {
|
||||
ui.add(
|
||||
egui::Slider::new(unsafe { l.as_mut() }, 0.0..=10.0)
|
||||
.step_by(0.1),
|
||||
);
|
||||
});
|
||||
ci += 1;
|
||||
}
|
||||
|
||||
if ri == vertices.len() - 1 {
|
||||
row.col(|ui| {});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
1
tsp-utility/src/gui/mod.rs
Normal file
1
tsp-utility/src/gui/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod lengths_table;
|
||||
@ -1 +1,2 @@
|
||||
pub mod graph;
|
||||
pub mod gui;
|
||||
Loading…
Reference in New Issue
Block a user