cat_engine_basement 0.0.0-alpha7

The CatEnigne's basement
Documentation
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use crate::windows::{
    WinCore,
    WinError,
    core::window::{
        WindowData,
        WindowHandle
    },
    core::device_context::DeviceContextHandle,
    // level0::cursor::Cursor,
};

pub use crate::windows::core::window::{
    WindowStyle,
    WindowStyles,
    ExtendedWindowStyle,
    ExtendedWindowStyles,
};

use super::{
    // structures
    WindowClass,
    Monitor,
    // trait
    WindowProcedure,
    // functions
    window_procedure,
    // consts
    window_settings_auto_redraw,
};

use winapi::{
    um::{
        winuser::{
            RedrawWindow,
            // other
            CW_USEDEFAULT,
            RDW_INVALIDATE,
            SWP_SHOWWINDOW,
            SWP_NOSIZE,
            SWP_NOMOVE,
            SWP_FRAMECHANGED,
        },
    }
};

use std::{
    ptr::null_mut,
};


pub enum Fullscreen{
    None,
    Monitor(Monitor)
}

pub struct CreateParameters<A>{
    pub window_procedure:unsafe extern "system" fn(
        handle:WindowHandle,
        message:u32,
        w_param:usize,
        l_param:isize,
    )->isize,

    pub create_parameters:*mut A,
    /// Defines whether redraw is requested immediately after redraw event processes.
    pub auto_redraw:bool,
}

/// A window handle.
/// 
/// The window mustn't outlive its class otherwise the class won't be unregistered properly.
/// 
/// Note that all windows/classes that an application creates/registers are destroyed/unregistered
/// when it terminates.
/// So there no need to do it when the application closes,
/// but it makes sense when you create-destroy windows and register-unregister classes at the run time.
#[repr(transparent)]
pub struct Window{
    pub (crate) handle:WindowHandle,
}

impl Window{
    pub fn new<W:WindowProcedure>(
        class:&WindowClass,
        attributes:WindowAttributes,
        create_parameters:&mut W::CreateParameters,
    )->Result<Window,WinError>{
        let window_name:Vec<u16>=attributes.name
            .encode_utf16()
            .chain([0].into_iter())
            .collect();

        let mut style=WindowStyles::new()
            .set(WindowStyle::SystemMenu)
            .set(WindowStyle::Caption);

        let mut extended_style=ExtendedWindowStyles::new();

        // Enabling file dropping
        #[cfg(feature="file_drop")]{
            extended_style|=WS_EX_ACCEPTFILES;
        }

        if attributes.visible{
            style=style.set(WindowStyle::Visible);
        }

        if attributes.topmost{
            extended_style=extended_style.set(ExtendedWindowStyle::TopMost);
        }

        // Размер, установленный пользователем
        let [mut width,mut height]=if let Some(size)=attributes.size{
            size
        }
        else{
            [CW_USEDEFAULT,CW_USEDEFAULT]
        };
        // Положение, установленное пользователем
        let [mut x,mut y]=if let Some(position)=attributes.position{
            position
        }
        else{
            [CW_USEDEFAULT,CW_USEDEFAULT]
        };

        match attributes.fullscreen{
            Fullscreen::None=>{
                style=style.set(WindowStyle::SizeBox)
                    .set(WindowStyle::MaximizeBox)
                    .set(WindowStyle::MaximizeBox);
            }
            Fullscreen::Monitor(monitor)=>{
                if let Some(info)=monitor.get_monitor_info(){
                    style=style.set(WindowStyle::PopUp);
                    style=style.remove(WindowStyle::SizeBox)
                        .remove(WindowStyle::Caption)
                        .remove(WindowStyle::MaximizeBox)
                        .remove(WindowStyle::MaximizeBox);

                    extended_style=extended_style.set(ExtendedWindowStyle::AppWwindow);

                    x=info.rcMonitor.left;
                    y=info.rcMonitor.top;
                    width=info.rcMonitor.right-info.rcMonitor.left;
                    height=info.rcMonitor.bottom-info.rcMonitor.top;
                }
            }
        };

        unsafe{
            let mut create_parameters=CreateParameters{
                window_procedure:window_procedure::<W>,
                create_parameters,
                auto_redraw:attributes.auto_redraw,
            };

            if let Some(window_handle)=WinCore.window.create(
                class.identifier(),
                window_name.as_ptr(),
                style,
                extended_style,
                [x,y,width,height],
                None,
                None,
                None,
                Some(&mut create_parameters),
            ){
                Ok(Self{
                    handle:window_handle,
                })
            }
            else{
                Err(WinError::get_last_error())
            }
        }
    }

    pub fn handle(&self)->WindowHandle{
        self.handle
    }

    pub fn get_context(&self)->Option<DeviceContextHandle>{
        unsafe{
            WinCore.window.get_device_context(Some(self.handle))
        }
    }

    pub unsafe fn get_context_unchecked(&self)->DeviceContextHandle{
        WinCore.window.get_device_context_unchecked(Some(self.handle))
    }
}

/// Requests and sending events.
impl Window{
    pub fn redraw(&self){
        unsafe{
            RedrawWindow(self.handle.as_raw(),null_mut(),null_mut(),RDW_INVALIDATE);
        }
    }

    pub fn destroy(&self)->Result<(),WinError>{
        unsafe{
            if WinCore.window.destroy(self.handle){
                Ok(())
            }
            else{
                Err(WinError::get_last_error())
            }
        }
    }
}

/// Window sizes and positions.
impl Window{
    /// Returns the window size.
    /// 
    /// Возвращает размеры окна.
    /// 
    /// [width, height]
    pub fn size(&self)->[u32;2]{
        let mut window_rectangle=[0i32;4];
        unsafe{
            WinCore.window.get_window_rectangle(self.handle,&mut window_rectangle);
        }
        let [x1,y1,x2,y2]=window_rectangle;
        [
            (x2-x1) as u32,
            (y2-y1) as u32,
        ]
    }

    /// Returns coordinates of window's upper-left corner.
    /// 
    /// Возвращает координаты верхнего левого угла окна.
    /// 
    /// [x, y]
    pub fn position(&self)->[i32;2]{
        let mut window_rectangle=[0i32;4];
        unsafe{
            WinCore.window.get_window_rectangle(self.handle,&mut window_rectangle);
        }
        let [x1,y1,_,_]=window_rectangle;
        [
            x1,
            y1,
        ]
    }

    /// Returns the window rectangle with the coordinates of it's upper-left and lower-right corners.
    /// 
    /// Возвращает оконный прямоугольник с координатами верхнего левого и нижнего правого углов.
    /// 
    /// [x1, y1, x2, y2]
    pub fn rectangle(&self)->[i32;4]{
        let mut window_rectangle=[0i32;4];
        unsafe{
            WinCore.window.get_window_rectangle(self.handle,&mut window_rectangle);
        }
        window_rectangle
    }

    /// Returns window's client area size.
    /// 
    /// Возвращает размеры клиентской области окна.
    /// 
    /// [width, height]
    pub fn client_size(&self)->[u32;2]{
        let mut client_rectangle=[0i32;4];
        unsafe{
            WinCore.window.get_client_rectangle(self.handle,&mut client_rectangle);
        }
        let [_,_,width,height]=client_rectangle;

        [
            width as u32,
            height as u32,
        ]
    }
}

/// Styles and positioning.
impl Window{
    pub fn set_fullscreen(&self,fullscreen:Fullscreen){
        unsafe{
            let mut style=self.get_style();
            let mut extended_style=self.get_extended_style();

            match fullscreen{
                Fullscreen::None=>{
                    style=style.set(WindowStyle::SizeBox)
                    .set(WindowStyle::Caption)
                    .set(WindowStyle::MaximizeBox)
                    .set(WindowStyle::MaximizeBox)
                    .remove(WindowStyle::PopUp);

                    extended_style=extended_style.remove(ExtendedWindowStyle::AppWwindow);

                    self.set_style(style);
                    self.set_extended_style(extended_style);
                    WinCore.window.set_window_position(
                        self.handle,
                        None,
                        [0,0,0,0],
                        SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOSIZE|SWP_FRAMECHANGED
                    );
                }
                Fullscreen::Monitor(monitor)=>{
                    if let Some(info)=monitor.get_monitor_info(){
                        style=style.remove(WindowStyle::SizeBox)
                        .remove(WindowStyle::Caption)
                        .remove(WindowStyle::MaximizeBox)
                        .remove(WindowStyle::MaximizeBox);

                        extended_style=extended_style.set(ExtendedWindowStyle::AppWwindow);

                        let x=info.rcMonitor.left;
                        let y=info.rcMonitor.top;
                        let width=info.rcMonitor.right-info.rcMonitor.left;
                        let height=info.rcMonitor.bottom-info.rcMonitor.top;

                        self.set_style(style);
                        self.set_extended_style(extended_style);
                        self.set_window_position([x,y,width,height])
                    }
                }
            }
        }
    }

    /// Will not make effect until you call the `Window::set_window_position()`.
    pub unsafe fn set_extended_style(&self,style:ExtendedWindowStyles){
        WinCore.window.set_window_long_ptr(self.handle,WindowData::ExtendedStyle,style.flag as isize);
    }

    pub unsafe fn get_extended_style(&self)->ExtendedWindowStyles{
        ExtendedWindowStyles::raw(WinCore.window.get_window_long_ptr(self.handle,WindowData::ExtendedStyle) as u32)
    }

    /// Will not make effect until you call the `Window::set_window_position()`.
    pub unsafe fn set_style(&self,style:WindowStyles){
        WinCore.window.set_window_long_ptr(self.handle,WindowData::Style,style.flag as isize);
    }

    pub unsafe fn get_style(&self)->WindowStyles{
        WindowStyles::raw(WinCore.window.get_window_long_ptr(self.handle,WindowData::Style) as u32)
    }

    pub unsafe fn set_window_position(&self,[x,y,width,height]:[i32;4]){
        WinCore.window.set_window_position(self.handle,None,[x,y,width,height],SWP_SHOWWINDOW);
    }
}

/// Special functions.
impl Window{
    pub fn set_auto_redraw(&self,enabled:bool){
        unsafe{
            WinCore.window.set_window_long_ptr(self.handle,window_settings_auto_redraw,enabled as isize);
        }
    }

    pub (crate) unsafe fn set_user_data<D:Sized>(&self,data:&mut D){
        WinCore.window.set_window_long_ptr(self.handle,WindowData::UserData,data as *const D as isize);
    }

    pub (crate) unsafe fn get_user_data(&self)->isize{
        WinCore.window.get_window_long_ptr(self.handle,WindowData::UserData)
    }

    pub unsafe fn set_window_procedure(&self,procedure:unsafe extern "system" fn(WindowHandle,u32,usize,isize)->isize){
        WinCore.window.set_window_long_ptr(self.handle,WindowData::WindowProcedure,procedure as isize);
    }

    pub unsafe fn set_window_handle<W:WindowProcedure>(&self){
        WinCore.window.set_window_long_ptr(self.handle,WindowData::WindowProcedure,window_procedure::<W> as isize);
    }
}

/// Cursor functions.
impl Window{
    /// Returns window's cursor position.
    /// 
    /// Возвращает положение курсора окна.
    pub fn cursor_position(&self)->[i32;2]{
        unsafe{
            let mut point=[0i32;2];
            WinCore.cursor.get_position(&mut point);

            WinCore.window.screen_to_client(self.handle,&mut point);

            point
        }
    }

    /// Sets window's cursor position.
    /// 
    /// Устанавливает положение курсора окна.
    pub fn set_cursor_position(&self,mut point:[i32;2]){
        unsafe{
            WinCore.window.client_to_screen(self.handle,&mut point);
            WinCore.cursor.set_position(point);
        }
    }

    pub fn show_cursor(&self,show:bool){
        // This function sets an internal display counter that determines
        // whether the cursor should be displayed.
        // The cursor is displayed only if the display count is greater than or equal to 0.
        // If a mouse is installed, the initial display count is 0.
        // If no mouse is installed, the display count is –1.
        unsafe{
            let counter=WinCore.cursor.show(show);

            if show{
                if counter>0{
                    WinCore.cursor.show(false);
                }
            }
            else{
                if counter<(-1){
                    WinCore.cursor.show(true);
                }
            }
        }
    }
}

impl Drop for Window{
    fn drop(&mut self){
        let _=self.destroy();
    }
}


pub struct WindowAttributes{
    /// The window name and title.
    pub name:String,

    /// The window size.
    /// 
    /// The default is `None`.
    pub size:Option<[i32;2]>,

    /// The window position.
    pub position:Option<[i32;2]>,

    /// The default is `true`.
    pub visible:bool,

    /// The window should be placed above all non-topmost windows
    /// and should stay above them,
    /// even when the window is deactivated.
    /// 
    /// The default is `false`.
    pub topmost:bool,

    /// Defines whether a new redraw event is requested
    /// directly after processing the last one.
    /// 
    /// The default is `true`.
    pub auto_redraw:bool,

    /// If `Fullscreen::Monitor` is set the size and position are ignored,
    /// but if some error accures they are used to build a window.
    /// 
    /// The default is `false`.
    pub fullscreen:Fullscreen,
}

impl WindowAttributes{
    pub fn new(name:&str)->WindowAttributes{
        Self{
            name:String::from(name),
            size:None,
            position:None,
            visible:true,
            topmost:false,
            auto_redraw:true,
            fullscreen:Fullscreen::None,
        }
    }
}