@@ -57,15 +57,15 @@ class GameHand extends CustomPainterComponent
57
57
GameHand () : super (anchor: Anchor .topLeft, painter: GameHandCustomPainter ());
58
58
59
59
@override
60
- void update (double dt) {
60
+ Future < void > update (double dt) async {
61
61
if (_needsLayout) {
62
62
_layoutChildren ();
63
63
_needsLayout = false ;
64
64
}
65
65
if (_isDirty) {
66
66
_isDirty = false ;
67
67
if (isMounted) {
68
- _buildHand (bloc.state);
68
+ await _buildHand (bloc.state);
69
69
}
70
70
_needsLayout = true ;
71
71
}
@@ -76,8 +76,8 @@ class GameHand extends CustomPainterComponent
76
76
77
77
@override
78
78
void onNewState (ClientWorldState state) {
79
- _currentScroll = 0 ;
80
79
_isDirty = true ;
80
+ _currentScroll = 0 ;
81
81
}
82
82
83
83
@override
@@ -107,21 +107,19 @@ class GameHand extends CustomPainterComponent
107
107
final childrenLength = children.length;
108
108
if (childrenLength == 0 ) return ;
109
109
final center = Vector2 (width / 2 , height);
110
- final double active = _currentScroll.clamp (0 , childrenLength - 1 );
110
+ final double active = (_currentScroll - childrenLength + 1 )
111
+ .abs ()
112
+ .clamp (0 , childrenLength - 1 );
111
113
112
114
children.toList ().whereType <HandItem >().forEachIndexed ((index, element) {
113
- final double activeRelative = index - active ;
115
+ final double activeRelative = active - index ;
114
116
// Figure out how "active" this item is (0 = fully active, 1 = inactive)
115
- final progress = 1 - activeRelative.abs ().clamp (0 , 1 );
116
117
var x = center.x + 128 * activeRelative;
117
118
var y = center.y;
118
119
if (game.settingsCubit.state.stackedCards) {
119
- final currentWidth =
120
- itemWidth + (activeItemWidth - itemWidth) * progress;
121
-
122
120
final offset = activeRelative <= - 1 ? - activeItemWidth / 2 : 0 ;
123
121
y = center.y + itemYOffset * activeRelative.abs ();
124
- x = center.x + offset + activeRelative * currentWidth ;
122
+ x = center.x + offset + activeRelative * itemWidth ;
125
123
element.angle = activeRelative * itemAngle;
126
124
}
127
125
@@ -131,47 +129,50 @@ class GameHand extends CustomPainterComponent
131
129
});
132
130
}
133
131
134
- void _buildHand (ClientWorldState state) {
132
+ Future <void > _addChildren (Iterable <Component > items) {
133
+ final reversed = items.toList ().reversed;
134
+ return addAll (reversed);
135
+ }
136
+
137
+ Future <void > _buildHand (ClientWorldState state) {
135
138
for (final child in children) {
136
139
child.removeFromParent ();
137
140
}
138
141
painter = GameHandCustomPainter (
139
142
showHand: state.showHand, color: state.colorScheme.surface);
140
- if (! state.showHand) return ;
143
+ if (! state.showHand) return Future . value () ;
141
144
final selected = state.selectedCell;
142
145
final cell = state.table.cells[selected];
143
146
if (selected == null ) {
144
147
final deck = state.selectedDeck;
145
148
final packItem =
146
149
deck != null ? state.assetManager.getDeckItem (deck) : null ;
147
150
if (packItem != null ) {
148
- _buildDeckHand (state, packItem, state.showDuplicates);
151
+ return _buildDeckHand (state, packItem, state.showDuplicates);
149
152
} else {
150
- _buildFreeHand (state);
153
+ return _buildFreeHand (state);
151
154
}
152
155
} else {
153
- _buildCellHand (selected, cell);
156
+ return _buildCellHand (selected, cell);
154
157
}
155
158
}
156
159
157
- void _buildFreeHand (ClientWorldState state) {
160
+ Future < void > _buildFreeHand (ClientWorldState state) {
158
161
final decks = state.packs.expand ((e) => e.value.getDeckItems (e.key));
159
- for (final deck in decks) {
160
- final item = DeckDefinitionHandItem (item: deck);
161
- if (item.matches (state, state.searchTerm)) add (item);
162
- }
162
+ return _addChildren (decks.map ((e) => DeckDefinitionHandItem (item: e)).where (
163
+ (e) => e.matches (state, state.searchTerm),
164
+ ));
163
165
}
164
166
165
- void _addFigures (ClientWorldState state,
167
+ Future < void > _addFigures (ClientWorldState state,
166
168
Iterable <(PackItem <FigureDefinition >, String ?)> figures) {
167
- for (final figure in figures) {
168
- final item = FigureDefinitionHandItem (item: figure);
169
- if (item.matches (state, state.searchTerm)) add (item);
170
- }
169
+ return _addChildren (figures
170
+ .map ((e) => FigureDefinitionHandItem (item: e))
171
+ .where ((e) => e.matches (state, state.searchTerm)));
171
172
}
172
173
173
- void _buildDeckHand (ClientWorldState state, PackItem < DeckDefinition > deck ,
174
- bool showDuplicates) {
174
+ Future < void > _buildDeckHand (ClientWorldState state,
175
+ PackItem < DeckDefinition > deck, bool showDuplicates) {
175
176
Iterable <FigureDeckDefinition > deckFigures = deck.item.figures;
176
177
Iterable <BoardDeckDefinition > boards = deck.item.boards;
177
178
if (! showDuplicates) {
@@ -195,27 +196,37 @@ class GameHand extends CustomPainterComponent
195
196
},
196
197
);
197
198
}
198
- for (final board in deck.item.boards) {
199
- final definition = deck.pack.getBoardItem (board.name, deck.namespace);
200
- if (definition == null ) continue ;
201
- add (BoardDefinitionHandItem (item: definition));
202
- }
203
- final figures = deckFigures.map ((e) {
204
- final figure = deck.pack.getFigureItem (e.name, deck.namespace);
205
- if (figure == null ) return null ;
206
- return (figure, e.variation);
207
- }).nonNulls;
208
- _addFigures (state, figures);
199
+ return Future .wait ([
200
+ _addFigures (
201
+ state,
202
+ deckFigures.map ((e) {
203
+ final figure = deck.pack.getFigureItem (e.name, deck.namespace);
204
+ if (figure == null ) return null ;
205
+ return (figure, e.variation);
206
+ }).nonNulls),
207
+ _addChildren (
208
+ boards
209
+ .map ((e) => deck.pack.getBoardItem (e.name, deck.namespace))
210
+ .nonNulls
211
+ .map ((e) => BoardDefinitionHandItem (item: e))
212
+ .where ((e) => e.matches (state, state.searchTerm)),
213
+ ),
214
+ ]);
209
215
}
210
216
211
- void _buildCellHand (VectorDefinition location, TableCell ? cell) {
212
- for (final tile in cell? .tiles.asMap ().entries ?? const Iterable .empty ()) {
213
- add (BoardTileHandItem (item: (location, tile.key, tile.value)));
214
- }
215
- for (final object
216
- in cell? .objects.asMap ().entries ?? const Iterable .empty ()) {
217
- add (GameObjectHandItem (item: (location, object.key, object.value)));
218
- }
217
+ Future <void > _buildCellHand (VectorDefinition location, TableCell ? cell) {
218
+ return Future .wait ([
219
+ _addChildren (
220
+ cell? .objects.asMap ().entries.map (
221
+ (e) => GameObjectHandItem (item: (location, e.key, e.value))) ??
222
+ const Iterable .empty (),
223
+ ),
224
+ _addChildren (
225
+ cell? .tiles.asMap ().entries.map (
226
+ (e) => BoardTileHandItem (item: (location, e.key, e.value))) ??
227
+ const Iterable .empty (),
228
+ ),
229
+ ]);
219
230
}
220
231
221
232
bool get isShowing => bloc.state.showHand;
@@ -267,14 +278,14 @@ class GameHand extends CustomPainterComponent
267
278
super .onDragEnd (event);
268
279
}
269
280
270
- void dragScroll (double delta) => scroll (delta * - 0.025 );
281
+ void dragScroll (double delta) => scroll (delta * 0.025 );
271
282
272
283
void scroll (double delta) {
273
284
if (! isShowing) return ;
274
- _currentScroll = (_currentScroll + delta).clamp (0 , children.length - 1 );
285
+ _currentScroll = (_currentScroll - delta).clamp (0 , children.length - 1 );
275
286
_needsLayout = true ;
276
287
}
277
288
278
- void moveLeft () => scroll (- 1 );
279
- void moveRight () => scroll (1 );
289
+ void moveLeft () => scroll (1 );
290
+ void moveRight () => scroll (- 1 );
280
291
}
0 commit comments