wasm_game_lib/graphics/
window.rs

1use super::image::Image;
2use super::canvas::Canvas;
3use crate::inputs::event::EventManager;
4use web_sys::{window, Window as WebSysWindow, Document};
5
6/// A struct representing the tab in the web browser.
7/// It provide event handling.
8pub struct Window {
9    document: Document,
10    pub window: WebSysWindow,
11    events: EventManager
12}
13
14impl Window {
15    /// Create a [Canvas](../canvas/struct.Canvas.html) and a Window.
16    /// Events will not be activated!
17    pub fn init() -> (Window, Canvas) {
18        let window = window().unwrap();
19        let document = window.document().unwrap();
20
21        let canvas = Canvas::new();
22        document
23            .body()
24            .unwrap()
25            .append_child(&canvas.element)
26            .unwrap();
27        canvas.element.set_width(document.document_element().unwrap().client_width() as u32);
28        canvas.element.set_height(document.document_element().unwrap().client_height() as u32);
29
30        (Window {
31            window,
32            document,
33            events: EventManager::new()
34        }, canvas)
35    }
36
37    /// Create a [Canvas](../canvas/struct.Canvas.html) and a Window.
38    /// You may specify which [types of event](../../inputs/event/types/index.html) you want to record. 
39    /// 
40    /// # Example
41    /// 
42    /// ```no_run
43    /// use wasm_game_lib::inputs::event::types::*; // there are only a few events so the wildcard is not a problem
44    /// use wasm_game_lib::graphics::window::Window;
45    /// 
46    /// // create a window recording three types of event
47    /// let (window, canvas) = Window::init_with_events(MOUSE_EVENT + KEYBOARD_EVENT + FOCUS_EVENT);
48    /// ```
49    #[allow(clippy::unreadable_literal)]
50    pub fn init_with_events(events: u8) -> (Window, Canvas) {
51        let mouse_events    = 0b00000001 & events == 0b00000001;
52        let key_events      = 0b00000010 & events == 0b00000010;
53        let size_events     = 0b00000100 & events == 0b00000100;
54        let focus_events    = 0b00001000 & events == 0b00001000;
55        let joystick_events = 0b00010000 & events == 0b00010000;
56
57        let (mut window, canvas) = Window::init();
58        if mouse_events {
59            window.events.start_recording_mouse_events();
60        }
61        if key_events {
62            window.events.start_recording_keyboard_events();
63        }
64        if size_events {
65            window.events.start_recording_size_events();
66        }
67        if focus_events {
68            window.events.start_recording_focus_events();
69        }
70        if joystick_events {
71            unimplemented!("joysticks are not implemented for now");
72        }
73        
74        (window, canvas)
75    }
76
77    /// Return an Iterator of every events fired after the last call of this method.
78    /// Make sure events are activated: [init_with_events()](#method.init_with_events).
79    /// 
80    /// # Example
81    /// 
82    /// ```no_run
83    /// use wasm_game_lib::inputs::event::types::*;
84    /// use wasm_game_lib::graphics::window::Window;
85    /// 
86    /// // create a window recording three types of event
87    /// let (mut window, canvas) = Window::init_with_events(MOUSE_EVENT + KEYBOARD_EVENT + FOCUS_EVENT);
88    /// 
89    /// loop {
90    ///     for event in window.poll_events() {
91    ///         // Do something with your event
92    ///     }
93    /// }
94    /// ```
95    pub fn poll_events(&mut self) -> &mut EventManager {
96        &mut self.events
97    }
98
99    /// Set the title of the tab.
100    pub fn set_title(&mut self, title: &str) {
101        self.document.set_title(title)
102    }
103
104    /// Get the title of the tab.
105    pub fn get_title(&self) -> String {
106        self.document.title()
107    }
108    
109    /// Set the icon of the tab.
110    /// UNIMPLEMENTED
111    pub fn set_icon(&mut self, _icon: &Image) {
112        unimplemented!()
113    }
114    
115    /// Get the icon of the tab.
116    /// UNIMPLEMENTED
117    pub fn get_icon(&self) -> Image {
118        unimplemented!()
119    }
120
121    /// Return the width of the tab in pixels
122    pub fn get_width(&self) -> u32 {
123        self.document.document_element().unwrap().client_width() as u32
124    }
125
126    /// Return the height of the tab in pixels
127    pub fn get_height(&self) -> u32 {
128        self.document.document_element().unwrap().client_height() as u32
129    }
130}
131