-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathselection.go
112 lines (87 loc) · 2.78 KB
/
selection.go
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
package main
import (
"github.com/veandco/go-sdl2/gfx"
"github.com/veandco/go-sdl2/sdl"
)
type Selection struct {
Page *Page
Cards map[*Card]bool
BoxSelecting bool
BoxStart Point
}
func NewSelection(board *Page) *Selection {
return &Selection{Page: board, Cards: map[*Card]bool{}}
}
func (selection *Selection) Update() {
if globals.State == StateNeutral {
if globals.Mouse.Button(sdl.BUTTON_LEFT).Pressed() {
selection.BoxSelecting = true
selection.BoxStart = globals.Mouse.WorldPosition()
}
if selection.BoxSelecting && globals.Mouse.Button(sdl.BUTTON_LEFT).Released() {
selectionRect := NewCorrectingRect(selection.BoxStart.X, selection.BoxStart.Y, globals.Mouse.WorldPosition().X, globals.Mouse.WorldPosition().Y).SDLRect()
if !globals.Keybindings.Pressed(KBAddToSelection) && !globals.Keybindings.Pressed(KBRemoveFromSelection) {
selection.Clear()
}
if globals.Keybindings.Pressed(KBRemoveFromSelection) {
for _, card := range selection.Page.Cards {
if card.Rect.HasIntersection(selectionRect) {
selection.Remove(card)
}
}
} else {
for _, card := range selection.Page.Cards {
if card.Rect.HasIntersection(selectionRect) {
selection.Add(card)
}
}
}
selection.BoxSelecting = false
}
}
}
func (selection *Selection) Add(card *Card) {
if !card.selected {
card.Page.Raise(card)
}
card.Select()
selection.Cards[card] = true
}
func (selection *Selection) Remove(card *Card) {
card.Deselect()
delete(selection.Cards, card)
}
func (selection *Selection) Has(card *Card) bool {
for c := range selection.Cards {
if card == c {
return true
}
}
return false
}
func (selection *Selection) AsSlice() []*Card {
cards := []*Card{}
for card := range selection.Cards {
cards = append(cards, card)
}
return cards
}
func (selection *Selection) Clear() {
for card := range selection.Cards {
card.Deselect()
}
selection.Cards = map[*Card]bool{}
}
func (selection *Selection) Draw() {
if selection.BoxSelecting {
globals.Renderer.SetScale(1, 1)
unprojected := selection.Page.Project.Camera.UntranslatePoint(selection.BoxStart)
unprojected = unprojected.Mult(globals.Project.Camera.Zoom)
other := globals.Mouse.Position()
boxColor := getThemeColor(GUIMenuColor).SDLColor()
gfx.ThickLineColor(globals.Renderer, int32(unprojected.X), int32(unprojected.Y), int32(other.X), int32(unprojected.Y), 4, boxColor)
gfx.ThickLineColor(globals.Renderer, int32(unprojected.X), int32(unprojected.Y), int32(unprojected.X), int32(other.Y), 4, boxColor)
gfx.ThickLineColor(globals.Renderer, int32(other.X), int32(unprojected.Y), int32(other.X), int32(other.Y), 4, boxColor)
gfx.ThickLineColor(globals.Renderer, int32(unprojected.X), int32(other.Y), int32(other.X), int32(other.Y), 4, boxColor)
}
}