This commit is contained in:
Arkitu 2024-09-19 21:40:59 +02:00
parent f7d1c4d96e
commit 287b0d28d8
2 changed files with 71 additions and 8 deletions

View File

@ -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<Vertex>,
@ -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;
}

View File

@ -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<Touch>
}
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<Vertex>, indices: &mut Vec<u32>) {