Skip to content

Commit

Permalink
add always display bbox/radius/offset buttons (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Baezon authored Mar 8, 2024
1 parent 165bd98 commit 3d0ffcf
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 61 deletions.
8 changes: 8 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,12 @@ pub(crate) struct PofToolsGui {
pub ui_state: UiState,
pub display_mode: DisplayMode,
pub glow_point_simulation: bool,
pub always_show_bbox: bool,
pub always_show_radius: bool,
pub always_show_offset: bool,

pub dock_demo_img: egui::TextureHandle,

pub camera_pitch: f32,
pub camera_heading: f32,
pub camera_scale: f32,
Expand Down Expand Up @@ -573,6 +578,9 @@ impl PofToolsGui {
glow_point_sim_start: std::time::Instant::now(),
ui_state: Default::default(),
display_mode: DisplayMode::Textured,
always_show_bbox: false,
always_show_offset: false,
always_show_radius: false,
glow_point_simulation: Default::default(),
dock_demo_img: {
ctx.load_texture(
Expand Down
167 changes: 106 additions & 61 deletions src/ui_properties_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;
use std::hash::Hash;
use std::str::FromStr;

use egui::Align2;
use egui::{style::Widgets, text::LayoutJob, CollapsingHeader, Color32, DragValue, Label, Response, RichText, TextEdit, TextFormat, TextStyle, Ui};
use glium::glutin::surface::WindowSurface;
use glium::Display;
Expand Down Expand Up @@ -104,11 +105,28 @@ impl<T: Clone> IndexingButtonsResponse<T> {
}
}

// fn text_edit_single_numeric(ui: &mut Ui, id: impl Hash, string: &mut String) -> Response {
// let response = text_edit_single(ui, id, string);
// string.retain(|c| c.is_ascii_digit() || c == '.' || c == ',' || c.is_whitespace());
// response
// }
pub fn visibility_button(ui: &mut Ui, toggled: bool) -> Response {
let res = ui.selectable_label(toggled, RichText::new(" ").text_style(TextStyle::Button));

let painter = ui.painter_at(res.rect);
let mut pos = res.rect.center();
pos.x += 0.25;
painter.text(
pos,
Align2::CENTER_CENTER,
"👁",
egui::TextStyle::Heading.resolve(ui.style()),
if res.hovered() || toggled {
ui.visuals().strong_text_color()
} else {
ui.visuals().text_color()
},
);

ui.add_space(-3.0);

res
}

fn text_edit_single(ui: &mut Ui, id: impl Hash, string: &mut String) -> Response {
let id = egui::Id::new(id);
Expand Down Expand Up @@ -906,15 +924,11 @@ impl PofToolsGui {
let mut bbox_changed = false;
let mut display_bbox = false;
ui.horizontal(|ui| {
ui.label("Bounding Box:");
let response = ui.button("Recalculate");

if response.clicked() {
self.model.recalc_bbox();
self.ui_state.properties_panel_dirty = true;
bbox_changed = true;
if visibility_button(ui, self.always_show_bbox).clicked() {
self.always_show_bbox = !self.always_show_bbox;
}
display_bbox = response.hovered() || response.has_focus() || display_bbox;

ui.label("Bounding Box:");
});

ui.horizontal(|ui| {
Expand All @@ -931,7 +945,7 @@ impl PofToolsGui {
if response.changed() {
bbox_changed = true;
}
display_bbox = response.hovered() || response.has_focus() || display_bbox;
display_bbox |= response.hovered() || response.has_focus();
});

ui.horizontal(|ui| {
Expand All @@ -948,9 +962,19 @@ impl PofToolsGui {
if response.changed() {
bbox_changed = true;
}
self.ui_state.display_bbox = response.hovered() || response.has_focus() || display_bbox;
display_bbox |= response.hovered() || response.has_focus();
});

let response = ui.button("Recalculate");
if response.clicked() {
self.model.recalc_bbox();
self.ui_state.properties_panel_dirty = true;
bbox_changed = true;
}
display_bbox |= response.hovered() || response.has_focus();

self.ui_state.display_bbox = self.always_show_bbox || display_bbox;

let bbox = &self.model.header.bbox;
ui.horizontal_wrapped(|ui| {
ui.label(format!("Width:{NON_BREAK_SPACE}{:.1}", bbox.x_width()));
Expand All @@ -963,15 +987,11 @@ impl PofToolsGui {
let mut radius_changed = false;
let mut display_radius = false;
ui.horizontal(|ui| {
ui.add(egui::Label::new("Radius:"));
let response = ui.button("Recalculate");

if response.clicked() {
self.model.recalc_radius();
radius_changed = true;
self.ui_state.properties_panel_dirty = true;
if visibility_button(ui, self.always_show_radius).clicked() {
self.always_show_radius = !self.always_show_radius;
}
display_radius = response.hovered() || response.has_focus() || display_radius;

ui.label("Radius:");
});

let response = UiState::model_value_edit(
Expand All @@ -982,11 +1002,22 @@ impl PofToolsGui {
Some(&mut self.model.header.max_radius),
radius_string,
);

if response.changed() {
radius_changed = true;
}
self.ui_state.display_radius = response.hovered() || response.has_focus() || display_radius;
display_radius |= response.hovered() || response.has_focus();

let response = ui.button("Recalculate");
if response.clicked() {
self.model.recalc_radius();
radius_changed = true;
self.ui_state.properties_panel_dirty = true;
}
display_radius |= response.hovered() || response.has_focus();

self.ui_state.display_radius = self.always_show_radius || display_radius;

ui.separator();

ui.horizontal(|ui| {
ui.add(egui::Label::new("Mass:"));
Expand Down Expand Up @@ -1256,22 +1287,18 @@ impl PofToolsGui {
UiState::reset_widget_color(ui);
});

ui.add_space(5.0);
ui.separator();

// Bounding Box edit ================================================================

let mut bbox_changed = false;
let mut display_bbox = false;
ui.horizontal(|ui| {
ui.label("Bounding Box:");
let response = ui.add_enabled(selected_id.is_some(), egui::Button::new("Recalculate"));

if response.clicked() {
self.model.sub_objects[selected_id.unwrap()].recalc_bbox();
self.ui_state.properties_panel_dirty = true;
bbox_changed = true;
if visibility_button(ui, self.always_show_bbox).clicked() {
self.always_show_bbox = !self.always_show_bbox;
}
display_bbox = response.hovered() || response.has_focus() || display_bbox;

ui.label("Bounding Box:");
});

ui.horizontal(|ui| {
Expand All @@ -1288,7 +1315,7 @@ impl PofToolsGui {
if response.changed() {
bbox_changed = true;
}
display_bbox = response.hovered() || response.has_focus() || display_bbox;
display_bbox |= response.hovered() || response.has_focus();
});

ui.horizontal(|ui| {
Expand All @@ -1308,7 +1335,15 @@ impl PofToolsGui {
display_bbox = response.hovered() || response.has_focus() || display_bbox;
});

self.ui_state.display_bbox = display_bbox;
let response = ui.add_enabled(selected_id.is_some(), egui::Button::new("Recalculate"));
if response.clicked() {
self.model.sub_objects[selected_id.unwrap()].recalc_bbox();
self.ui_state.properties_panel_dirty = true;
bbox_changed = true;
}
display_bbox |= response.hovered() || response.has_focus();

self.ui_state.display_bbox = self.always_show_bbox || display_bbox;

if bbox_changed {
self.model.recheck_warnings(One(Warning::BBoxTooSmall(selected_id)));
Expand All @@ -1328,26 +1363,19 @@ impl PofToolsGui {

// Offset edit ================================================================

let mut display_origin = false;
ui.horizontal_wrapped(|ui| {
if visibility_button(ui, self.always_show_offset).clicked() {
self.always_show_offset = !self.always_show_offset;
}

if self.model.header.detail_levels.first().map_or(false, |id| selected_id == Some(*id))
&& self.model.warnings.contains(&Warning::Detail0NonZeroOffset)
{
ui.visuals_mut().override_text_color = Some(WARNING_YELLOW);
}
ui.label("Offset:");
ui.visuals_mut().override_text_color = None;

let response = ui.add_enabled(selected_id.is_some(), egui::Button::new("Recalculate"));

if response.clicked() {
self.model.recalc_subobj_offset(selected_id.unwrap());
self.model.recheck_warnings(One(Warning::RadiusTooSmall(selected_id)));

self.ui_state.viewport_3d_dirty = true;
buffer_ids_to_rebuild.push(selected_id.unwrap());
self.ui_state.properties_panel_dirty = true;
}
self.ui_state.display_origin |= response.hovered() || response.has_focus();
});

if let Some(id) = selected_id {
Expand All @@ -1368,36 +1396,44 @@ impl PofToolsGui {
self.model.recheck_warnings(One(Warning::Detail0NonZeroOffset));
}
}
self.ui_state.display_origin |= response.hovered() || response.has_focus();
display_origin |= response.hovered() || response.has_focus();
ui.visuals_mut().override_text_color = None;
} else {
ui.add_enabled(false, TextEdit::singleline(offset_string));
}

let response = ui.add_enabled(selected_id.is_some(), egui::Button::new("Recalculate"));
if response.clicked() {
self.model.recalc_subobj_offset(selected_id.unwrap());
self.model.recheck_warnings(One(Warning::RadiusTooSmall(selected_id)));

self.ui_state.viewport_3d_dirty = true;
buffer_ids_to_rebuild.push(selected_id.unwrap());
self.ui_state.properties_panel_dirty = true;
}
display_origin |= response.hovered() || response.has_focus();

let response = ui
.add_enabled(selected_id.is_some(), egui::Checkbox::new(&mut self.ui_state.move_only_offset, "Modify Offset Only"))
.on_hover_text(
"Changes will affect only the offset/center of this subobject, all other geometry remains in place.\nThe radius will be recalculated.",
);
display_origin |= response.hovered() || response.has_focus();

self.ui_state.display_origin |= response.hovered() || response.has_focus();
self.ui_state.display_origin = display_origin || self.always_show_offset;

ui.add_space(5.0);
ui.separator();

// Radius edit ================================================================

let mut radius_changed = false;
let mut display_radius = false;
ui.horizontal(|ui| {
ui.label("Radius:");
let response = ui.add_enabled(selected_id.is_some(), egui::Button::new("Recalculate"));

if response.clicked() {
self.model.sub_objects[selected_id.unwrap()].recalc_radius();
self.ui_state.properties_panel_dirty = true;
radius_changed = true;
if visibility_button(ui, self.always_show_radius).clicked() {
self.always_show_radius = !self.always_show_radius;
}
display_radius = response.hovered() || response.has_focus() || display_radius;

ui.label("Radius:");
});

let response = UiState::model_value_edit(
Expand All @@ -1408,11 +1444,20 @@ impl PofToolsGui {
selected_id.map(|id| &mut self.model.sub_objects[id].radius),
radius_string,
);

if response.changed() {
radius_changed = true;
}
self.ui_state.display_radius = response.hovered() || response.has_focus() || display_radius;
display_radius |= response.hovered() || response.has_focus();

let response = ui.add_enabled(selected_id.is_some(), egui::Button::new("Recalculate"));
if response.clicked() {
self.model.sub_objects[selected_id.unwrap()].recalc_radius();
self.ui_state.properties_panel_dirty = true;
radius_changed = true;
}
display_radius |= response.hovered() || response.has_focus();

self.ui_state.display_radius = self.always_show_radius || display_radius;

if radius_changed {
self.model.recheck_warnings(One(Warning::RadiusTooSmall(selected_id)));
Expand Down

0 comments on commit 3d0ffcf

Please sign in to comment.