Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Jun 12, 2023
1 parent 943789a commit 8c9b248
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
9 changes: 1 addition & 8 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,12 @@ pub fn handle_event_loop(event: Event<()>, state: &mut State, control_flow: &mut
..
} => {
println!("Space press");
state.current_pipeline_idx = if state.current_pipeline_idx == 1 {
0
} else {
1
};
state.window.set_title("SPACE");
}
WindowEvent::Resized(physical_size) => {
state.resize(*physical_size);
}
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
// new_inner_size is &&mut so we have to dereference it twice
state.resize(**new_inner_size);
}
_ => {}
Expand Down Expand Up @@ -74,8 +69,6 @@ pub fn handle_event_loop(event: Event<()>, state: &mut State, control_flow: &mut
//state.window.focus_window();
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
state.window.request_redraw();
}
_ => {}
Expand Down
65 changes: 58 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rand::prelude::*;
use wasm_bindgen::prelude::*;
use wgpu::util::DeviceExt;
use winit::{
event::WindowEvent,
event::{ElementState, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::EventLoop,
window::{Window, WindowBuilder},
};
Expand Down Expand Up @@ -35,7 +35,7 @@ fn setup_html_canvas(window: &mut Window) {
.and_then(|win| win.document())
.and_then(|doc| {
let dst = doc.get_element_by_id("wasm-example")?;
let mut canvas = web_sys::Element::from(window.canvas());
let canvas = web_sys::Element::from(window.canvas());
canvas.set_id("webgpu-canvas");
dst.append_child(&canvas).ok()?;
Some(())
Expand Down Expand Up @@ -67,7 +67,6 @@ pub async fn run() {
pub struct State<'a> {
device: wgpu::Device,
queue: wgpu::Queue,
current_pipeline_idx: u8,
clear_color: wgpu::Color,
texture_view_descriptor: wgpu::TextureViewDescriptor<'a>,
command_encoder_descriptor: wgpu::CommandEncoderDescriptor<'a>,
Expand Down Expand Up @@ -165,7 +164,7 @@ impl<'a> State<'a> {
let mut rng = rand::thread_rng();
let mut cells_vec = vec![0_u32; cells_width * cells_width];
for cell in cells_vec.iter_mut() {
if rng.gen::<f32>() < 0.20 {
if rng.gen::<f32>() < 0.30 {
*cell = 1;
}
}
Expand Down Expand Up @@ -408,7 +407,6 @@ impl<'a> State<'a> {
frame_count: 0,
device,
queue,
current_pipeline_idx: 0,
clear_color: wgpu::Color {
r: 0.0,
g: 0.0,
Expand Down Expand Up @@ -512,8 +510,61 @@ impl<'a> State<'a> {
Ok(())
}

fn input(&mut self, _event: &WindowEvent) -> bool {
false
fn input(&mut self, event: &WindowEvent) -> bool {
match event {
WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::NumpadAdd),
..
},
..
} => {
self.window.set_title("plus");
true
}
WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Minus),
..
},
..
} => {
self.window.set_title("minus");
true
}
WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Up),
..
},
..
} => {
self.window.set_title("up");
true
}
WindowEvent::ReceivedCharacter(c) => {
self.window.set_title(&format!("char: {}", c));
true
}
WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
..
},
..
} => {
println!("key = {:?}", event);
true
}
_ => false,
}
}
}

Expand Down

0 comments on commit 8c9b248

Please sign in to comment.