aboutsummaryrefslogtreecommitdiff
path: root/go/client/keybindings.go
blob: 2bfc52c788ebf0522ea476b9c60a85cd5d7da655 (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
package main

import (
	"log"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/inpututil"

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

type (
	keyAction   func(*Game) bool
	keyBindings map[interface{}]keyAction
)

var tapToHandle ui.Tap

func (g *Game) handleKeyBindings(bindings keyBindings) {
	// FIXME: Use new ui.InputEvent queue

	for _k, a := range bindings {
		var remove bool
		switch k := _k.(type) {
		case ebiten.MouseButton:
			if inpututil.IsMouseButtonJustPressed(k) {
				remove = a(g)
			}

		case ui.TouchType:
			if k == ui.TouchTypes.Tap {
				for _, tap := range ui.TouchManager.Taps {
					tapToHandle = tap
					log.Println("handle tap", tap)
					remove := a(g)
					if remove {
						break
					}
				}
			}

		case ebiten.Key:
			if inpututil.IsKeyJustPressed(k) {
				remove = a(g)
			}
		}

		if remove {
			delete(bindings, _k)
		}
	}
}

func clickSelection(g *Game) bool {
	x, y := ebiten.CursorPosition()
	return selection(g, x, y)
}

func touchSelection(g *Game) bool {
	log.Println("touch selection")
	return selection(g, tapToHandle.X, tapToHandle.Y)
}

func selection(g *Game, x, y int) bool {
	obj := g.findObjectAt(x, y)

	if g.prompt != nil {
		if err := g.prompt.Add(obj); err != nil {
			log.Println("Not added", obj, "to active prompt:", err)
			g.handleSelection(obj, x, y)
		} else {
			g.updateHighlight()
		}
	} else {
		g.handleSelection(obj, x, y)
	}

	return false
}

func pass(g *Game) bool {
	g.passButton.Click(0, 0)
	return false
}

func reset(g *Game) bool {
	g.removeChoice()
	g.selectedObject = nil
	g.ResetHover()
	g.clearHighlights()
	g.hideStore()
	g.RemoveWidget(g.discardPileView)
	g.RemoveWidget(g.resetButton)
	if g.prompt != nil {
		if g.prompt.Action().Targets().NoSelections() {
			g.removePrompt()
		} else {
			g.prompt.ClearSelections()
		}
	}
	return false
}