input stuff

This commit is contained in:
2021-10-14 07:30:12 +02:00
parent 3d449456e6
commit 35f3d0e4a8
4 changed files with 72 additions and 51 deletions

View File

@@ -12,9 +12,9 @@ use player::Player;
use crate::{config::LogConfig, vulkan};
use crate::input::InputState;
use crate::vulkan::{Game, MeshHandle, TextVertex, Texture, TextureHandle, Vertex, VulkanRenderer};
use crate::vulkan::{Game, MeshHandle, TextVertex, TextureHandle, Vertex, VulkanRenderer};
use crate::vulkan::gameobject::{GameObject, GameObjectHandle, Updatable};
use crate::vulkan::mesh::{self, CPUMesh, CPUVertex};
use crate::vulkan::mesh::{self, CPUMesh, CPUVertexList};
use crate::vulkan::pipelines::vs::ty::ObjectUniformData;
pub mod player;
@@ -30,7 +30,8 @@ pub struct TestGame {
pub last_time: f32,
pub components: Vec<Box<dyn Updatable>>,
pub paused: bool,
pub font: FontArc
pub font: FontArc,
pub test_str: String,
}
impl Game for TestGame {
@@ -82,10 +83,17 @@ impl Game for TestGame {
}
if self.input.button_just_pressed("test") {
// self.paused = !self.paused;
self.text_objects[0].update_text("holy shit look at this", 200.0, renderer, &mut self.game_objects);
self.paused = !self.paused;
}
for char in self.input.typed_characters.iter() {
match char {
'\u{8}' => { self.test_str.pop(); },
c => { self.test_str.push(*c); },
}
}
self.text_objects[0].update_text(&self.test_str, 30., renderer, &mut self.game_objects);
// Custom game object stuff
let light_pos = vec3(2.0, 0.5, 2.0);
if !self.paused {
@@ -124,6 +132,7 @@ impl TestGame {
components: vec![],
paused: false,
font: FontArc::try_from_slice(include_bytes!("../../models/FiraCode-Regular.ttf")).unwrap(),
test_str: String::new()
}
}
@@ -255,17 +264,18 @@ pub fn update_text_quads(quads: Vec<Vec<TextVertex>>, texture_index: usize, mesh
let mut final_indices: Vec<u32> = vec![];
let mut index_offset = 0;
for quad in quads {
final_vertices.append(&mut quad.iter().map(|v| CPUVertex::VertexText(TextVertex { position: v.position, uv: [v.uv[0], v.uv[1]] })).collect());
for mut quad in quads {
let len = quad.len();
final_vertices.append(&mut quad);
final_indices.append(&mut [0, 2, 3, 0, 3, 1].iter().map(|x| *x + index_offset).collect());
index_offset += quad.len() as u32;
index_offset += len as u32;
}
if let Some(idx) = mesh_index {
renderer.update_mesh(idx, final_vertices, final_indices);
renderer.update_mesh(idx, CPUVertexList::VertexText(final_vertices), final_indices);
None
} else {
let mesh = CPUMesh {
vertices: final_vertices,
vertices: CPUVertexList::VertexText(final_vertices),
indices: final_indices,
local_texture_index: Some(texture_index),
local_normal_map_index: None,

View File

@@ -10,7 +10,7 @@ use gilrs;
use gilrs::{EventType, Gilrs};
use serde_derive::{Deserialize, Serialize};
use toml;
use winit::event::{DeviceEvent, ElementState, Event, ModifiersState, MouseButton, MouseScrollDelta, ScanCode, Touch, TouchPhase, WindowEvent};
use winit::event::{DeviceEvent, ElementState, Event, ModifiersState, MouseButton, MouseScrollDelta, ScanCode, Touch, TouchPhase, VirtualKeyCode, WindowEvent};
use crate::config::LogConfig;
@@ -77,15 +77,16 @@ pub struct InputState {
pub mouse_delta_x: f64,
pub mouse_delta_y: f64,
pub mouse_position: Vector2<f64>,
pub typed_characters: Vec<char>,
input_events: HashSet<DigitalInputEvent>,
pressed_scan_codes: HashSet<ScanCode>,
pressed_mouse_buttons: HashSet<MouseButton>,
pub pressed_scan_codes: HashSet<ScanCode>,
pub pressed_mouse_buttons: HashSet<MouseButton>,
pressed_touch_positions: HashMap<u64, Vector2<f64>>,
analog_wheel_state: f32,
config: InputConfigConfig,
log_config: LogConfig,
controller_input: Gilrs,
touch_inputs: Vec<TouchInput>
touch_inputs: Vec<TouchInput>,
}
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
@@ -115,7 +116,7 @@ impl InputState {
input_events: HashSet::new(), pressed_scan_codes: HashSet::new(),
pressed_mouse_buttons: HashSet::new(), pressed_touch_positions: HashMap::new(), touch_inputs: vec![], analog_wheel_state: 0.0,
config: config.config, mouse_delta_x: 0.0, mouse_delta_y: 0.0, mouse_position: Vector2::zero(), log_config,
controller_input: Gilrs::new().unwrap() };
controller_input: Gilrs::new().unwrap(), typed_characters: vec![] };
// Create virtual buttons from config
config.button.iter().for_each(|bn| {
@@ -130,7 +131,7 @@ impl InputState {
// Keyboard buttons
if let Some(scan_code) = bn.scan_code {
inputs.push(DigitalInput::Keyboard(KeyboardInput { scan_code, modifiers: modifiers.clone() }));
inputs.push(DigitalInput::Keyboard(KeyboardInput { scan_code, key_code: None, modifiers: modifiers.clone() }));
}
// Mouse buttons
@@ -235,7 +236,7 @@ impl InputState {
}
}
self.on_keyboard_event(input.state, input.scancode, KeyboardModifierState::from_deprecated_state(&input.modifiers));
self.on_keyboard_event(input.state, input.scancode, input.virtual_keycode, KeyboardModifierState::from_deprecated_state(&input.modifiers));
},
Event::WindowEvent { event: WindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => {
if self.log_config.input.buttons {
@@ -277,6 +278,9 @@ impl InputState {
}
self.on_touch_event(touch);
},
Event::WindowEvent { event: WindowEvent::ReceivedCharacter(c), .. } => {
self.typed_characters.push(*c);
},
_ => {}
}
}
@@ -300,6 +304,26 @@ impl InputState {
}
}
pub fn _scan_code_just_pressed(&self, scan_code: &ScanCode) -> bool {
self.input_events.iter().any(|ie| match ie {
DigitalInputEvent::Pressed(di) => match di {
DigitalInput::Keyboard(ki) => ki.scan_code == *scan_code,
_ => false,
},
_ => false,
})
}
pub fn _scan_code_just_released(&self, scan_code: &ScanCode) -> bool {
self.input_events.iter().any(|ie| match ie {
DigitalInputEvent::Released(di) => match di {
DigitalInput::Keyboard(ki) => ki.scan_code == *scan_code,
_ => false,
},
_ => false,
})
}
pub fn button_just_pressed(self: &Self, button_code: &str) -> bool {
match self.virtual_buttons.get(button_code) {
Some(virtual_button) => {
@@ -500,8 +524,8 @@ impl InputState {
if input.digital_inputs.iter().any(|di| self.digital_input_pressed(di)) { 1.0 } else { 0.0 }
}
pub fn on_keyboard_event(self: &mut Self, state: ElementState, scan_code: ScanCode, modifiers: KeyboardModifierState) {
let input = DigitalInput::Keyboard(KeyboardInput { scan_code, modifiers });
pub fn on_keyboard_event(self: &mut Self, state: ElementState, scan_code: ScanCode, key_code: Option<VirtualKeyCode>, modifiers: KeyboardModifierState) {
let input = DigitalInput::Keyboard(KeyboardInput { scan_code, key_code, modifiers });
match state {
ElementState::Pressed => {
if self.pressed_scan_codes.contains(&scan_code) { return; }
@@ -604,6 +628,7 @@ impl InputState {
self.mouse_delta_x = 0.0;
self.mouse_delta_y = 0.0;
self.input_events.clear();
self.typed_characters.clear();
// Store final touch positions as base for next frame
for touch_input in &mut self.touch_inputs {
@@ -641,6 +666,7 @@ pub enum DigitalInput {
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub struct KeyboardInput {
scan_code: ScanCode,
key_code: Option<VirtualKeyCode>,
modifiers: KeyboardModifierState,
}

View File

@@ -28,14 +28,14 @@ impl From<String> for LoadError {
}
#[derive(Debug)]
pub enum CPUVertex {
Vertex3D(Vertex),
VertexText(TextVertex)
pub enum CPUVertexList {
Vertex3D(Vec<Vertex>),
VertexText(Vec<TextVertex>)
}
#[derive(Debug)]
pub struct CPUMesh {
pub vertices: Vec<CPUVertex>,
pub vertices: CPUVertexList,
pub indices: Vec<u32>,
pub local_texture_index: Option<usize>,
pub local_normal_map_index: Option<usize>,
@@ -88,16 +88,18 @@ pub fn load_mesh<V>(mesh_path: &str, print_status: bool) -> Result<(Vec<CPUMesh>
reader.read_tangents(),
reader.read_joints(0),
reader.read_weights(0));
let vertices = vertices_result?.iter().map(|v| CPUVertex::Vertex3D(v.clone())).collect();
let verts = vertices_result?;
let vert_count = verts.len();
let cpu_mesh = CPUMesh {
vertices,
vertices: CPUVertexList::Vertex3D(verts),
indices: indices.into_u32().collect(),
local_texture_index: texture_index,
local_normal_map_index: normal_map_index,
name: mesh.name().map(|n| n.to_owned()),
};
if print_status {
let vert_count = cpu_mesh.vertices.len();
let index_count = cpu_mesh.indices.len();
total_vertices += vert_count;

View File

@@ -31,7 +31,7 @@ use pipelines::vs;
use crate::config::RenderConfig;
use crate::vulkan::gameobject::GameObject;
use self::mesh::CPUVertex;
use self::mesh::{CPUVertexList};
pub mod pipelines;
pub mod gameobject;
@@ -428,22 +428,13 @@ impl VulkanRenderer {
pub fn upload_mesh(self: &mut Self, mesh: CPUMesh, original_path: Option<String>) -> usize {
let index_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::index_buffer(), false, mesh.indices.into_iter()).unwrap();
match mesh.vertices.get(0).unwrap() {
CPUVertex::Vertex3D(_) => {
let verts: Vec<Vertex> = mesh.vertices.into_iter().filter_map(|v| match v {
CPUVertex::Vertex3D(vert) => Some(vert),
CPUVertex::VertexText(_) => None
}).collect();
match mesh.vertices {
CPUVertexList::Vertex3D(verts) => {
let vertex_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::vertex_buffer(), false, verts.into_iter()).unwrap();
self.game_data.meshes.push(Mesh { vertex_buffer, index_buffer, original_path });
self.game_data.meshes.len() - 1
},
CPUVertex::VertexText(_) => {
let verts: Vec<TextVertex> = mesh.vertices.into_iter().filter_map(|v| match v {
CPUVertex::Vertex3D(_) => None,
CPUVertex::VertexText(vert) => Some(vert)
}).collect();
CPUVertexList::VertexText(verts) => {
let vertex_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::vertex_buffer(), false, verts.into_iter()).unwrap();
self.game_data.meshes_text.push(Mesh { vertex_buffer, index_buffer, original_path });
self.game_data.meshes_text.len() - 1
@@ -451,24 +442,16 @@ impl VulkanRenderer {
}
}
pub fn update_mesh(self: &mut Self, mesh_index: usize, vertices: Vec<CPUVertex>, indices: Vec<u32>) {
pub fn update_mesh(self: &mut Self, mesh_index: usize, vertices: CPUVertexList, indices: Vec<u32>) {
let index_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::index_buffer(), false, indices.into_iter()).unwrap();
match vertices.get(0).unwrap() {
CPUVertex::Vertex3D(_) => {
let verts: Vec<Vertex> = vertices.into_iter().filter_map(|v| match v {
CPUVertex::Vertex3D(vert) => Some(vert),
CPUVertex::VertexText(_) => None
}).collect();
match vertices {
CPUVertexList::Vertex3D(verts) => {
let mesh = &mut self.game_data.meshes[mesh_index];
mesh.vertex_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::vertex_buffer(), false, verts.into_iter()).unwrap();
mesh.index_buffer = index_buffer;
},
CPUVertex::VertexText(_) => {
let verts: Vec<TextVertex> = vertices.into_iter().filter_map(|v| match v {
CPUVertex::Vertex3D(_) => None,
CPUVertex::VertexText(vert) => Some(vert)
}).collect();
CPUVertexList::VertexText(verts) => {
let mesh = &mut self.game_data.meshes_text[mesh_index];
mesh.vertex_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::vertex_buffer(), false, verts.into_iter()).unwrap();
mesh.index_buffer = index_buffer;