aboutsummaryrefslogtreecommitdiff
path: root/go/ui/prompt.go
blob: 9f8bad00d33fc0bfb8ab32ee193e293cadc0b945 (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
package ui

import (
	"image/color"

	"github.com/hajimehoshi/ebiten/v2"

	"muhq.space/muhqs-game/go/game"
)

const (
	PROMPT_HEIGHT int = 50
)

type Prompt struct {
	EventHandlersMap
	y, width   int
	action     game.Action
	components []Widget
	cancelable bool
}

func NewPrompt(y, width int, action game.Action, promptText string) *Prompt {
	p := &Prompt{
		EventHandlersMap: NewEventHandlersMap(),
		y:                y,
		width:            width,
		action:           action,
		components:       []Widget{NewFixedTextBox(0, y, width, PROMPT_HEIGHT, promptText).Centering(true)},
	}
	p.RegisterHandler("hover", p.Hover)
	return p
}

func NewCancelablePrompt(y, width int, action game.Action, promptText string) *Prompt {
	p := NewPrompt(y, width, action, promptText)
	p.cancelable = true
	return p
}

func (p *Prompt) Cancelable() bool    { return p.cancelable }
func (p *Prompt) Action() game.Action { return p.action }

func (p *Prompt) Add(obj any) error {
	if handCard, ok := obj.(HandCard); ok {
		obj = handCard.C
	}

	return p.action.Targets().AddSelection(obj)
}

func (p *Prompt) Height() int {
	return PROMPT_HEIGHT
}

func (p *Prompt) ClearSelections() {
	p.action.Targets().ClearSelections()
}

func (p *Prompt) Draw(screen *ebiten.Image) {
	for _, comp := range p.components {
		comp.Draw(screen)
	}
}

func (p *Prompt) Contains(x, y int) bool {
	return x >= 0 && y >= 0 && x <= p.width && y >= p.y && y <= p.y+PROMPT_HEIGHT
}

func (p *Prompt) FindObjectAt(screenX, screenY int) any { return nil }
func (*Prompt) Render(Widget) *ebiten.Image             { return nil }

func (p *Prompt) setTextBgAlpha(alpha uint8) {
	for _, comp := range p.components {
		textBox, ok := comp.(*TextBox)
		if !ok {
			continue
		}
		bg := color.RGBAModel.Convert(textBox.bg).(color.RGBA)
		bg.A = alpha
		textBox.Bg(bg)
		textBox.ForceRedraw()
	}
}

func (p *Prompt) Hover(hover bool, x int, y int) {
	if hover {
		p.setTextBgAlpha(0x7f)
	} else {
		p.setTextBgAlpha(0xff)
	}
}

func (p *Prompt) Layout() (int, int) {
	return p.width, PROMPT_HEIGHT
}