Skip to content

Commit

Permalink
Add mobile mode for simple.{html,js}
Browse files Browse the repository at this point in the history
Most of this code is taken from the similar behavior in GB-studio's copy
of GameBoyOnline. I've modified it somewhat to match binjgb.

* On-screen gamepad. Displayed on start if the browser supports touch
  events. Hidden when keyboard is used, shown when touched. This matches
  GB-Studio's emulator's behavior.
* Only start AudioContext on user input. In my testing this seems to
  work in Chrome and iPhone Safari, but there may be more needed to get
  it to work on itch.io.
  • Loading branch information
binji committed Oct 8, 2021
1 parent 964984e commit 8f6a681
Show file tree
Hide file tree
Showing 4 changed files with 542 additions and 20 deletions.
21 changes: 21 additions & 0 deletions docs/LICENSE.gbstudio
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Chris Maltby

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
328 changes: 328 additions & 0 deletions docs/simple.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,328 @@
/* copied from
* https://github.com/chrismaltby/gb-studio/blob/v2beta/appData/js-emulator/css/style.css
* (see LICENSE.gbstudio)
*
* modifications are marked with binjgb below.
*/
body {
background: #031921;
color: #fff;
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue",
Helvetica, Arial, "Lucida Grande", sans-serif;
font-weight: 300;
margin: 0; /* binjgb */
padding: 0; /* binjgb */
touch-action: none;
text-align: center;
-webkit-touch-callout: none;
user-select: none;
-webkit-user-select: none; /* binjgb */
overflow: hidden;
}

#game {
display: flex;
flex-direction: column;
position: absolute;
width: 100%;
height: 100%;
touch-action: none;
text-align: center;
}

#game canvas {
object-fit: contain;
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-crisp-edges;
image-rendering: pixelated;
image-rendering: crisp-edges;
}

#controller {
display: none;
position: fixed;
bottom: 0px;
height: 210px;
width: 100%;
touch-action: none;
opacity: 0.8;
}

#controller_dpad {
position: absolute;
bottom: 20px;
left: 0px;
width: 184px;
height: 184px;
}

#controller_dpad:before {
content: "";
display: block;
width: 48px;
height: 48px;
background: #5c5c5c;
background: radial-gradient(
ellipse at center,
#5c5c5c 0%,
#555 59%,
#5c5c5c 60%
);
position: absolute;
left: 68px;
top: 68px;
}

#controller_left {
position: absolute;
left: 20px;
top: 68px;
width: 48px;
height: 48px;
background: #666;
background: radial-gradient(ellipse at center, #666 0%, #5c5c5c 80%);
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
}

#controller_right {
position: absolute;
left: 116px;
top: 68px;
width: 48px;
height: 48px;
background: #666;
background: radial-gradient(ellipse at center, #666 0%, #5c5c5c 80%);
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
}

#controller_up {
position: absolute;
left: 68px;
top: 20px;
width: 48px;
height: 48px;
background: #666;
background: radial-gradient(ellipse at center, #666 0%, #5c5c5c 80%);
border-top-left-radius: 4px;
border-top-right-radius: 4px;
}

#controller_down {
position: absolute;
left: 68px;
top: 116px;
width: 48px;
height: 48px;
background: #666;
background: radial-gradient(ellipse at center, #666 0%, #5c5c5c 80%);
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
}

#controller_a {
position: absolute;
bottom: 110px;
right: 20px;
}

#controller_b {
position: absolute;
bottom: 80px;
right: 100px;
}

.roundBtn {
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
font-size: 32px;
color: #440f1f;
line-height: 64px;
width: 64px;
height: 64px;
border-radius: 64px;
background: #870a4c;
background: radial-gradient(ellipse at center, #ab1465 0%, #8b1e57 100%);
box-shadow: 0px 4px 5px rgba(0, 0, 0, 0.2);
}

.capsuleBtn {
font-weight: bold;
font-size: 10px;
color: #111;
display: flex;
justify-content: center;
align-items: center;
line-height: 40px;
text-transform: uppercase;
width: 64px;
height: 32px;
border-radius: 40px;
background: #222;
background: radial-gradient(ellipse at center, #666 0%, #555 100%);
box-shadow: 0px 4px 5px rgba(0, 0, 0, 0.2);
}

#controller_start {
position: absolute;
bottom: 20px;
right: 15px;
}

#controller_select {
position: absolute;
bottom: 20px;
right: 100px;
}

.btnPressed {
opacity: 0.5;
}

.spinner {
height: 50px;
width: 50px;
margin: 0px auto;
-webkit-animation: rotation 0.8s linear infinite;
-moz-animation: rotation 0.8s linear infinite;
-o-animation: rotation 0.8s linear infinite;
animation: rotation 0.8s linear infinite;
border-left: 10px solid #306850;
border-right: 10px solid #306850;
border-bottom: 10px solid #306850;
border-top: 10px solid #88c070;
border-radius: 100%;
background-color: #031921;
}
@-webkit-keyframes rotation {
from {
-webkit-transform: rotate(0deg);
}
to {
-webkit-transform: rotate(360deg);
}
}
@-moz-keyframes rotation {
from {
-moz-transform: rotate(0deg);
}
to {
-moz-transform: rotate(360deg);
}
}
@-o-keyframes rotation {
from {
-o-transform: rotate(0deg);
}
to {
-o-transform: rotate(360deg);
}
}
@keyframes rotation {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media only screen and (max-width: 640px) {
#game canvas {
margin-top: 0px;
width: 100%;
max-width: 480px;
border: 0px;
border-radius: 0px;
}
}

@media only screen and (max-device-width: 812px) and (orientation: portrait) {
body {
margin: 0;
}

#game {
width: 100%;
position: fixed;
touch-action: none;
}

#game canvas {
margin: 0;
display: block;
width: 100% !important;
height: auto !important;
}
}

@media only screen and (max-device-width: 320px) and (orientation: portrait) {
#controller_dpad {
left: -5px;
bottom: -5px;
}

#controller_a {
right: 5px;
bottom: 95px;
}

#controller_b {
right: 80px;
}

#controller_start {
right: 5px;
}

#controller_select {
right: 80px;
}
}

@media only screen and (max-width: 500px) and (max-height: 400px) {
#controller {
display: none;
}
}

/* Small devices in landscape */
@media only screen and (max-device-width: 300px) and (orientation: landscape) {
html,
body {
height: 100%;
}
body {
display: flex;
justify-content: center;
align-items: center;
}

#game:after {
content: "PLEASE ROTATE ↻";
font-size: 24px;
font-weight: bold;
color: #fff;
}

#game canvas {
display: none;
max-width: 480px;
}

#controller {
display: none;
}
}

/* Devices large enough for landscape */
@media only screen and (min-width: 300px) and (orientation: landscape) {
#controller {
bottom: 50%;
transform: translateY(50%);
opacity: 0.5;
}
}
35 changes: 25 additions & 10 deletions docs/simple.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
<!DOCTYPE html>
<!-- copied from
https://github.com/chrismaltby/gb-studio/blob/v2beta/appData/js-emulator/index.html
(see LICENSE.gbstudio)
-->
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="simple.css">
<title>binjgb (simple)</title>
<style>
canvas {
width: 480px;
height: 432px;
border: 1px solid gray;
image-rendering: pixelated;
image-rendering: -moz-crisp-edges;
}
</style>
</head>
<body>
<canvas width="160" height="144"></canvas>
<div id="gameboy_shell">
<div id="game">
<canvas id="mainCanvas" width="160" height="144">No Canvas Support</canvas>
</div>
<div id="controller">
<div id="controller_dpad">
<div id="controller_left"></div>
<div id="controller_right"></div>
<div id="controller_up"></div>
<div id="controller_down"></div>
</div>
<div id="controller_select" class="capsuleBtn">Select</div>
<div id="controller_start" class="capsuleBtn">Start</div>
<div id="controller_b" class="roundBtn">B</div>
<div id="controller_a" class="roundBtn">A</div>
</div>
</div>
<script src="binjgb.js"></script>
<script src="simple.js"></script>
</body>
Loading

0 comments on commit 8f6a681

Please sign in to comment.