summaryrefslogtreecommitdiff
path: root/client/src/ui.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-01-10 22:04:34 +0100
committermetamuffin <metamuffin@disroot.org>2025-01-10 22:04:34 +0100
commit2c8360de342872f65bdef037f3fb5d598b8f26a0 (patch)
treed98b670f73a4f799f1f2c459caec09166b9304e9 /client/src/ui.rs
parentcb46b760ae8d4aeaa0e92a9c313927ffdef27873 (diff)
downloadweareserver-2c8360de342872f65bdef037f3fb5d598b8f26a0.tar
weareserver-2c8360de342872f65bdef037f3fb5d598b8f26a0.tar.bz2
weareserver-2c8360de342872f65bdef037f3fb5d598b8f26a0.tar.zst
unprojecting to ui surf is hard
Diffstat (limited to 'client/src/ui.rs')
-rw-r--r--client/src/ui.rs70
1 files changed, 50 insertions, 20 deletions
diff --git a/client/src/ui.rs b/client/src/ui.rs
index 54192e4..13e58d0 100644
--- a/client/src/ui.rs
+++ b/client/src/ui.rs
@@ -15,10 +15,10 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use egui::{
- Context, ImageData, TextureId, ViewportId, ViewportInfo,
+ Context, Event, ImageData, TextureId, ViewportId, ViewportInfo,
epaint::{ImageDelta, Primitive, Vertex},
};
-use glam::{Affine3A, Mat2, Mat3, Mat4};
+use glam::{Affine3A, Mat2, Mat3, Mat4, Vec2, vec2};
use log::info;
use rand::random;
use std::{
@@ -36,13 +36,15 @@ use wgpu::{
PrimitiveTopology, PushConstantRange, Queue, RenderPassColorAttachment,
RenderPassDepthStencilAttachment, RenderPassDescriptor, RenderPipeline,
RenderPipelineDescriptor, SamplerBindingType, SamplerDescriptor, ShaderStages, StoreOp,
- Texture, TextureAspect, TextureDescriptor, TextureDimension, TextureFormat, TextureSampleType,
- TextureUsages, TextureView, TextureViewDescriptor, TextureViewDimension, VertexBufferLayout,
- VertexState, VertexStepMode, include_wgsl,
+ SurfaceConfiguration, Texture, TextureAspect, TextureDescriptor, TextureDimension,
+ TextureFormat, TextureSampleType, TextureUsages, TextureView, TextureViewDescriptor,
+ TextureViewDimension, VertexBufferLayout, VertexState, VertexStepMode, include_wgsl,
util::{DeviceExt, TextureDataOrder},
vertex_attr_array,
};
+use crate::state::InputState;
+
pub struct UiRenderer {
device: Arc<Device>,
queue: Arc<Queue>,
@@ -55,7 +57,8 @@ pub struct UiRenderer {
pub struct UiSurface {
pub transform: Affine3A,
- pub content: Arc<dyn Fn(&Context) + Send + Sync + 'static>,
+ pub content: Arc<dyn Fn(&Context) -> bool + Send + Sync + 'static>,
+ size: Vec2,
index: Buffer,
index_capacity: usize,
vertex: Buffer,
@@ -149,7 +152,7 @@ impl UiRenderer {
pub fn add_surface(
&mut self,
transform: Affine3A,
- content: impl Fn(&Context) + Send + Sync + 'static,
+ content: impl Fn(&Context) -> bool + Send + Sync + 'static,
) {
let index_capacity = 1024;
let vertex_capacity = 1024;
@@ -165,17 +168,17 @@ impl UiRenderer {
usage: BufferUsages::VERTEX | BufferUsages::COPY_DST,
mapped_at_creation: false,
});
- self.surfaces.write().unwrap().insert(
- ViewportId::from_hash_of(random::<u128>()),
- UiSurface {
- transform,
- content: Arc::new(content),
- index,
- vertex,
- index_capacity,
- vertex_capacity,
- },
- );
+ let id = ViewportId::from_hash_of(random::<u128>());
+ info!("ui surface added: {id:?}");
+ self.surfaces.write().unwrap().insert(id, UiSurface {
+ transform,
+ content: Arc::new(content),
+ index,
+ vertex,
+ index_capacity,
+ vertex_capacity,
+ size: Vec2::ZERO,
+ });
}
pub fn apply_texture_delta(&self, texid: TextureId, delta: ImageDelta) {
@@ -273,6 +276,8 @@ impl UiRenderer {
target: &TextureView,
depth: &TextureView,
projection: Mat4,
+ input_state: &InputState,
+ surface_configuration: &SurfaceConfiguration,
) {
let mut surfaces = self.surfaces.write().unwrap();
if surfaces.is_empty() {
@@ -302,7 +307,20 @@ impl UiRenderer {
rpass.set_pipeline(&self.pipeline);
+ let _screen_size = vec2(
+ surface_configuration.width as f32,
+ surface_configuration.height as f32,
+ );
+
let mut raw_input = egui::RawInput::default();
+ raw_input.events.push(Event::PointerMoved(egui::Pos2::new(
+ input_state.cursor_pos.x,
+ input_state.cursor_pos.y,
+ )));
+ raw_input
+ .events
+ .extend(input_state.egui_events.iter().cloned());
+
raw_input.viewport_id = surfaces.keys().next().copied().unwrap();
raw_input.viewports = surfaces
.keys()
@@ -314,8 +332,16 @@ impl UiRenderer {
})
.collect();
- for (_viewport_id, surf) in surfaces.iter_mut() {
- let full_output = self.ctx.run(raw_input.clone(), |ctx| (surf.content)(ctx));
+ let mut surfaces_closed = Vec::new();
+ for (viewport_id, surf) in surfaces.iter_mut() {
+ let mut close = false;
+ let full_output = self.ctx.run(raw_input.clone(), |ctx| {
+ close = !(surf.content)(ctx);
+ surf.size = Vec2::new(ctx.used_size().x, ctx.used_size().y)
+ });
+ if close {
+ surfaces_closed.push(*viewport_id)
+ }
for (texid, delta) in full_output.textures_delta.set {
self.apply_texture_delta(texid, delta);
@@ -428,5 +454,9 @@ impl UiRenderer {
rpass.draw_indexed(index, base_vertex, 0..1);
}
}
+ for s in surfaces_closed {
+ info!("ui surface closed: {s:?}");
+ surfaces.remove(&s);
+ }
}
}