crib/cardwidget.go
2020-10-03 21:26:32 -07:00

139 lines
4.5 KiB
Go

package main
import (
"math"
"strconv"
"github.com/gdamore/tcell/v2"
"gitlab.com/tslocum/cview"
"gitlab.com/tslocum/joker"
)
type cardWidget struct {
*cview.Box
joker.Card
selected bool
selectedFunc func(joker.Card)
}
func newCardWidget(card joker.Card, selectedFunc func(joker.Card)) *cardWidget {
c := cardWidget{Box: cview.NewBox().ShowFocus(false), Card: card, selectedFunc: selectedFunc}
c.Box.SetBorder(true)
return &c
}
func (c *cardWidget) Select() {
c.selected = !c.selected
if c.selectedFunc != nil {
c.selectedFunc(c.Card)
}
app.Draw()
}
func (c *cardWidget) Draw(screen tcell.Screen) {
c.Box.Draw(screen)
color := tcell.ColorDefault
if c.Card.Suit == joker.SuitHearts || c.Card.Suit == joker.SuitDiamonds {
color = tcell.NewHexColor(0xFF3232)
}
x, y, w, h := c.GetInnerRect()
cview.Print(screen, cardFaceShort(c.Card), x, y, math.MaxInt64, cview.AlignLeft, tcell.ColorDefault)
cview.Print(screen, c.Suit.Name()[0:1], x, y+1, math.MaxInt64, cview.AlignLeft, color)
// Center
if c.Face == joker.FaceAce || c.Face == joker.Face3 || c.Face == joker.Face5 {
cview.Print(screen, c.Suit.Symbol(), x+(w/2), y+(h/2), math.MaxInt64, cview.AlignLeft, color)
} else if c.Face == joker.FaceJack || c.Face == joker.FaceQueen || c.Face == joker.FaceKing {
cview.Print(screen, c.Face.Name()[0:1], x+(w/2), y+(h/2), math.MaxInt64, cview.AlignLeft, color)
}
// Top/bottom 2/3
if c.Face == joker.Face2 || c.Face == joker.Face3 {
cview.Print(screen, c.Suit.Symbol(), x+(w/2), y+2, math.MaxInt64, cview.AlignLeft, color)
cview.Print(screen, c.Suit.Symbol(), x+(w/2), (y+h)-3, math.MaxInt64, cview.AlignLeft, color)
}
// Top/bottom all others
if c.Face == joker.Face4 || c.Face == joker.Face5 || c.Face == joker.Face6 || c.Face == joker.Face7 || c.Face == joker.Face8 || c.Face == joker.Face9 || c.Face == joker.Face10 {
cview.Print(screen, c.Suit.Symbol(), x+(w/4), y+2, math.MaxInt64, cview.AlignLeft, color)
cview.Print(screen, c.Suit.Symbol(), x+(w/2)+(w/4), y+2, math.MaxInt64, cview.AlignLeft, color)
cview.Print(screen, c.Suit.Symbol(), x+(w/4), (y+h)-3, math.MaxInt64, cview.AlignLeft, color)
cview.Print(screen, c.Suit.Symbol(), x+(w/2)+(w/4), (y+h)-3, math.MaxInt64, cview.AlignLeft, color)
}
// Left/right
if c.Face == joker.Face6 || c.Face == joker.Face7 {
cview.Print(screen, c.Suit.Symbol(), x+(w/4), y+(h/2), math.MaxInt64, cview.AlignLeft, color)
cview.Print(screen, c.Suit.Symbol(), x+(w/2)+(w/4), y+(h/2), math.MaxInt64, cview.AlignLeft, color)
}
// Center 7
if c.Face == joker.Face7 {
cview.Print(screen, c.Suit.Symbol(), x+(w/2), y+(h/2)-1, math.MaxInt64, cview.AlignLeft, color)
}
// Secondary top/bottom
if c.Face == joker.Face8 || c.Face == joker.Face9 || c.Face == joker.Face10 {
cview.Print(screen, c.Suit.Symbol(), x+(w/4), y+4, math.MaxInt64, cview.AlignLeft, color)
cview.Print(screen, c.Suit.Symbol(), x+(w/2)+(w/4), y+4, math.MaxInt64, cview.AlignLeft, color)
cview.Print(screen, c.Suit.Symbol(), x+(w/4), (y+h)-5, math.MaxInt64, cview.AlignLeft, color)
cview.Print(screen, c.Suit.Symbol(), x+(w/2)+(w/4), (y+h)-5, math.MaxInt64, cview.AlignLeft, color)
}
// Center 9/10
if c.Face == joker.Face9 || c.Face == joker.Face10 {
cview.Print(screen, c.Suit.Symbol(), x+(w/2), y+(h/2)-2, math.MaxInt64, cview.AlignLeft, color)
}
// Center 10
if c.Face == joker.Face10 {
cview.Print(screen, c.Suit.Symbol(), x+(w/2), y+(h/2)+2, math.MaxInt64, cview.AlignLeft, color)
}
cview.Print(screen, c.Suit.Name()[0:1], (x+w)-1, (y+h)-2, math.MaxInt64, cview.AlignLeft, color)
bottomFaceOffset := 0
if c.Card.Face == joker.Face10 {
bottomFaceOffset = 1
}
cview.Print(screen, cardFaceShort(c.Card), ((x+w)-1)-bottomFaceOffset, (y+h)-1, math.MaxInt64, cview.AlignLeft, tcell.ColorDefault)
_ = w
_ = h
}
func cardFaceShort(card joker.Card) string {
switch card.Face {
case joker.FaceAce, joker.FaceJack, joker.FaceQueen, joker.FaceKing:
return card.Face.Name()[0:1]
default:
return strconv.Itoa(int(card.Face))
}
}
// MouseHandler returns the mouse handler for this primitive.
func (c *cardWidget) MouseHandler() func(action cview.MouseAction, event *tcell.EventMouse, setFocus func(p cview.Primitive)) (consumed bool, capture cview.Primitive) {
return c.WrapMouseHandler(func(action cview.MouseAction, event *tcell.EventMouse, setFocus func(p cview.Primitive)) (consumed bool, capture cview.Primitive) {
if !c.InRect(event.Position()) {
return false, nil
}
// Process mouse event.
if action == cview.MouseLeftClick {
consumed = true
c.Select()
}
return
})
}