From eb3c837663e4eaf6730b949c97c8bb750cc59ea5 Mon Sep 17 00:00:00 2001 From: ghostly_zsh Date: Mon, 24 Mar 2025 11:30:14 -0500 Subject: [PATCH] mouse input added --- crates/client/src/components.rs | 21 +++++++++++++++++- crates/client/src/rendering/mod.rs | 34 +++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/crates/client/src/components.rs b/crates/client/src/components.rs index 61d5063629cf63e0613c359a10d57fdf40ccf32a..ee513daa40cc977f1dd9e024569f8721ee23251b 100644 --- a/crates/client/src/components.rs +++ b/crates/client/src/components.rs @@ -1,5 +1,5 @@ use bevy_ecs::{bundle::Bundle, component::Component, event::Event, system::Resource}; -use nalgebra::{Matrix4, Rotation2, Scale3, Translation3}; +use nalgebra::{Matrix3, Matrix4, Rotation2, Scale2, Scale3, Translation2, Translation3}; use starkingdoms_common::packet::Packet; #[derive(Component, Debug)] @@ -33,6 +33,25 @@ pub struct Camera { pub width: u32, // screen width (these are for aspect ratio) pub height: u32, // screen height } +impl Camera { + pub fn to_matrix(&self) -> Matrix4 { + let x_scale = self.zoom / self.width as f32 * 2.0; + let y_scale = self.zoom / self.height as f32 * 2.0; + Matrix4::from_vec(vec![ + x_scale, 0.0, 0.0, 0.0, + 0.0, y_scale, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + self.x*x_scale, self.y*y_scale, 0.0, 1.0, + ]) + } + pub fn to_cursor_matrix(&self) -> Matrix3 { + let x = -(self.width as f32 / 2.0); + let y = -(self.height as f32 / 2.0); + Translation2::new(-self.x, -self.y).to_homogeneous() + * Scale2::new(1.0/self.zoom, 1.0/self.zoom).to_homogeneous() + * Translation2::new(x, y).to_homogeneous() + } +} #[derive(Component, Debug, Clone, Copy)] pub struct Player; diff --git a/crates/client/src/rendering/mod.rs b/crates/client/src/rendering/mod.rs index dffeef1d38f2b2389202c416e4a693047ed4acd9..51368fd94520f7d81b1fe5f781e0671de44b0c04 100644 --- a/crates/client/src/rendering/mod.rs +++ b/crates/client/src/rendering/mod.rs @@ -5,6 +5,7 @@ use std::sync::Arc; use assets::Assets; use bevy_ecs::entity::Entity; use bevy_ecs::event::Events; +use bevy_ecs::query::With; use bevy_ecs::world::World; use egui::{Label, ProgressBar}; use egui_glow::EguiGlow; @@ -15,20 +16,22 @@ use glutin::surface::{Surface, WindowSurface, GlSurface, SwapInterval}; use glutin::{config::{ConfigTemplateBuilder, GlConfig}, context::{ContextApi, ContextAttributesBuilder, PossiblyCurrentContext}, display::GetGlDisplay, prelude::{GlDisplay, NotCurrentGlContext}}; #[cfg(not(target_arch = "wasm32"))] use glutin_winit::{DisplayBuilder, GlWindow}; -use starkingdoms_common::packet::Packet; +use nalgebra::{Vector3, Vector4}; +use starkingdoms_common::packet::{ButtonType, Packet}; use starkingdoms_common::PlanetType; #[cfg(target_arch = "wasm32")] use wasm_bindgen::{prelude::Closure, JsCast}; #[cfg(target_arch = "wasm32")] use web_sys::{Event, HtmlCanvasElement}; -use winit::event::{ElementState, MouseScrollDelta}; +use winit::dpi::PhysicalPosition; +use winit::event::{ElementState, MouseButton, MouseScrollDelta}; use winit::event_loop::ControlFlow; use winit::keyboard::{KeyCode, PhysicalKey}; #[cfg(target_arch = "wasm32")] use winit::platform::web::{WindowAttributesExtWebSys, WindowExtWebSys}; use winit::{application::ApplicationHandler, dpi::LogicalSize, event::WindowEvent, event_loop::ActiveEventLoop, raw_window_handle::HasWindowHandle, window::{Window, WindowAttributes}}; -use crate::components::{Camera, RecvPacket, SendPacket, Texture, Transform}; +use crate::components::{Camera, Player, RecvPacket, SendPacket, Texture, Transform}; use crate::networking::process_packets; use crate::networking::ws::Ws; #[cfg(not(target_arch="wasm32"))] @@ -66,6 +69,7 @@ pub struct App { amount: u32, max: u32, + mouse_pos: PhysicalPosition, } const VERTICES: [f32; 16] = [ @@ -326,6 +330,27 @@ impl ApplicationHandler for App { PhysicalKey::Unidentified(_) => {} // unsupported } } + WindowEvent::CursorMoved { position, .. } => self.mouse_pos = position, + WindowEvent::MouseInput { state, button, .. } => { + let button = match button { + MouseButton::Left => ButtonType::Left, + MouseButton::Middle => ButtonType::Middle, + MouseButton::Right => ButtonType::Right, + _ => return, + }; + let camera = self.world.get_resource::().unwrap(); + tracing::info!("{}, {}", self.mouse_pos.x, self.mouse_pos.y); + let view = camera.to_cursor_matrix(); + let pos = view * Vector3::new(self.mouse_pos.x as f32, self.mouse_pos.y as f32, 1.0); + let pos = pos / pos.z; + tracing::info!("{}, {}", pos.x, pos.y); + self.send_packet_events.send(SendPacket(Packet::PlayerMouseInput { + x: pos.x, + y: pos.y, + released: !state.is_pressed(), + button, + })); + } _ => {} } let event_response = self.egui_glow.as_mut().unwrap() @@ -351,6 +376,8 @@ impl ApplicationHandler for App { let window = self.window.as_ref().unwrap(); let gl = self.gl.as_ref().unwrap(); + let mut player = self.world.query_filtered::<&Transform, With>(); + let player = player.single(&self.world); self.egui_glow.as_mut().unwrap().run( self.window.as_ref().unwrap(), |ctx| { @@ -359,6 +386,7 @@ impl ApplicationHandler for App { ui.add(Label::new("Fuel:")); ui.add(ProgressBar::new((self.amount as f32)/(self.max as f32)).corner_radius(0)); + ui.add(Label::new(format!("Pos: {}, {}", player.translation.x as u32, player.translation.y as u32))); }); }, );