Add basic ObjectWidget

This commit is contained in:
mars 2022-05-16 17:46:01 -06:00
parent 3606cabb53
commit 96f7d10e36
1 changed files with 90 additions and 0 deletions

View File

@ -22,6 +22,7 @@ pub struct UserInterface {
quit: bool,
show_about: bool,
log_contents: String,
object: ObjectWidget,
}
impl UserInterface {
@ -33,6 +34,7 @@ impl UserInterface {
quit: false,
show_about: false,
log_contents: "Hello logging!\n".to_string(),
object: ObjectWidget::new(),
}
}
@ -143,6 +145,12 @@ impl UserInterface {
});
}
egui::SidePanel::left("objects_panel")
.resizable(true)
.show(ctx, |ui| {
self.object.ui(ui);
});
egui::TopBottomPanel::bottom("info_panel")
.resizable(true)
.show(ctx, |ui| {
@ -246,3 +254,85 @@ impl egui::Widget for &mut ViewportWidget {
response
}
}
pub struct ObjectWidget {
pub name: String,
pub position: [f32; 3],
pub rotation: [f32; 3],
pub scale: f32,
pub dirty: bool,
}
impl ObjectWidget {
pub fn new() -> Self {
Self {
name: "Example Object".to_string(),
position: [0.0; 3],
rotation: [0.0; 3],
scale: 1.0,
dirty: true,
}
}
pub fn ui(&mut self, ui: &mut egui::Ui) {
egui::CollapsingHeader::new(&self.name)
.default_open(true)
.show(ui, |ui| {
self.ui_self(ui);
});
}
pub fn ui_self(&mut self, ui: &mut egui::Ui) {
egui::Grid::new("root_object")
.num_columns(4)
.striped(true)
.show(ui, |ui| {
ui.label("Position: ");
self.ui_position(ui);
ui.end_row();
ui.label("Rotation: ");
self.ui_rotation(ui);
ui.end_row();
ui.label("Scale: ");
self.ui_scale(ui);
ui.end_row();
});
}
pub fn ui_position(&mut self, ui: &mut egui::Ui) {
let speed = 0.1 * self.scale;
for axis in self.position.iter_mut() {
let drag = egui::DragValue::new(axis).speed(speed);
if ui.add(drag).changed() {
self.dirty = true;
}
}
}
pub fn ui_rotation(&mut self, ui: &mut egui::Ui) {
for axis in self.rotation.iter_mut() {
if ui.drag_angle(axis).changed() {
self.dirty = true;
}
}
}
pub fn ui_scale(&mut self, ui: &mut egui::Ui) {
let scale_speed = self.scale * 0.01;
let drag = egui::DragValue::new(&mut self.scale)
.clamp_range(0.0..=f32::INFINITY)
.speed(scale_speed);
if ui.add(drag).changed() {
self.dirty = true;
}
}
pub fn flush_dirty(&mut self, mut f: impl FnMut(&mut Self)) {
if self.dirty {
f(self);
self.dirty = false;
}
}
}