-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_point_confirmation_view.go
109 lines (90 loc) · 2.94 KB
/
select_point_confirmation_view.go
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
package main
import (
"fmt"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/dustin/go-humanize/english"
)
func showSelectPointConfirmationView(m model) (tea.Model, tea.Cmd) {
m.view = newSelectPointConfirmationView()
m.help.ShowAll = false
return m, nil
}
type selectPointConfirmationViewKeyMap struct {
EndGame key.Binding
Confirm key.Binding
}
func newSelectPointConfirmationViewKeys() selectPointConfirmationViewKeyMap {
return selectPointConfirmationViewKeyMap{
EndGame: newEndGameKeyBinding(),
Confirm: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("↵", "continue"),
),
}
}
func (s selectPointConfirmationViewKeyMap) ShortHelp() []key.Binding {
return []key.Binding{s.Confirm, s.EndGame}
}
func (s selectPointConfirmationViewKeyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{s.Confirm, s.EndGame},
}
}
type selectPointConfirmationView struct {
keys selectPointConfirmationViewKeyMap
}
func newSelectPointConfirmationView() selectPointConfirmationView {
return selectPointConfirmationView{
keys: newSelectPointConfirmationViewKeys(),
}
}
func (s selectPointConfirmationView) update(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, s.keys.EndGame):
return showEndGameConfirmationView(m)
case key.Matches(msg, s.keys.Confirm):
matches := findMatches(m.grid)
if len(matches) == 0 {
return returnToSelectFirstPointView(m)
} else {
return showRefreshGridView(m)
}
}
}
return m, nil
}
func (s selectPointConfirmationView) draw(m model) string {
matches := findMatches(m.grid)
var text string
var selectedPoints []vector2d
if len(matches) != 0 {
symbol1 := m.grid[m.point1.y][m.point1.x]
symbol2 := m.grid[m.point2.y][m.point2.x]
swappedText := fmt.Sprintf("Swapped %s (%d, %d) and %s (%d, %d).",
m.symbolSet.formatSymbol(symbol1), m.point1.x, m.point1.y, m.symbolSet.formatSymbol(symbol2), m.point2.x,
m.point2.y)
matchText := fmt.Sprintf("%s formed!", english.PluralWord(len(matches), "Match", ""))
var pointsGainedText string
if m.hintShown {
pointsGainedText = "No points since hint was shown."
} else {
matchesScore := computeMatchesScore(matches)
pointsGainedText = fmt.Sprintf("+%d points!", matchesScore)
}
text = lipgloss.JoinVertical(lipgloss.Left, swappedText, "", matchText, pointsGainedText)
selectedPoints = flatten(matches)
} else {
text = "Not swapping as swap would not result in a match.\n\nPlease try again."
selectedPoints = []vector2d{m.point1, m.point2}
}
gridText := drawGrid(m, selectedPoints)
m.help.Width = m.windowSize.x - lipgloss.Width(gridText) - 8 - 3
helpView := m.help.View(s.keys)
selectPointConfirmationText := lipgloss.JoinVertical(lipgloss.Left, text, "", helpView)
gridLayoutText := drawGridLayout(m, gridText, selectPointConfirmationText)
return gridLayoutText
}