forestiles/src/state/ui.rs
2024-09-22 21:17:58 +02:00

92 lines
2.9 KiB
Rust

use winit::event::{Touch, TouchPhase, WindowEvent};
use crate::graphics::Vertex;
const BOTTOM_TAB_BAR_HEIGHT: f32 = 0.2;
const SECONDARY_COLOR: [f32; 4] = [84./255., 33./255., 32./255., 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 {
kind_selected: Kind::Entities,
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>) {
let vs = [
// Terrain
Vertex { color: SECONDARY_COLOR, pos: [-1., -1.+(BOTTOM_TAB_BAR_HEIGHT*2.)], effect: 0 },
Vertex { color: SECONDARY_COLOR, pos: [-1., -1.], effect: 0 },
Vertex { color: SECONDARY_COLOR, pos: [0., -1.], effect: 0 },
Vertex { color: SECONDARY_COLOR, pos: [0., -1.+(BOTTOM_TAB_BAR_HEIGHT*2.)], effect: 0 },
// Entities
// 3,
// 2,
Vertex { color: SECONDARY_COLOR, pos: [1., -1.], effect: 0 },
Vertex { color: SECONDARY_COLOR, pos: [1., -1.+(BOTTOM_TAB_BAR_HEIGHT*2.)], effect: 0 }
];
let ids = [
0,1,2,
2,3,0,
3,2,4,
4,5,3
];
let i = vertices.len() as u32;
vertices.extend_from_slice(&vs);
indices.extend_from_slice(&ids.map(|id| id+i));
}
}