diff options
Diffstat (limited to 'go/ui/hoverable.go')
| -rw-r--r-- | go/ui/hoverable.go | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/go/ui/hoverable.go b/go/ui/hoverable.go new file mode 100644 index 00000000..54d4ec15 --- /dev/null +++ b/go/ui/hoverable.go @@ -0,0 +1,120 @@ +package ui + +import ( + "muhq.space/muhqs-game/go/game" +) + +const ( + HOVER_THRESHOLD = 60 + + HOVER_CARD_WIDTH = 500 + HOVER_CARD_HEIGHT = 700 +) + +type Hoverable interface { + onHover(ticks int, x, y int, c *Collection) bool + stopHover(ticks int, x, y int) bool +} + +type hoverWidget struct { + createHint func(x, y int) Widget + reset func() + xMax, yMax int +} + +func (h *hoverWidget) showHint(x, y int, c *Collection) { + hint := h.createHint(x, y) + if hint == nil { + return + } + + c.AddWidget(hint) + h.reset = func() { + c.RemoveWidget(hint) + } +} + +func (h *hoverWidget) onHover(ticks int, x, y int, c *Collection) bool { + if h.xMax == 0 || h.yMax == 0 { + width, height := c.Layout() + h.xMax = width + h.yMax = height + } + + if ticks == HOVER_THRESHOLD { + h.showHint(x, y, c) + return true + } + + return false +} + +func (h *hoverWidget) stopHover(ticks int, x, y int) bool { + if h.reset != nil { + h.reset() + } + return true +} + +type hoverCardView struct { + hoverWidget +} + +func (h *hoverCardView) init(w Widget) { + h.createHint = func(x, y int) Widget { + card := w.FindObjectAt(x, y).(*game.Card) + if card == nil { + return nil + } + + wx, wy := x, y + if x+HOVER_CARD_WIDTH > h.xMax || y+HOVER_CARD_HEIGHT > h.yMax { + wx = x - HOVER_CARD_WIDTH + wy = y - HOVER_CARD_HEIGHT + } + if wx < 0 { + wx = 0 + } + if wy < 0 { + wy = 0 + } + + return NewScaledCardView(wx, wy, HOVER_CARD_WIDTH, HOVER_CARD_HEIGHT, card.Path()) + } +} + +type hoverPermInfo struct { + hoverWidget +} + +func (h *hoverPermInfo) init(w Widget) { + h.createHint = func(x, y int) Widget { + obj := w.FindObjectAt(x, y) + if obj == nil { + return nil + } + + perm, ok := obj.(game.Permanent) + if !ok { + return nil + } + + hint := NewPermInfo(x+TILE_WIDTH, y, perm) + + wx, wy := x, y + if hint.X+hint.Width > h.xMax || y+hint.Height > h.yMax { + wx = x - hint.Width + wy = y - hint.Height + } + if wx < 0 { + wx = 0 + } + if wy < 0 { + wy = 0 + } + + hint.X, hint.Y = wx, wy + + return hint + } +} |
