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
use crate::{
    Colour,
    graphics::{Graphics2D,Graphics,GraphicsSettings},
};

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

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

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

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

use image::DynamicImage;

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

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

    fn on_close_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_file_dropped(&mut self,window:&mut Self::Window,path:PathBuf);
    fn on_file_hovered(&mut self,window:&mut Self::Window,path:PathBuf);
    fn on_file_hovered_canceled(&mut self,window:&mut Self::Window);
}

/// Типаж, помогающий создать более сложное окно на базе `WindowBase`.
/// A trait that helps to create more a complex window based on `WindowBase`.
pub trait Window:Sized{
    type UserEvent:'static;

    fn window_base(&self)->&WindowBase<Self::UserEvent>;
    fn window_base_mut(&mut self)->&mut WindowBase<Self::UserEvent>;

    fn raw(
        window_builder:WindowBuilder,
        context_builder:ContextBuilder<NotCurrent>,
        graphics_settings:GraphicsSettings,
        event_loop:EventLoop<Self::UserEvent>,
        initial_colour:Option<Colour>,

        #[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::<Self::UserEvent>::with_user_event();
        let monitors=event_loop.available_monitors().collect();

        let mut window_settings=WindowSettings::new();


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

        let initial_colour=window_settings.initial_colour;

        #[cfg(feature="mouse_cursor_icon")]
        let (window_builder,context_builder,graphics_settings,mouse_cursor_icon_settings)
                =window_settings.devide::<PathBuf>();

        #[cfg(not(feature="mouse_cursor_icon"))]
        let (window_builder,context_builder,graphics_settings)
                =window_settings.devide();


        #[cfg(feature="mouse_cursor_icon")]
        let window=Self::raw(
            window_builder,
            context_builder,
            graphics_settings,
            event_loop,
            initial_colour,
            mouse_cursor_icon_settings,
        );

        #[cfg(not(feature="mouse_cursor_icon"))]
        let window=Self::raw(
            window_builder,
            context_builder,
            graphics_settings,
            event_loop,
            initial_colour,
        );

        window
    }

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

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

    /// Даёт прямое управление над кадром.
    /// 
    /// Gives frame to raw 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")]
    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)
    }
}