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
use super::image::Image;
use super::canvas::Canvas;
use crate::inputs::event::EventManager;
use web_sys::{window, Window as WebSysWindow, Document};
/// A struct representing the tab in the web browser.
/// It provide event handling.
pub struct Window {
document: Document,
pub window: WebSysWindow,
events: EventManager
}
impl Window {
/// Create a [Canvas](../canvas/struct.Canvas.html) and a Window.
/// Events will not be activated!
pub fn init() -> (Window, Canvas) {
let window = window().unwrap();
let document = window.document().unwrap();
let canvas = Canvas::new();
document
.body()
.unwrap()
.append_child(&canvas.element)
.unwrap();
canvas.element.set_width(document.document_element().unwrap().client_width() as u32);
canvas.element.set_height(document.document_element().unwrap().client_height() as u32);
(Window {
window,
document,
events: EventManager::new()
}, canvas)
}
/// Create a [Canvas](../canvas/struct.Canvas.html) and a Window.
/// You may specify which [types of event](../../inputs/event/types/index.html) you want to record.
///
/// # Example
///
/// ```no_run
/// use wasm_game_lib::inputs::event::types::*; // there are only a few events so the wildcard is not a problem
/// use wasm_game_lib::graphics::window::Window;
///
/// // create a window recording three types of event
/// let (window, canvas) = Window::init_with_events(MOUSE_EVENT + KEYBOARD_EVENT + FOCUS_EVENT);
/// ```
#[allow(clippy::unreadable_literal)]
pub fn init_with_events(events: u8) -> (Window, Canvas) {
let mouse_events = 0b00000001 & events == 0b00000001;
let key_events = 0b00000010 & events == 0b00000010;
let size_events = 0b00000100 & events == 0b00000100;
let focus_events = 0b00001000 & events == 0b00001000;
let joystick_events = 0b00010000 & events == 0b00010000;
let (mut window, canvas) = Window::init();
if mouse_events {
window.events.start_recording_mouse_events();
}
if key_events {
window.events.start_recording_keyboard_events();
}
if size_events {
window.events.start_recording_size_events();
}
if focus_events {
window.events.start_recording_focus_events();
}
if joystick_events {
unimplemented!("joysticks are not implemented for now");
}
(window, canvas)
}
/// Return an Iterator of every events fired after the last call of this method.
/// Make sure events are activated: [init_with_events()](#method.init_with_events).
///
/// # Example
///
/// ```no_run
/// use wasm_game_lib::inputs::event::types::*;
/// use wasm_game_lib::graphics::window::Window;
///
/// // create a window recording three types of event
/// let (mut window, canvas) = Window::init_with_events(MOUSE_EVENT + KEYBOARD_EVENT + FOCUS_EVENT);
///
/// loop {
/// for event in window.poll_events() {
/// // Do something with your event
/// }
/// }
/// ```
pub fn poll_events(&mut self) -> &mut EventManager {
&mut self.events
}
/// Set the title of the tab.
pub fn set_title(&mut self, title: &str) {
self.document.set_title(title)
}
/// Get the title of the tab.
pub fn get_title(&self) -> String {
self.document.title()
}
/// Set the icon of the tab.
/// UNIMPLEMENTED
pub fn set_icon(&mut self, _icon: &Image) {
unimplemented!()
}
/// Get the icon of the tab.
/// UNIMPLEMENTED
pub fn get_icon(&self) -> Image {
unimplemented!()
}
/// Return the width of the tab in pixels
pub fn get_width(&self) -> u32 {
self.document.document_element().unwrap().client_width() as u32
}
/// Return the height of the tab in pixels
pub fn get_height(&self) -> u32 {
self.document.document_element().unwrap().client_height() as u32
}
}