Skip to content

Commit

Permalink
Add app handler
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed May 15, 2024
1 parent 66d546f commit ce0c4d6
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 77 deletions.
60 changes: 57 additions & 3 deletions dunge/src/el.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@ use {
state::State,
time::{Fps, Time},
update::Update,
window::View,
window::{self, View},
},
std::{cell::Cell, error, fmt, ops, time::Duration},
wgpu::SurfaceError,
winit::{
application::ApplicationHandler,
error::EventLoopError,
event,
event_loop::{self, EventLoop},
event_loop::{self, ActiveEventLoop, EventLoop},
keyboard,
window::WindowId,
},
};

pub fn run_local<U>(view: View, upd: U) -> Result<(), window::Error>
where
U: Update,
{
let lu = EventLoop::with_user_event().build()?;
let mut handler = Handler::new(view, upd);
lu.run_app(&mut handler)?;
Ok(())
}

/// Code representing the location of a physical key.
pub type KeyCode = keyboard::KeyCode;

Expand All @@ -25,6 +37,7 @@ pub type SmolStr = keyboard::SmolStr;
/// Describes a button of a mouse controller.
pub type MouseButton = event::MouseButton;

#[deprecated]
pub(crate) struct Loop<V>(EventLoop<V>)
where
V: 'static;
Expand Down Expand Up @@ -94,10 +107,51 @@ impl error::Error for LoopError {
}
}

struct Handler<U> {
ctrl: Control,
upd: U,
}

impl<U> Handler<U> {
fn new(view: View, upd: U) -> Self {
let ctrl = Control {
view,
resized: None,
min_delta_time: Cell::new(Duration::from_secs_f32(1. / 60.)),
delta_time: Duration::ZERO,
fps: 0,
pressed_keys: vec![],
released_keys: vec![],
cursor_position: None,
mouse: Mouse {
wheel_delta: (0., 0.),
pressed_buttons: Buttons(vec![]),
released_buttons: Buttons(vec![]),
},
};

Self { ctrl, upd }
}
}

impl<U> ApplicationHandler<U::Event> for Handler<U>
where
U: Update,
{
fn resumed(&mut self, el: &ActiveEventLoop) {
todo!()
}

fn window_event(&mut self, el: &ActiveEventLoop, id: WindowId, ev: event::WindowEvent) {
todo!()
}
}

type Event<V> = event::Event<V>;
type Target = event_loop::ActiveEventLoop;
type Failure = Option<Box<dyn error::Error>>;

#[deprecated]
fn handle<U>(cx: Context, view: View, mut upd: U) -> impl FnMut(Event<U::Event>, &Target) -> Failure
where
U: Update,
Expand All @@ -112,7 +166,7 @@ where
const WAIT_TIME: Duration = Duration::from_millis(100);

let mut ctrl = Control {
view,
view: View::new(unimplemented!()),
resized: None,
min_delta_time: Cell::new(Duration::from_secs_f32(1. / 60.)),
delta_time: Duration::ZERO,
Expand Down
4 changes: 3 additions & 1 deletion dunge/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ use crate::{
///
pub trait Update: Draw {
type Flow: Flow;
type Event;
type Event: 'static;
fn update(&mut self, ctrl: &Control) -> Self::Flow;
fn event(&mut self, _: Self::Event) {}
}
Expand Down Expand Up @@ -129,6 +129,7 @@ pub fn update_with_event<S, U, E, V, F, D>(
where
U: FnMut(&mut S, &Control) -> F,
E: FnMut(&mut S, V),
V: 'static,
F: Flow,
D: Fn(&S, Frame),
{
Expand All @@ -155,6 +156,7 @@ where
where
U: FnMut(&mut S, &Control) -> F,
E: FnMut(&mut S, V),
V: 'static,
F: Flow,
D: Fn(&S, Frame),
{
Expand Down
Loading

0 comments on commit ce0c4d6

Please sign in to comment.