aboutsummaryrefslogtreecommitdiff
path: root/go/ui/hoverable.go
blob: 89adb912e8d71e2d08d7450820b3a0123de90052 (plain)
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
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
	}
}