Skip to content

Commit 082397f

Browse files
authored
Merge pull request #2 from julien2512/main
A stack of dragged float cards moves together
2 parents d8a0e71 + 4e73955 commit 082397f

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

render.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (t *tableRender) Layout(size fyne.Size) {
103103
updateCardPosition(t.pile1, smallPad+cardSize.Width, 0)
104104
updateCardPosition(t.pile2, smallPad+cardSize.Width+overlap, 0)
105105
updateCardPosition(t.pile3, smallPad+cardSize.Width+overlap*2, 0)
106-
updateCardPosition(t.table.float, 0, 0)
106+
updateCardPosition(t.table.float[0], 0, 0)
107107

108108
updateCardPosition(t.build1, size.Width-smallPad*3-cardSize.Width*4, 0)
109109
updateCardPosition(t.build2, size.Width-smallPad*2-cardSize.Width*3, 0)
@@ -207,18 +207,18 @@ func (t *tableRender) positionForCard(_ *Card) *canvas.Image {
207207
return nil
208208
}
209209

210-
func (t *tableRender) findCard(pos fyne.Position) (*Card, *canvas.Image, bool) {
210+
func (t *tableRender) findCard(pos fyne.Position) ([]*Card, []*canvas.Image, bool) {
211211
if t.game.Draw3 != nil {
212212
if withinCardBounds(t.pile3, pos) {
213-
return t.game.Draw3, t.pile3, false
213+
return []*Card{t.game.Draw3}, []*canvas.Image{t.pile3}, false
214214
}
215215
} else if t.game.Draw2 != nil {
216216
if withinCardBounds(t.pile2, pos) {
217-
return t.game.Draw2, t.pile2, false
217+
return []*Card{t.game.Draw2}, []*canvas.Image{t.pile2}, false
218218
}
219219
} else if t.game.Draw1 != nil {
220220
if withinCardBounds(t.pile1, pos) {
221-
return t.game.Draw1, t.pile1, true
221+
return []*Card{t.game.Draw1}, []*canvas.Image{t.pile1}, true
222222
}
223223
}
224224

@@ -243,10 +243,10 @@ func (t *tableRender) findCard(pos fyne.Position) (*Card, *canvas.Image, bool) {
243243
return nil, nil, false
244244
}
245245

246-
func (t *tableRender) findOnStack(render *stackRender, stack *Stack, pos fyne.Position) (*Card, *canvas.Image) {
246+
func (t *tableRender) findOnStack(render *stackRender, stack *Stack, pos fyne.Position) ([]*Card, []*canvas.Image) {
247247
for i := len(stack.Cards) - 1; i >= 0; i-- {
248248
if withinCardBounds(render.cards[i], pos) {
249-
return stack.Cards[i], render.cards[i]
249+
return stack.Cards[i:len(stack.Cards)],render.cards[i:len(stack.Cards)]
250250
}
251251
}
252252

@@ -304,7 +304,9 @@ func newTableRender(table *Table) *tableRender {
304304
render.appendStack(render.stack6)
305305
render.appendStack(render.stack7)
306306

307-
render.objects = append(render.objects, container.NewWithoutLayout(table.float))
307+
for i:=0; i<len(table.float); i++ {
308+
render.objects = append(render.objects, container.NewWithoutLayout(table.float[i]))
309+
}
308310
render.Refresh()
309311
return render
310312
}

table.go

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ type Table struct {
2222
game *Game
2323
selected *Card
2424

25-
float *canvas.Image
26-
floatSource *canvas.Image
25+
float []*canvas.Image
26+
floatSource []*canvas.Image
2727
floatPos fyne.Position
28+
2829
shuffle *widget.ToolbarAction
2930

30-
findCard func(fyne.Position) (*Card, *canvas.Image, bool)
31+
findCard func(fyne.Position) ([]*Card, []*canvas.Image, bool)
3132
stackPos func(int) fyne.Position
3233
}
3334

@@ -119,8 +120,13 @@ func (t *Table) Restart() {
119120
// Dragged is called when the user drags on the table widget
120121
func (t *Table) Dragged(event *fyne.DragEvent) {
121122
t.floatPos = event.Position
122-
if !t.float.Hidden { // existing drag
123-
t.float.Move(t.float.Position().Add(event.Dragged))
123+
if !t.float[0].Hidden { // existing drag
124+
125+
for i:=0;i<len(t.float);i++ {
126+
if (t.float[i] != nil && !t.float[i].Hidden) {
127+
t.float[i].Move(t.float[i].Position().Add(event.Dragged))
128+
}
129+
}
124130
return
125131
}
126132

@@ -132,34 +138,43 @@ func (t *Table) Dragged(event *fyne.DragEvent) {
132138
if card == nil {
133139
return
134140
}
135-
if !card.FaceUp { // only drag visible cards
141+
if !card[0].FaceUp { // only drag visible cards
136142
return
137143
}
138144

139-
t.floatSource = source
140-
t.float.Resource = source.Resource
141-
t.selected = card
142-
source.Resource = nil
143-
if last {
144-
source.Resource = faces.ForSpace()
145+
t.selected = card[0]
146+
147+
for i:=0;i<len(source);i++ {
148+
t.floatSource[i] = source[i]
149+
t.float[i].Resource = source[i].Resource
150+
if last && i==0 {
151+
source[i].Resource = faces.ForSpace()
152+
} else {
153+
source[i].Resource = nil
154+
}
155+
source[i].Image = nil
156+
source[i].Refresh()
157+
t.float[i].Refresh()
158+
updateCardPosition(t.float[i], source[i].Position().X, source[i].Position().Y)
159+
t.float[i].Show()
145160
}
146-
source.Image = nil
147-
source.Refresh()
148-
t.float.Refresh()
149-
t.float.Move(source.Position())
150-
t.float.Show()
151161
}
152162

153163
// DragEnd is called when the user stops dragging on the table widget
154164
func (t *Table) DragEnd() {
155-
t.float.Hide()
165+
for i:=0;i<ValueKing;i++ {
166+
t.float[i].Hide()
167+
}
168+
156169
if t.dropCard(t.floatPos) {
157170
return
158171
}
159-
160-
if t.floatSource != nil {
161-
t.floatSource.Resource = t.float.Resource
162-
t.floatSource.Refresh()
172+
173+
for i:=0;i<ValueKing;i++ {
174+
if t.floatSource[i] != nil {
175+
t.floatSource[i].Resource = t.float[i].Resource
176+
t.floatSource[i].Refresh()
177+
}
163178
}
164179
}
165180

@@ -301,7 +316,12 @@ func NewTable(g *Game) *Table {
301316
table := &Table{game: g}
302317
table.ExtendBaseWidget(table)
303318

304-
table.float = &canvas.Image{}
305-
table.float.Hide()
319+
table.float = make([]*canvas.Image,ValueKing)
320+
for i:=0;i<ValueKing;i++ {
321+
table.float[i] = &canvas.Image{}
322+
table.float[i].Hide()
323+
}
324+
table.floatSource = make([]*canvas.Image,ValueKing)
325+
306326
return table
307327
}

0 commit comments

Comments
 (0)