Skip to content

Commit

Permalink
fix: eliminate nametag/selection click-through by using egui responses
Browse files Browse the repository at this point in the history
  • Loading branch information
cohaereo committed Jul 13, 2024
1 parent 6e705b1 commit 1792beb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 36 deletions.
13 changes: 12 additions & 1 deletion crates/alkahest-renderer/src/renderer/pickbuffer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::mem::size_of;
use std::{
mem::size_of,
sync::atomic::{AtomicBool, Ordering},
};

use alkahest_data::{
dxgi::DxgiFormat,
Expand Down Expand Up @@ -114,6 +117,8 @@ pub struct Pickbuffer {
pub is_drawing_selection: bool,

pub(super) selection_request: AtomicCell<Option<(u32, u32)>>,
selection_ready: AtomicBool,

pub outline_depth: DepthState,
pub pick_buffer: RenderTarget,
pub pick_buffer_staging: CpuStagingBuffer,
Expand Down Expand Up @@ -155,6 +160,7 @@ impl Pickbuffer {
Ok(Self {
is_drawing_selection: false,
selection_request: AtomicCell::new(None),
selection_ready: AtomicBool::new(false),
outline_depth: DepthState::create(gctx.clone(), window_size)
.context("Outline Depth")?,
pick_buffer: RenderTarget::create(
Expand Down Expand Up @@ -205,6 +211,7 @@ impl Pickbuffer {

pub fn request_selection(&self, x: u32, y: u32) {
self.pocus().selection_request.store(Some((x, y)));
self.selection_ready.store(false, Ordering::Relaxed);
}

pub fn cancel_request(&self) {
Expand All @@ -214,6 +221,9 @@ impl Pickbuffer {
/// Finish the current selection request and return the entity id at the request coordinates
/// Must only be called after the current frame has been processed by the GPU
pub fn finish_request(&self) -> Option<u32> {
if !self.selection_ready.load(Ordering::Relaxed) {
return None;
}
self.pocus()
.selection_request
.take()
Expand Down Expand Up @@ -248,6 +258,7 @@ impl Pickbuffer {
pub fn end(&self, gpu: &GpuContext) {
self.pick_buffer.copy_to_staging(&self.pick_buffer_staging);
self.pocus().is_drawing_selection = false;
self.selection_ready.store(true, Ordering::Relaxed);
unsafe {
gpu.context().RSSetScissorRects(None);
}
Expand Down
16 changes: 1 addition & 15 deletions crates/alkahest/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl AlkahestApp {
false,
)
.unwrap();
renderer.set_render_settings(config::with(|c|{c.renderer.clone()}));
renderer.set_render_settings(config::with(|c| c.renderer.clone()));
resources.insert(renderer.clone());
let stringmap = Arc::new(GlobalStringmap::load());
resources.insert(stringmap);
Expand Down Expand Up @@ -260,20 +260,6 @@ impl AlkahestApp {
.update_mouse(Vec2::ZERO, scroll_y);
}
}
WindowEvent::MouseInput { .. } => {
let input = resources.get::<InputState>();
if input.mouse_left_clicked()
&& !gui.egui.wants_pointer_input()
&& !resources.get::<SelectedEntity>().changed_this_frame
{
if let Some(mouse_pos) = gui.egui.pointer_interact_pos() {
renderer.pickbuffer.request_selection(
(mouse_pos.x as f64 * window.scale_factor()).round() as u32,
(mouse_pos.y as f64 * window.scale_factor()).round() as u32,
);
}
}
}
WindowEvent::Resized(new_dims) => {
if let Some(swap_chain) = gctx.swap_chain.as_ref() {
let _ = gui
Expand Down
4 changes: 2 additions & 2 deletions crates/alkahest/src/gui/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use crate::{
bottom_bar::BottomBar,
configuration::RenderSettingsPanel,
console::ConsolePanel,
crosshair::CrosshairOverlay,
fps_display::FpsDisplayOverlay,
inspector::InspectorPanel,
menu::MenuBar,
node_gizmos::NodeGizmoOverlay,
crosshair::CrosshairOverlay,
outliner::OutlinerPanel,
profiler::PuffinProfiler,
tfx::{TfxErrorViewer, TfxExternEditor},
Expand Down Expand Up @@ -194,6 +194,7 @@ impl GuiViewManager {
pub fn with_default_views() -> Self {
let mut views = Self::default();

views.insert(NodeGizmoOverlay);
views.insert(TfxErrorViewer::default());
views.insert(TfxExternEditor::default());
views.insert(RenderSettingsPanel);
Expand All @@ -203,7 +204,6 @@ impl GuiViewManager {
views.insert(InspectorPanel);
views.insert(ConsolePanel::default());
views.insert(PuffinProfiler);
views.insert(NodeGizmoOverlay);
views.insert(CrosshairOverlay);

views.insert_overlay(FpsDisplayOverlay::default());
Expand Down
69 changes: 51 additions & 18 deletions crates/alkahest/src/gui/node_gizmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ use alkahest_renderer::{
transform::Transform,
},
icons::ICON_HELP,
renderer::RendererShared,
resources::Resources,
ColorExt,
};
use egui::{Color32, Context, Pos2, Rect};
use egui::{Color32, Context, Frame, Pos2, Rect, Sense, Ui};
use glam::Vec2;
use winit::window::Window;

use crate::{
config, gui::context::{GuiCtx, GuiView, ViewResult}, maplist::MapList
config,
gui::context::{GuiCtx, GuiView, ViewResult},
maplist::MapList,
};

pub struct NodeGizmoOverlay;
Expand All @@ -36,6 +39,24 @@ impl GuiView for NodeGizmoOverlay {
let screen_size = ctx.screen_rect().size();
let painter = ctx.layer_painter(egui::LayerId::background());

// egui::CentralPanel::default()
// .frame(Frame::default())
// .show(ctx, |ui| {

let mut panel_ui = Ui::new(
ctx.clone(),
egui::LayerId::background(),
"node_nametags".into(),
ctx.available_rect(),
ctx.screen_rect(),
);

let mut selected_entity = resources.get_mut::<SelectedEntity>();
let mut top_hovered = None;
let mut rp_list = vec![];
let response = panel_ui.allocate_response(panel_ui.available_size(), Sense::click());
// let response = panel_ui.allocate_response(egui::vec2(4.0, 4.0), Sense::click());

// {
// let mut debugshapes = resources.get_mut::<DebugShapes>().unwrap();
// for (text, point, anchor, color) in debugshapes.label_list() {
Expand Down Expand Up @@ -67,8 +88,6 @@ impl GuiView for NodeGizmoOverlay {

// if self.debug_overlay.borrow().show_map_resources {
if true {
let mut selected_entity = resources.get_mut::<SelectedEntity>();

let maps = resources.get::<MapList>();
if let Some(map) = maps.current_map() {
struct NodeDisplayPoint {
Expand All @@ -78,8 +97,6 @@ impl GuiView for NodeGizmoOverlay {
icon: Option<Icon>,
}

let mut rp_list = vec![];

let filters = resources.get::<NodeFilterSet>();
for (e, (transform, origin, label, icon, filter)) in map
.scene
Expand Down Expand Up @@ -176,8 +193,6 @@ impl GuiView for NodeGizmoOverlay {

rp_list.reverse();

let mut top_hovered = None;

for (i, (e, _, transform, node)) in rp_list.iter().enumerate() {
let projected_point = camera
.world_to_projective
Expand Down Expand Up @@ -230,9 +245,11 @@ impl GuiView for NodeGizmoOverlay {
);
}

if let Some(mouse_pos) = ctx.input(|i| i.pointer.latest_pos()) {
if debug_string_rect.expand(4.0).contains(mouse_pos) {
top_hovered = Some((i, debug_string_rect));
if response.hovered() {
if let Some(mouse_pos) = ctx.input(|i| i.pointer.latest_pos()) {
if debug_string_rect.expand(4.0).contains(mouse_pos) {
top_hovered = Some((i, debug_string_rect));
}
}
}

Expand Down Expand Up @@ -314,13 +331,14 @@ impl GuiView for NodeGizmoOverlay {
}
}

if let Some((top_index, top_rect)) = top_hovered {
let is_hovered = if egui_lmb_clicked(ctx).is_some() {
selected_entity.select(rp_list[top_index].0);
false
} else {
selected_entity.selected() != Some(rp_list[top_index].0)
};
if let Some((_top_index, top_rect)) = top_hovered {
// let is_hovered = if egui_lmb_clicked(ctx).is_some() {
// selected_entity.select(rp_list[top_index].0);
// false
// } else {
// selected_entity.selected() != Some(rp_list[top_index].0)
// };
let is_hovered = true;

painter.rect(
top_rect.expand(8.0),
Expand All @@ -340,6 +358,21 @@ impl GuiView for NodeGizmoOverlay {
}
}

if response.clicked() {
if let Some((top_index, _top_rect)) = top_hovered {
selected_entity.select(rp_list[top_index].0);
} else {
if let Some(mouse_pos) = ctx.pointer_interact_pos() {
let renderer = resources.get::<RendererShared>();
renderer.pickbuffer.request_selection(
(mouse_pos.x * ctx.pixels_per_point()).round() as u32,
(mouse_pos.y * ctx.pixels_per_point()).round() as u32,
);
}
}
}
// });

None
}
}
Expand Down

0 comments on commit 1792beb

Please sign in to comment.