package ui import ( "muhq.space/muhqs-game/go/game" ) const ( HOVER_CARD_WIDTH = 500 HOVER_CARD_HEIGHT = 700 ) type hoverWidget struct { c *Collection createHint func(x, y int) Widget reset func() xMax, yMax int } func (h *hoverWidget) Hover(started bool, x, y int) { if started { h.onHover(x, y) } else { h.stopHover(x, y) } } func (h *hoverWidget) showHint(x, y int) { hint := h.createHint(x, y) if hint == nil { return } h.c.AddWidget(hint) oldReset := h.reset h.reset = func() { if oldReset != nil { oldReset() } h.c.RemoveWidget(hint) } } func (h *hoverWidget) onHover(x, y int) { if h.xMax == 0 || h.yMax == 0 { width, height := h.c.Layout() h.xMax = width h.yMax = height } h.showHint(x, y) } func (h *hoverWidget) stopHover(x, y int) { if h.reset != nil { h.reset() h.reset = nil } } type hoverCardView struct { hoverWidget } func (h *hoverCardView) init(w Widget, c *Collection) { h.c = c h.createHint = func(x, y int) Widget { obj := w.FindObjectAt(x, y) if obj == nil { return nil } card := obj.(*game.Card) 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(mv *MapView, c *Collection) { h.c = c h.createHint = func(x, y int) Widget { obj := mv.FindObjectAt(x, y) if obj == nil { return nil } perm, ok := obj.(game.Permanent) if !ok { return nil } if unit, ok := obj.(*game.Unit); ok { movable := unit.MoveRangeTiles() for _, t := range movable { mv.AddHighlightTile(t, HighlightMovementColor) } h.reset = func() { mv.ClearTileHighlights() } } 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 } }