1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use window;
use gl;
use na as math;
use window::{Window, WindowT, EventsPoll};
use events::StreamT;
use util::Result;

use std::cell::RefCell;
use std::path::PathBuf;

pub struct Application<App>{
    app: RefCell<App>,
    window: window::Window,
    events: EventsPoll,
    renderer: gl::Renderer<'static>,
}

impl<App: ApplicationCallbacks> Application<App>{
    pub fn new(app: App, events: EventsPoll, window: Window, renderer: gl::Renderer<'static>) -> Result<Application<App>>{
        Ok(Application{
            app: RefCell::new(app),
            events,
            window,
            renderer
        })
    }

    pub fn run(&mut self){
        let mut events = self.window.event_stream().iter_async();
        while !self.window.should_close(){
            let frame_time_s = self.window.curr_frame_time_s();

			self.window.make_current();

			self.app.borrow_mut().update(frame_time_s, &mut self.renderer, &self.window);
			self.app.borrow_mut().draw(&mut self.renderer, &self.window);

            self.window.swap_buffers();

            self.events.poll_events();
            self.window.update();

			for event in events.by_ref(){
	            self.handle_event(&event);
			}

        }
    }

    #[cfg(not(target_os="android"))]
    fn handle_event(&mut self, event: &window::Event){
        //if let &events::Next(ref _time, ref event) = event{
        let mut app = self.app.borrow_mut();
        match *event{
            window::Event::KeyPressed{key, mods, repeat}	    => {app.key_pressed(key,mods,repeat);},
            window::Event::KeyReleased{key}     => {app.key_released(key);}
            window::Event::MouseMoved{pos}        => {app.mouse_moved(pos);}
            window::Event::MousePressed{pos,button,mods}   => {app.mouse_pressed(pos,button,mods);}
            window::Event::MouseReleased{pos,button,mods}  => {app.mouse_released(pos,button,mods);}
            window::Event::WindowMoved{pos}       => {app.window_moved(pos);}
            window::Event::WindowResized{size}    => {app.window_resized(size);}
            window::Event::Scroll{scroll}         => {app.scroll(scroll);}
            window::Event::WindowClosing          => {app.window_closing();}
            _ => {}
        }
	    //}
    }

    #[cfg(target_os="android")]
    fn handle_event(&mut self, event: &(u64,WindowEvent)){
        let &(_time,event) = event;
        match event{
            KeyPressedEvent{key}	    => {app.borrow_mut().key_pressed(key);},
            KeyReleasedEvent{key}       => {app.borrow_mut().key_released(key);},
            MouseMovedEvent{pos}        => {app.borrow_mut().mouse_moved(pos);}
            MousePressedEvent{pos,button}   => {app.borrow_mut().mouse_pressed(pos,button);}
            MouseReleasedEvent{pos,button}  => {app.borrow_mut().mouse_released(pos,button);}
            WindowMovedEvent{pos}       => {app.borrow_mut().window_moved(pos);}
            WindowResizedEvent{size}    => {app.borrow_mut().window_resized(size);}
            ScrollEvent{scroll}         => {app.borrow_mut().scroll(scroll);}
            WindowClosingEvent          => {
                debug!("closing");
                app.borrow_mut().window_closing();
            }
            PauseEvent                  => {
                debug!("pause");
                app_data.borrow().window.borrow_mut().set_should_close();
                app.borrow_mut().pause();
            }
            StopEvent                   => {
                debug!("stop");
                app_data.borrow().window.borrow_mut().set_should_close();
                app.borrow_mut().stop();
            }
            ResumeEvent                 => {
                debug!("resume");
                app.borrow_mut().resume();
            }
            DestroyEvent                => {
                debug!("destroy");
                app_data.borrow().window.borrow_mut().set_should_close();
                app.borrow_mut().destroy();
            }
            GLReadyEvent                => {app.borrow_mut().gl_ready();}
            _ => {}
        }
    }
}

#[cfg(not(target_os="android"))]
pub trait ApplicationCallbacks{
    fn update(&mut self, _delta: f64, _gl: &gl::Renderer, _window: &window::WindowT){}
    fn draw(&mut self, _gl: &gl::Renderer, _window: &window::WindowT){}

    fn key_pressed(&mut self, _key: window::Key, _mods: window::KeyModifiers, _repeat: bool){}
    fn key_released(&mut self, _key: window::Key){}

    fn mouse_moved(&mut self, _pos: math::Pnt2<f64>){}
    fn mouse_pressed(&mut self, _pos: math::Pnt2<f64>, _button: window::MouseButton, _mods: window::KeyModifiers){}
    fn mouse_released(&mut self, _pos: math::Pnt2<f64>, _button: window::MouseButton, _mods: window::KeyModifiers){}

    fn window_moved(&mut self, _pos: math::Pnt2<i32>){}
    fn window_resized(&mut self, _size: math::Vec2<i32>){}

    fn scroll(&mut self, _scroll: math::Vec2<f64>){}

    fn window_closing(&mut self){}
    fn dropped(&mut self, paths: &[PathBuf]){}
}

#[cfg(target_os="android")]
pub trait ApplicationCallbacks: window::LifeCycleListener + window::GLReadyListener{
    fn update<R: RendererMut>(&mut self, _frame_time_s: f64, gl: &mut R){}
    fn draw<R: RendererMut>(&mut self, gl: &mut R){}

    fn key_pressed(&mut self, _key: window::Key){}
    fn key_released(&mut self, _key: window::Key){}

    fn mouse_moved(&mut self, _pos: math::Pnt2<f64>){}
    fn mouse_pressed(&mut self, _pos: math::Pnt2<f64>, _button: window::MouseButton){}
    fn mouse_released(&mut self, _pos: math::Pnt2<f64>, _button: window::MouseButton){}

    fn window_moved(&mut self, _pos: math::Pnt2<i32>){}
    fn window_resized(&mut self, _size: math::Vec2<i32>){}

    fn scroll(&mut self, _scroll: math::Vec2<f64>){}

    fn window_closing(&mut self){}
    fn dropped(&mut self, paths: &[PathBuf]){}
}