diff --git a/src/graphics.rs b/src/graphics.rs index 0eebe15..138c79b 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -23,15 +23,14 @@ impl Vertex { #[repr(C)] #[derive(Clone, Copy, Zeroable, Pod, Debug)] pub struct Uniforms { - // [x, y, zoom] - pub camera: [f32; 3], - pub darkness: f32 + pub camera: [f32; 2], + pub zooms: [f32; 2] } impl Default for Uniforms { fn default() -> Self { Self { - camera: [0., 0., 1.], - darkness: 0. + camera: [0., 0.], + zooms: [1., 1.] } } } @@ -82,7 +81,6 @@ impl<'a> Graphics<'a> { ) .await .expect("Failed to create device"); - dbg!(&device); let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Vertex Buffer"), diff --git a/src/lib.rs b/src/lib.rs index 9c4a290..469d8cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,6 +30,11 @@ impl ApplicationHandler for App<'_> { WindowAttributes::default() .with_inner_size(PhysicalSize::new(1080*2/5, 2000*2/5)) ).unwrap(); + #[cfg(target_os = "android")] + let window = event_loop.create_window( + WindowAttributes::default() + // .with_inner_size(PhysicalSize::new(1080*2/5, 2000*2/5)) + ).unwrap(); self.window = Some(Arc::new(window)); self.graphics = Some(pollster::block_on(Graphics::init(&self.state, self.window.clone().unwrap()))); } @@ -39,7 +44,7 @@ impl ApplicationHandler for App<'_> { window_id: winit::window::WindowId, event: WindowEvent, ) { - match event { + match &event { WindowEvent::CloseRequested => event_loop.exit(), WindowEvent::RedrawRequested => { if let Some(g) = &mut self.graphics { @@ -52,21 +57,10 @@ impl ApplicationHandler for App<'_> { WindowEvent::MouseWheel { delta, .. } => { dbg!(delta); }, - e => { - if let Some(g) = &mut self.graphics { - g.window_event(&e, &self.window.as_ref().unwrap()); - } - self.state.window_event(&e, &self.window.as_ref().unwrap()); - } + _ => {} } - } - fn device_event( - &mut self, - event_loop: &winit::event_loop::ActiveEventLoop, - device_id: winit::event::DeviceId, - event: winit::event::DeviceEvent, - ) { - self.state.device_event(&event); + self.graphics.as_mut().unwrap().window_event(&event, &self.window.as_ref().unwrap()); + self.state.window_event(&event, &self.window.as_ref().unwrap()); } fn about_to_wait(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) { if let Some(window) = self.window.as_ref() { @@ -195,9 +189,11 @@ pub fn main() { fn android_main(app: winit::platform::android::activity::AndroidApp) { use winit::platform::android::{EventLoopBuilderExtAndroid, activity::WindowManagerFlags}; - android_logger::init_once(android_logger::Config::default()); - log::error!("testbbbbbbbbbbb"); - println!("coucou"); + android_logger::init_once( + android_logger::Config::default() + .with_max_level(log::LevelFilter::Debug) + .with_filter(android_logger::FilterBuilder::new().parse("app").build()) + ); app.set_window_flags(WindowManagerFlags::KEEP_SCREEN_ON & WindowManagerFlags::FULLSCREEN, WindowManagerFlags::empty()); @@ -205,13 +201,15 @@ fn android_main(app: winit::platform::android::activity::AndroidApp) { .with_android_app(app) .build() .unwrap(); - let window = winit::window::WindowBuilder::new().build(&event_loop).unwrap(); + // let window = winit::window::WindowBuilder::new().build(&event_loop).unwrap(); - App { - window: &window, - graphics: None, - state: State::new() - }.run(event_loop); + event_loop.run_app(&mut App::new()).unwrap(); + + // App { + // window: &window, + // graphics: None, + // state: State::new() + // }.run(event_loop); // main(); // use std::thread; // use std::time::Duration; diff --git a/src/shader.wgsl b/src/shader.wgsl index f367341..e89bcfb 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -1,6 +1,6 @@ struct Uniforms { - camera: vec3f, - darkness: f32 + camera: vec2f, + zooms: vec2f } @group(0) @binding(0) var uniforms : Uniforms; @@ -16,8 +16,7 @@ fn vs_main( ) -> VertexOutput { var out: VertexOutput; out.color = color; - // out.color[3] -= uniforms.darkness; - out.pos = vec4f((pos.xy-uniforms.camera.xy)/uniforms.camera.z, 0, 1); + out.pos = vec4f((pos - uniforms.camera) * uniforms.zooms, 0, 1); return out; } diff --git a/src/state.rs b/src/state.rs index 09872fd..a3a01a5 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,9 +1,9 @@ use std::{collections::{BTreeMap, HashMap}, time::Instant}; -use log::trace; +use log::{debug, trace}; use map::{CellKind, Map}; use rand::prelude::*; use voronoice::Point; -use winit::{dpi::PhysicalSize, event::{DeviceEvent, Event, KeyEvent, MouseButton, MouseScrollDelta, WindowEvent}, keyboard::{KeyCode, PhysicalKey}, window::Window}; +use winit::{dpi::PhysicalSize, event::{DeviceEvent, Event, KeyEvent, MouseButton, MouseScrollDelta, Touch, TouchPhase, WindowEvent}, keyboard::{KeyCode, PhysicalKey}, window::Window}; use crate::graphics::{Uniforms, Vertex}; @@ -17,6 +17,7 @@ pub struct State { pub vertices: Vec, pub indices: Vec, pub uniforms: Uniforms, + zoom: f32, map: Map, ui: UI, start: Instant, @@ -27,7 +28,8 @@ pub struct State { framerate: f32, // Update per second pub entities: BTreeMap, // entity id --> Entities next_eid: usize, - pub cells_entities: HashMap> // cell id --> entities id + pub cells_entities: HashMap>, // cell id --> entities id + touches: Vec } impl State { pub fn new() -> Self { @@ -35,6 +37,7 @@ impl State { vertices: vec![], indices: vec![], uniforms: Uniforms::default(), + zoom: 1., start: Instant::now(), last_frame: Instant::now(), t: 0, @@ -45,14 +48,45 @@ impl State { framerate: 1., entities: BTreeMap::new(), next_eid: 0, - cells_entities: HashMap::new() + cells_entities: HashMap::new(), + touches: Vec::new() }; // Create vertices / indices to estimate vertex / index buffer size s.render(PhysicalSize::new(1, 1)); s } + fn update_zooms(&mut self, screen_size: PhysicalSize) { + self.uniforms.zooms = [ + (screen_size.height as f32 / screen_size.width as f32).max(1.) * self.zoom, + (screen_size.width as f32 / screen_size.height as f32).max(1.) * self.zoom + ]; + } pub fn window_event(&mut self, event: &WindowEvent, window: &Window) { match event { + WindowEvent::Touch(touch) => { + debug!(target:"app", "{:?}", touch); + match touch.phase { + TouchPhase::Started => { + self.touches.push(*touch); + }, + TouchPhase::Moved => { + let w_size = window.inner_size(); + debug!(target:"app", "{:?}", w_size); + let old_touch_n = self.touches.iter().position(|t| t.id == touch.id).unwrap(); + let old_touch = self.touches[old_touch_n]; + self.uniforms.camera[0] -= (touch.location.x - old_touch.location.x) as f32/w_size.width as f32 * Map::WIDTH / self.uniforms.zooms[0]; + self.uniforms.camera[1] += (touch.location.y - old_touch.location.y) as f32/w_size.height as f32 * Map::HEIGHT / self.uniforms.zooms[1]; + debug!(target:"app", "{:?}", self.uniforms.camera); + self.touches[old_touch_n] = *touch; + }, + TouchPhase::Cancelled => { + self.touches.remove(self.touches.iter().position(|t| t.id == touch.id).unwrap()); + }, + TouchPhase::Ended => { + self.touches.remove(self.touches.iter().position(|t| t.id == touch.id).unwrap()); + } + } + } WindowEvent::MouseInput { state, button, ..} => { if state.is_pressed() { match button { @@ -75,10 +109,10 @@ impl State { }, WindowEvent::CursorMoved { position, .. } => { let w_size = window.inner_size(); - dbg!(position); + self.update_zooms(w_size); let pos = Point { - x: (((position.x / w_size.width as f64)*2.)-1.)*(w_size.width as f64/w_size.height as f64).min(1.)*(self.uniforms.camera[2] as f64) + self.uniforms.camera[0] as f64, - y: -(((position.y / w_size.height as f64)*2.)-1.)*(w_size.height as f64/w_size.width as f64).min(1.)*(self.uniforms.camera[2] as f64) + self.uniforms.camera[1] as f64 + x: (((position.x / w_size.width as f64)*2.)-1.)/(self.uniforms.zooms[0] as f64) + self.uniforms.camera[0] as f64, + y: -(((position.y / w_size.height as f64)*2.)-1.)/(self.uniforms.zooms[1] as f64) + self.uniforms.camera[1] as f64 }; let c = self.map.voronoi.cell(self.selected_tile); if self.mouse_pressed { @@ -97,49 +131,43 @@ impl State { if state.is_pressed() { match kc { KeyCode::KeyW => { - self.uniforms.camera[1] += 0.1; + self.uniforms.camera[1] += 0.1 * self.zoom; }, KeyCode::KeyS => { - self.uniforms.camera[1] -= 0.1; + self.uniforms.camera[1] -= 0.1 / self.zoom; }, KeyCode::KeyA => { - self.uniforms.camera[0] -= 0.1; + self.uniforms.camera[0] -= 0.1 / self.zoom; }, KeyCode::KeyD => { - self.uniforms.camera[0] += 0.1; + self.uniforms.camera[0] += 0.1 / self.zoom; }, KeyCode::KeyR => { - self.uniforms.camera[2] -= 0.1; - self.uniforms.camera[2] = self.uniforms.camera[2].clamp(0.1, 1.); + self.zoom += 0.1; + self.zoom = self.zoom.clamp(1., 10.); }, KeyCode::KeyF => { - self.uniforms.camera[2] += 0.1; - self.uniforms.camera[2] = self.uniforms.camera[2].clamp(0.1, 1.); + self.zoom -= 0.1; + self.zoom = self.zoom.clamp(1., 10.); }, _ => {} } + self.update_zooms(window.inner_size()); } }, - _ => {} - } - } - pub fn device_event(&mut self, event: &DeviceEvent) { - match event { - DeviceEvent::MouseWheel { delta } => { + WindowEvent::MouseWheel { delta, .. } => { self.framerate -= match delta { MouseScrollDelta::PixelDelta(pos) => pos.y as f32, MouseScrollDelta::LineDelta(_, y) => *y } * 0.1; self.framerate = self.framerate.max(0.); - }, + } _ => {} } } pub fn render(&mut self, screen_size: PhysicalSize) { - let screen_size = (screen_size.width as f32, screen_size.height as f32); - let y_ratio = (screen_size.0 / screen_size.1).max(1.); - let x_ratio = (screen_size.1 / screen_size.0).max(1.); trace!("render"); + self.update_zooms(screen_size); self.vertices = Vec::new(); self.indices = Vec::new(); @@ -185,11 +213,6 @@ impl State { } } - for v in self.vertices.iter_mut() { - v.pos[0] *= x_ratio; - v.pos[1] *= y_ratio; - } - self.ui.render(&mut self.vertices, &mut self.indices); } pub fn update_if_needed(&mut self) {