|
@@ -0,0 +1,152 @@
|
|
1
|
+/*
|
|
2
|
+ * encantar.js
|
|
3
|
+ * GPU-accelerated Augmented Reality for the web
|
|
4
|
+ * Copyright (C) 2022-2024 Alexandre Martins <alemartf(at)gmail.com>
|
|
5
|
+ *
|
|
6
|
+ * This program is free software: you can redistribute it and/or modify
|
|
7
|
+ * it under the terms of the GNU Lesser General Public License as published
|
|
8
|
+ * by the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+ * (at your option) any later version.
|
|
10
|
+ *
|
|
11
|
+ * This program is distributed in the hope that it will be useful,
|
|
12
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+ * GNU Lesser General Public License for more details.
|
|
15
|
+ *
|
|
16
|
+ * You should have received a copy of the GNU Lesser General Public License
|
|
17
|
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+ *
|
|
19
|
+ * fullscreen-button.ts
|
|
20
|
+ * A built-in fullscreen button introduced as a convenience
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+import { Viewport } from '../core/viewport';
|
|
24
|
+
|
|
25
|
+/** Button icon to be displayed when the fullscreen mode is disabled */
|
|
26
|
+const BUTTON_ICON_OFF = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAbUlEQVRYR+2WOQ4AIAgE5f+PVhobDZANBZAsraAwXMoqFil+f9GBj8BW8dIiKt45at/XgShStHgvmfdekwAdIIEyAmh1Z/U5ikmABPoRsLZWtt+5DUlgHgGr6qM1Pf9XnO131L7fJEQjyOqXEzjP1YAhNmUTrgAAAABJRU5ErkJggg==';
|
|
27
|
+
|
|
28
|
+/** Button icon to be displayed when the fullscreen mode is enabled */
|
|
29
|
+const BUTTON_ICON_ON = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAZElEQVRYR+2WwRIAEAhE9f8fTQ5OhtkLxbzOyc5rJSvBYcH3FwTIBKpHb5d57Nqm5o0aCIBAPgLDxSunq69APT8RCBdwezTLHjglDAEQgEC+QZR2EqqbjprHRgSB9wjwHX9LoAHP1YAhXF4Z/QAAAABJRU5ErkJggg==';
|
|
30
|
+
|
|
31
|
+/** Button size, in pixels */
|
|
32
|
+const BUTTON_SIZE = 64;
|
|
33
|
+
|
|
34
|
+/** Button margin, in pixels */
|
|
35
|
+const BUTTON_MARGIN = 24;
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+/**
|
|
40
|
+ * Built-in fullscreen button
|
|
41
|
+ */
|
|
42
|
+export class FullscreenButton
|
|
43
|
+{
|
|
44
|
+ /** The viewport associated to this panel */
|
|
45
|
+ private readonly _viewport: Viewport;
|
|
46
|
+
|
|
47
|
+ /** The HTML element of the button */
|
|
48
|
+ private readonly _button: HTMLButtonElement;
|
|
49
|
+
|
|
50
|
+ /** Bound event handler */
|
|
51
|
+ private readonly _boundEventHandler: EventListener;
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+ /**
|
|
56
|
+ * Constructor
|
|
57
|
+ * @param viewport Viewport
|
|
58
|
+ */
|
|
59
|
+ constructor(viewport: Viewport)
|
|
60
|
+ {
|
|
61
|
+ this._viewport = viewport;
|
|
62
|
+ this._button = this._createButton();
|
|
63
|
+ this._boundEventHandler = this._handleFullscreenEvent.bind(this);
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ /**
|
|
67
|
+ * Initialize
|
|
68
|
+ */
|
|
69
|
+ init(): void
|
|
70
|
+ {
|
|
71
|
+ this._viewport.hud.container.appendChild(this._button);
|
|
72
|
+ this._viewport.addEventListener('fullscreenchange', this._boundEventHandler);
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ /**
|
|
76
|
+ * Release
|
|
77
|
+ */
|
|
78
|
+ release(): void
|
|
79
|
+ {
|
|
80
|
+ this._viewport.removeEventListener('fullscreenchange', this._boundEventHandler);
|
|
81
|
+ this._button.remove();
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ /**
|
|
85
|
+ * Create the <button> element
|
|
86
|
+ */
|
|
87
|
+ private _createButton(): HTMLButtonElement
|
|
88
|
+ {
|
|
89
|
+ const button = document.createElement('button');
|
|
90
|
+ const icon = document.createElement('img');
|
|
91
|
+
|
|
92
|
+ button.style.display = 'inline-block';
|
|
93
|
+ button.style.position = 'absolute';
|
|
94
|
+ button.style.bottom = BUTTON_MARGIN + 'px';
|
|
95
|
+ button.style.right = BUTTON_MARGIN + 'px';
|
|
96
|
+ button.style.width = BUTTON_SIZE + 'px';
|
|
97
|
+ button.style.height = BUTTON_SIZE + 'px';
|
|
98
|
+ button.style.padding = '2px';
|
|
99
|
+
|
|
100
|
+ button.style.backgroundColor = '#7c5ec2';
|
|
101
|
+ button.style.borderColor = '#5c3ba3';
|
|
102
|
+ button.style.borderStyle = 'solid';
|
|
103
|
+ button.style.borderWidth = '2px';
|
|
104
|
+ button.style.borderRadius = '8px';
|
|
105
|
+ button.style.cursor = 'pointer';
|
|
106
|
+ button.draggable = false;
|
|
107
|
+
|
|
108
|
+ icon.src = BUTTON_ICON_OFF;
|
|
109
|
+ icon.draggable = false;
|
|
110
|
+ icon.style.display = 'inline';
|
|
111
|
+ icon.style.width = '100%';
|
|
112
|
+ icon.style.height = '100%';
|
|
113
|
+ icon.style.imageRendering = 'pixelated';
|
|
114
|
+ button.appendChild(icon);
|
|
115
|
+
|
|
116
|
+ const highlight = () => {
|
|
117
|
+ button.style.backgroundColor = '#ffd500';
|
|
118
|
+ button.style.borderColor = '#bb9100';
|
|
119
|
+ };
|
|
120
|
+
|
|
121
|
+ const dehighlight = () => {
|
|
122
|
+ button.style.backgroundColor = '#7e56c2';
|
|
123
|
+ button.style.borderColor = '#5c3ba3';
|
|
124
|
+ };
|
|
125
|
+
|
|
126
|
+ button.addEventListener('pointerdown', highlight);
|
|
127
|
+ button.addEventListener('pointerup', dehighlight);
|
|
128
|
+ button.addEventListener('pointermove', dehighlight);
|
|
129
|
+
|
|
130
|
+ button.addEventListener('click', () => {
|
|
131
|
+ if(!this._viewport.fullscreen) {
|
|
132
|
+ this._viewport.requestFullscreen().catch(err => {
|
|
133
|
+ alert(`Can't enable the fullscreen mode. ` + err.toString());
|
|
134
|
+ });
|
|
135
|
+ }
|
|
136
|
+ else {
|
|
137
|
+ this._viewport.exitFullscreen();
|
|
138
|
+ }
|
|
139
|
+ });
|
|
140
|
+
|
|
141
|
+ return button;
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ /**
|
|
145
|
+ * Handle a fullscreenchange event
|
|
146
|
+ */
|
|
147
|
+ private _handleFullscreenEvent(event: Event): void
|
|
148
|
+ {
|
|
149
|
+ const icon = this._button.querySelector('img') as HTMLImageElement;
|
|
150
|
+ icon.src = this._viewport.fullscreen ? BUTTON_ICON_ON : BUTTON_ICON_OFF;
|
|
151
|
+ }
|
|
152
|
+}
|