forked from stemkoski/A-Frame-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen-controls-buttons-look.html
192 lines (158 loc) · 4.91 KB
/
screen-controls-buttons-look.html
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
<!DOCTYPE html>
<html>
<head>
<title>Screen Controls</title>
<meta name="description" content="A-Frame Examples">
<script src="js/aframe-master-v1.3.0.min.js"></script>
<script src="js/aframe-environment-component.min.js"></script>
<script src="js/extended-wasd-controls.js"></script>
<style>
body
{
/* disable long press in iOS? */
-webkit-touch-callout: none;
}
.mainUI
{
border: 0px solid pink;
position: fixed;
top: 0px;
width:99%;
height:99%;
z-index: 1;
pointer-events: none; /* allow click-through in transparent areas */
}
.regionUI
{
border: 0px solid yellow;
position: absolute;
display: flex;
flex-direction: row;
pointer-events: none;
}
.buttonUI
{
border: 0px solid lime;
display: flex;
flex-direction: column;
justify-content: center;
pointer-events: auto;
filter: drop-shadow(0px 0px 20px white);
}
.skyColor
{
filter: hue-rotate(240deg) saturate(100%) brightness(100%) drop-shadow(0px 0px 20px white);
}
.baseColor
{
filter: hue-rotate(24deg) saturate(68%) brightness(100%) drop-shadow(0px 0px 20px white);
}
.grayColor
{
filter: hue-rotate(0deg) saturate(0%) brightness(100%) drop-shadow(0px 0px 20px white);
}
</style>
</head>
<body>
<!-- note: by using red base images for buttons, can tint (HSV) using filter: hue-rotate(); etc. -->
<div class="mainUI" id="uiDiv" oncontextmenu="return false;">
<!-- top-left -->
<div class="regionUI skyColor" style="top: 10px; left: 10px;" oncontextmenu="return false;">
<div class="buttonUI">
<img src="images/buttons/red/up.png" id="buttonMoveUp" /><br/>
<img src="images/buttons/red/down.png" id="buttonMoveDown" />
</div>
</div>
<!-- top-right -->
<div class="regionUI" style="top: 10px; right: 10px;">
</div>
<!-- bottom-left -->
<div class="regionUI" style="bottom: 10px; left: 10px;">
</div>
<!-- bottom-right -->
<div class="regionUI baseColor" style="bottom: 10px; right: 10px;">
<div class="buttonUI">
<img src="images/buttons/red/left.png" id="buttonMoveLeft" />
</div>
<div class="buttonUI">
<img src="images/buttons/red/up.png" id="buttonMoveForward" /><br/>
<img src="images/buttons/red/down.png" id="buttonMoveBackward" />
</div>
<div class="buttonUI">
<img src="images/buttons/red/right.png" id="buttonMoveRight" />
</div>
</div>
</div>
<script>
function haltEvent(event)
{
event.preventDefault && event.preventDefault();
event.stopPropagation && event.stopPropagation();
event.cancelBubble = true;
event.returnValue = false;
return false;
}
// prevent right-click menus from appearing
document.addEventListener("contextmenu", haltEvent );
// more attempts to prevent right-click menus from appearing -- avoid; they block touchscreen/iPad controls
// document.addEventListener("touchmove", haltEvent );
// document.addEventListener("touchcancel", haltEvent );
// add events for both touch and mouse controls
function addButtonEventListeners( buttonElementID, startFunction, endFunction )
{
let element = document.getElementById( buttonElementID );
element.addEventListener('touchstart', startFunction);
element.addEventListener('mousedown', startFunction);
element.addEventListener('touchend', endFunction);
element.addEventListener('mouseup', endFunction);
}
function associateMovementControls( buttonElementID, component, keyName )
{
addButtonEventListeners(buttonElementID,
function(event)
{
component.registerKeyDown( keyName );
return haltEvent(event);
},
function(event)
{
component.registerKeyUp( keyName );
return haltEvent(event);
}
);
}
// need to run javascript code after a-scene entities and components are loaded
AFRAME.registerComponent('screen-controls',
{
init: function ()
{
let component = document.getElementById("camera").components["extended-wasd-controls"];
associateMovementControls( "buttonMoveForward", component, component.data.moveForwardKey );
associateMovementControls( "buttonMoveBackward", component, component.data.moveBackwardKey );
associateMovementControls( "buttonMoveLeft", component, component.data.moveLeftKey );
associateMovementControls( "buttonMoveRight", component, component.data.moveRightKey );
associateMovementControls( "buttonMoveUp", component, component.data.moveUpKey );
associateMovementControls( "buttonMoveDown", component, component.data.moveDownKey );
},
tick: function(time, deltaTime)
{
}
});
</script>
<!--
disable press "F" to enter fullscreen mode
disable WASD controls attached to default camera
-->
<a-scene
environment="preset: arches; shadow: true;"
keyboard-shortcuts="enterVR: false;"
vr-mode-ui="enabled: false;"
screen-controls>
<!-- adding in look controls; automatically sets turnEnabled/lookEnabled to false -->
<a-entity id="camera" camera look-controls
position="0 1.6 0"
extended-wasd-controls="flyEnabled: true;">
</a-entity>
</a-scene>
</body>
</html>