Skip to content

Commit

Permalink
mute sound button
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinychen committed May 29, 2020
1 parent 42ecaf0 commit 5f68a89
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
Binary file added public/sound-off.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/sound-on.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions src/client/lobby.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,22 @@
width: 100vw;
height: 100vh;
}

.toggle-sound {
background-size: cover;
position: fixed;
bottom: 5px;
left: 5px;
width: 30px;
height: 30px;
cursor: pointer;
background-color: lightgray;
}

.toggle-sound.off {
background-image: url('/sound-off.png');
}

.toggle-sound.on {
background-image: url('/sound-on.png');
}
13 changes: 2 additions & 11 deletions src/client/lobby.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,19 @@ export default class SanGuoShaLobby extends React.Component {

componentDidMount() {
// Mobile requires explicit user action to play audio
document.querySelector('#lobby-view').addEventListener('click', this.playAudio);
document.querySelector('#lobby-view').addEventListener('click', this.props.playAudio);
}

componentWillUnmount() {
document.querySelector('#lobby-view').removeEventListener('click', this.playAudio);
document.querySelector('#lobby-view').removeEventListener('click', this.props.playAudio);
}

render() {
return <div className='lobby'>
<div
// TODO remove hack button to turn off sound if too loud
style={{ position: 'absolute', width: '100px', height: '100px' }}
onClick={() => this.props.audio.volume = 0.1 - this.props.audio.volume}
/>
<div className='title'>
<img src='./name.png' alt='sanguosha' />
</div>
<Lobby gameServer={SERVER} lobbyServer={SERVER} gameComponents={GAMES} />
</div>;
}

playAudio = () => {
this.props.audio.play();
};
}
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as classNames from 'classnames';
import React from 'react';
import { render } from 'react-dom';
import Room from './client/room';
Expand All @@ -12,10 +13,20 @@ class App extends React.Component {
this.audio.volume = 0.1;
this.audio.loop = true;
this.audio.play();
this.state = { volume: 0.1 };
}

render() {
return process.env.REACT_APP_LOCAL ? <Room /> : <Lobby audio={this.audio} />;
return <div>
{process.env.REACT_APP_LOCAL ? <Room /> : <Lobby playAudio={() => this.audio.play()} />}
<button
className={classNames('toggle-sound', this.state.volume === 0 ? 'off' : 'on')}
onClick={() => {
this.audio.volume = 0.1 - this.audio.volume;
this.setState({ volume: this.audio.volume });
}}
/>
</div>;
}
}

Expand Down

0 comments on commit 5f68a89

Please sign in to comment.