~whereswaldon/pointstar

ref: v0.0.1 pointstar/client/skin.go -rw-r--r-- 14.1 KiB
1e806177Chris Waldon bugfix: avoid crashing when laying out help 1 year, 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package main

import (
	"fmt"
	"image/color"
	"strconv"

	"gioui.org/f32"
	"gioui.org/layout"
	"gioui.org/op/paint"
	"gioui.org/text"
	"gioui.org/unit"
	"gioui.org/widget/material"
	"git.sr.ht/~whereswaldon/pointstar/client/log"
	"git.sr.ht/~whereswaldon/pointstar/client/widgets"
	"git.sr.ht/~whereswaldon/pointstar/client/widgets/gameui"
	"git.sr.ht/~whereswaldon/pointstar/gamestate"
	"git.sr.ht/~whereswaldon/pointstar/server/protocol"
)

var (
	WarshipCardTextColor       = color.RGBA{A: 255}
	SpecialCardTextColor       = color.RGBA{R: 155, A: 255}
	BattlefieldCardTextColor   = color.RGBA{R: 148, B: 211, A: 255}
	VictoryTextColor           = color.RGBA{R: 255, G: 215, A: 255}
	CurrentTurnPlayerTextColor = color.RGBA{R: 51, G: 204, B: 51, A: 255}
	PassedPlayerTextColor      = color.RGBA{R: 100, G: 100, B: 100, A: 255}
	CardWidth                  = unit.Dp(70)
	UIMaxWidth                 = CardWidth.Scale(15)
	DisabledButtonColor        = color.RGBA{R: 100, G: 100, B: 100, A: 255}
)

// Skin provides methods for rendering the game UI
type Skin struct {
	*gameui.Theme
}

func (s *Skin) LayoutAll(g *gamestate.GameState, u *UIState, gtx *layout.Context) {
	s.LayoutBackground(g, u, gtx)

	switch {
	case !u.Connected && !u.HasConnected:
		log.Println("connecting")
		layout.Center.Layout(gtx, func() {
			material.H3(s.Theme.Theme, "Connecting to server...").Layout(gtx)
		})
	case !u.Connected && u.HasConnected:
		log.Println("Disconnected")
		layout.Center.Layout(gtx, func() {
			material.H3(s.Theme.Theme, "Disconnected unexpectedly! Try reloading to play again.").Layout(gtx)
		})
	default:
		switch g.Phase {
		case gamestate.Lobby:
			s.LayoutLobby(g, u, gtx)
		default:
			layout.N.Layout(gtx, func() {
				gtx.Constraints.Width.Max = gtx.Constraints.Width.Constrain(gtx.Px(UIMaxWidth))
				clicked := false
				for u.HelpButton.Clicked(gtx) {
					clicked = true
				}
				if clicked {
					u.DisplayingHelp = !u.DisplayingHelp
				}
				if u.DisplayingHelp {
					s.LayoutHelp(g, u, gtx)
				} else {
					s.LayoutTable(g, u, gtx)
				}
			})
		}
	}
}

func (s *Skin) LayoutBackground(g *gamestate.GameState, u *UIState, gtx *layout.Context) {
	colorOp := paint.ColorOp{}
	switch g.Battlefield {
	case gamestate.Asteroids:
		colorOp.Color = s.Theme.AsteriodsBackground
	case gamestate.SolarWind:
		colorOp.Color = s.Theme.SolarWindBackground
	default:
		colorOp.Color = s.Theme.Background
	}
	colorOp.Add(gtx.Ops)
	paint.PaintOp{
		Rect: f32.Rectangle{
			Max: f32.Point{
				X: float32(gtx.Constraints.Width.Max),
				Y: float32(gtx.Constraints.Height.Max),
			},
		},
	}.Add(gtx.Ops)
}

func (s *Skin) LayoutVerboseHeader(gtx *layout.Context) {
	layout.Flex{Axis: layout.Vertical}.Layout(gtx,
		layout.Rigid(func() {
			gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
			layout.Center.Layout(gtx, func() {
				heading := material.H1(s.Theme.Theme, ".*")
				heading.Alignment = text.Middle
				heading.Layout(gtx)
			})
		}),
		layout.Rigid(func() {
			gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
			layout.Center.Layout(gtx, func() {
				body := material.H5(s.Theme.Theme, "Welcome to pointstar, a sci-fi strategy card game!")
				body.Alignment = text.Middle
				body.Layout(gtx)
			})
		}),
	)
}

func (s *Skin) LayoutLobby(g *gamestate.GameState, u *UIState, gtx *layout.Context) {
	layout.Center.Layout(gtx, func() {
		layout.Flex{Axis: layout.Vertical}.Layout(gtx,
			layout.Rigid(func() { s.LayoutVerboseHeader(gtx) }),
			layout.Rigid(func() {
				gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
				layout.Center.Layout(gtx, func() {
					if u.PlayerNum == -1 {
						clicked := false
						for u.NameSubmit.Clicked(gtx) {
							clicked = true
						}
						if clicked {
							u.Worker.ToServer <- protocol.SetPlayerName{Name: u.NameEditor.Text()}
						}
						layout.Flex{Axis: layout.Vertical}.Layout(gtx,
							layout.Flexed(.5, func() {}),
							layout.Rigid(func() {
								layout.UniformInset(unit.Dp(8)).Layout(gtx, func() {
									material.Editor(s.Theme.Theme, "Username").Layout(gtx, &u.NameEditor)
								})
							}),
							layout.Rigid(func() {
								layout.UniformInset(unit.Dp(8)).Layout(gtx, func() {
									material.Button(s.Theme.Theme, "Submit").Layout(gtx, &u.NameSubmit)
								})
							}),
							layout.Flexed(.5, func() {}))
					} else {
						clicked := false
						for u.StartButton.Clicked(gtx) {
							clicked = true
						}
						if clicked {
							u.Worker.ToServer <- protocol.StartGame{}
						}
						clicked = false
						for u.AddAIPlayerButton.Clicked(gtx) {
							clicked = true
						}
						if clicked {
							u.Worker.ToServer <- protocol.AddAIPlayer{}
						}
						layout.Flex{Axis: layout.Vertical}.Layout(gtx,
							layout.Rigid(func() {
								gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
								layout.Center.Layout(gtx, func() {
									layout.UniformInset(unit.Dp(8)).Layout(gtx, func() {
										msg := "waiting for other players..."
										material.H6(s.Theme.Theme, msg).Layout(gtx)
									})
								})
							}),
							layout.Rigid(func() {
								gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
								players := "Players: "
								for _, p := range g.Players {
									players += p.Name + " "
								}
								layout.Center.Layout(gtx, func() {
									layout.UniformInset(unit.Dp(8)).Layout(gtx, func() {
										material.Body1(s.Theme.Theme, players).Layout(gtx)
									})
								})
							}),
							layout.Rigid(func() {
								gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
								layout.Center.Layout(gtx, func() {
									layout.Flex{}.Layout(gtx,
										layout.Rigid(func() {
											layout.UniformInset(unit.Dp(8)).Layout(gtx, func() {
												if u.PlayerNum == 0 {
													material.Button(s.Theme.Theme, "Start game").Layout(gtx, &u.StartButton)
												} else {
													material.H6(s.Theme.Theme, "The first player to join can start the game at any time.").Layout(gtx)
												}
											})
										}),
										layout.Rigid(func() {
											layout.UniformInset(unit.Dp(8)).Layout(gtx, func() {
												if u.PlayerNum == 0 {
													material.Button(s.Theme.Theme, "Add AI Player").Layout(gtx, &u.AddAIPlayerButton)
												}
											})
										}),
									)
								})
							}))
					}
				})
			}),
			layout.Flexed(.5, func() {}))
	})
}

// LayoutHelp displays help information for playing the game
func (s *Skin) LayoutHelp(g *gamestate.GameState, u *UIState, gtx *layout.Context) {
	cardWidth := gtx.Px(s.Theme.CardWidth)
	helpWidth := cardWidth * 10
	layout.Center.Layout(gtx, func() {
		layout.Flex{Axis: layout.Vertical}.Layout(gtx,
			layout.Rigid(func() {
				gtx.Constraints.Width.Max = helpWidth
				gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
				layout.Center.Layout(gtx, func() {
					layout.UniformInset(unit.Dp(8)).Layout(gtx, func() {
						material.Button(s.Theme.Theme, "Close Help").Layout(gtx, &u.HelpButton)
					})
				})
			}),
			layout.Rigid(func() {
				gtx.Constraints.Width.Max = helpWidth
				gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
				material.Body1(s.Theme.Theme, gamestate.RulesText).Layout(gtx)
			}),
			layout.Flexed(1, func() {
				gtx.Constraints.Width.Max = helpWidth
				gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
				highestCard := gamestate.Hologram
				u.HelpExampleCards.Axis = layout.Vertical
				u.HelpExampleCards.Layout(gtx, int(highestCard), func(index int) {
					s.Theme.LayoutCardHelp(highestCard-gamestate.Card(index), gtx)
				})
			}),
		)
	})
}

// LayoutTable renders the entire game interface into the provided context
func (s *Skin) LayoutTable(g *gamestate.GameState, u *UIState, gtx *layout.Context) {
	for i := range u.HandCardState[:] {
		if u.HandCardState[i].Clicked(gtx) && g.IsPlayersTurn(u.PlayerNum) {
			u.Worker.ToServer <- protocol.PlayCard{Index: i}
			(&u.HandCardState[i]).ClearHover()
		}
	}
	if u.PassButton.Clicked(gtx) && g.IsPlayersTurn(u.PlayerNum) {
		u.Worker.ToServer <- protocol.PassTurn{}
	}
	var children []layout.FlexChild
	// populate the slice with functions to lay out each player's hand
	for i := range g.Players {
		playerNum := i
		children = append(children, layout.Rigid(func() {
			s.LayoutPlayed(g, playerNum, u, gtx)
		}))
		children = append(children, layout.Flexed(.15, func() {}))
	}
	children = append(children, layout.Flexed(1, func() {
		hoveredCard := gamestate.NoCard
		for i := range u.HandCardState {
			if i >= len(g.Players[u.PlayerNum].Hand) {
				// ensure we aren't out-of-bounds in the hand slice
				break
			}
			if (&u.HandCardState[i]).Hovered(gtx) {
				hoveredCard = g.Players[u.PlayerNum].Hand[i]
			}
		}
		if hoveredCard == gamestate.NoCard {
			for i := range u.PlayedCardState[:len(g.Players)] {
				for k := range u.PlayedCardState[i][:len(g.Players[i].Played)] {
					if (&u.PlayedCardState[i][k]).Hovered(gtx) {
						hoveredCard = g.Players[i].Played[k]
					}
				}
			}
		}
		if hoveredCard != gamestate.NoCard {
			layout.S.Layout(gtx, func() {
				layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
					layout.Rigid(func() {
						layout.Inset{
							Top:  unit.Dp(8),
							Left: unit.Dp(8),
						}.Layout(gtx, func() {
							s.Theme.LayoutCardHeader(hoveredCard, gtx)
						})
					}),
					layout.Rigid(func() {
						layout.UniformInset(unit.Dp(8)).Layout(gtx, func() {
							material.Body2(s.Theme.Theme, gamestate.CardHelpText[hoveredCard]).Layout(gtx)
						})
					}),
				)
			})
		}
	}))
	children = append(children, layout.Rigid(func() {
		s.LayoutHand(g, u, gtx)
	}))
	// lay out all of the rows
	layout.Flex{Axis: layout.Vertical}.Layout(gtx, children...)
}

// LayoutPlayed arranges a row of information about a player and the cards
// that they have played during the current battle
func (s *Skin) LayoutPlayed(g *gamestate.GameState, playerNum int, u *UIState, gtx *layout.Context) {
	player := g.Players[playerNum]
	// prepend player info
	wonCurrentBattle := false
	if g.Phase == gamestate.FinishingBattle {
		winners := g.Winners()
		if len(winners) > 0 {
			for _, winner := range winners {
				if winner == playerNum {
					wonCurrentBattle = true
				}
			}
		}
	}
	hand := player.Played
	layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
		layout.Rigid(func() {
			layout.Inset{Left: unit.Dp(8)}.Layout(gtx, func() {
				gtx.Constraints.Width.Max = gtx.Px(unit.Dp(150))
				gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
				layout.Flex{Axis: layout.Vertical}.Layout(gtx,
					layout.Rigid(func() {
						playerName := material.H3(s.Theme.Theme, player.Name)
						if wonCurrentBattle {
							playerName.Color = VictoryTextColor
						} else if g.Turn == playerNum && g.Phase != gamestate.FinishingBattle {
							playerName.Color = CurrentTurnPlayerTextColor
						}
						if player.IsPassing {
							playerName.Color = PassedPlayerTextColor
						}
						playerName.MaxLines = 1
						playerName.Layout(gtx)
					}),
					layout.Rigid(func() {
						material.H6(s.Theme.Theme, fmt.Sprintf("Victories: %d", player.Victories)).Layout(gtx)
					}),
					layout.Rigid(func() {
						material.H6(s.Theme.Theme, fmt.Sprintf("Hand: %d", len(player.Hand))).Layout(gtx)
					}),
				)
			})
		}),
		layout.Flexed(1, func() {
			layout.Center.Layout(gtx, func() {
				u.PlayedLists[playerNum].Layout(gtx, len(player.Played), func(index int) {
					layout.Center.Layout(gtx, func() {
						cardState := &u.PlayedCardState[playerNum][index]
						s.LayoutCardPadded(player.Played[index], cardState, unit.Dp(2), gtx)
					})
				})
			})
		}),
		layout.Rigid(func() {
			layout.Inset{Right: unit.Dp(8), Top: unit.Dp(8)}.Layout(gtx, func() {
				layout.Flex{Axis: layout.Vertical}.Layout(gtx,
					layout.Rigid(func() {
						var heading material.LabelStyle
						if player.IsPassing {
							heading = material.H6(s.Theme.Theme, "Passed:")
							heading.Color = PassedPlayerTextColor
						} else if wonCurrentBattle {
							heading = material.H6(s.Theme.Theme, "Victory:")
							heading.Color = VictoryTextColor
						} else {
							heading = material.H6(s.Theme.Theme, "Strength:")
						}
						heading.Layout(gtx)
					}),
					layout.Rigid(func() {
						strength := material.H2(s.Theme.Theme, strconv.Itoa(hand.Score(g)))
						if wonCurrentBattle {
							strength.Color = VictoryTextColor
						}
						if player.IsPassing {
							strength.Color = PassedPlayerTextColor
						}
						strength.Layout(gtx)
					}),
				)
			})
		}),
	)
}

// LayoutHand lays out the cards in a player's hand with buttons to play each card
// and to pass the turn.
func (s *Skin) LayoutHand(g *gamestate.GameState, u *UIState, gtx *layout.Context) {
	player := g.Players[u.PlayerNum]
	hand := player.Hand
	isPlayersTurn := g.IsPlayersTurn(u.PlayerNum)
	layout.Flex{Axis: layout.Vertical}.Layout(gtx,
		layout.Rigid(func() {
			layout.SW.Layout(gtx, func() {
				layout.Inset{Left: unit.Dp(8)}.Layout(gtx, func() {
					handLabel := "Your hand:"
					if isPlayersTurn {
						handLabel = "Your hand: (click to play)"
					}
					material.H6(s.Theme.Theme, handLabel).Layout(gtx)
				})
			})
		}),
		layout.Rigid(func() {
			layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
				layout.Flexed(1, func() {
					layout.Center.Layout(gtx, func() {
						u.HandList.Layout(gtx, len(hand), func(index int) {
							card := hand[index]
							cardState := &u.HandCardState[index]
							layout.UniformInset(unit.Dp(2)).Layout(gtx, func() {
								s.LayoutCard(card, cardState, gtx)
							})
						})
					})
				}),
				layout.Rigid(func() {
					layout.Center.Layout(gtx, func() {
						layout.Flex{Axis: layout.Vertical}.Layout(gtx,
							layout.Rigid(func() {
								layout.UniformInset(unit.Dp(8)).Layout(gtx, func() {
									passBtn := material.Button(s.Theme.Theme, "Pass")
									if !isPlayersTurn {
										passBtn.Background = DisabledButtonColor
									}
									passBtn.Layout(gtx, &u.PassButton)
								})
							}),
							layout.Rigid(func() {
								layout.UniformInset(unit.Dp(8)).Layout(gtx, func() {
									material.Button(s.Theme.Theme, "Help").Layout(gtx, &u.HelpButton)
								})
							}),
						)
					})
				}),
			)
		}),
	)
}

func (s *Skin) LayoutCardPadded(c gamestate.Card, cardState *widgets.Card, padding unit.Value, gtx *layout.Context) {
	layout.UniformInset(padding).Layout(gtx, func() {
		s.LayoutCard(c, cardState, gtx)
	})
}