Skip to content

Commit

Permalink
Remove unused
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jun 2, 2024
1 parent 314b80a commit 913efb2
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 486 deletions.
252 changes: 1 addition & 251 deletions dunge/src/el.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use {
application::ApplicationHandler,
error::EventLoopError,
event::{self, StartCause, WindowEvent},
event_loop::{self, ActiveEventLoop, ControlFlow, EventLoop},
event_loop::{self, ActiveEventLoop, ControlFlow},
keyboard,
window::WindowId,
},
Expand Down Expand Up @@ -66,51 +66,6 @@ where
Ok(())
}

#[deprecated]
pub(crate) struct Loop<V>(EventLoop<V>)
where
V: 'static;

impl<V> Loop<V> {
pub fn new() -> Result<Self, EventLoopError> {
let inner = EventLoop::with_user_event().build()?;
Ok(Self(inner))
}

pub fn inner(&self) -> &EventLoop<V> {
&self.0
}

#[cfg(not(target_arch = "wasm32"))]
pub fn run<U>(self, cx: Context, view: View, upd: U) -> Result<(), LoopError>
where
U: Update<Event = V>,
{
let mut fail = Ok(());
let mut handle = handle(cx, view, upd);
let wrap = |ev, target: &_| {
if let Some(err) = handle(ev, target) {
fail = Err(LoopError::Failed(err));
}
};

let out = self.0.run(wrap).map_err(LoopError::EventLoop);
fail.or(out)
}

#[cfg(target_arch = "wasm32")]
pub fn spawn<U>(self, cx: Context, view: View, upd: U)
where
U: Update<Event = V> + 'static,
{
use winit::platform::web::EventLoopExtWebSys;

let mut handle = handle(cx, view, upd);
let wrap = move |ev, target: &_| _ = handle(ev, target);
self.0.spawn(wrap);
}
}

/// The event loop error.
#[derive(Debug)]
pub enum LoopError {
Expand Down Expand Up @@ -375,211 +330,6 @@ where
}
}

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

#[deprecated]
#[allow(dead_code)]
fn handle<U>(cx: Context, view: View, mut upd: U) -> impl FnMut(Event<U::Event>, &Target) -> Failure
where
U: Update,
{
use {
event::{ElementState, KeyEvent, MouseScrollDelta, StartCause, WindowEvent},
event_loop::ControlFlow,
keyboard::PhysicalKey,
winit::dpi::{PhysicalPosition, PhysicalSize},
};

const WAIT_TIME: Duration = Duration::from_millis(100);

let mut ctrl = Control {
view: unimplemented!(),
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![]),
},
};

// Initial state
let mut active = false;
let mut time = Time::now();
let mut fps = Fps::default();
move |ev, target| {
match ev {
Event::NewEvents(cause) => match cause {
StartCause::ResumeTimeReached { .. } => {
log::debug!("resume time reached");
ctrl.view.set_window_size();
ctrl.view.request_redraw();
}
StartCause::WaitCancelled {
requested_resume, ..
} => {
log::debug!("wait cancelled");
let flow = match requested_resume {
Some(resume) => ControlFlow::WaitUntil(resume),
None => ControlFlow::wait_duration(WAIT_TIME),
};

target.set_control_flow(flow);
}
StartCause::Poll => log::debug!("poll"),
StartCause::Init => {
// TODO: create the window here
log::debug!("init");
}
},
Event::WindowEvent { event, window_id } if window_id == ctrl.view.id() => match event {
WindowEvent::Resized(PhysicalSize { width, height }) => {
log::debug!("resized: {width}, {height}");
ctrl.resize(cx.state());
}
WindowEvent::CloseRequested => {
log::debug!("close requested");
target.exit();
}
WindowEvent::Focused(true) => {
log::debug!("focused");
ctrl.view.request_redraw();
}
WindowEvent::KeyboardInput {
event:
KeyEvent {
physical_key,
text,
location,
state,
..
},
is_synthetic: false,
..
} => {
let code = match physical_key {
PhysicalKey::Code(code) => {
log::debug!("keyboard input: {code:?}");
code
}
PhysicalKey::Unidentified(code) => {
log::debug!("keyboard input: (unidentified) {code:?}");
return None;
}
};

// TODO: Support key location
_ = location;

let key = Key { code, text };
match state {
ElementState::Pressed => ctrl.pressed_keys.push(key),
ElementState::Released => ctrl.released_keys.push(key),
}
}
WindowEvent::CursorMoved {
position: PhysicalPosition { x, y },
..
} => ctrl.cursor_position = Some((x as f32, y as f32)),
WindowEvent::CursorLeft { .. } => ctrl.cursor_position = None,
WindowEvent::MouseWheel {
delta: MouseScrollDelta::LineDelta(x, y),
..
} => {
ctrl.mouse.wheel_delta.0 += x;
ctrl.mouse.wheel_delta.1 += y;
}
WindowEvent::MouseInput { state, button, .. } => match state {
ElementState::Pressed => ctrl.mouse.pressed_buttons.push(button),
ElementState::Released => ctrl.mouse.released_buttons.push(button),
},
WindowEvent::RedrawRequested => {
if active {
log::debug!("redraw requested");
} else {
log::debug!("redraw requested (non-active)");

// Wait a while to become active
target.set_control_flow(ControlFlow::wait_duration(WAIT_TIME));
return None;
}

let delta_time = time.delta();
let min_delta_time = ctrl.min_delta_time.get();
if delta_time < min_delta_time {
let wait = min_delta_time - delta_time;
target.set_control_flow(ControlFlow::wait_duration(wait));
return None;
}

time.reset();
ctrl.delta_time = delta_time;
if let Some(fps) = fps.count(delta_time) {
ctrl.fps = fps;
}

match upd.update(&ctrl).flow() {
Then::Run => {}
Then::Close => {
log::debug!("close");
target.exit();
return None;
}
Then::Fail(err) => {
log::error!("failed: {err:?}");
target.exit();
return Some(err);
}
}

ctrl.clear_state();
match ctrl.view.output() {
Ok(output) => {
let target = output.target();
cx.state().draw(target, &upd);
output.present();
}
Err(SurfaceError::Timeout) => log::info!("suface error: timeout"),
Err(SurfaceError::Outdated) => log::info!("suface error: outdated"),
Err(SurfaceError::Lost) => {
log::info!("suface error: lost");
ctrl.resize(cx.state());
}
Err(SurfaceError::OutOfMemory) => {
log::error!("suface error: out of memory");
target.exit();
}
}
}
_ => {}
},
Event::UserEvent(ev) => upd.event(ev),
Event::Suspended => {
log::debug!("suspended");
active = false;
}
Event::Resumed => {
log::debug!("resumed");
active = true;
ctrl.view.request_redraw();

// Reset the timer before start the loop
time.reset();
}
_ => {}
}

None
}
}

/// The control type of the main event loop.
pub struct Control {
view: View,
Expand Down
4 changes: 2 additions & 2 deletions dunge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ pub use {
};

#[cfg(all(feature = "winit", not(target_arch = "wasm32")))]
pub use crate::window::{window, window_state};
pub use crate::window::window;

#[cfg(all(feature = "winit", target_arch = "wasm32"))]
pub use crate::window::{from_element, window_state_from_element};
pub use crate::window::from_element;

#[cfg(feature = "winit")]
pub use crate::{
Expand Down
Loading

0 comments on commit 913efb2

Please sign in to comment.