pel 0.1.0

OpenGL backed framebuffer
Documentation
use std::marker::PhantomData;

use glutin::{
    event::{DeviceEvent, DeviceId, MouseScrollDelta, StartCause, WindowEvent},
    event_loop::ControlFlow,
    window::WindowId,
};

use crate::{input::MouseInput, Context, Events};

pub(crate) struct Wrapper<E> {
    inner: E,
    phantom: PhantomData<*const ()>, // !Send + !Sync
}

impl<E: Events + 'static> Wrapper<E> {
    pub(crate) fn build(inner: E) -> Self {
        Self {
            inner,
            phantom: PhantomData,
        }
    }

    pub(crate) fn window_event(&mut self, ctx: &mut Context, _: WindowId, event: WindowEvent, flow: &mut ControlFlow) {
        match event {
            WindowEvent::Resized(physical_size) => {
                ctx.resize_viewport(physical_size);
                let (w, h): (u32, u32) = physical_size.into();
                self.inner.window_resized(ctx, (w as usize, h as usize));
            },
            WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
                ctx.resize_viewport(*new_inner_size);
                let (w, h): (u32, u32) = (*new_inner_size).into();
                self.inner.window_resized(ctx, (w as usize, h as usize));
            },
            WindowEvent::Focused(focus) => {
                self.inner.window_focus(ctx, focus);
            },
            WindowEvent::CursorLeft { .. } => {
                self.inner.cursor_focus(ctx, false);
            },
            WindowEvent::CursorEntered { .. } => {
                self.inner.cursor_focus(ctx, true);
            },
            WindowEvent::KeyboardInput { input, .. } => {
                self.inner.key_input(ctx, input.into());
            },
            WindowEvent::ReceivedCharacter(character) => {
                self.inner.text_input(ctx, character);
            },
            WindowEvent::CursorMoved { position, .. } => {
                let (x, y): (u32, u32) = position.into();
                self.inner.mouse_moved(ctx, (x as usize, y as usize));
            },
            WindowEvent::MouseInput { state, button, .. } => {
                self.inner.mouse_click(ctx, MouseInput {
                    state: state.into(),
                    button: button.into(),
                });
            },
            WindowEvent::MouseWheel { delta, .. } => {
                if let MouseScrollDelta::LineDelta(_, y_delta) = delta {
                    self.inner.mouse_scroll(ctx, y_delta);
                }
            },
            WindowEvent::CloseRequested => {
                if self.inner.exit_requested(ctx) {
                    *flow = ControlFlow::Exit;
                } else {
                    ctx.request_redraw();
                }
            },
            WindowEvent::Destroyed => {
                *flow = ControlFlow::Exit;
            },
            _ => (),
        }
    }

    pub(crate) fn device_event(&mut self, ctx: &mut Context, _: DeviceId, event: DeviceEvent) {
        if let DeviceEvent::ModifiersChanged(modifiers) = event {
            self.inner.keyboard_modifiers(ctx, modifiers.into());
        }
    }

    pub(crate) fn start(&mut self, ctx: &mut Context, cause: StartCause) {
        if let StartCause::Init = cause {
            self.inner.init(ctx);
            ctx.request_redraw();
        }
    }

    pub(crate) fn update(&mut self, ctx: &mut Context) {
        self.inner.update(ctx);
        ctx.request_redraw();
    }

    pub(crate) fn draw(&mut self, ctx: &mut Context, _: WindowId) {
        self.inner.draw(ctx);
        ctx.draw();
        ctx.swap_buffers();
    }

    pub(crate) fn loop_destroyed(&mut self) {
        self.inner.exit();
    }
}