anvilkit-render 0.1.0

Cross-platform rendering system built on wgpu and winit for AnvilKit game engine
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! # 窗口配置和状态管理
//! 
//! 提供窗口的配置参数和状态管理功能。

use winit::dpi::{LogicalSize, PhysicalSize};
use winit::window::{Window, WindowAttributes, Fullscreen};

/// 窗口配置
/// 
/// 定义窗口的初始属性和行为参数。
/// 
/// # 示例
/// 
/// ```rust
/// use anvilkit_render::window::WindowConfig;
/// 
/// // 使用默认配置
/// let config = WindowConfig::default();
/// 
/// // 自定义配置
/// let config = WindowConfig::new()
///     .with_title("我的游戏")
///     .with_size(1920, 1080)
///     .with_fullscreen(true)
///     .with_vsync(true);
/// ```
#[derive(Debug, Clone)]
pub struct WindowConfig {
    /// 窗口标题
    pub title: String,
    /// 窗口宽度(逻辑像素)
    pub width: u32,
    /// 窗口高度(逻辑像素)
    pub height: u32,
    /// 是否全屏
    pub fullscreen: bool,
    /// 是否可调整大小
    pub resizable: bool,
    /// 是否可见
    pub visible: bool,
    /// 是否启用垂直同步
    pub vsync: bool,
    /// 最小窗口大小
    pub min_size: Option<(u32, u32)>,
    /// 最大窗口大小
    pub max_size: Option<(u32, u32)>,
}

impl Default for WindowConfig {
    fn default() -> Self {
        Self {
            title: "AnvilKit Application".to_string(),
            width: 1280,
            height: 720,
            fullscreen: false,
            resizable: true,
            visible: true,
            vsync: true,
            min_size: Some((320, 240)),
            max_size: None,
        }
    }
}

impl WindowConfig {
    /// 创建新的窗口配置
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowConfig;
    /// 
    /// let config = WindowConfig::new();
    /// assert_eq!(config.title, "AnvilKit Application");
    /// ```
    pub fn new() -> Self {
        Self::default()
    }
    
    /// 设置窗口标题
    /// 
    /// # 参数
    /// 
    /// - `title`: 窗口标题字符串
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowConfig;
    /// 
    /// let config = WindowConfig::new().with_title("我的应用");
    /// assert_eq!(config.title, "我的应用");
    /// ```
    pub fn with_title<T: Into<String>>(mut self, title: T) -> Self {
        self.title = title.into();
        self
    }
    
    /// 设置窗口大小
    /// 
    /// # 参数
    /// 
    /// - `width`: 窗口宽度(逻辑像素)
    /// - `height`: 窗口高度(逻辑像素)
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowConfig;
    /// 
    /// let config = WindowConfig::new().with_size(1920, 1080);
    /// assert_eq!(config.width, 1920);
    /// assert_eq!(config.height, 1080);
    /// ```
    pub fn with_size(mut self, width: u32, height: u32) -> Self {
        self.width = width;
        self.height = height;
        self
    }
    
    /// 设置是否全屏
    /// 
    /// # 参数
    /// 
    /// - `fullscreen`: 是否启用全屏模式
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowConfig;
    /// 
    /// let config = WindowConfig::new().with_fullscreen(true);
    /// assert!(config.fullscreen);
    /// ```
    pub fn with_fullscreen(mut self, fullscreen: bool) -> Self {
        self.fullscreen = fullscreen;
        self
    }
    
    /// 设置是否可调整大小
    /// 
    /// # 参数
    /// 
    /// - `resizable`: 是否允许调整窗口大小
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowConfig;
    /// 
    /// let config = WindowConfig::new().with_resizable(false);
    /// assert!(!config.resizable);
    /// ```
    pub fn with_resizable(mut self, resizable: bool) -> Self {
        self.resizable = resizable;
        self
    }
    
    /// 设置是否启用垂直同步
    /// 
    /// # 参数
    /// 
    /// - `vsync`: 是否启用垂直同步
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowConfig;
    /// 
    /// let config = WindowConfig::new().with_vsync(false);
    /// assert!(!config.vsync);
    /// ```
    pub fn with_vsync(mut self, vsync: bool) -> Self {
        self.vsync = vsync;
        self
    }
    
    /// 设置最小窗口大小
    /// 
    /// # 参数
    /// 
    /// - `min_size`: 最小窗口大小,None 表示无限制
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowConfig;
    /// 
    /// let config = WindowConfig::new().with_min_size(Some((640, 480)));
    /// assert_eq!(config.min_size, Some((640, 480)));
    /// ```
    pub fn with_min_size(mut self, min_size: Option<(u32, u32)>) -> Self {
        self.min_size = min_size;
        self
    }
    
    /// 设置最大窗口大小
    /// 
    /// # 参数
    /// 
    /// - `max_size`: 最大窗口大小,None 表示无限制
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowConfig;
    /// 
    /// let config = WindowConfig::new().with_max_size(Some((1920, 1080)));
    /// assert_eq!(config.max_size, Some((1920, 1080)));
    /// ```
    pub fn with_max_size(mut self, max_size: Option<(u32, u32)>) -> Self {
        self.max_size = max_size;
        self
    }
    
    /// 将配置转换为 winit 的 WindowAttributes
    /// 
    /// # 返回
    /// 
    /// 返回配置好的 WindowAttributes
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowConfig;
    /// 
    /// let config = WindowConfig::new().with_title("测试窗口");
    /// let attributes = config.to_window_attributes();
    /// ```
    pub fn to_window_attributes(&self) -> WindowAttributes {
        let mut attributes = Window::default_attributes()
            .with_title(&self.title)
            .with_inner_size(LogicalSize::new(self.width, self.height))
            .with_resizable(self.resizable)
            .with_visible(self.visible);
        
        if let Some((min_width, min_height)) = self.min_size {
            attributes = attributes.with_min_inner_size(LogicalSize::new(min_width, min_height));
        }
        
        if let Some((max_width, max_height)) = self.max_size {
            attributes = attributes.with_max_inner_size(LogicalSize::new(max_width, max_height));
        }
        
        if self.fullscreen {
            attributes = attributes.with_fullscreen(Some(Fullscreen::Borderless(None)));
        }
        
        attributes
    }
}

/// 窗口状态
/// 
/// 跟踪窗口的当前状态和属性。
/// 
/// # 示例
/// 
/// ```rust
/// use anvilkit_render::window::WindowState;
/// 
/// let mut state = WindowState::new();
/// state.set_size(1920, 1080);
/// assert_eq!(state.size(), (1920, 1080));
/// ```
#[derive(Debug, Clone)]
pub struct WindowState {
    /// 当前窗口大小(物理像素)
    size: PhysicalSize<u32>,
    /// 缩放因子
    scale_factor: f64,
    /// 是否聚焦
    focused: bool,
    /// 是否最小化
    minimized: bool,
    /// 是否最大化
    maximized: bool,
    /// 是否全屏
    fullscreen: bool,
}

impl Default for WindowState {
    fn default() -> Self {
        Self {
            size: PhysicalSize::new(1280, 720),
            scale_factor: 1.0,
            focused: true,
            minimized: false,
            maximized: false,
            fullscreen: false,
        }
    }
}

impl WindowState {
    /// 创建新的窗口状态
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowState;
    /// 
    /// let state = WindowState::new();
    /// assert_eq!(state.size(), (1280, 720));
    /// ```
    pub fn new() -> Self {
        Self::default()
    }
    
    /// 获取窗口大小
    /// 
    /// # 返回
    /// 
    /// 返回 (宽度, 高度) 元组,单位为物理像素
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowState;
    /// 
    /// let state = WindowState::new();
    /// let (width, height) = state.size();
    /// assert_eq!(width, 1280);
    /// assert_eq!(height, 720);
    /// ```
    pub fn size(&self) -> (u32, u32) {
        (self.size.width, self.size.height)
    }
    
    /// 设置窗口大小
    /// 
    /// # 参数
    /// 
    /// - `width`: 窗口宽度(物理像素)
    /// - `height`: 窗口高度(物理像素)
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowState;
    /// 
    /// let mut state = WindowState::new();
    /// state.set_size(1920, 1080);
    /// assert_eq!(state.size(), (1920, 1080));
    /// ```
    pub fn set_size(&mut self, width: u32, height: u32) {
        self.size = PhysicalSize::new(width, height);
    }
    
    /// 获取缩放因子
    /// 
    /// # 返回
    /// 
    /// 返回当前的 DPI 缩放因子
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowState;
    /// 
    /// let state = WindowState::new();
    /// assert_eq!(state.scale_factor(), 1.0);
    /// ```
    pub fn scale_factor(&self) -> f64 {
        self.scale_factor
    }
    
    /// 设置缩放因子
    /// 
    /// # 参数
    /// 
    /// - `scale_factor`: DPI 缩放因子
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowState;
    /// 
    /// let mut state = WindowState::new();
    /// state.set_scale_factor(2.0);
    /// assert_eq!(state.scale_factor(), 2.0);
    /// ```
    pub fn set_scale_factor(&mut self, scale_factor: f64) {
        self.scale_factor = scale_factor;
    }
    
    /// 检查窗口是否聚焦
    /// 
    /// # 返回
    /// 
    /// 如果窗口当前聚焦则返回 true
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowState;
    /// 
    /// let state = WindowState::new();
    /// assert!(state.is_focused());
    /// ```
    pub fn is_focused(&self) -> bool {
        self.focused
    }
    
    /// 设置窗口聚焦状态
    /// 
    /// # 参数
    /// 
    /// - `focused`: 是否聚焦
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowState;
    /// 
    /// let mut state = WindowState::new();
    /// state.set_focused(false);
    /// assert!(!state.is_focused());
    /// ```
    pub fn set_focused(&mut self, focused: bool) {
        self.focused = focused;
    }
    
    /// 检查窗口是否最小化
    /// 
    /// # 返回
    /// 
    /// 如果窗口当前最小化则返回 true
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowState;
    /// 
    /// let state = WindowState::new();
    /// assert!(!state.is_minimized());
    /// ```
    pub fn is_minimized(&self) -> bool {
        self.minimized
    }
    
    /// 设置窗口最小化状态
    /// 
    /// # 参数
    /// 
    /// - `minimized`: 是否最小化
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowState;
    /// 
    /// let mut state = WindowState::new();
    /// state.set_minimized(true);
    /// assert!(state.is_minimized());
    /// ```
    pub fn set_minimized(&mut self, minimized: bool) {
        self.minimized = minimized;
    }

    /// 检查窗口是否最大化
    pub fn is_maximized(&self) -> bool {
        self.maximized
    }

    /// 设置窗口最大化状态
    pub fn set_maximized(&mut self, maximized: bool) {
        self.maximized = maximized;
    }

    /// 检查窗口是否全屏
    /// 
    /// # 返回
    /// 
    /// 如果窗口当前全屏则返回 true
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowState;
    /// 
    /// let state = WindowState::new();
    /// assert!(!state.is_fullscreen());
    /// ```
    pub fn is_fullscreen(&self) -> bool {
        self.fullscreen
    }
    
    /// 设置窗口全屏状态
    /// 
    /// # 参数
    /// 
    /// - `fullscreen`: 是否全屏
    /// 
    /// # 示例
    /// 
    /// ```rust
    /// use anvilkit_render::window::WindowState;
    /// 
    /// let mut state = WindowState::new();
    /// state.set_fullscreen(true);
    /// assert!(state.is_fullscreen());
    /// ```
    pub fn set_fullscreen(&mut self, fullscreen: bool) {
        self.fullscreen = fullscreen;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_window_config_creation() {
        let config = WindowConfig::new()
            .with_title("Test")
            .with_size(800, 600)
            .with_fullscreen(true)
            .with_resizable(false)
            .with_vsync(false);
        
        assert_eq!(config.title, "Test");
        assert_eq!(config.width, 800);
        assert_eq!(config.height, 600);
        assert!(config.fullscreen);
        assert!(!config.resizable);
        assert!(!config.vsync);
    }
    
    #[test]
    fn test_window_state_operations() {
        let mut state = WindowState::new();
        
        // 测试大小设置
        state.set_size(1920, 1080);
        assert_eq!(state.size(), (1920, 1080));
        
        // 测试缩放因子
        state.set_scale_factor(2.0);
        assert_eq!(state.scale_factor(), 2.0);
        
        // 测试状态标志
        state.set_focused(false);
        assert!(!state.is_focused());
        
        state.set_minimized(true);
        assert!(state.is_minimized());
        
        state.set_fullscreen(true);
        assert!(state.is_fullscreen());
    }
    
    #[test]
    fn test_window_attributes_conversion() {
        let config = WindowConfig::new()
            .with_title("Test Window")
            .with_size(1024, 768);

        let _attributes = config.to_window_attributes();
        // 注意:无法直接测试 WindowAttributes 的内容,
        // 因为它们没有实现 PartialEq
        // 这里只是确保转换不会 panic
    }

    #[test]
    fn test_window_config_all_builders() {
        let config = WindowConfig::new()
            .with_title("Test Window")
            .with_size(1920, 1080)
            .with_resizable(false)
            .with_fullscreen(true)
            .with_vsync(false)
            .with_min_size(Some((320, 240)))
            .with_max_size(Some((3840, 2160)));

        assert_eq!(config.title, "Test Window");
        assert_eq!(config.width, 1920);
        assert_eq!(config.height, 1080);
        assert!(!config.resizable);
        assert!(config.fullscreen);
        assert!(!config.vsync);
        assert_eq!(config.min_size, Some((320, 240)));
        assert_eq!(config.max_size, Some((3840, 2160)));
    }

    #[test]
    fn test_window_config_default_values() {
        let config = WindowConfig::new();
        assert_eq!(config.title, "AnvilKit Application");
        assert_eq!(config.width, 1280);
        assert_eq!(config.height, 720);
        assert!(config.resizable);
        assert!(!config.fullscreen);
        assert!(config.vsync);
    }

    #[test]
    fn test_window_state_set_size() {
        let mut state = WindowState::new();
        assert_eq!(state.size(), (1280, 720));

        state.set_size(1024, 768);
        assert_eq!(state.size(), (1024, 768));
    }

    #[test]
    fn test_window_state_scale_factor() {
        let mut state = WindowState::new();
        assert_eq!(state.scale_factor(), 1.0);

        state.set_scale_factor(2.0);
        assert_eq!(state.scale_factor(), 2.0);
    }

    #[test]
    fn test_window_state_focus() {
        let mut state = WindowState::new();
        assert!(state.is_focused());

        state.set_focused(false);
        assert!(!state.is_focused());

        state.set_focused(true);
        assert!(state.is_focused());
    }

    #[test]
    fn test_window_state_minimized() {
        let mut state = WindowState::new();
        assert!(!state.is_minimized());

        state.set_minimized(true);
        assert!(state.is_minimized());
    }

    #[test]
    fn test_window_state_fullscreen() {
        let mut state = WindowState::new();
        assert!(!state.is_fullscreen());

        state.set_fullscreen(true);
        assert!(state.is_fullscreen());
    }
}