This is my first version of cards.
I can drag and drop them around the screen.
Issues:
- Flex instead of Stack + Positioned means that I can't have cards overlap.
- tons of commented out code and debug strings/info.
- Stack + Positioned errors when I use it. Constraints are violated?
I need to investigate the latter two issues so that I can send bug reports.
At the same time, this may involve deleting the extra commented out code.
diff --git a/lib/components/card.dart b/lib/components/card.dart
index 8fb5c7b..e6723f2 100644
--- a/lib/components/card.dart
+++ b/lib/components/card.dart
@@ -3,10 +3,11 @@
import 'package:sky/widgets.dart' as widgets;
import 'package:sky/theme/colors.dart' as colors;
import 'dart:sky' as sky;
+import 'package:vector_math/vector_math.dart' as vector_math;
class CardComponent extends widgets.StatefulComponent {
- final Card card;
- final bool faceUp;
+ Card card;
+ bool faceUp;
widgets.DragController dragController;
widgets.Offset displacement = widgets.Offset.zero;
@@ -18,6 +19,8 @@
//assert(false); // Why do we need to do this?
//dragController = other.dragController;
//displacement = other.displacement;
+ card = other.card;
+ faceUp = other.faceUp;
}
widgets.Widget build() {
@@ -27,8 +30,8 @@
onPointerCancel: _cancelDrag,
onPointerUp: _drop,
child: new widgets.Container(
- width: card_constants.CARD_WIDTH,
- height: card_constants.CARD_HEIGHT,
+ //width: card_constants.CARD_WIDTH,
+ //height: card_constants.CARD_HEIGHT,
child: new widgets.Container(
decoration: new widgets.BoxDecoration(
border: new widgets.Border.all(
@@ -37,10 +40,13 @@
),
backgroundColor: dragController == null ? colors.Orange[500] : colors.Brown[500]
),
- child: new widgets.Flex([
- imageFromCard(card, faceUp),
- new widgets.Text(status)
- ], direction: widgets.FlexDirection.vertical)
+ child: new widgets.Transform(
+ transform: new vector_math.Matrix4.identity().translate(displacement.dx, displacement.dy),
+ child: new widgets.Flex([
+ imageFromCard(card, faceUp),
+ new widgets.Text(status)
+ ], direction: widgets.FlexDirection.vertical)
+ )
)
)
);
@@ -64,7 +70,6 @@
widgets.EventDisposition _updateDrag(sky.PointerEvent event) {
setState(() {
- dragController = new widgets.DragController(new CardDragData(this.card));
dragController.update(new widgets.Point(event.x, event.y));
displacement += new widgets.Offset(event.dx, event.dy);
status = 'dragU ${event.x.toStringAsFixed(0)} ${event.y.toStringAsFixed(0)}';
@@ -103,4 +108,8 @@
final Card card;
CardDragData(this.card);
+
+ String toString() {
+ return card.toString();
+ }
}
\ No newline at end of file
diff --git a/lib/components/card_collection.dart b/lib/components/card_collection.dart
index 056cded..d041951 100644
--- a/lib/components/card_collection.dart
+++ b/lib/components/card_collection.dart
@@ -11,34 +11,62 @@
}
class CardCollectionComponent extends StatefulComponent {
- final List<Card> cards;
- final Orientation orientation;
- final bool faceUp;
- final Function parentHandleAccept;
+ List<Card> cards;
+ Orientation orientation;
+ bool faceUp;
+ Function parentCallback;
String status = 'bar';
- CardCollectionComponent(this.cards, this.faceUp, this.orientation, this.parentHandleAccept);
+ CardCollectionComponent(this.cards, this.faceUp, this.orientation, this.parentCallback);
void syncFields(CardCollectionComponent other) {
//assert(false); // Why do we need to do this?
+ //status = other.status;
+ cards = other.cards;
+ orientation = other.orientation;
+ faceUp = other.faceUp;
+ parentCallback = other.parentCallback;
}
void _handleAccept(CardDragData data) {
setState(() {
- status = 'ACCEPT';
+ status = 'ACCEPT ${data.card.toString()}';
+ parentCallback(data.card, this.cards);
});
- this.parentHandleAccept(data.card, this.cards);
+ }
+
+ List<Widget> flexCards(List<Widget> cardWidgets) {
+ List<Widget> flexWidgets = new List<Widget>();
+ cardWidgets.forEach((cardWidget) => flexWidgets.add(new Flexible(child: cardWidget)));
+ return flexWidgets;
+ }
+
+ Widget wrapCards(List<Widget> cardWidgets) {
+ switch (this.orientation) {
+ case Orientation.vert:
+ return new Flex(flexCards(cardWidgets), direction: FlexDirection.vertical);
+ case Orientation.horz:
+ return new Flex(flexCards(cardWidgets));
+ case Orientation.fan:
+ // unimplemented, so we'll fall through to show1, for now.
+ // Probably a Stack + Positioned
+ case Orientation.show1:
+ return new Stack(cardWidgets);
+ default:
+ assert(false);
+ return null;
+ }
}
Widget build() {
// Let's just do horizontal for now, it's too complicated otherwise.
- double cardDelta = card_constants.CARD_WIDTH;
+ /*double cardDelta = card_constants.CARD_WIDTH;
if (cards.length > 6) {
//cardDelta = card_constants.CARD_WIDTH / cards.length; // just make it tiny
cardDelta -= card_constants.CARD_WIDTH * (cards.length - 6) / cards.length;
- }
+ }*/
List<Widget> cardComponents = new List<Widget>();
cardComponents.add(new Text(status));
@@ -49,10 +77,11 @@
// left: i * cardDelta,
child: new CardComponent(cards[i], faceUp)
));*/
- cardComponents.add(new Transform(
+ /*cardComponents.add(new Transform(
transform: new vector_math.Matrix4.identity().translate(i * cardDelta, 40.0),
child: new CardComponent(cards[i], faceUp)
- ));
+ ));*/
+ cardComponents.add(new CardComponent(cards[i], faceUp)); // flex
}
@@ -70,6 +99,8 @@
return new DragTarget<CardDragData>(
onAccept: _handleAccept,
builder: (List<CardDragData> data, _) {
+ print(this.cards.length);
+ print(data);
return new Container(
decoration: new BoxDecoration(
border: new Border.all(
@@ -78,9 +109,9 @@
),
backgroundColor: data.isEmpty ? colors.Grey[500] : colors.Green[500]
),
- height: 150.0,
+ height: 100.0,
margin: new EdgeDims.all(10.0),
- child: new Stack(cardComponents)
+ child: wrapCards(cardComponents)//new Stack(cardComponents)
);
}
);
diff --git a/lib/components/game.dart b/lib/components/game.dart
index 626434c..246393b 100644
--- a/lib/components/game.dart
+++ b/lib/components/game.dart
@@ -7,11 +7,13 @@
import 'package:vector_math/vector_math.dart' as vector_math;
class GameComponent extends StatefulComponent {
- final Game game;
+ Game game;
GameComponent(this.game);
- void syncFields(GameComponent other) {}
+ void syncFields(GameComponent other) {
+ this.game = other.game;
+ }
Widget build() {
switch (game.gameType) {
@@ -22,38 +24,43 @@
}
}
- void _parentHandleAccept(Card card, List<Card> toList) {
- // That means that this card was dragged to this other Card collection component.
+ _updateGameCallback(Card card, List<Card> dest) {
setState(() {
- game.move(card, toList);
+ game.move(card, dest);
});
}
Widget buildHearts() {
List<Widget> cardCollections = new List<Widget>();
+
+ // debugString
+ cardCollections.add(new Text(game.debugString));
+
for (int i = 0; i < 4; i++) {
List<Card> cards = game.cardCollections[i];
- CardCollectionComponent c = new CardCollectionComponent(cards, true, Orientation.horz, _parentHandleAccept);
+ CardCollectionComponent c = new CardCollectionComponent(cards, true, Orientation.horz, _updateGameCallback);
- cardCollections.add(new Positioned(
+ /*cardCollections.add(new Positioned(
top: i * (card_constants.CARD_HEIGHT + 20.0),
child: c
- ));
+ ));*/
/*cardCollections.add(new Transform(
transform: new vector_math.Matrix4.identity().translate(0.0, i * (card_constants.CARD_HEIGHT + 20.0)),
child: c
));*/
+
+ cardCollections.add(c); // flex
}
// game.cardCollections[4] is a discard pile
- cardCollections.add(new Transform(
+ /*cardCollections.add(new Transform(
transform: new vector_math.Matrix4.identity().translate(0.0, 4 * (card_constants.CARD_HEIGHT + 20.0)),
child: new Container(
decoration: new BoxDecoration(backgroundColor: colors.Green[500], borderRadius: 5.0),
child: new CardCollectionComponent(game.cardCollections[4], true, Orientation.horz, _parentHandleAccept)
)
- ));
+ ));*/
/*cardCollections.add(new Positioned(
top: 4 * (card_constants.CARD_HEIGHT + 20.0),
child: new Container(
@@ -62,11 +69,17 @@
)
));*/
+ cardCollections.add(new Container(
+ decoration: new BoxDecoration(backgroundColor: colors.Green[500], borderRadius: 5.0),
+ child: new CardCollectionComponent(game.cardCollections[4], true, Orientation.show1, _updateGameCallback)
+ ));
+
// game.cardCollections[5] is just not shown
+
return new Container(
decoration: new BoxDecoration(backgroundColor: colors.Pink[500]),
- child: new Stack(cardCollections)
+ child: new Flex(cardCollections, direction: FlexDirection.vertical)//new Stack(cardCollections)
);
}
}
\ No newline at end of file
diff --git a/lib/logic/game.dart b/lib/logic/game.dart
index 8329ec9..0aeaf9d 100644
--- a/lib/logic/game.dart
+++ b/lib/logic/game.dart
@@ -66,6 +66,7 @@
];
final Random random = new Random();
+ String debugString = 'hello?';
Game.hearts(int playerNumber) : gameType = GameType.Hearts {
// playerNumber would be used in a real game, but I have to ignore it for debugging.
@@ -90,15 +91,21 @@
void move(Card card, List<Card> dest) {
// The first step is to find the card. Where is it?
// then we can remove it and add to the dest.
+ debugString = 'Moving... ${card.toString()}';
int i = findCard(card);
- assert(i != -1);
+ if (i == -1) {
+ debugString = 'NO... ${card.toString()}';
+ return;
+ }
cardCollections[i].remove(card);
dest.add(card);
+ debugString = 'Move ${i} ${card.toString()}';
+ print(debugString);
}
// Which card collection has the card?
int findCard(Card card) {
- for (var i = 0; i < cardCollections; i++) {
+ for (int i = 0; i < cardCollections.length; i++) {
if (cardCollections[i].contains(card)) {
return i;
}