From 47e5b44576e581ae0b62ad1e3bed444b8a82cefd Mon Sep 17 00:00:00 2001 From: metamuffin Date: Fri, 10 Jan 2025 15:35:23 +0100 Subject: spawn ui in world with click --- client/src/state.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'client/src/state.rs') diff --git a/client/src/state.rs b/client/src/state.rs index b0d19e6..70746d8 100644 --- a/client/src/state.rs +++ b/client/src/state.rs @@ -14,13 +14,17 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ -use crate::{camera::Camera, download::Downloader, network::Network, renderer::Renderer}; +use crate::{ + camera::Camera, download::Downloader, network::Network, renderer::Renderer, ui::UiSurface, +}; use anyhow::{Context, Result}; +use egui::ViewportId; use glam::{Vec2, Vec3}; use log::{info, warn}; -use std::{net::TcpStream, time::Instant}; +use rand::random; +use std::{net::TcpStream, sync::Arc, time::Instant}; use weareshared::{store::ResourceStore, tree::SceneTree}; -use winit::window::Window; +use winit::event::MouseButton; pub struct State<'a> { pub network: Network, @@ -35,10 +39,11 @@ pub struct DeltaState { time: Instant, pub move_dir: Vec3, pub mouse_acc: Vec2, + pub cursor_pos: Vec2, } impl<'a> State<'a> { - pub fn new(conn: TcpStream, window: &'a Window) -> Result> { + pub fn new(conn: TcpStream, window: &'a winit::window::Window) -> Result> { info!("new state"); Ok(Self { camera: Camera::new(), @@ -50,6 +55,7 @@ impl<'a> State<'a> { time: Instant::now(), move_dir: Vec3::ZERO, mouse_acc: Vec2::ZERO, + cursor_pos: Vec2::ZERO, }, }) } @@ -62,6 +68,26 @@ impl<'a> State<'a> { self.renderer.resize(width, height); self.camera.aspect = width as f32 / height as f32; } + pub fn click(&mut self, button: MouseButton, down: bool) { + if !down || button != MouseButton::Right { + return; + } + self.renderer.ui_renderer.surfaces.insert( + ViewportId::from_hash_of(random::()), + UiSurface { + transform: self.camera.new_ui_affine(), + content: Arc::new(|ctx| { + egui::Window::new("Funny window") + .default_open(true) + .show(ctx, |ui| { + ui.label("world space ui actually kinda works now"); + ui.label("Does input work?"); + ui.button("Yes").clicked(); + }); + }), + }, + ); + } pub fn update(&mut self) -> Result<()> { let now = Instant::now(); let dt = (now - self.delta.time).as_secs_f32(); -- cgit v1.2.3-70-g09d2