aboutsummaryrefslogtreecommitdiff
path: root/go/ui/hoverable.go
blob: 30415c8d93cd822ec2f588c404316310b130dbbb (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
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 {
		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(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
	}
}