Skip to content

Commit 8ff4745

Browse files
committed
drag-n-drop for cards and decks
1 parent 44260ce commit 8ff4745

21 files changed

Lines changed: 984 additions & 216 deletions

examples/react/modules/routes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import phases from './phases';
1212
import secret_state from './secret-state';
1313
import random from './random';
1414
import turnorder from './turnorder';
15+
import ui from './ui';
1516
import threejs from './threejs';
1617

1718
const routes = [
@@ -39,6 +40,10 @@ const routes = [
3940
name: 'Secret State',
4041
routes: secret_state.routes,
4142
},
43+
{
44+
name: 'UI',
45+
routes: ui.routes,
46+
},
4247
{
4348
name: 'Other Frameworks',
4449
routes: threejs.routes,
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2018 The boardgame.io Authors.
3+
*
4+
* Use of this source code is governed by a MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import React from 'react';
10+
import { UI, Card, Deck } from 'boardgame.io/ui';
11+
12+
function handler(type, fn) {
13+
return arg => {
14+
console.log(type + ': ' + JSON.stringify(arg));
15+
if (fn) fn(arg);
16+
};
17+
}
18+
19+
class Board extends React.Component {
20+
constructor(props) {
21+
super(props);
22+
23+
this.state = {
24+
deck1: [1, 2, 3],
25+
deck2: [],
26+
free: [4],
27+
};
28+
}
29+
30+
deck1Drop = arg => {
31+
this.setState(s => ({
32+
free: s.free.filter(t => t != arg),
33+
deck2: s.deck2.filter(t => t != arg),
34+
deck1: [...s.deck1.filter(t => t != arg), arg],
35+
}));
36+
};
37+
38+
deck2Drop = arg => {
39+
this.setState(s => ({
40+
free: s.free.filter(t => t != arg),
41+
deck1: s.deck1.filter(t => t != arg),
42+
deck2: [...s.deck2.filter(t => t != arg), arg],
43+
}));
44+
};
45+
46+
render() {
47+
return (
48+
<UI sandboxMode={true}>
49+
<Deck
50+
onDrop={handler('deck1 onDrop', this.deck1Drop)}
51+
onClick={handler('deck1 onClick')}
52+
onRemove={handler('deck1 onRemove', this.deck1Remove)}
53+
>
54+
{this.state.deck1.map(c => (
55+
<Card key={c} data={c} back={c} onClick={handler(c + ' onClick')} />
56+
))}
57+
</Deck>
58+
59+
<Deck
60+
onDrop={handler('deck2 onDrop', this.deck2Drop)}
61+
onClick={handler('deck2 onClick')}
62+
onRemove={handler('deck2 onRemove', this.deck2Remove)}
63+
>
64+
{this.state.deck2.map(c => (
65+
<Card key={c} data={c} back={c} onClick={handler(c + ' onClick')} />
66+
))}
67+
</Deck>
68+
69+
<div>
70+
{this.state.free.map(c => (
71+
<Card key={c} data={c} back={c} onClick={handler(c + ' onClick')} />
72+
))}
73+
</div>
74+
</UI>
75+
);
76+
}
77+
}
78+
79+
export default Board;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2017 The boardgame.io Authors.
3+
*
4+
* Use of this source code is governed by a MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import React from 'react';
10+
import { Client } from 'boardgame.io/react';
11+
import TicTacToe from '../game';
12+
import Board from './board';
13+
14+
const App = Client({
15+
game: TicTacToe,
16+
board: Board,
17+
});
18+
19+
const Singleplayer = () => (
20+
<div style={{ padding: 50 }}>
21+
<h1>Cards & Decks</h1>
22+
<App gameID="single" />
23+
</div>
24+
);
25+
26+
export default Singleplayer;

examples/react/modules/ui/game.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright 2017 The boardgame.io Authors
3+
*
4+
* Use of this source code is governed by a MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import { Game } from 'boardgame.io/core';
10+
11+
const UIDemo = Game({
12+
name: 'ui-demo',
13+
});
14+
15+
export default UIDemo;

examples/react/modules/ui/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright 2017 The boardgame.io Authors.
3+
*
4+
* Use of this source code is governed by a MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import routes from './routes';
10+
11+
// Any other additional setup for this this module
12+
13+
export default {
14+
routes,
15+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2017 The boardgame.io Authors.
3+
*
4+
* Use of this source code is governed by a MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import Singleplayer from './components/singleplayer';
10+
11+
const routes = [
12+
{
13+
path: '/ui',
14+
text: 'Cards & Decks',
15+
component: Singleplayer,
16+
},
17+
];
18+
19+
export default routes;

0 commit comments

Comments
 (0)