aboutsummaryrefslogtreecommitdiff
path: root/selection.go
blob: 730570a62484f11a6457550737050f5630014718 (plain) (blame)
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

package main

import (
	"image/color"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/vector"
	"golang.org/x/exp/slog"
)

type Selection struct {
	IsSelected bool
}

func (e *Entity) SelectMultiple(cursor Point, selection GlobalSelection) {
	if e.Selection.IsEnabled {
		e.Selection.Value.IsSelected = false
		selectionBounds := Rectangle{selection.Start, cursor}.Canon()
		if selectionBounds.Overlaps(e.Bounds()) {
			e.Selection.Value.IsSelected = true
			slog.Info("entity selected", slog.String("position", e.Position.Value.String()))
		}
	}
}

func (e *Entity) SelectSingle(cursor Point, canBeSelected bool) bool {
	if e.Selection.IsEnabled {
		e.Selection.Value.IsSelected = false
		if canBeSelected && cursor.In(e.Bounds()) {
			e.Selection.Value.IsSelected = true
			slog.Info("entity selected", slog.String("position", e.Position.Value.String()))
			return true
		}
	}
	return false
}

func DrawSelection(screen *ebiten.Image, e *Entity) {
	if e.Image.IsEnabled && e.Position.IsEnabled && e.Selection.IsEnabled {
		if e.Selection.Value.IsSelected {
			vector.StrokeRect(screen, float32(e.Position.Value.X-5), float32(e.Position.Value.Y-5), 110, 110, 10, color.RGBA{0xff, 0x00, 0x00, 0xff}, false)
		}
	}
}