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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use crate::graphics::{
    Graphics,
    GraphicsSettings,
    two_dimensions::Graphics2D
};

#[cfg(feature="mouse_cursor_icon")]
use super::MouseCursorIconSettings;

use super::{
    // enums
    MouseButton,
    KeyboardButton,
    InnerWindowEvent,
    // structs
    WindowBase,
    GeneralSettings,
    WindowSettings,
};

use glium::{
    Display,
    Frame,
    draw_parameters::DrawParameters,
    backend::glutin::DisplayCreationError,
    SwapBuffersError
};

use glium::glutin::{
    ContextBuilder,
    NotCurrent,
    monitor::MonitorHandle,
    event_loop::EventLoop,
    event::{MouseScrollDelta,ModifiersState},
    window::WindowBuilder,
};

use image::DynamicImage;

use std::path::{Path,PathBuf};

/// Типаж для создания страниц окна.
/// A trait for implementing window pages.
pub trait WindowPage<'a>{
    type Window:Window+'a;

    /// The return type of output when the loop is closed.
    type Output;

    /// Called when the window has been requested to close.
    fn on_window_close_requested(&mut self,window:&mut Self::Window);

    /// feature != "lazy"
    #[cfg(not(feature="lazy"))]
    fn on_update_requested(&mut self,window:&mut Self::Window);

    fn on_redraw_requested(&mut self,window:&mut Self::Window);

    fn on_mouse_pressed(&mut self,window:&mut Self::Window,button:MouseButton);
    fn on_mouse_released(&mut self,window:&mut Self::Window,button:MouseButton);
    fn on_mouse_scrolled(&mut self,window:&mut Self::Window,scroll:MouseScrollDelta);
    fn on_mouse_moved(&mut self,window:&mut Self::Window,position:[f32;2]);

    fn on_keyboard_pressed(&mut self,window:&mut Self::Window,button:KeyboardButton);
    fn on_keyboard_released(&mut self,window:&mut Self::Window,button:KeyboardButton);
    fn on_character_recieved(&mut self,window:&mut Self::Window,character:char);

    fn on_window_resized(&mut self,window:&mut Self::Window,new_size:[u32;2]);
    fn on_window_moved(&mut self,window:&mut Self::Window,position:[i32;2]);

    fn on_window_focused(&mut self,window:&mut Self::Window,focused:bool);

    fn on_suspended(&mut self,window:&mut Self::Window);
    fn on_resumed(&mut self,window:&mut Self::Window);

    fn on_modifiers_changed(&mut self,window:&mut Self::Window,modifiers:ModifiersState);

    /// feature = "file_drop"
    #[cfg(feature="file_drop")]
    fn on_file_dropped(&mut self,window:&mut Self::Window,path:PathBuf);
    /// feature = "file_drop"
    #[cfg(feature="file_drop")]
    fn on_file_hovered(&mut self,window:&mut Self::Window,path:PathBuf);
    /// feature = "file_drop"
    #[cfg(feature="file_drop")]
    fn on_file_hovered_canceled(&mut self,window:&mut Self::Window);

    /// Event loop has been stopped.
    /// 
    /// The page closes after this function is called.
    fn on_event_loop_closed(&mut self,window:&mut Self::Window)->Self::Output;
}

/// Типаж, помогающий создать более сложное окно на базе `WindowBase`.
/// A trait that helps to create a more complex window based on a `WindowBase`.
pub trait Window:Sized{
    fn window_base(&self)->&WindowBase;
    fn window_base_mut(&mut self)->&mut WindowBase;

    fn raw(
        window_builder:WindowBuilder,
        context_builder:ContextBuilder<NotCurrent>,
        graphics_settings:GraphicsSettings,
        event_loop:EventLoop<InnerWindowEvent>,
        general_settings:GeneralSettings,

        #[cfg(feature="mouse_cursor_icon")]
        mouse_cursor_icon_settings:MouseCursorIconSettings<PathBuf>,
    )->Result<Self,DisplayCreationError>;

    fn new<F>(setting:F)->Result<Self,DisplayCreationError>
            where F:FnOnce(Vec<MonitorHandle>,&mut WindowSettings){
        let event_loop=EventLoop::<InnerWindowEvent>::with_user_event();
        let monitors=event_loop.available_monitors().collect();

        let mut window_settings=WindowSettings::new();


        // Настройка
        setting(monitors,&mut window_settings);


        let mut window_builder=WindowBuilder::default();
        window_builder.window=window_settings.window_attributes;

        let mut context_builder=ContextBuilder::new();
        context_builder.gl_attr.vsync=window_settings.vsync;
        context_builder.gl_attr.debug=window_settings.debug;

        context_builder.pf_reqs=window_settings.pixel_fmt_req;

        Self::raw(
            window_builder,
            context_builder,
            window_settings.graphics_base_settings,
            event_loop,
            window_settings.general,
            #[cfg(feature="mouse_cursor_icon")]window_settings.mouse_cursor_icon_settings,
        )
    }

    #[inline(always)]
    fn display(&self)->&Display{
        &self.window_base().display
    }

    /// Возвращает графическую основу.
    /// 
    /// Returns the graphics base.
    #[inline(always)]
    fn graphics(&mut self)->&mut Graphics2D{
        &mut self.window_base_mut().graphics
    }

    /// Даёт кадр для рисования.
    /// 
    /// Gives a frame for drawing.
    #[inline(always)]
    fn draw_raw<F:FnOnce(&mut DrawParameters,&mut Frame)>(&self,f:F)->Result<(),SwapBuffersError>{
        self.window_base().draw_raw(f)
    }

    /// Выполняет замыкание.
    /// 
    /// Executes the closure.
    #[inline(always)]
    fn draw<F:FnOnce(&mut DrawParameters,&mut Graphics)>(&self,f:F)->Result<(),SwapBuffersError>{
        self.window_base().draw(f)
    }


    /// Sets alpha channel for drawing with a changing alpha channel.
    #[cfg(feature="alpha_smoothing")]
    #[inline(always)]
    fn set_alpha(&mut self,alpha:f32){
        self.window_base_mut().set_alpha(alpha)
    }

    /// Sets smooth for drawing with a changing alpha channel.
    #[cfg(feature="alpha_smoothing")]
    #[inline(always)]
    fn set_smooth(&mut self,smooth:f32){
        self.window_base_mut().set_smooth(smooth)
    }

    /// Sets smooth and zeroes alpha channel for drawing with a changing alpha channel.
    #[cfg(feature="alpha_smoothing")]
    #[inline(always)]
    fn set_new_smooth(&mut self,smooth:f32){
        self.window_base_mut().set_new_smooth(smooth)
    }

    /// Выдаёт альфа-канал, возвращает его следующее значение.
    /// 
    /// Нужна для рисования с изменяющимся альфа-канала.
    /// 
    /// Gives an alpha channel, returns it's next value.
    /// 
    /// Needed for drawing with a changing alpha channel.
    /// 
    /// feature = "alpha_smoothing"
    #[cfg(feature="alpha_smoothing")]
    #[inline(always)]
    fn draw_smooth<F:FnOnce(f32,&mut DrawParameters,&mut Graphics)>(&mut self,f:F)->Result<f32,SwapBuffersError>{
        self.window_base_mut().draw_smooth(f)
    }


    /// Возвращает скриншот.
    /// 
    /// Returns a screenshot.
    #[inline(always)]
    fn screenshot(&self)->Option<DynamicImage>{
        self.window_base().screenshot()
    }

    /// Сохраняет скриншот в формате png.
    /// 
    /// Saves a screenshot in png format.
    #[inline(always)]
    fn save_screenshot<P:AsRef<Path>>(&self,path:P){
        self.window_base().save_screenshot(path)
    }




    /// feature = "mouse_cursor_icon"
    #[cfg(feature="mouse_cursor_icon")]
    #[inline(always)]
    fn set_user_cursor_visible(&mut self,visible:bool){
        self.window_base_mut().mouse_icon.set_visible(visible)
    }
}