aboutsummaryrefslogtreecommitdiff
path: root/go/ui/hoverable.go
blob: c70340bbee98832fb94d68c043885295396a82e7 (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
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)
	h.reset = func() {
		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() }
}

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
		}

		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
	}
}