From 8381c01d38dd7c765bdbea1ce2ff476f59844b5c Mon Sep 17 00:00:00 2001 From: Songyi Huang Date: Sun, 23 May 2021 00:23:56 -0700 Subject: [PATCH] add support for sliding to select cards --- src/assets/cards.css | 12 ++- src/assets/doudizhu.scss | 2 +- src/components/GameBoard/DoudizhuGameBoard.js | 97 +++++++++++++++++-- src/view/PvEView/PvEDoudizhuDemoView.js | 18 +++- 4 files changed, 117 insertions(+), 12 deletions(-) diff --git a/src/assets/cards.css b/src/assets/cards.css index 6e76e70..23af653 100644 --- a/src/assets/cards.css +++ b/src/assets/cards.css @@ -70,7 +70,17 @@ bottom: 0; } -.playingCards.selectable label.card:active::after { +/* .playingCards.selectable label.card:active::after { + background-color: rgba(23, 146, 210, 0.5); + content: ''; + position: absolute; + left: 0; + top: 0; + width: 3em; + height: 4em; +} */ + +.playingCards.selectable label.card.user-selecting::after { background-color: rgba(23, 146, 210, 0.5); content: ''; position: absolute; diff --git a/src/assets/doudizhu.scss b/src/assets/doudizhu.scss index 7b026c4..01b0df8 100644 --- a/src/assets/doudizhu.scss +++ b/src/assets/doudizhu.scss @@ -232,7 +232,7 @@ .played-card-area { width: 100%; position: relative; - //transform: translateY(25px); + transform: translateY(-10px); .playingCards ul.hand { margin-bottom: 0; diff --git a/src/components/GameBoard/DoudizhuGameBoard.js b/src/components/GameBoard/DoudizhuGameBoard.js index ceed829..c8dd91e 100644 --- a/src/components/GameBoard/DoudizhuGameBoard.js +++ b/src/components/GameBoard/DoudizhuGameBoard.js @@ -10,6 +10,16 @@ import PlaceHolderPlayer from '../../assets/images/Portrait/Player.png'; import { computeHandCardsWidth, millisecond2Second, sortDoudizhuCards, translateCardData } from '../../utils'; class DoudizhuGameBoard extends React.Component { + constructor(props) { + super(props); + + this.isSelectingCards = false; + this.selectingCards = { start: null, cards: [] }; + this.state = { + highlightedCards: [], + }; + } + computePlayerPortrait(playerId, playerIdx) { if (this.props.playerInfo.length > 0) { return this.props.playerInfo[playerIdx].role === 'landlord' ? ( @@ -32,6 +42,52 @@ class DoudizhuGameBoard extends React.Component { ); } + handleContainerMouseLeave() { + if (this.isSelectingCards) { + console.log('container leave'); + this.isSelectingCards = false; + this.selectingCards = { start: null, cards: [] }; + this.setState({ highlightedCards: [] }); + } + } + + handleContainerMouseUp() { + if (this.isSelectingCards) { + console.log('container up'); + this.isSelectingCards = false; + this.props.handleSelectedCards(this.selectingCards.cards); + this.selectingCards = { start: null, cards: [] }; + this.setState({ highlightedCards: [] }); + } else { + this.props.handleMainPlayerAct('deselect'); + } + } + + handleCardMouseDown(card, idx) { + console.log('down'); + this.isSelectingCards = true; + this.selectingCards.start = idx; + this.selectingCards.cards = [card]; + this.setState({ highlightedCards: this.selectingCards.cards }); + console.log(this.selectingCards); + } + + handleCardMouseOver(allCards, card, idx) { + if (this.isSelectingCards) { + console.log('over'); + let tmpCards; + if (idx > this.selectingCards.start) { + tmpCards = allCards.slice(this.selectingCards.start, idx + 1); + } else if (idx < this.selectingCards.start) { + tmpCards = allCards.slice(idx, this.selectingCards.start + 1); + } else { + tmpCards = [card]; + } + this.selectingCards = { ...this.selectingCards, cards: tmpCards }; + this.setState({ highlightedCards: this.selectingCards.cards }); + } + } + computeSingleLineHand(inputCards, fadeClassName = '', cardSelectable = false) { const cards = inputCards === 'pass' ? inputCards : sortDoudizhuCards(inputCards); if (cards === 'pass') { @@ -48,19 +104,30 @@ class DoudizhuGameBoard extends React.Component { }`} >