summaryrefslogtreecommitdiff
path: root/objs/cursor.go
blob: bf4d6245ef82168814a7de9ae79a6b0c0371d548 (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
package objs

import (
	"github.com/Zyko0/go-sdl3/sdl"
	"ponger/ecs"
	"ponger/events"
)

type Cursor struct {
	*ecs.GameObject
	MaxX, MaxY float32
	Trail      []sdl.FPoint
	Pressed    bool
}

func (c *Cursor) ProcessMousePress(ev events.MousePress) {
	c.Pressed = ev.IsPressed
}

func (c *Cursor) ProcessWindowChange(ev events.WindowChange) {
	c.MaxX = float32(ev.Width)
	c.MaxY = float32(ev.Height)
}

func (c *Cursor) Render(renderer *sdl.Renderer) {
	if c.Pressed {
		c.Trail = append(c.Trail, c.Position)
	} else {
		c.Trail = nil
	}
	c.Safe(renderer.SetDrawColor(255, 0, 0, 255))
	size := float32(13)
	r := &sdl.FRect{
		X: c.Position.X - size/2,
		Y: c.Position.Y - size/2,
		W: size,
		H: size,
	}
	c.Safe(renderer.RenderRect(r))
	if c.Trail != nil {
		c.Safe(renderer.RenderLines(c.Trail))
	}
}

func (c *Cursor) ProcessMouseMotion(ev events.MouseMotion) {
	c.Position.X += ev.Dx
	c.Position.Y += ev.Dy
	if c.Position.X < 0 {
		c.Position.X = 0
	}
	if c.Position.Y < 0 {
		c.Position.Y = 0
	}
	if c.Position.X > c.MaxX {
		c.Position.X = c.MaxX
	}
	if c.Position.Y > c.MaxY {
		c.Position.Y = c.MaxY
	}
}
func NewCursor() ecs.IsGameObject {
	return &Cursor{
		GameObject: &ecs.GameObject{},
	}
}