From 287b0d28d8a11cd50a98faeda72ef6f36fe88880 Mon Sep 17 00:00:00 2001 From: Arkitu <85173315+Arkitu@users.noreply.github.com> Date: Thu, 19 Sep 2024 21:40:59 +0200 Subject: [PATCH] save --- src/state.rs | 19 +++++++++++----- src/state/ui.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 8 deletions(-) diff --git a/src/state.rs b/src/state.rs index 671f915..7e51f2a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -11,7 +11,12 @@ mod entity; mod map; mod ui; use entity::{Entity, EntityKind, ExternOp}; -use ui::UI; +use ui::{Kind, UI}; + +fn rgba_to_grayscale(c: [f32; 4]) -> [f32; 4] { + let v = (c[0]*0.299) + (c[1]*0.587) + (c[2]*0.114); + [v, v, v, c[3]] +} pub struct State { pub vertices: Vec, @@ -65,16 +70,17 @@ impl State { ]; } pub fn window_event(&mut self, event: &WindowEvent, window: &Window) { + if self.ui.window_event(event) { + return + } 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]; let t = [ @@ -96,7 +102,6 @@ impl State { } self.uniforms.camera[0] -= (t[0] - old_t[0]) / self.touches.len() as f32; self.uniforms.camera[1] += (t[1] - old_t[1]) / self.touches.len() as f32; - debug!(target:"app", "{:?}", self.uniforms.camera); self.touches[old_touch_n] = *touch; }, TouchPhase::Cancelled => { @@ -191,6 +196,9 @@ impl State { for (c, cd) in self.map.voronoi.iter_cells().zip(self.map.cells_data.iter()).filter(|(_,cd)| cd.kind != CellKind::Forest) { let mut color = cd.color(); + if self.ui.kind_selected != Kind::Terrain && self.ui.kind_selected != Kind::None { + color = rgba_to_grayscale(color); + } if c.site() == self.selected_tile { color[0] = (color[0]+0.4).clamp(0., 1.); color[1] = (color[1]+0.4).clamp(0., 1.); @@ -209,7 +217,7 @@ impl State { } for e in self.entities.values() { - e.render(&mut self.vertices, &mut self.indices, &self.map); + e.render(&mut self.vertices, &mut self.indices, &self.map, ); } for (c, cd) in self.map.voronoi.iter_cells().zip(self.map.cells_data.iter()).filter(|(_,cd)| cd.kind == CellKind::Forest) { @@ -300,7 +308,6 @@ impl State { None => {self.entities.remove(&eid);} } } - self.t += 1; } diff --git a/src/state/ui.rs b/src/state/ui.rs index 3f9002c..7cba175 100644 --- a/src/state/ui.rs +++ b/src/state/ui.rs @@ -1,11 +1,67 @@ +use winit::event::{Touch, TouchPhase, WindowEvent}; use crate::graphics::Vertex; -pub struct UI { +const BOTTOM_TAB_BAR_HEIGHT: f32 = 0.1; +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Kind { + None, + Terrain, + Entities +} + +pub struct UI { + pub kind_selected: Kind, + touch: Option } impl UI { pub fn new() -> Self { - Self {} + Self { + kind_selected: Kind::None, + touch: None + } + } + // Returns true if event was handled by UI + pub fn window_event(&mut self, event: &WindowEvent) -> bool { + match event { + WindowEvent::Touch(ref t) => { + match t.phase { + TouchPhase::Started => { + if !self.touch.is_some() { + self.touch = Some(*t); + true + } else { + false + } + }, + TouchPhase::Moved => { + if let Some(old_t) = self.touch { + if old_t.id == t.id { + // TODO + true + } else { + false + } + } else { + false + } + }, + TouchPhase::Cancelled | TouchPhase::Ended => { + if let Some(old_t) = self.touch { + if old_t.id == t.id { + self.touch = None; + true + } else { + false + } + } else { + false + } + } + } + }, + _ => false + } } pub fn render(&self, vertices: &mut Vec, indices: &mut Vec) {